def open_theme_file(self, event):
    """Open a theme file in a new session and apply the theme."""
    c = event and event.get('c')
    if not c:
        return
    # Get the file name.
    themes_dir = g.os_path_finalize_join(g.app.loadDir, '..', 'themes')
    fn = g.app.gui.runOpenFileDialog(
        c,
        title="Open Theme File",
        filetypes=[
            ("Leo files", "*.leo *.db"),
            ("All files", "*"),
        ],
        defaultextension=g.defaultLeoFileExtension(c),
        startpath=themes_dir,
    )
    if not fn:
        return
    leo_dir = g.os_path_finalize_join(g.app.loadDir, '..', '..')
    os.chdir(leo_dir)
    #
    # #1425: Open the theme file in a separate process.
    command = f'{g.sys.executable} {g.app.loadDir}/runLeo.py "{fn}"'
    g.execute_shell_commands(command)  # #1564.
    os.chdir(leo_dir)
示例#2
0
def run(fn, verbose):
    '''Run pylint on fn.'''
    trace = False and not g.unitTesting
    # theDir is empty for the -f option.
    from pylint import lint
    assert lint
    rc_fn = os.path.abspath(os.path.join('leo','test','pylint-leo-rc.txt'))
    if not os.path.exists(rc_fn):
        print('pylint rc file not found: %s' % (rc_fn))
        return
    if verbose:
        path = g.os_path_dirname(fn)
        dirs = path.split(os.sep)
        theDir = dirs and dirs[-1] or ''
        print('pylint-leo.py: %s%s%s' % (theDir,os.sep,g.shortFileName(fn)))
    # Call pylint in a subprocess so Pylint doesn't abort *this* process.
    args = ','.join([
        "fn=r'%s'" % (fn),
        "rc=r'%s'" % (rc_fn),
    ])
    if 0: # Prints error number.
        args.append('--msg-template={path}:{line}: [{msg_id}({symbol}), {obj}] {msg}')
    command = '%s -c "import leo.core.leoGlobals as g; g.run_pylint(%s)"' % (
        sys.executable, args)
    t1 = time.clock()
    g.execute_shell_commands(command)
    t2 = time.clock()
    if trace:
        g.trace('%4.2f %s' % (t2-t1, g.shortFileName(fn)))
示例#3
0
def run(fn, verbose):
    '''Run pylint on fn.'''
    trace = False and not g.unitTesting
    # theDir is empty for the -f option.
    from pylint import lint
    assert lint
    rc_fn = os.path.abspath(os.path.join('leo', 'test', 'pylint-leo-rc.txt'))
    if not os.path.exists(rc_fn):
        print('pylint rc file not found: %s' % (rc_fn))
        return
    if verbose:
        path = g.os_path_dirname(fn)
        dirs = path.split(os.sep)
        theDir = dirs and dirs[-1] or ''
        print('pylint-leo.py: %s%s%s' % (theDir, os.sep, g.shortFileName(fn)))
    # Call pylint in a subprocess so Pylint doesn't abort *this* process.
    args = ','.join([
        "fn=r'%s'" % (fn),
        "rc=r'%s'" % (rc_fn),
    ])
    if 0:  # Prints error number.
        args.append(
            '--msg-template={path}:{line}: [{msg_id}({symbol}), {obj}] {msg}')
    command = '%s -c "import leo.core.leoGlobals as g; g.run_pylint(%s)"' % (
        sys.executable, args)
    t1 = time.clock()
    g.execute_shell_commands(command)
    t2 = time.clock()
    if trace:
        g.trace('%4.2f %s' % (t2 - t1, g.shortFileName(fn)))
