Example #1
0
 def test_inplace_with_multi_files(self):
     with disable_stderr():
         try:
             autopep8.parse_args(['test.py', 'dummy.py'])
             self.assertEqual("not work", "test has failed!!")
         except SystemExit as e:
             self.assertEqual(e.code, 2)
Example #2
0
 def test_no_argument(self):
     with disable_stderr():
         try:
             autopep8.parse_args([])
             self.assertEqual("not work", "test has failed!!")
         except SystemExit as e:
             self.assertEqual(e.code, 2)
Example #3
0
def pram2mesa(sim: Simulation, name: str, autopep: bool = True) -> None:
    """
    Converts a PyPRAM simulation object to equivalent Mesa Agent and Model classes.
    This function should be the only function a user must call.

    :param sim: The PyPRAM Simulation to translate
    :param name: The prefix of the files to be created.
    :param autopep: Should the files be run through autopep8 to clean the code?
                    If autopep evaluates to False, autopep8 will not be used.
                    If custom autopep8 usage is desired, set autopep to False and do so manually
    :return: None. Creates two Python files containing the new Mesa Agent and Model classes and three JSON data files.
             From there, instantiate one of these Models and proceed using standard Mesa tools.
    """
    directory = _make_filename(name, extension='')
    os.mkdir(directory)
    os.chdir(directory)
    # model relies on make_python_identifier so we pack it up
    shutil.copy(inspect.getsourcefile(mpi), '.')
    group_file, site_file, rule_file = create_json_data(sim, name)
    rw = RuleWriter()

    new_rules, rule_imports = translate_rules([type(r) for r in sim.rules], rw)
    top_level_rules = [type(r).__name__ for r in sim.rules]

    group_setup = sim.fn.group_setup or ''
    if group_setup:
        tree = ast.parse(textwrap.dedent(inspect.getsource(group_setup)))
        tree.body[0].name = '_group_setup'
        # tree.body[0].args.args[0].arg = 'self'  # FIXME: this is hacky
        tree.body[0].decorator_list.append('staticmethod')
        group_setup = astor.to_source(rw.visit(tree))

    agent_file = create_agent_class(name,
                                    new_rules,
                                    top_level_rules,
                                    rule_file,
                                    used_functions=rw.used,
                                    custom_imports='\n'.join(rule_imports))
    model_file = create_model_class(name,
                                    group_file,
                                    site_file,
                                    agent_file,
                                    top_level_rules,
                                    group_setup,
                                    used_functions=rw.used)

    if autopep:
        autopep8.fix_file(agent_file,
                          options=autopep8.parse_args(
                              ['--in-place', agent_file]))
        autopep8.fix_file(model_file,
                          options=autopep8.parse_args(
                              ['--in-place', model_file]))
Example #4
0
def formatters(aggressive, apply_config, filename=''):
    """Return list of code formatters."""
    if aggressive:
        yield autoflake.fix_code
        autopep8_options = autopep8.parse_args(
            [filename] + int(aggressive) * ['--aggressive'],
            apply_config=apply_config)
    else:
        autopep8_options = autopep8.parse_args([filename],
                                               apply_config=apply_config)

    yield lambda code: autopep8.fix_code(code, options=autopep8_options)
    yield docformatter.format_code
    yield unify.format_code
Example #5
0
    def on_source_autopep8_mi(self, mi):

        try:
            import autopep8
        except:
            logging.exception("Can't use autopep8")
        else:

            buff = self.main_window.current_buffer

            if buff is not None:

                b = buff.get_buffer()

                t = b.get_text(b.get_start_iter(), b.get_end_iter(), False)

                buff.save_state()

                t = autopep8.fix_code(t,
                                      options=autopep8.parse_args([
                                          '--aggressive', '--ignore', 'E123',
                                          ''
                                      ]))

                b.set_text(t)

                buff.restore_state()

        return
Example #6
0
def gen(path,
        method=None,
        query_params=None,
        data=None,
        content_type=None):
    # generates python code representing a call via django client.
    # useful for use in testing
    method = method.lower()
    t = jinja2.Template(template)
    if method == 'get':
        r = t.render(path=path,
                     data=query_params,
                     lower_case_method=method,
                     content_type=content_type)
    else:
        if query_params:
            query_params = _encode_query_params(query_params)
            path += query_params
        if is_str_typ(data):
            data = "'%s'" % data
        r = t.render(path=path,
                     data=data,
                     lower_case_method=method,
                     query_params=query_params,
                     content_type=content_type)
    return autopep8.fix_code(
        r, options=autopep8.parse_args(['--aggressive', ''])
    )
Example #7
0
    def test_pep8(self):
        # verify all files are nicely styled
        python_filepaths = get_python_filepaths()
        style_guide = get_style_guide()
        fake_stdout = io.StringIO()
        with patch('sys.stdout', fake_stdout):
            report = style_guide.check_files(python_filepaths)

        # if flake8 didnt' report anything, we're done
        if report.total_errors == 0:
            return

        # grab on which files we have issues
        flake8_issues = fake_stdout.getvalue().split('\n')
        broken_filepaths = {
            item.split(':')[0]
            for item in flake8_issues if item
        }

        # give hints to the developer on how files' style could be improved
        options = autopep8.parse_args([''])
        options.aggressive = 1
        options.diff = True
        options.max_line_length = 99

        issues = []
        for filepath in broken_filepaths:
            diff = autopep8.fix_file(filepath, options=options)
            if diff:
                issues.append(diff)

        report = ["Please fix files as suggested by autopep8:"] + issues
        report += ["\n-- Original flake8 reports:"] + flake8_issues
        self.fail("\n".join(report))
