def get_external_file_with_sentinels(self, root): ''' root is an @<file> node. Return the result of writing the file *with* sentinels, even if the external file would normally *not* have sentinels. ''' at, c = self.c.atFileCommands, self.c if root.isAtAutoNode(): # We must treat @auto nodes specially because # Leo does not write sentinels in the root @auto node. # at.writeOneAtAutoNode handle's all kinds of @auto nodes. ok = at.writeOneAtAutoNode( root, toString=True, force=True, trialWrite=False, forceSentinels=True) return ok and at.stringOutput or '' else: return g.getScript( c, root, useSelectedText=False, forcePythonSentinels=False, # Fix #247: @language c breaks goto-global-line. # self.get_delims returns language-specific delims, # so this code must do so too. useSentinels=True)
def runDebugScriptCommand(self, event=None): """Called when user presses the 'debug-script' button or executes the debug-script command.""" c = self.c p = c.p script = g.getScript(c, p, useSelectedText=True, useSentinels=False) if script: # @+<< set debugging if debugger is active >> # @+node:ekr.20060523084441: *6* << set debugging if debugger is active >> g.trace(self.debuggerKind) if self.debuggerKind == "winpdb": try: import rpdb2 debugging = rpdb2.g_debugger is not None except ImportError: debugging = False elif self.debuggerKind == "idle": # import idlelib.Debugger.py as Debugger # debugging = Debugger.interacting debugging = True else: debugging = False # @-<< set debugging if debugger is active >> if debugging: # @+<< create leoScriptModule >> # @+node:ekr.20060524073716: *6* << create leoScriptModule >> target = g.os_path_join(g.app.loadDir, "leoScriptModule.py") f = None try: f = file(target, "w") f.write("# A module holding the script to be debugged.\n") if self.debuggerKind == "idle": # This works, but uses the lame pdb debugger. f.write("import pdb\n") f.write("pdb.set_trace() # Hard breakpoint.\n") elif self.debuggerKind == "winpdb": f.write("import rpdb2\n") f.write("if rpdb2.g_debugger is not None: # don't hang if the debugger isn't running.\n") f.write(' rpdb2.start_embedded_debugger(pwd="",fAllowUnencrypted=True) # Hard breakpoint.\n') # f.write('# Remove all previous variables.\n') f.write("# Predefine c, g and p.\n") f.write("import leo.core.leoGlobals as g\n") f.write('c = g.app.scriptDict.get("c")\n') f.write("p = c.p\n") f.write("# Actual script starts here.\n") f.write(script + "\n") finally: if f: f.close() # @-<< create leoScriptModule >> # pylint: disable=E0611 # E0611:runDebugScriptCommand: No name 'leoScriptModule' in module 'leo.core' g.app.scriptDict["c"] = c if "leoScriptModule" in sys.modules.keys(): del sys.modules["leoScriptModule"] # Essential. import leo.core.leoScriptModule as leoScriptModule else: g.error("No debugger active") c.bodyWantsFocus()
def applyNodeAction(pScript, pClicked, c): script = g.getScript(c, pScript) if script: working_directory = os.getcwd() file_directory = c.frame.openDirectory os.chdir(file_directory) script += '\n' #Redirect output if c.config.redirect_execute_script_output_to_log_pane: g.redirectStdout() # Redirect stdout g.redirectStderr() # Redirect stderr try: namespace = { 'c':c, 'g':g, 'pClicked': pClicked, 'pScript' : pScript, 'shellScriptInWindowNA': shellScriptInWindowNA } # exec script in namespace exec(script,namespace) #Unredirect output if c.config.redirect_execute_script_output_to_log_pane: g.restoreStderr() g.restoreStdout() except: #Unredirect output if c.config.redirect_execute_script_output_to_log_pane: g.restoreStderr() g.restoreStdout() g.es("exception in NodeAction plugin") g.es_exception(full=False,c=c) os.chdir(working_directory)
def create_script_list(self, p, delim): '''Create the state_list from the tree of script nodes rooted in p.''' c = self.c aList = [] after = p.nodeAfterTree() while p and p != after: if p.h.startswith('@ignore-tree'): p.moveToNodeAfterTree() elif p.h.startswith('@ignore'): p.moveToThreadNext() else: script = g.getScript(c, p, useSelectedText=False, forcePythonSentinels=False, useSentinels=False, ) if script.strip(): aList.append(script) p.moveToThreadNext() # Now split each element of the list. # This is a big advance in scripting! result = [] for s in aList: result.extend(self.parse_script_string(s, delim)) return result
def getScript(self,p): '''Return the script composed from p and its descendants.''' return ( g.getScript(self.c,p, useSelectedText=False, forcePythonSentinels=True, useSentinels=True, ))
def script(self): """ Method to get the 'tangled' contents of the node (parse @others, section references etc.) """ c = self.c return g.getScript(c,self.p,useSelectedText=False,useSentinels=False)
def runDebugScriptCommand (self,event=None): '''Called when user presses the 'debug-script' button or executes the debug-script command.''' c = self.c ; p = c.p script = g.getScript(c,p,useSelectedText=True,useSentinels=False) if script: #@+<< set debugging if debugger is active >> #@+node:ekr.20060523084441: *5* << set debugging if debugger is active >> g.trace(self.debuggerKind) if self.debuggerKind == 'winpdb': try: import rpdb2 debugging = rpdb2.g_debugger is not None except ImportError: debugging = False elif self.debuggerKind == 'idle': # import idlelib.Debugger.py as Debugger # debugging = Debugger.interacting debugging = True else: debugging = False #@-<< set debugging if debugger is active >> if debugging: #@+<< create leoScriptModule >> #@+node:ekr.20060524073716: *5* << create leoScriptModule >> (mod_scripting.py) target = g.os_path_join(g.app.loadDir,'leoScriptModule.py') f = None try: f = file(target,'w') f.write('# A module holding the script to be debugged.\n') if self.debuggerKind == 'idle': # This works, but uses the lame pdb debugger. f.write('import pdb\n') f.write('pdb.set_trace() # Hard breakpoint.\n') elif self.debuggerKind == 'winpdb': f.write('import rpdb2\n') f.write('if rpdb2.g_debugger is not None: # don\'t hang if the debugger isn\'t running.\n') f.write(' rpdb2.start_embedded_debugger(pwd="",fAllowUnencrypted=True) # Hard breakpoint.\n') # f.write('# Remove all previous variables.\n') f.write('# Predefine c, g and p.\n') f.write('import leo.core.leoGlobals as g\n') f.write('c = g.app.scriptDict.get("c")\n') f.write('p = c.p\n') f.write('# Actual script starts here.\n') f.write(script + '\n') finally: if f: f.close() #@-<< create leoScriptModule >> # pylint: disable=no-name-in-module g.app.scriptDict ['c'] = c if 'leoScriptModule' in sys.modules.keys(): del sys.modules ['leoScriptModule'] # Essential. import leo.core.leoScriptModule as leoScriptModule else: g.error('No debugger active') c.bodyWantsFocus()
def find_gnx_helper(self, c, gnx, openFlag, trace): """ c is the commander of a just-opened settings file. Perform the search, and close the file unless openFlag is True. """ for p in c.all_positions(): if p.gnx == gnx: if trace: g.trace("found", p.h, "in", c.shortFileName()) if openFlag: script = g.getScript(c, p, useSelectedText=True, useSentinels=False) return p, script else: script = g.getScript(c, p, useSelectedText=True, useSentinels=False) c.close() return None, script # Not found: always close the commander. c.close() return None, None
def new_position_view(self, p): """new_position_view - update viewer for new position WARNING: unlike new_position() this uses p without regard for self.track :param position p: the new position """ DBG("new view position") if self.mode != 'edit': if self.recurse: text = g.getScript(self.c, p, useSelectedText=False, useSentinels=False) else: text = p.b self.view_widget.new_text(text)
def update_position_view(self, p): """update_position_view - update viewer for current position WARNING: unlike update_position() this uses p without regard for self.track :param position p: the position to update to """ DBG("update view position") if self.update and self.mode != 'edit': if self.recurse: text = g.getScript(self.c, p, useSelectedText=False, useSentinels=False) else: text = p.b self.view_widget.update_text(text)
def find_script_line(self, n, root): ''' Go to line n (zero based) of the script with the given root. Return p, offset, found for unit testing. ''' c = self.c if n < 0: return None, -1, False script = g.getScript(c, root, useSelectedText=False) lines = g.splitLines(script) # Script lines now *do* have gnx's. gnx, h, offset = self.scan_sentinel_lines(lines, n, root) p, found = self.find_gnx(root, gnx, h) if gnx and found: self.success(lines, n, offset, p) return p, offset, True else: self.fail(lines, n, root) return None, -1, False
def applyFileAction(p, filename, c): script = g.getScript(c, p) if script: working_directory = os.getcwd() file_directory = c.frame.openDirectory os.chdir(file_directory) script += '\n' #@+<< redirect output >> #@+node:ekr.20040915105758.17: *3* << redirect output >> if c.config.redirect_execute_script_output_to_log_pane: g.redirectStdout() # Redirect stdout g.redirectStderr() # Redirect stderr #@-<< redirect output >> try: namespace = { 'c':c, 'g':g, 'filename': filename, 'shellScriptInWindow': shellScriptInWindow } # exec script in namespace exec(script,namespace) #@+<< unredirect output >> #@+node:ekr.20040915105758.18: *3* << unredirect output >> if c.config.redirect_execute_script_output_to_log_pane: g.restoreStderr() g.restoreStdout() #@-<< unredirect output >> except: #@+<< unredirect output >> #@+node:ekr.20040915105758.18: *3* << unredirect output >> if c.config.redirect_execute_script_output_to_log_pane: g.restoreStderr() g.restoreStdout() #@-<< unredirect output >> g.es("exception in FileAction plugin") g.es_exception(full=False,c=c) os.chdir(working_directory)
def exec_helper(self,event): '''This helper is required because an unqualified "exec" may not appear in a nested function. ''' c = event and event.get('c') ipk = g.app.ipk ns = ipk.namespace # The actual IPython namespace. ipkernel = ipk.ipkernel # IPKernelApp shell = ipkernel.shell # ZMQInteractiveShell if c and ns is not None: try: script = g.getScript(c,c.p) if 1: code = compile(script,c.p.h,'exec') shell.run_code(code) # Run using IPython. else: exec(script,ns) # Run in Leo in the IPython namespace. except Exception: g.es_exception() finally: sys.stdout.flush()
def find_script_line(self, n, root): ''' Go to line n (zero based) of the script with the given root. Return p, offset, found for unit testing. ''' trace = False and not g.unitTesting c = self.c if n < 0: return None, -1, False script = g.getScript(c, root, useSelectedText=False) lines = g.splitLines(script) if trace: aList = ['%3s %s' % (i, s) for i, s in enumerate(lines)] g.trace('n: %s script: ...\n%s' % (n, ''.join(aList))) # Script lines now *do* have gnx's. gnx, h, offset = self.scan_sentinel_lines(lines, n, root) p, found = self.find_gnx(root, gnx, h) if gnx and found: self.success(lines, n, offset, p) return p, offset, True else: self.fail(lines, n, root) return None, -1, False
def collect_data(): p = c.rootPosition() seen = set() while p: v = p.v if v.gnx in seen: p.moveToNodeAfterTree() continue seen.add(v.gnx) h = v.h if h.startswith('@transformer '): name = h.partition(' ')[2].strip() trscripts[name] = g.getScript(c, p.copy(), useSentinels=False, forcePythonSentinels=True) p.moveToNodeAfterTree() elif h.startswith('@transform-node '): name = h.partition(' ')[2].strip() name, sep, rest = name.partition('(') if not sep: g.warning('wrong syntax expected "("', nodeLink=p) p.moveToThreadNext() continue srcgnx, sep, rest = rest.partition(')') if not sep: g.warning('wrong syntax expected ")"', nodeLink=p) p.moveToThreadNext() continue srcgnx = srcgnx.strip() if not srcgnx in c.fileCommands.gnxDict: g.warning('unknown gnx', srcgnx, nodeLink=p) p.moveToThreadNext() continue trtargets[v.gnx] = (name, srcgnx) p.moveToThreadNext() else: p.moveToThreadNext()
def invoke_debugger(self, event=None): ''' Start an external debugger in another process to debug a script. The script is the presently selected text or then entire tree's script. ''' c, p = self.c, self.c.p python = sys.executable script = g.getScript(c, p) winpdb = self.findDebugger() if not winpdb: return #check for doctest examples try: import doctest parser = doctest.DocTestParser() examples = parser.get_examples(script) # if this is doctest, extract the examples as a script if len(examples) > 0: script = doctest.script_from_examples(script) except ImportError: pass # Special case: debug code may include g.es("info string"). # insert code fragment to make this expression legal outside Leo. hide_ges = "class G:\n def es(s,c=None):\n pass\ng = G()\n" script = hide_ges + script # Create a temp file from the presently selected node. filename = c.writeScriptFile(script) if not filename: return # Invoke the debugger, retaining the present environment. os.chdir(g.app.loadDir) if False and subprocess: cmdline = '%s %s -t %s' % (python, winpdb, filename) subprocess.Popen(cmdline) else: args = [sys.executable, winpdb, '-t', filename] os.spawnv(os.P_NOWAIT, python, args)
def untangle(self, p): return g.getScript(self.c, p, useSelectedText=False, useSentinels=False)
def untangle(self,p): return g.getScript(self.c,p, useSelectedText=False, useSentinels=False)
def test_beautifier(c, h, p, settings): '''Test Leo's beautifier code''' if not p: g.trace('not found: %s' % h) return s = g.getScript(c, p, useSelectedText=False, forcePythonSentinels=True, useSentinels=False) g.trace(h.strip()) t1 = time.time() s1 = g.toEncodedString(s) node1 = ast.parse(s1, filename='before', mode='exec') t2 = time.time() readlines = g.ReadLinesClass(s).next tokens = list(tokenize.generate_tokens(readlines)) t3 = time.time() beautifier = PythonTokenBeautifier(c) keep_blank_lines = settings.get('tidy-keep-blank-lines') if keep_blank_lines is not None: beautifier.delete_blank_lines = not keep_blank_lines s2 = beautifier.run(tokens) t4 = time.time() try: s2_e = g.toEncodedString(s2) node2 = ast.parse(s2_e, filename='before', mode='exec') ok = compare_ast(node1, node2) except Exception: g.es_exception() ok = False t5 = time.time() # Update the stats beautifier.n_input_tokens += len(tokens) beautifier.n_output_tokens += len(beautifier.code_list) beautifier.n_strings += len(s2) beautifier.parse_time += (t2 - t1) beautifier.tokenize_time += (t3 - t2) beautifier.beautify_time += (t4 - t3) beautifier.check_time += (t5 - t4) beautifier.total_time += (t5 - t1) if settings.get('input_string'): print('==================== input_string') for i, z in enumerate(g.splitLines(s)): print('%4s %s' % (i + 1, z.rstrip())) if settings.get('input_lines'): print('==================== input_lines') dump_tokens(tokens, verbose=False) if settings.get('input_tokens'): print('==================== input_tokens') dump_tokens(tokens, verbose=True) if settings.get('output_tokens'): print('==================== code_list') for i, z in enumerate(beautifier.code_list): print('%4s %s' % (i, z)) if settings.get('output_string'): print('==================== output_string') for i, z in enumerate(g.splitLines(s2)): if z == '\n': print('%4s' % (i + 1)) elif z.rstrip(): print('%4s %s' % (i + 1, z.rstrip())) else: print('%4s %r' % (i + 1, str(z))) if settings.get('stats'): beautifier.print_stats() if not ok: print('*************** fail: %s ***************' % (h)) return beautifier