示例#4
0
    def command_helper(self, event, kind, preview, verbose):
        def predicate(p):
            return self.filename(p)

        # Find all roots.

        t1 = time.time()
        c = self.c
        self.kind = kind
        p = event.p if event and hasattr(event, 'p') else c.p
        roots = g.findRootsWithPredicate(c, p, predicate=predicate)
        if not roots:
            g.warning('No @adoc nodes in', p.h)
            return []
        # Write each root to a file.
        i_paths = []
        for p in roots:
            try:
                i_path = self.filename(p)
                # #1398.
                i_path = c.expand_path_expression(i_path)
                i_path = g.os_path_finalize(i_path)
                with open(i_path, 'w', encoding='utf-8',
                          errors='replace') as self.output_file:
                    self.write_root(p)
                    i_paths.append(i_path)
            except IOError:
                g.es_print(f"Can not open {i_path!r}")
            except Exception:
                g.es_print(f"Unexpected exception opening {i_path!r}")
                g.es_exception()
        # Convert each file to html.
        o_paths = []
        for i_path in i_paths:
            o_path = self.compute_opath(i_path)
            o_paths.append(o_path)
            if kind == 'adoc':
                self.run_asciidoctor(i_path, o_path)
            elif kind == 'pandoc':
                self.run_pandoc(i_path, o_path)
            elif kind == 'sphinx':
                self.run_sphinx(i_path, o_path)
            else:
                g.trace('BAD KIND')
                return None
            if kind != 'sphinx':
                print(f"{kind}: wrote {o_path}")
        if preview:
            if kind == 'sphinx':
                g.es_print('preview not available for sphinx')
            else:
                # open .html files in the default browser.
                g.execute_shell_commands(o_paths)
        t2 = time.time()
        if verbose:
            n = len(i_paths)
            g.es_print(f"{kind}: wrote {n} file{g.plural(n)} "
                       f"in {(t2-t1):4.2f} sec.")
        return i_paths
示例#5
0
 def run_pandoc(self, i_path, o_path):
     """
      Process the input file given by i_path with pandoc.
     """
     global pandoc_exec
     assert pandoc_exec, g.callers()
     # Call pandoc to write the output file.
     command = f"pandoc {i_path} -t html5 -o {o_path}"
     # --quiet does no harm.
     g.execute_shell_commands(command)
示例#6
0
 def run_asciidoctor(self, i_path, o_path):
     """
     Process the input file given by i_path with asciidoctor or asciidoc3.
     """
     global asciidoctor_exec, asciidoc3_exec
     assert asciidoctor_exec or asciidoc3_exec, g.callers()
     # Call the external program to write the output file.
     prog = 'asciidoctor' if asciidoctor_exec else 'asciidoc3'
     command = f"{prog} {i_path} -o {o_path} -b html5"
     # The -e option deletes css.
     g.execute_shell_commands(command)
示例#7
0
def test_leo_client_and_server(event=None):
    """
    Test leoserver.py with leoclient.py.

    The test-all command does *not* run this command, because there is
    no corresponding test*.py file.
    """
    g.cls()
    leo_dir = os.path.abspath(os.path.join(g.app.loadDir, '..', '..'))
    assert os.path.exists(leo_dir), repr(leo_dir)
    os.chdir(leo_dir)
    g.execute_shell_commands(
        'start cmd /k "python -m leo.core.leoserver --trace=request,response,verbose"'
    )
    time.sleep(1.5)
    g.execute_shell_commands('start cmd /k "python -m leo.core.leoclient"')
示例#8
0
def blacken_files(event):
    """Run black on one or more files at c.p."""
    c = event.get('c')
    if not c or not c.p:
        return
    tag = 'blacken-files'
    if not black:
        g.es_print(f"{tag} can not import black")
        return
    for root in g.findRootsWithPredicate(c, c.p):
        path = g.fullPath(c, root)
        if path and os.path.exists(path):
            g.es_print(f"{tag}: {path}")
            g.execute_shell_commands(f"&black --skip-string-normalization {path}")
        else:
            print(f"{tag}: file not found:{path}")
            g.es(f"{tag}: file not found:\n{path}")