Example #8
0
def main():
    readme_path = 'README.rst'
    before_key = 'Before running autopep8.\n\n.. code-block:: python'
    after_key = 'After running autopep8.\n\n.. code-block:: python'
    options_key = 'Options::'

    (top, before, bottom) = split_readme(readme_path,
                                         before_key=before_key,
                                         after_key=after_key,
                                         options_key=options_key,
                                         end_key='Features\n========')

    input_code = textwrap.dedent(before)

    output_code = autopep8.fix_code(
        input_code,
        options=autopep8.parse_args(['', '-aa']))

    check(output_code)

    new_readme = '\n\n'.join([
        top,
        before_key, before,
        after_key, indent(output_code).rstrip(),
        options_key, indent(help_message()),
        bottom])

    with open(readme_path, 'w') as output_file:
        output_file.write(new_readme)
Example #9
0
    def check_file(self, path):
        """
        Check one regular file with pylint for py syntax errors.

        :param path: Path to a regular file.
        :return: False, if pylint found syntax problems, True, if pylint didn't
                 find problems, or path is not a python module or script.
        """
        inspector = PathInspector(path)
        if not inspector.is_python():
            return True
        opt_obj = pep8.StyleGuide().options
        ignore_list = self.ignored_errors.split(',') + list(opt_obj.ignore)
        opt_obj.ignore = tuple(set(ignore_list))
        runner = pep8.Checker(filename=path, options=opt_obj)
        status = runner.check_all()
        if status != 0:
            log.error('PEP8 check fail: %s', path)
            self.failed_paths.append(path)
            log.error('Trying to fix errors with autopep8')
            try:
                opt_obj = autopep8.parse_args([path,
                                               '--ignore',
                                               self.ignored_errors,
                                               '--in-place'])
                autopep8.fix_file(path, opt_obj)
            except Exception, details:
                log.error('Not able to fix errors: %s', details)
Example #10
0
def check(expected_filename, input_filename, aggressive):
    """Test and compare output.

    Return True on success.

    """
    got = autopep8.fix_file(
        input_filename,
        options=autopep8.parse_args([''] + aggressive * ['--aggressive']))

    try:
        with autopep8.open_with_encoding(expected_filename) as expected_file:
            expected = expected_file.read()
    except IOError:
        expected = None

    if expected == got:
        return True
    else:
        got_filename = expected_filename + '.err'
        encoding = autopep8.detect_encoding(input_filename)

        with autopep8.open_with_encoding(got_filename,
                                         encoding=encoding,
                                         mode='w') as got_file:
            got_file.write(got)

        print('{begin}{got} does not match expected {expected}{end}'.format(
            begin=RED, got=got_filename, expected=expected_filename, end=END),
              file=sys.stdout)

        return False
def gen(path,
        method=None,
        query_params=None,
        data=None,
        content_type=None):
    # generates python code representing a call via django client.
    # useful for use in testing
    method = method.lower()
    t = jinja2.Template(template)
    if method == 'get':
        r = t.render(path=path,
                     data=query_params,
                     lower_case_method=method,
                     content_type=content_type)
    else:
        if query_params:
            query_params = _encode_query_params(query_params)
            path += query_params
        if is_str_typ(data):
            data = "'%s'" % data
        r = t.render(path=path,
                     data=data,
                     lower_case_method=method,
                     query_params=query_params,
                     content_type=content_type)
    return autopep8.fix_code(
        r, options=autopep8.parse_args(['--aggressive', ''])
    )
Example #12
0
def create_plugin_code(code, name, category=None, menu=False, toolbar=False,
                       icon=None):
    """Create a plugin with an action that will execute 'code' when triggered.
    If 'menu' and/or 'toolbar' is True, the corresponding items will be added
    for the action.
    """

    if category is None:
        category = name

    safe_name = string.capwords(name).replace(" ", "")
    plugin_code = header.format(safe_name, name)
    if icon is None:
        plugin_code += action_noicon.format()
    else:
        plugin_code += action_icon.format(icon)
    if menu:
        plugin_code += menu_def.format(category)
    if toolbar:
        plugin_code += toolbar_def.format(category)

    # Indent code by two levels
    code = indent(code, 2 * 4)
    plugin_code += default.format(code)
    try:
        import autopep8
        plugin_code = autopep8.fix_code(plugin_code,
                                        options=autopep8.parse_args(
                                         ['--aggressive', '--aggressive', '']))
    except ImportError:
        pass
    return plugin_code
