Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def executeSubprocess(self, event, command):
"""Execute a command in a separate process."""
trace = False
import sys
k = self.c.k
try:
p = subprocess.Popen(
shlex.split(command),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL if trace else subprocess.PIPE,
shell=sys.platform.startswith('win'),
)
out, err = p.communicate()
for line in g.splitLines(out):
g.es_print(g.toUnicode(line.rstrip()))
except Exception:
g.es_exception()
k.keyboardQuit()
# Inits vim mode too.
g.es(f"Done: {command}")
#@+node:ekr.20150514063305.92: *3* print plugins info...
def pickle(self, p):
"""Pickle val and return the hexlified result."""
try:
ua = p.v.u
s = pickle.dumps(ua, protocol=1)
s2 = binascii.hexlify(s)
s3 = g.toUnicode(s2, 'utf-8')
return s3
except pickle.PicklingError:
g.warning("ignoring non-pickleable value", ua, "in", p.h)
return ''
except Exception:
g.error("pd.pickle: unexpected exception in", p.h)
g.es_exception()
return ''
#@+node:ekr.20140713135856.17744: *5* pd.unpickle
# if s and s[-1] == '\n':
# if len(s) > 1: s = s[:-1]
# else: s = ''
while s and s[-1] == '\n':
s = s[: -1]
# Warn if there are multiple lines.
i = s.find('\n')
if i > -1:
# g.trace(i,len(s),repr(s))
g.warning("truncating headline to one line")
s = s[: i]
limit = 1000
if len(s) > limit:
g.warning("truncating headline to", limit, "characters")
s = s[: limit]
s = g.toUnicode(s or '')
#@-<< truncate s if it has multiple lines >>
# Make the change official, but undo to the *old* revert point.
oldRevert = self.revertHeadline
changed = s != oldRevert
self.revertHeadline = s
p.initHeadString(s)
if trace: g.trace('changed', changed, 'new', repr(s))
if g.doHook("headkey1", c=c, p=p, v=p, ch=ch, changed=changed):
return # The hook claims to have handled the event.
if changed:
undoData = u.beforeChangeNodeContents(p, oldHead=oldRevert)
if not c.changed: c.setChanged(True)
# New in Leo 4.4.5: we must recolor the body because
# the headline may contain directives.
c.frame.scanForTabWidth(p)
c.frame.body.recolor(p, incremental=True)
def read_words(self, kind, fn):
"""Return all the words from the dictionary file."""
words = set()
try:
with open(fn, 'rb') as f:
s = g.toUnicode(f.read())
for line in g.splitLines(s):
self.add_expanded_line(line, words)
except Exception:
g.es_print(f"can not open {kind} dictionary: {fn}")
return words
#@+node:ekr.20180207132550.1: *4* default.add_expanded_line
def sn_getenckey(dummy=None):
txt,ok = QInputDialog.getText(None,
'Enter key',
'Enter key.\nData lost if key is lost.\nSee docs. for key upgrade notes.',
)
if not ok:
return
if str(txt).startswith('v0:'):
txt = QString(txt[3:])
else:
txt = g.toUnicode(txt)
# arbitrary kludge to convert string to 256 bits - don't change
sha = SHA.new()
md5 = MD5.new()
sha.update(txt.encode('utf-8'))
md5.update(txt.encode('utf-8'))
__ENCKEY[0] = sha.digest()[:16] + md5.digest()[:16]
if len(__ENCKEY[0]) != 32:
raise Exception("sn_getenckey failed to build key")
#@+node:tbrown.20141214173054.3: ** class TextEditSearch
def replaceClipboardWith(self, s):
"""Replace the clipboard with the string s."""
cb = self.qtApp.clipboard()
if cb:
# cb.clear() # unnecessary, breaks on some Qt versions
s = g.toUnicode(s)
QtWidgets.QApplication.processEvents()
# Fix #241: QMimeData object error
cb.setText(QString(s))
QtWidgets.QApplication.processEvents()
else:
g.trace('no clipboard!')
#@+node:ekr.20160917125948.1: *4* qt_gui.getTextFromClipboard
c = self.c
# This is called at idle-time, and there can be problems when closing the window.
if g.app.killed or not c or not hasattr(c,'frame'):
return
w = c.frame.body.bodyCtrl
tab_width = c.frame.tab_width
s = w.getAllText()
index = w.getInsertPoint()
row,col = g.convertPythonIndexToRowCol(s,index)
if col > 0:
s2 = s[index-col:index]
s2 = g.toUnicode(s2)
col = g.computeWidth (s2,c.tab_width)
if row != self.lastRow or col != self.lastCol:
s = "line %d, col %d " % (row,col)
self.label.configure(text=s)
self.lastRow,self.lastCol = row,col
if 0: # Done in idle handler.
self.label.after(500,self.update)
#@-others
s = ''.join(lines)
else:
# Get the file from the working directory.
path = g.os_path_finalize_join(self.repo_dir, fn)
if g.os_path_exists(path):
try:
with open(path, 'rb') as f: # Was 'r'
s = f.read()
except Exception:
g.es_print('Can not read', path)
g.es_exception()
s = ''
else:
g.trace('not found:', path)
s = ''
return g.toUnicode(s).replace('\r', '')
#@+node:ekr.20170806094320.9: *4* gdc.get_files
def trial_write(self):
'''Return the trial write for self.root.'''
at = self.c.atFileCommands
# Leo 5.6: Allow apparent section refs for *all* languages.
ivar = 'allow_undefined_refs'
try:
setattr(at, ivar, True)
result = at.atAutoToString(self.root)
finally:
if hasattr(at, ivar):
delattr(at, ivar)
return g.toUnicode(result, self.encoding)
#@+node:ekr.20161108131153.15: *3* i.Utils
def getAllText(self):
'''StringTextWrapper.'''
s = self.s
return g.toUnicode(s)
#@+node:ekr.20140903172510.18584: *4* stw.getInsertPoint