示例#9
0
def flake8_command(event):
    """
    Run flake8 on all nodes of the selected tree,
    or the first @<file> node in an ancestor.
    """
    tag = 'flake8-files'
    if not flake8:
        g.es_print(f"{tag} can not import flake8")
        return
    c = event and event.get('c')
    if not c or not c.p:
        return
    python = sys.executable
    for root in g.findRootsWithPredicate(c, c.p):
        path = g.fullPath(c, root)
        if path and os.path.exists(path):
            g.es_print(f"{tag}: {path}")
            g.execute_shell_commands(f'&"{python}" -m flake8 "{path}"')
        else:
            g.es_print(f"{tag}: file not found:{path}")
示例#10
0
def blacken_files_diff(event):
    """
    Show the diffs that would result from blacking the external files at
    c.p.
    """
    c = event.get('c')
    if not c or not c.p:
        return
    tag = 'blacken-files-diff'
    if not black:
        g.es_print(f"{tag} can not import black")
        return
    for root in g.findRootsWithPredicate(c, c.p):
        path = g.fullPath(c, root)
        if path and os.path.exists(path):
            g.es_print(f"{tag}: {path}")
            g.execute_shell_commands(f"&black --skip-string-normalization --diff {path}")
        else:
            print(f"{tag}: file not found:{path}")
            g.es(f"{tag}: file not found:\n{path}")
示例#11
0
 def run_sphinx(self, i_path, o_path):
     """Process i_path and o_path with sphinx."""
     trace = True
     # cd to the command directory, or i_path's directory.
     command_dir = g.os_path_finalize(self.sphinx_command_dir
                                      or os.path.dirname(i_path))
     if os.path.exists(command_dir):
         if trace:
             g.trace(f"\nos.chdir: {command_dir!r}")
         os.chdir(command_dir)
     else:
         g.error(f"command directory not found: {command_dir!r}")
         return
     #
     # If a default command exists, just call it.
     # The user is responsible for making everything work.
     if self.sphinx_default_command:
         if trace:
             g.trace(f"\ncommand: {self.sphinx_default_command!r}\n")
         g.execute_shell_commands(self.sphinx_default_command)
         return
     # Compute the input directory.
     input_dir = g.os_path_finalize(self.sphinx_input_dir
                                    or os.path.dirname(i_path))
     if not os.path.exists(input_dir):
         g.error(f"input directory not found: {input_dir!r}")
         return
     # Compute the output directory.
     output_dir = g.os_path_finalize(self.sphinx_output_dir
                                     or os.path.dirname(o_path))
     if not os.path.exists(output_dir):
         g.error(f"output directory not found: {output_dir!r}")
         return
     #
     # Call sphinx-build to write the output file.
     # sphinx-build [OPTIONS] SOURCEDIR OUTPUTDIR [FILENAMES...]
     command = f"sphinx-build {input_dir} {output_dir} {i_path}"
     if trace:
         g.trace(f"\ncommand: {command!r}\n")
     g.execute_shell_commands(command)