Example #13
0
def autopep8(args):
    """Format code according to PEP8
    """
    try:
        import autopep8
    except:
        error('autopep8 not found! Run "paver install_devtools".')
        sys.exit(1)

    if any(x not in args for x in ['-i', '--in-place']):
        args.append('-i')

    args.append('--ignore=E261,E265,E402,E501')
    args.insert(0, 'dummy')

    cmd_args = autopep8.parse_args(args)

    ext_libs_basename = os.path.basename(options.plugin.ext_libs)
    ext_src_basename = os.path.basename(options.plugin.ext_src)
    excludes = (ext_libs_basename, ext_src_basename)
    for p in options.plugin.source_dir.walk():
        if any(exclude in p for exclude in excludes):
            continue

        if p.fnmatch('*.py'):
            autopep8.fix_file(p, options=cmd_args)
Example #14
0
 def __format_file(self, path):
     autopep8.fix_file(
         path,
         options=autopep8.parse_args(
             ['--in-place', '--aggressive', path]
         )
     )
Example #15
0
    def check_file(self, path):
        """
        Check one regular file with pylint for py syntax errors.

        :param path: Path to a regular file.
        :return: False, if pylint found syntax problems, True, if pylint didn't
                 find problems, or path is not a python module or script.
        """
        inspector = PathInspector(path)
        if not inspector.is_python():
            return True
        opt_obj = pep8.StyleGuide().options
        ignore_list = self.ignored_errors.split(',') + list(opt_obj.ignore)
        opt_obj.ignore = tuple(set(ignore_list))
        runner = pep8.Checker(filename=path, options=opt_obj)
        status = runner.check_all()
        if status != 0:
            log.error('PEP8 check fail: %s', path)
            self.failed_paths.append(path)
            log.error('Trying to fix errors with autopep8')
            try:
                opt_obj = autopep8.parse_args(
                    [path, '--ignore', self.ignored_errors, '--in-place'])
                autopep8.fix_file(path, opt_obj)
            except Exception, details:
                log.error('Not able to fix errors: %s', details)
Example #16
0
def main():
    readme_path = 'README.rst'
    before_key = 'Before running autopep8.\n\n.. code-block:: python'
    after_key = 'After running autopep8.\n\n.. code-block:: python'

    (top, before, bottom) = split_readme(readme_path,
                                         before_key=before_key,
                                         after_key=after_key,
                                         end_key='Options::')

    input_code = textwrap.dedent(before)

    output_code = autopep8.fix_string(
        input_code,
        options=autopep8.parse_args(['', '--aggressive'])[0])
    compile(output_code, '<string>', 'exec', dont_inherit=True)

    new_readme = '\n\n'.join([
        top,
        before_key, before,
        after_key, indent(output_code),
        bottom])

    with open(readme_path, 'w') as output_file:
        output_file.write(new_readme)
Example #17
0
def main():
    readme_path = 'README.rst'
    before_key = 'Before running autopep8.\n\n.. code-block:: python'
    after_key = 'After running autopep8.\n\n.. code-block:: python'
    options_key = 'Options::'

    (top, before, bottom) = split_readme(readme_path,
                                         before_key=before_key,
                                         after_key=after_key,
                                         options_key=options_key,
                                         end_key='Features\n========')

    input_code = textwrap.dedent(before)

    output_code = autopep8.fix_code(input_code,
                                    options=autopep8.parse_args(['', '-aa']))

    check(output_code)

    new_readme = '\n\n'.join([
        top, before_key, before, after_key,
        indent(output_code).rstrip(), options_key,
        indent(help_message()), bottom
    ])

    with open(readme_path, 'w') as output_file:
        output_file.write(new_readme)
Example #18
0
 def _inner_setup(self, line):
     f = open(self.tempfile[1], 'w')
     f.write(line)
     f.close()
     opts, _ = autopep8.parse_args([self.tempfile[1]])
     sio = StringIO()
     autopep8.fix_file(filename=self.tempfile[1], opts=opts, output=sio)
     self.result = sio.getvalue()
Example #19
0
 def _inner_setup(self, line, options=()):
     with open(self.tempfile[1], 'w') as temp_file:
         temp_file.write(line)
     opts, _ = autopep8.parse_args([self.tempfile[1]] + list(options))
     sio = StringIO()
     autopep8.fix_file(filename=self.tempfile[1],
                       opts=opts,
                       output=sio)
     self.result = sio.getvalue()
Example #20
0
def _write_code(code: str, base_path: str, file_name: str) -> None:
    file_path: str = os.path.join(base_path, file_name)
    logger.info(f'write {file_path}')
    fixed_init_py: str = fix_code(code,
                                  options=parse_args(
                                      ['--aggressive', '--aggressive', '']))

    with open(file_path, 'w') as f:
        f.write(fixed_init_py)
Example #21
0
 def _inner_setup(self, line):
     f = open(self.tempfile[1], 'w')
     f.write(line)
     f.close()
     opts, _ = autopep8.parse_args([self.tempfile[1]])
     sio = StringIO()
     autopep8.fix_file(filename=self.tempfile[1],
                       opts=opts,
                       output=sio)
     self.result = sio.getvalue()
    def get_options(self):
        options = autopep8.parse_args([''])[0]
        options.max_line_length = self.settings.get('max-line-length', 79)
        options.aggressive = self.settings.get('aggressive', False)
        if self.settings.get('ignore', []):
            options.ignore = ','.join(self.settings.get('ignore', []))
        if self.settings.get('select', []):
            options.select = ','.join(self.settings.get('select', []))

        return options
Example #23
0
    def test_autopep8_args(self):
        # TODO see that these are passes on (use a static method in Radius?)

        args = ["hello.py"]
        us = parse_args(args)
        them = autopep8.parse_args(args)
        self.assertEqual(us.select, them.select)
        self.assertEqual(us.ignore, them.ignore)

        args = ["hello.py", "--select=E1,W1", "--ignore=W601", "--max-line-length", "120"]
        us = parse_args(args)
        them = autopep8.parse_args(args)
        self.assertEqual(us.select, them.select)
        self.assertEqual(us.ignore, them.ignore)
        self.assertEqual(us.max_line_length, them.max_line_length)

        args = ["hello.py", "--aggressive", "-v"]
        us = parse_args(args)
        them = autopep8.parse_args(args)
        self.assertEqual(us.aggressive, them.aggressive)
    def get_options(self):
        options = autopep8.parse_args([''])[0]
        options.max_line_length = self.settings.get(
            'max-line-length', 79)
        options.aggressive = self.settings.get('aggressive', False)
        if self.settings.get('ignore', []):
            options.ignore = ','.join(self.settings.get('ignore', []))
        if self.settings.get('select', []):
            options.select = ','.join(self.settings.get('select', []))

        return options
Example #25
0
def reformat_white_space():
    try:
        import autopep8
    except ImportError:
        print(WARNING + "Could not find 'autopep8', exceeding whitespace will not be removed!" + TERMINATOR)
        return

    merchant_dir_path = os.path.abspath("./{{ cookiecutter.app_name }}")
    args = autopep8.parse_args(['--max-line-length 119', '--in-place', '--recursive'])
    if os.path.exists(merchant_dir_path):
        autopep8.fix_multiple_files([merchant_dir_path], args)
Example #26
0
def formatters(aggressive):
    """Return list of code formatters."""
    if aggressive:
        yield autoflake.fix_code
        autopep8_options = autopep8.parse_args(
            [''] + int(aggressive) * ['--aggressive'])
    else:
        autopep8_options = None

    yield lambda code: autopep8.fix_code(code, options=autopep8_options)
    yield docformatter.format_code
    yield unify.format_code
Example #27
0
def run_autopep(filename):
    cmd_args = ['dummy', '-d']
    args = autopep8.parse_args(cmd_args)
    args.aggressive = 2
    args.in_place = True
    args.diff = False
    args.max_line_length = 66
    autopep8.fix_file(filename, args)

    if not any(x in filename for x in import_fixed) or "internal" in filename:
        isort.SortImports(filename)
        run_autoflake(filename)
Example #28
0
    def check_file(self, path):
        """
        Check one regular file with pylint for py syntax errors.

        :param path: Path to a regular file.
        :return: False, if pylint found syntax problems, True, if pylint didn't
                 find problems, or path is not a python module or script.
        """
        inspector = PathInspector(path=path, args=self.args)
        if inspector.is_toignore():
            return True
        if not inspector.is_python():
            return True
        try:
            opt_obj = pep8.StyleGuide().options
            ignore_list = self.args.disable.split(',') + list(opt_obj.ignore)
            opt_obj.ignore = tuple(set(ignore_list))
            # pylint: disable=E1123
            runner = pep8.Checker(filename=path, options=opt_obj)
        except Exception:
            opts = ['--ignore'] + self.args.disable.split(',')
            pep8.process_options(opts)
            runner = pep8.Checker(filename=path)
        try:
            status = runner.check_all()
        except:
            log.error('Unexpected exception while checking %s', path)
            exc_info = sys.exc_info()
            stacktrace.log_exc_info(exc_info, 'inspektor.style')
            status = 1

        if status != 0:
            log.error('PEP8 check fail: %s', path)
            self.failed_paths.append(path)
            if AUTOPEP8_CAPABLE:
                if self.args.fix:
                    log.info('Trying to fix errors with autopep8')
                    try:
                        auto_args = [path, '--in-place',
                                     ('--max-line-length=%s' %
                                      self.args.max_line_length)]
                        if self.args.disable:
                            auto_args.append("--ignore='%s'" %
                                             self.args.disable)
                        opt_obj = autopep8.parse_args(auto_args)
                        autopep8.fix_file(path, opt_obj)
                    except Exception:
                        log.error('Unable to fix errors')
                        exc_info = sys.exc_info()
                        stacktrace.log_exc_info(exc_info, 'inspektor.style')

        return status == 0
Example #29
0
 def format_file(self, file_to_format_path, setup_path):
     if setup_path is None:
         options = None
     else:
         options = autopep8.parse_args(('--global-config='+setup_path.__str__(),
                                        file_to_format_path.__str__()), apply_config=True)
     temp_fd, temp_path = tempfile.mkstemp(prefix=f'{file_to_format_path.stem}_', 
                                           suffix=file_to_format_path.suffix, 
                                           text=True)
     with os.fdopen(temp_fd, 'w') as file:
         autopep8.fix_file(file_to_format_path.__str__(), output=file, options=options)
         
     return Path(temp_path)