示例#12
0
def run(theDir,fn,rpython=False):
    '''Run pylint on fn.'''
    fn = os.path.join('leo',theDir,fn)
    rc_fn = os.path.abspath(os.path.join('leo','test','pylint-leo-rc.txt'))
    # print('run:scope:%s' % scope)
    fn = os.path.abspath(fn)
    if not fn.endswith('.py'): fn = fn+'.py'
    if not os.path.exists(rc_fn):
        print('pylint rc file not found:',rc_fn)
        return
    if not os.path.exists(fn):
        print('file not found:',fn)
        return
    # Report the file name and one level of directory.
    path = g.os_path_dirname(fn)
    dirs = path.split(os.sep)
    theDir = dirs and dirs[-1] or ''
    print('pylint-leo.py: %s%s%s' % (theDir,os.sep,g.shortFileName(fn)))
    # Create the required args.
    args = ','.join([
        "fn=r'%s'" % (fn),
        "rc=r'%s'" % (rc_fn),
    ])
    if scope == 'stc-test': # The --tt option.
        # Report that Sherlock is enabled.
        print('pylint-leo.py --tt: enabling Sherlock traces')
        print('pylint-leo.py --tt: patterns contained in plyint-leo.py')
        # Report the source code.
        s = open(fn).read()
        print('pylint-leo.py: source:\n\n%s\n' % s)
        # Add the optional Sherlock args.
        dots = True
        patterns=[ 
        #@+<< Sherlock patterns for pylint >>
        #@+node:ekr.20130111060235.10182: *3* << Sherlock patterns for pylint >>
        # Note:  A leading * is never valid: change to .*

        '+.*infer*',
            # '+.*infer_name',
            '+.*infer_stmts',
            
        '+YES::__init__',

        ###'+TypeChecker::add_message',
            # '+.*add_message',
            # '+PyLinter::add_message',
            # '+TextReporter::add_message'

        # '+.*visit*',
            # '+TypeChecker::visit_getattr',
            # '+.*visit_class',
            # '+Basic*::visit_*',
            
        # '+.*__init__',
            # '+Instance::__init__',
            # '+Class::__init__',
            # '+Module::__init__',
            # '+Function::__init__',

        ###'+:.*typecheck.py',
        ###'+:.*inference.py',
        ###'+:.*variables.py',

        ###### Old traces

        # '+:.*bases.py',
        # '+.*path_raise_wrapper',

        # Enable everything.
        # # '+.*',

        # # Disable entire files.
        # # '-:.*\\lib\\.*', # Disables everything.

        # # Pylint files.
        # #'-:.*base.py',
        # #'-:.*bases.py',
        # '-:.*builder.py',
        # '-:.*__init__.py',
        # '-:.*format.py',
        # '-:.*interface.py', # implements
        # '-:.*rebuilder.py',
        # #'-:.*scoped_nodes',
        # # General library files.
        # '-:.*leoGlobals.py',
        # '-:.*codecs.py',
        # '-:.*config.py',
        # '-:.*configuration.py',
        # '-:.*ConfigParser.py',
        # '-:.*copy\.py',
        # '-:.*gettext.py',
        # '-:.*genericpath.py',
        # '-:.*graph.py',
        # '-:.*locale.py',
        # '-:.*optik_ext.py',
        # '-:.*optparse.py',
        # '-:.*os.py',
        # '-:.*ntpath.py',
        # '-:.*pickle.py',
        # '-:.*re.py',
        # '-:.*similar.py',
        # '-:.*shlex.py',
        # '-:.*sre_compile.py',
        # '-:.*sre_parse.py',
        # '-:.*string_escape.py',
        # '-:.*text.py',
        # '-:.*threading.py',
        # '-:.*tokenize.py',
        # '-:.*utils.py',

        # # Enable entire files.
        # # '+:.*base.py',
        # # '+:.*bases.py',
        # # '+:.*classes.py',
        # # '+:.*design_analysis.py',
        # # '+:.*format.py',
        # # '+:.*inference.py',
        # # '+:.*logging.py',
        # # '+:.*mixins.py',
        # # '+:.*newstyle.py',
        # # '+:.*node_classes.py',
        # # '+:.*protocols.py',
        # # '+:.*scoped_nodes.py',
        # # '+:.*typecheck.py',
        # # '+:.*variables.py',

        # # Disable individual methods.
        # '-close', # __init__.py
        # '-collect_block_lines', '-\<genexpr\>','-.*option.*','-.*register_checker','-set_reporter', # lint.py
        # '-frame','-root','-scope', # scoped_nodes
        # '-register', # various files.

        # # '-abspath','-normpath','-isstring','-normalize',
        # # '-splitext','-_splitext','-splitdrive','-splitstrip',
        # # '-.*option.*','-get','-set_option',
        # # '-unquote','-convert','-interpolate','-_call_validator', # compile stuff.
        # # '-_compile.*','-compile_.*','-_code','-identifyfunction', # compile stuff.
        # # '-_parse.*','-set_parser','-set_conflict_handler',
        # # '-append','-match',
        # # '-isbasestring',
        # # '-save.*','-memoize','-put',

        # # '-persistent_id',
        # # '-__next',
        # # '-nodes_of_class',
        # # '-__.*',
        # # '-_check.*',
        # # '-_.*',
        # # '-load_.*',
        #@-<< Sherlock patterns for pylint >>
        #@afterref
 ]
        show_return = True
        stats_patterns = [ 
        #@+<< Sherlock stats patterns for pylint >>
        #@+node:ekr.20140327164521.16846: *3* << Sherlock stats patterns for pylint >>
        # '+.*__init__',
            # astroid.bases.py
            '+BoundMethod::__init__',
            '+InferenceContext::__init__',
            '+Instance::__init__',
            '+UnboundMethod::__init__',
            # astroid.node_classes.py
            '+Arguments::__init__',
            '+CallFunc::__init__',
            '+Const::__init__',
            # astroid.scoped_nods.py
            '+Class::__init__',
            '+Function::__init__',
            '+Module::__init__',
        #@-<< Sherlock stats patterns for pylint >>
        #@afterref
 ]
        verbose = True
        args = args + ',' + ','.join([
            'dots=%s' % (dots),
            'patterns=%s' % (patterns),
            'sherlock=True',
            'show_return=%s' % (show_return),
            'stats_patterns=%s' % (stats_patterns),
            'verbose=%s' % (verbose),
        ])
    # Execute the command in a separate process.
    command = '%s -c "import leo.core.leoGlobals as g; g.run_pylint(%s)"' % (
        sys.executable,args)
    g.execute_shell_commands(command)   