Example #30
0
def formatters(aggressive,
               apply_config,
               filename='',
               remove_all_unused_imports=False,
               remove_unused_variables=False):
    """Return list of code formatters."""
    if aggressive:
        yield lambda code: autoflake.fix_code(
            code,
            remove_all_unused_imports=remove_all_unused_imports,
            remove_unused_variables=remove_unused_variables)

        autopep8_options = autopep8.parse_args(
            [filename] + int(aggressive) * ['--aggressive'],
            apply_config=apply_config)
    else:
        autopep8_options = autopep8.parse_args([filename],
                                               apply_config=apply_config)

    yield lambda code: autopep8.fix_code(code, options=autopep8_options)
    yield docformatter.format_code
    yield unify.format_code
Example #31
0
    def test_autopep8_args(self):
        import autopep8

        args = ['hello.py']
        us = parse_args(args)
        them = autopep8.parse_args(args)

        self.assertEqual(us.select, them.select)
        self.assertEqual(us.ignore, them.ignore)

        args = ['hello.py', '--select=E1,W1', '--ignore=W601',
                '--max-line-length', '120']
        us = parse_args(args)
        them = autopep8.parse_args(args)
        self.assertEqual(us.select, them.select)
        self.assertEqual(us.ignore, them.ignore)
        self.assertEqual(us.max_line_length, them.max_line_length)

        args = ['hello.py', '--aggressive', '-v']
        us = parse_args(args)
        them = autopep8.parse_args(args)
        self.assertEqual(us.aggressive, them.aggressive)
Example #32
0
def prg2py_after_preproc(data, parser_start, input_filename):
    input_stream = antlr4.InputStream(data)
    lexer = VisualFoxpro9Lexer(input_stream)
    stream = antlr4.CommonTokenStream(lexer)
    parser = VisualFoxpro9Parser(stream)
    tree = run_parser(stream, parser, parser_start)
    TreeCleanVisitor().visit(tree)
    output_tree = PythonConvertVisitor(input_filename).visit(tree)
    if not isinstance(output_tree, list):
        return output_tree
    output = add_indents(output_tree, 0)
    options = autopep8.parse_args(['--max-line-length', '100', '-'])
    return autopep8.fix_code(output, options)
Example #33
0
    def test_autopep8_args(self):
        # TODO see that these are passes on (use a static method in Radius?)

        args = ['hello.py']
        us = parse_args(args)
        them = autopep8.parse_args(args)
        self.assertEqual(us.select, them.select)
        self.assertEqual(us.ignore, them.ignore)

        args = [
            'hello.py', '--select=E1,W1', '--ignore=W601', '--max-line-length',
            '120'
        ]
        us = parse_args(args)
        them = autopep8.parse_args(args)
        self.assertEqual(us.select, them.select)
        self.assertEqual(us.ignore, them.ignore)
        self.assertEqual(us.max_line_length, them.max_line_length)

        args = ['hello.py', '--aggressive', '-v']
        us = parse_args(args)
        them = autopep8.parse_args(args)
        self.assertEqual(us.aggressive, them.aggressive)
Example #34
0
def create_file(fname, group, objs):
    source_files = []
    for obj in objs:
        class_source = generate_class(obj)
        if tidy:
            class_source = autopep8.fix_code(
                class_source, options=autopep8.parse_args(["--aggressive", "--aggressive", "--aggressive", ""])
            )
            class_source = format_code(class_source)
        source_files.append(class_source)

    source = generate_group(group, source_files)

    with open("../pyidf/{}.py".format(fname), "w") as f:
        f.write(source)
Example #35
0
def main(argv=None):
    argv = argv if argv is not None else sys.argv[1:]
    args = autopep8.parse_args(argv, apply_config=True)

    retv = 0
    for filename in args.files:
        original_contents = io.open(filename).read()
        new_contents = autopep8.fix_code(original_contents, args)
        if original_contents != new_contents:
            print('Fixing {0}'.format(filename))
            retv = 1
            with io.open(filename, 'w') as output_file:
                output_file.write(new_contents)

    return retv
Example #36
0
    def test_autopep8_args(self):
        import autopep8

        args = ['hello.py']
        us = parse_args(args)
        them = autopep8.parse_args(args)

        self.assertEqual(us.select, them.select)
        self.assertEqual(us.ignore, them.ignore)

        args = [
            'hello.py', '--select=E1,W1', '--ignore=W601', '--max-line-length',
            '120'
        ]
        us = parse_args(args)
        them = autopep8.parse_args(args)
        self.assertEqual(us.select, them.select)
        self.assertEqual(us.ignore, them.ignore)
        self.assertEqual(us.max_line_length, them.max_line_length)

        args = ['hello.py', '--aggressive', '-v']
        us = parse_args(args)
        them = autopep8.parse_args(args)
        self.assertEqual(us.aggressive, them.aggressive)
Example #37
0
    def _inner_setup(self, line, options=""):
        f = open(self.tempfile[1], 'w')
        f.write(line)
        f.close()
        opts, _ = autopep8.parse_args(options.split() + [self.tempfile[1]])
        sio = StringIO()

        # Monkey patch pep8 to trigger spawning
        original_pep8 = autopep8.pep8
        try:
            autopep8.pep8 = None
            autopep8.fix_file(filename=self.tempfile[1], opts=opts, output=sio)
        finally:
            autopep8.pep8 = original_pep8

        self.result = sio.getvalue()
Example #38
0
    def get_options(self):
        cmd_args = list()
        cmd_args.extend(['--aggressive'] * self.settings.get('aggressive', 0))
        if self.settings.get('list-fixes', False):
            cmd_args.append('--list-fixes')
        if self.settings.get('ignore', False):
            cmd_args.append('--ignore={0}'.format(self.settings.get('ignore')))
        if self.settings.get('select', False):
            cmd_args.append('--select={0}'.format(self.settings.get('ignore')))
        if self.settings.get('max-line-length', False):
            cmd_args.append('--max-line-length={0}'.format(
                self.settings.get('max-line-length')))
        #-- We must give a filename to pass the parse_args() tests
        cmd_args.append('filename')
        options, _ = autopep8.parse_args(cmd_args)

        return options
    def run(self, edit):
        replace_region = self.view.line(sublime.Region(0L, self.view.size()))
        source = self.view.substr(replace_region)
        options = autopep8.parse_args([''])[0]
        if IGNORE:
            options.ignore = IGNORE
        if SELECT:
            options.select = SELECT
        if MAX_LINE_LENGTH:
            options.max_line_length = MAX_LINE_LENGTH
        if AGGRESSIVE:
            options.aggressive = True

        fixed = autopep8.fix_string(source, options=options)
        is_dirty, err = MergeUtils.merge_code(self.view, edit, source, fixed)
        if err:
            sublime.error_message("%s: Merge failure: '%s'" % (PLUGIN_NAME, err))
Example #40
0
    def _inner_setup(self, line, options=""):
        with open(self.tempfile[1], 'w') as temp_file:
            temp_file.write(line)
        opts, _ = autopep8.parse_args(options.split() + [self.tempfile[1]])
        sio = StringIO()

        # Monkey patch pep8 to trigger spawning
        original_pep8 = autopep8.pep8
        try:
            autopep8.pep8 = None
            autopep8.fix_file(filename=self.tempfile[1],
                              opts=opts,
                              output=sio)
        finally:
            autopep8.pep8 = original_pep8

        self.result = sio.getvalue()
Example #41
0
def create_file(fname, group, objs):
    source_files = []
    for obj in objs:
        class_source = generate_class(obj)
        if tidy:
            class_source = autopep8.fix_code(class_source,
                                             options=autopep8.parse_args([
                                                 '--aggressive',
                                                 '--aggressive',
                                                 '--aggressive', ''
                                             ]))
            class_source = format_code(class_source)
        source_files.append(class_source)

    source = generate_group(group, source_files)

    with open("../pyidf/{}.py".format(fname), 'w') as f:
        f.write(source)
Example #42
0
    def get_options(self):
        cmd_args = list()
        cmd_args.extend(['--aggressive'] * self.settings.get('aggressive', 0))
        if self.settings.get('list-fixes', False):
            cmd_args.append('--list-fixes')
        if self.settings.get('ignore', False):
            cmd_args.append('--ignore={0}'.format(','.join(
                [o.strip() for o in self.settings.get('ignore') if o.strip()])))
        if self.settings.get('select', False):
            cmd_args.append('--select={0}'.format(','.join(
                [o.strip() for o in self.settings.get('select') if o.strip()])))
        if self.settings.get('max-line-length', False):
            cmd_args.append('--max-line-length={0}'.format(
                self.settings.get('max-line-length')))
        #-- We must give a filename to pass the parse_args() tests
        cmd_args.append('filename')
        options, _ = autopep8.parse_args(cmd_args)

        return options
Example #43
0
def format_file(path, with_meld=True, cwd='.'):
    _logger.info(
        f'Format the file: {path} using {settings.Tools.FILE_FORMATTER} '
        f'with merge mode in {settings.Tools.MERGE_TOOL}')

    path = Path(cwd) / path
    formatted_file_descriptor, formated_file_path = tempfile.mkstemp(
        prefix=f'{path.stem}_', suffix='.py', text=True)
    os.close(formatted_file_descriptor)
    formated_file_path = Path(formated_file_path)
    setup_file_path = (Path(cwd) / settings.FileName.SETUP_CFG).resolve()
    if path.is_file():
        with open(formated_file_path, 'w') as file:
            options = autopep8.parse_args(
                ['--global-config=' + str(setup_file_path),
                 str(path)],
                apply_config=True)
            autopep8.fix_file(str(path), output=file, options=options)
    else:
        raise exceptions.NotAFileError('Path must point to a file!', _logger)

    if with_meld:
        utils.execute_cmd([
            'meld',
            str(path),
            str(path),
            str(formated_file_path), '-o',
            str(path)
        ], str(cwd))
        formated_file_path.unlink()
    else:
        _logger.info(f'Formatted file has ben written to {formated_file_path}')

    _logger.info('Lint formatted file and show report')
    try:
        utils.execute_cmd(
            [settings.Tools.LINTER,
             str(path), f'--config={setup_file_path}'], cwd)
    except exceptions.ExecuteCmdError as e:
        print(e)
    else:
        _logger.info('Linter report is empty - file ok')