示例#13
0
def run(theDir, fn, silent, rpython=False):
    '''Run pylint on fn.'''
    global rc_warning_given
    # A little hack. theDir is empty for the -f option.
    if theDir:
        fn = os.path.join('leo', theDir, fn)
    rc_fn = os.path.abspath(os.path.join('leo', 'test', 'pylint-leo-rc.txt'))
    fn = os.path.abspath(fn)
    if not fn.endswith('.py'):
        fn = fn + '.py'
    if not os.path.exists(rc_fn):
        if not rc_warning_given:
            rc_warning_given = True
            print('pylint rc file not found: %s' % (rc_fn))
    if not os.path.exists(fn):
        print('file not found: %s' % (fn))
        return 0.0
    # Report the file name and one level of directory.
    path = g.os_path_dirname(fn)
    dirs = path.split(os.sep)
    theDir = dirs and dirs[-1] or ''
    if not silent:
        print('pylint-leo.py: %s%s%s' % (theDir, os.sep, g.shortFileName(fn)))
    # Create the required args.
    args = ','.join([
        "fn=r'%s'" % (fn),
        "rc=r'%s'" % (rc_fn),
    ])
    if scope == 'stc-test':  # The --tt option.
        # Report that Sherlock is enabled.
        print('pylint-leo.py --tt: enabling Sherlock traces')
        print('pylint-leo.py --tt: patterns contained in plyint-leo.py')
        # Report the source code.
        s = open(fn).read()
        print('pylint-leo.py: source:\n\n%s\n' % s)
        # Add the optional Sherlock args.
        dots = True
        patterns = [
            #@+<< Sherlock patterns for pylint >>
            #@+node:ekr.20130111060235.10182: *3* << Sherlock patterns for pylint >>
            #@@nobeautify

            # Note:  A leading * is never valid: change to .*
            '+.*infer*',
            # '+.*infer_name',
            '+.*infer_stmts',
            '+YES::__init__',

            # '+TypeChecker::add_message',
            # '+.*add_message',
            # '+PyLinter::add_message',
            # '+TextReporter::add_message'

            # '+.*visit*',
            # '+TypeChecker::visit_getattr',
            # '+.*visit_class',
            # '+Basic*::visit_*',

            # '+.*__init__',
            # '+Instance::__init__',
            # '+Class::__init__',
            # '+Module::__init__',
            # '+Function::__init__',

            # '+:.*typecheck.py',
            # '+:.*inference.py',
            # '+:.*variables.py',

            # Old traces

            # '+:.*bases.py',
            # '+.*path_raise_wrapper',

            # Enable everything.
            # # '+.*',

            # # Disable entire files.
            # # '-:.*\\lib\\.*', # Disables everything.

            # # Pylint files.
            # #'-:.*base.py',
            # #'-:.*bases.py',
            # '-:.*builder.py',
            # '-:.*__init__.py',
            # '-:.*format.py',
            # '-:.*interface.py', # implements
            # '-:.*rebuilder.py',
            # #'-:.*scoped_nodes',
            # # General library files.
            # '-:.*leoGlobals.py',
            # '-:.*codecs.py',
            # '-:.*config.py',
            # '-:.*configuration.py',
            # '-:.*ConfigParser.py',
            # '-:.*copy\.py',
            # '-:.*gettext.py',
            # '-:.*genericpath.py',
            # '-:.*graph.py',
            # '-:.*locale.py',
            # '-:.*optik_ext.py',
            # '-:.*optparse.py',
            # '-:.*os.py',
            # '-:.*ntpath.py',
            # '-:.*pickle.py',
            # '-:.*re.py',
            # '-:.*similar.py',
            # '-:.*shlex.py',
            # '-:.*sre_compile.py',
            # '-:.*sre_parse.py',
            # '-:.*string_escape.py',
            # '-:.*text.py',
            # '-:.*threading.py',
            # '-:.*tokenize.py',
            # '-:.*utils.py',

            # # Enable entire files.
            # # '+:.*base.py',
            # # '+:.*bases.py',
            # # '+:.*classes.py',
            # # '+:.*design_analysis.py',
            # # '+:.*format.py',
            # # '+:.*inference.py',
            # # '+:.*logging.py',
            # # '+:.*mixins.py',
            # # '+:.*newstyle.py',
            # # '+:.*node_classes.py',
            # # '+:.*protocols.py',
            # # '+:.*scoped_nodes.py',
            # # '+:.*typecheck.py',
            # # '+:.*variables.py',

            # # Disable individual methods.
            # '-close', # __init__.py
            # '-collect_block_lines', '-\<genexpr\>','-.*option.*','-.*register_checker','-set_reporter', # lint.py
            # '-frame','-root','-scope', # scoped_nodes
            # '-register', # various files.

            # # '-abspath','-normpath','-isstring','-normalize',
            # # '-splitext','-_splitext','-splitdrive','-splitstrip',
            # # '-.*option.*','-get','-set_option',
            # # '-unquote','-convert','-interpolate','-_call_validator', # compile stuff.
            # # '-_compile.*','-compile_.*','-_code','-identifyfunction', # compile stuff.
            # # '-_parse.*','-set_parser','-set_conflict_handler',
            # # '-append','-match',
            # # '-isbasestring',
            # # '-save.*','-memoize','-put',

            # # '-persistent_id',
            # # '-__next',
            # # '-nodes_of_class',
            # # '-__.*',
            # # '-_check.*',
            # # '-_.*',
            # # '-load_.*',
            #@-<< Sherlock patterns for pylint >>
        ]
        show_return = True
        stats_patterns = [
            #@+<< Sherlock stats patterns for pylint >>
            #@+node:ekr.20140327164521.16846: *3* << Sherlock stats patterns for pylint >>
            #@@nobeautify

            # '+.*__init__',
            # astroid.bases.py
            '+BoundMethod::__init__',
            '+InferenceContext::__init__',
            '+Instance::__init__',
            '+UnboundMethod::__init__',
            # astroid.node_classes.py
            '+Arguments::__init__',
            '+CallFunc::__init__',
            '+Const::__init__',
            # astroid.scoped_nods.py
            '+Class::__init__',
            '+Function::__init__',
            '+Module::__init__',
            #@-<< Sherlock stats patterns for pylint >>
        ]
        verbose = True
        args = args + ',' + ','.join([
            'dots=%s' % (dots),
            'patterns=%s' % (patterns),
            'sherlock=True',
            'show_return=%s' % (show_return),
            'stats_patterns=%s' % (stats_patterns),
            'verbose=%s' % (verbose),
        ])
    # Execute the command in a separate process.
    command = '%s -c "import leo.core.leoGlobals as g; g.run_pylint(%s)"' % (
        sys.executable, args)
    t1 = time.clock()
    g.execute_shell_commands(command)
    t2 = time.clock()
    return t2 - t1