Example #44
0
def prg2py_after_preproc(data, parser_start, input_filename):
    input_stream = antlr4.InputStream(data)
    lexer = VisualFoxpro9Lexer(input_stream)
    stream = antlr4.CommonTokenStream(lexer)
    parser = VisualFoxpro9Parser(stream)
    tree = run_parser(stream, parser, parser_start)
    TreeCleanVisitor().visit(tree)
    output_tree = PythonConvertVisitor(input_filename).visit(tree)
    if not isinstance(output_tree, list):
        return output_tree
    output = add_indents(output_tree, 0)
    options = autopep8.parse_args(['--max-line-length', '100000', '-'])
    output = autopep8.fix_code(output, options)
    tokens = list(tokenize.generate_tokens(io.StringIO(output).readline))
    for i, token in enumerate(tokens):
        token = list(token)
        if token[0] == tokenize.STRING and token[1].startswith('u'):
            token[1] = token[1][1:]
        tokens[i] = tuple(token)
    return tokenize.untokenize(tokens)
Example #45
0
def fix_code(cells, options):
    """
    Returns code lines fixed with autopep8.
    """
    autopep8_cmdline = '- ' + options.autopep8_options # Required filename arg
    autopep8_options, autopep8_args = autopep8.parse_args(autopep8_cmdline.split())
    fixed_cells = []
    for cell_num, cell_lines in cells:
        fixed_code = autopep8.fix_lines(cell_lines, autopep8_options)
        fixed_lines = fixed_code.splitlines(True)

        if options.no_newline and fixed_lines:
            # Remove the single newline at end of 'file' added by autopep8 to
            # each cell.
            fixed_lines[-1] = fixed_lines[-1][:-1]

            if options.end_semicolon and not fixed_lines[-1].endswith('?'):
                fixed_lines[-1] += ';'

        fixed_cells.append((cell_num, fixed_lines))
    return fixed_cells
Example #46
0
def autopep8(args):
    """Format code according to PEP8"""
    try:
        import autopep8
    except:
        error('autopep8 not found! Run "paver install_devtools".')
        sys.exit(1)

    if any(x not in args for x in ['-i', '--in-place']):
        args.append('-i')

    args.insert(0, 'dummy')
    cmd_args = autopep8.parse_args(args)[0]

    excludes = ('ext-lib', 'ext-src')
    for p in options.plugin.source_dir.walk():
        if any(exclude in p for exclude in excludes):
            continue

        if p.fnmatch('*.py'):
            autopep8.fix_file(p, options=cmd_args)
Example #47
0
def autopep8(args):
    """Format code according to PEP8"""
    try:
        import autopep8
    except:
        error('autopep8 not found! Run "paver install_devtools".')
        sys.exit(1)

    if any(x not in args for x in ['-i', '--in-place']):
        args.append('-i')

    args.insert(0, 'dummy')
    cmd_args = autopep8.parse_args(args)[0]

    excludes = ('ext-lib', 'ext-src')
    for p in options.plugin.source_dir.walk():
        if any(exclude in p for exclude in excludes):
            continue

        if p.fnmatch('*.py'):
            autopep8.fix_file(p, options=cmd_args)
Example #48
0
def run_autopep8(source, options=()):
    """Return source code formatted with autopep8."""
    import tempfile
    temp = tempfile.mkstemp(dir='.')
    with open(temp[1], 'w') as temp_file:
        temp_file.write(source)

    try:
        import autopep8
        opts, _ = autopep8.parse_args(list(options) + [temp[1]])
        sio = StringIO()
        autopep8.fix_file(filename=temp[1],
                          opts=opts,
                          output=sio)
        output = sio.getvalue()
        if source.strip() and not output.strip():
            raise ValueError('autopep8 failed')
        return output
    finally:
        import os
        os.remove(temp[1])
Example #49
0
def main():
    readme_path = 'README.rst'
    before_key = 'Before running autopep8.\n\n.. code-block:: python'
    after_key = 'After running autopep8.\n\n.. code-block:: python'

    (top, before, bottom) = split_readme(readme_path,
                                         before_key=before_key,
                                         after_key=after_key,
                                         end_key='Options::')

    import textwrap
    new_readme = '\n\n'.join([
        top,
        before_key, before,
        after_key, indent(autopep8.fix_string(
            textwrap.dedent(before),
            options=autopep8.parse_args(['', '--aggressive'])[0])),
        bottom])

    with open(readme_path, 'w') as output_file:
        output_file.write(new_readme)
Example #50
0
def main():
    README_PATH = 'README.rst'
    BEFORE_KEY = 'Before running autopep8.\n\n.. code-block:: python'
    AFTER_KEY = 'After running autopep8.\n\n.. code-block:: python'

    (top, before, bottom) = split_readme(README_PATH,
                                         before_key=BEFORE_KEY,
                                         after_key=AFTER_KEY,
                                         end_key='Options::')

    import textwrap
    new_readme = '\n\n'.join([
        top,
        BEFORE_KEY, before,
        AFTER_KEY, indent(autopep8.fix_string(
            textwrap.dedent(before),
            options=autopep8.parse_args(['', '--aggressive'])[0])),
        bottom])

    with open(README_PATH, 'w') as output_file:
        output_file.write(new_readme)
Example #51
0
def autofix_codeblock(codeblock, max_line_len=80,
                      aggressive=False,
                      very_aggressive=False,
                      experimental=False):
    r"""
    Uses autopep8 to format a block of code

    Example:
        >>> import utool as ut
        >>> codeblock = ut.codeblock(
            '''
            def func( with , some = 'Problems' ):


             syntax ='Ok'
             but = 'Its very messy'
             if None:
                    # syntax might not be perfect due to being cut off
                    ommiting_this_line_still_works=   True
            ''')
        >>> fixed_codeblock = ut.autofix_codeblock(codeblock)
        >>> print(fixed_codeblock)
    """
    # FIXME idk how to remove the blank line following the function with
    # autopep8. It seems to not be supported by them, but it looks bad.
    import autopep8
    arglist = ['--max-line-length', '80']
    if aggressive:
        arglist.extend(['-a'])
    if very_aggressive:
        arglist.extend(['-a', '-a'])
    if experimental:
        arglist.extend(['--experimental'])
    arglist.extend([''])
    autopep8_options = autopep8.parse_args(arglist)
    fixed_codeblock = autopep8.fix_code(codeblock, options=autopep8_options)
    return fixed_codeblock
Example #52
0
def check(expected_filename, input_filename):
    """Test and compare output.

    Return True on success.

    """
    got = autopep8.fix_file(
        input_filename,
        options=autopep8.parse_args(['', '--aggressive'])[0])

    try:
        with autopep8.open_with_encoding(expected_filename) as expected_file:
            expected = expected_file.read()
    except IOError:
        expected = None

    if expected == got:
        return True
    else:
        got_filename = expected_filename + '.err'
        encoding = autopep8.detect_encoding(input_filename)

        with autopep8.open_with_encoding(got_filename,
                                         encoding=encoding,
                                         mode='w') as got_file:
            got_file.write(got)

        print(
            '{begin}{got} does not match expected {expected}{end}'.format(
                begin=RED,
                got=got_filename,
                expected=expected_filename,
                end=END),
            file=sys.stdout)

        return False
Example #53
0
'''
import autopep8
from docformatter import format_code

from generator import generate_epw, generate_testclass
from iddparser import IDDParser


if __name__ == '__main__':
    parser = IDDParser()
    objs = parser.parse("epw.idd")

    source_file = generate_epw(objs)
    source_file = autopep8.fix_code(
        source_file, options=autopep8.parse_args(['--aggressive',
                                                  '--aggressive',
                                                  '--aggressive',
                                                  '']))
    source_file = format_code(source_file)
 
    with open("../pyepw/epw.py", 'w') as f:
        f.write(source_file)

    for obj in objs[:-1]:
        if not obj.is_list_object:
            source_file = generate_testclass(obj, objs)
            source_file = autopep8.fix_code(
                source_file, options=autopep8.parse_args(['--aggressive',
                                                          '--aggressive',
                                                          '--aggressive',
                                                          '']))
            source_file = format_code(source_file)
Example #54
0
"""Run autopep8 on the selected buffer in Vim.

map <C-I> :pyfile <path_to>/autopep8_vim.py<CR>

"""

import vim

if vim.eval("&syntax") == "python":
    encoding = vim.eval("&fileencoding")
    source = "\n".join(line.decode(encoding) for line in vim.current.buffer) + "\n"

    import autopep8

    options = autopep8.parse_args(["--range", str(1 + vim.current.range.start), str(1 + vim.current.range.end), ""])[0]

    formatted = autopep8.fix_code(source, options=options)

    if source != formatted:
        if formatted.endswith("\n"):
            formatted = formatted[:-1]

        vim.current.buffer[:] = [line.encode(encoding) for line in formatted.splitlines()]
Example #55
0
"""Run autopep8 on the selected buffer in Vim.

map <C-I> :pyfile <path_to>/autopep8_vim.py<CR>

"""

import vim
if vim.eval('&syntax') == 'python':
    encoding = vim.eval('&fileencoding')
    source = '\n'.join(line.decode(encoding)
                       for line in vim.current.buffer) + '\n'

    import autopep8
    options = autopep8.parse_args(['--range',
                                   str(1 + vim.current.range.start),
                                   str(1 + vim.current.range.end),
                                   ''])[0]

    formatted = autopep8.fix_string(source, options=options)

    if source != formatted:
        if formatted.endswith('\n'):
            formatted = formatted[:-1]

        vim.current.buffer[:] = [line.encode(encoding)
                                 for line in formatted.splitlines()]
Example #56
0
        # fix ident: space -> tab
        ident = 0
        while line.startswith("    "):
            ident += 1
            line = line[4:]
        line = "\t" * ident + line.rstrip()

        # done
        prevline = line
        lines.append(line)

    return "\n".join(lines).strip() + "\n"


files = [(root, f) for root, dirs, files in os.walk(os.path.dirname(os.path.realpath(__file__))) for f in files]

for root, fname in files:
    path = os.path.join(root, fname)
    source = None
    if fname.endswith(".py"):
        tmppath = prepare(path)
        source = autopep8.fix_file(tmppath, autopep8.parse_args([tmppath, "--ignore=E123,E124,E127,E128,E501"])[0])
        source = lint(source, root)
        os.remove(tmppath)
    elif fname.endswith(".html") or fname.endswith(".css") or fname.endswith(".js"):
        source = text_lint(path)
    if source != None:
        with open(path, "w") as f:
            f.write(source.encode("utf8"))