Exemplo n.º 1
0
def format_file(path):
    '''
    The function, using 'for' loop scans all the directories as well
    as sub-directories of the folder for python file and checks
    for pep8 format.
    '''
    p = ""
    for dirpath, _, filenames in os.walk(path):
        if(os.path.exists(os.path.join(dirpath, 'bin/activate'))):
            p = dirpath
        for f in filenames:
            if f.endswith('.py'):
                filename = os.path.abspath(os.path.join(dirpath, f))
                if (not bool(p)):
                    if os.access(filename, os.W_OK):
                        try:
                            file1 = open(filename, 'r')
                            source_code = file1.read()
                            file1.seek(0)
                            file1 = open(filename, 'w')
                            file1.truncate()
                            file1.write(autopep8.fix_code(source_code))
                            file1.close()
                        except:
                            print(filename)
                            print(sys.exc_info()[0])
Exemplo n.º 2
0
def fix_code(code):
    """Formats Python code to conform to the PEP 8 style guide.

    """
    if not autopep8:
        raise Fault('autopep8 not installed, cannot fix code.', code=400)
    return autopep8.fix_code(code, apply_config=True)
Exemplo n.º 3
0
def generate_file(target_path,
                  filename,
                  template_name,
                  context=None,
                  raw=False):
    """
    Generates a new file using Jinja2 template

    :param dirname:
    :param template_name:
    :param context:
    :return:
    """
    filename = os.path.join(target_path, filename)

    dirname = os.path.dirname(filename)
    if len(dirname) and not os.path.exists(dirname):
        os.makedirs(dirname)

    with open(filename, 'w') as f:
        if not raw:
            rendered = render_template(template_name, context=context)
        else:
            rendered = render_file(template_name)

        if not NO_PEP and filename.endswith('.py'):
            rendered = autopep8.fix_code(rendered)

        f.write(rendered)
Exemplo n.º 4
0
def format_file(target_path, filename):
    filename = os.path.join(target_path, filename)

    with open(filename, 'r') as f:
        code = f.read()
    with open(filename, 'w') as f:
        f.write(autopep8.fix_code(code))
Exemplo n.º 5
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', ''])
    )
Exemplo n.º 6
0
def build_code():
    imports = list(aug_dict.keys())

    res_imports = build_code_substring(imports)
    composes = build_code_substring(apply_changes(aug_dict))
    pytorch2tensor = ''
    if st.sidebar.checkbox('add ToTensorv2()'):  
        pytorch2tensor = 'from albumentations.pytorch import ToTensorV2'
        composes += '\nToTensorV2(),'

    result = """{pytorch2tensor}
    from albumentations import (
        Compose,
        {imports}
    )
    
    transformations = Compose([
    {tf}
    ])""".format(
        pytorch2tensor=pytorch2tensor,
        imports=res_imports,
        tf=composes,
    )

    return autopep8.fix_code(result, options={
        'max_line_length': 80,
    })
Exemplo n.º 7
0
    def run(self, filename, file,
            max_line_length: int=80,
            tab_width: int=SpacingHelper.DEFAULT_TAB_WIDTH,
            pep_ignore: typed_list(str)=(),
            pep_select: typed_list(str)=(),
            local_pep8_config: bool=False):
        """
        Detects and fixes PEP8 incompliant code. This bear will not change
        functionality of the code in any way.

        :param max_line_length:   Maximum number of characters for a line.
        :param tab_width:         Number of spaces per indent level.
        :param pep_ignore:        A list of errors/warnings to ignore.
        :param pep_select:        A list of errors/warnings to exclusively
                                  apply.
        :param local_pep8_config: Set to true if autopep8 should use a config
                                  file as if run normally from this directory.
        """
        options = {"ignore": pep_ignore,
                   "select": pep_select,
                   "max_line_length": max_line_length,
                   "indent_size": tab_width}

        corrected = autopep8.fix_code(''.join(file),
                                      apply_config=local_pep8_config,
                                      options=options).splitlines(True)

        diffs = Diff.from_string_arrays(file, corrected).split_diff()

        for diff in diffs:
            yield Result(self,
                         "The code does not comply to PEP8.",
                         affected_code=(diff.range(filename),),
                         diffs={filename: diff})
Exemplo n.º 8
0
Arquivo: app.py Projeto: harupy/dbauto
def format_code():
    old_code = request.args.get('code')

    # TODO: add an option which allows users to select autoformat provider (yapf or autopep8)
    new_code = autopep8.fix_code(old_code, options=read_options())
    new_code = new_code.strip()
    return jsonify(code=new_code)
Exemplo n.º 9
0
def code_for_builder(builder, var_name=None):
    '''
    Returns code that can be executed to construct the given builder, including
    import statements.

    :param builder: :class:`~tda.orders.generic.OrderBuilder` to generate.
    :param var_name: If set, emit code that assigns the builder to a variable
                     with this name.
    '''
    ast = construct_order_ast(builder)

    imports = defaultdict(set)
    lines = []
    ast.render(imports, lines)

    import_lines = []
    for module, names in imports.items():
        line = 'from {} import {}'.format(module, ', '.join(names))
        if len(line) > 80:
            line = 'from {} import (\n{}\n)'.format(module, ',\n'.join(names))
        import_lines.append(line)

    if var_name:
        var_prefix = f'{var_name} = '
    else:
        var_prefix = ''

    return autopep8.fix_code('\n'.join(import_lines) + '\n\n' + var_prefix +
                             '\n'.join(lines))
Exemplo n.º 10
0
def fix_line_range(source_code, start, end, options):
    """Apply autopep8 (and docformatter) between the lines start and end of
    source."""
    # TODO confirm behaviour outside range (indexing starts at 1)
    start = max(start, 1)

    options.line_range = [start, end]
    from autopep8 import fix_code
    fixed = fix_code(source_code, options)

    try:
        if options.docformatter:
            from docformatter import format_code
            fixed = format_code(
                fixed,
                summary_wrap_length=options.max_line_length - 1,
                description_wrap_length=(options.max_line_length -
                                         2 * options.indent_size),
                pre_summary_newline=options.pre_summary_newline,
                post_description_blank=options.post_description_blank,
                force_wrap=options.force_wrap,
                line_range=[start, end])
    except AttributeError:  # e.g. using autopep8.parse_args, pragma: no cover
        pass

    return fixed
Exemplo n.º 11
0
    def translate(self, command):
        """
        translates one command to pseudo command(s)
        :param command: pseudo code string
        :return: python code
        """
        original_command = command
        try:
            # remove tabs from the beginning of the first line and counts them
            tabs = count_on_start(command, TAB)
            command = command[tabs*4:]

            command_parts = self.split_by_strings(command)
            tokens = sum([self.tokenize(part) for part in command_parts], [])  # produce list of tokens
            if not tokens:
                # empty line
                return ""
            replacements = self.pos_model.evaluate(tokens)
            generic_pseudo = [replacements[t] if t in replacements.keys() else t for t in tokens]
            generic_python = self.g2g_model.evaluate(generic_pseudo)
            python_tokens = [replacements.inverse[t] if t in replacements.values() else t for t in generic_python]
            python_code = self.join_tokens(python_tokens)
            if not self.is_valid(python_code):
                raise TranslationException("wrong syntax for generated python code")
            return self.add_tabs(autopep8.fix_code(python_code), tabs)
        except TranslationException:
            return original_command
Exemplo n.º 12
0
def generate_lightweight_component(template, step_name, step_data, nb_path,
                                   metadata):
    """Use the function template to generate Python code."""
    step_source_raw = step_data.get('source', [])

    def _encode_source(s):
        # Encode line by line a multiline string
        return "\n".join([line.encode("unicode_escape").decode("utf-8")
                          for line in s.splitlines()])

    # Since the code will be wrapped in triple quotes inside the template,
    # we need to escape triple quotes as they will not be escaped by
    # encode("unicode_escape").
    step_source = [re.sub(r"'''", "\\'\\'\\'", _encode_source(s))
                   for s in step_source_raw]

    step_marshal_in = step_data.get('ins', [])
    step_marshal_out = step_data.get('outs', [])
    step_parameters = get_args(step_data.get('parameters', {}))

    fn_code = template.render(
        step_name=step_name,
        function_body=step_source,
        in_variables=step_marshal_in,
        out_variables=step_marshal_out,
        parameters=step_parameters,
        nb_path=nb_path,
        # step_parameters overrides the parameters fields of metadata
        **{**metadata, **step_parameters}
    )
    # fix code style using pep8 guidelines
    return autopep8.fix_code(fn_code)
Exemplo n.º 13
0
def _format(config, document, line_range=None):
    options = _autopep8_config(config, document)
    if line_range:
        options['line_range'] = list(line_range)

    # Temporarily re-monkey-patch the continued_indentation checker - #771
    del pycodestyle._checks['logical_line'][pycodestyle.continued_indentation]
    pycodestyle.register_check(autopep8_c_i)

    new_source = fix_code(document.source, options=options)

    # Switch it back
    del pycodestyle._checks['logical_line'][autopep8_c_i]
    pycodestyle.register_check(pycodestyle.continued_indentation)

    if new_source == document.source:
        return []

    # I'm too lazy at the moment to parse diffs into TextEdit items
    # So let's just return the entire file...
    return [{
        'range': {
            'start': {
                'line': 0,
                'character': 0
            },
            # End char 0 of the line after our document
            'end': {
                'line': len(document.lines),
                'character': 0
            }
        },
        'newText': new_source
    }]
Exemplo n.º 14
0
def fix_line_range(source_code, start, end, options):
    """Apply autopep8 (and docformatter) between the lines start and end of
    source."""
    # TODO confirm behaviour outside range (indexing starts at 1)
    start = max(start, 1)

    options.line_range = [start, end]
    from autopep8 import fix_code
    fixed = fix_code(source_code, options)

    try:
        if options.docformatter:
            from docformatter import format_code
            fixed = format_code(
                fixed,
                summary_wrap_length=options.max_line_length - 1,
                description_wrap_length=(options.max_line_length
                                         - 2 * options.indent_size),
                pre_summary_newline=options.pre_summary_newline,
                post_description_blank=options.post_description_blank,
                force_wrap=options.force_wrap,
                line_range=[start, end])
    except AttributeError:  # e.g. using autopep8.parse_args, pragma: no cover
        pass

    return fixed
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', ''])
    )
Exemplo n.º 16
0
    def run_test(self, func_body):
        # This is only will be used by function unit test implementation
        unit_file = self.parent.unit_file
        exec_import_path = "\nfrom importlib import reload\n"

        # import_path += "import {}".format(self.parent.module_name)
        import_path = self.parent.import_path
        import_path += " import "
        # print("node module")
        # print(self.parent.parent.node_module)
        import_path += ",".join(self.parent.node_module)

        exec_import_path += "import {}\n".format(unit_file.getRoot())

        unit_file = self.parent.unit_file

        # print("===>{}==>{}".format(unit_file.getModule(), unit_file.getRoot()))

        # print(import_path)

        exec_import_path += "\nreload({})\n".format(
            unit_file.getReloadPath())

        code_str = import_path + exec_import_path + "\n"+func_body
        func_body_format = (autopep8.fix_code(code_str))

        # print(func_body_format)
        codeObejct = compile(func_body_format, 'test', "exec")
        Vars = {}
        exec(codeObejct, globals(), Vars)
        return Vars
Exemplo n.º 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(['', '--aggressive'])[0])
    compile(output_code, '<string>', 'exec', dont_inherit=True)

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

    with open(readme_path, 'w') as output_file:
        output_file.write(new_readme)
Exemplo n.º 18
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,E721',
                        #'--ignore', '',
                        ''
                    ]))

                b.set_text(t)

                buff.restore_state()

        return
Exemplo n.º 19
0
	def level(self, level):
		# check this is a valid level
		assert level in sf_api.GM_LEVELNAMES
		ret = self.run(cmd=sf_api.GM_LEVELNAMES[level])
		if ret[0] != 0:
			return -1, ret[1]
		# get the level path
		level_path = os.path.join(LIBSFPATH, 'level%i' % level)
		# store the instructions
		if 'instructions' in ret[1]:
			instructions_filename = os.path.join(level_path, INSTRUCTIONS_FILENAME)
			with open(instructions_filename, 'w') as f:
				for k, v in ret[1]['instructions'].iteritems():
					f.write('\n== %s\n\n%s\n' % (k, v))
			del ret[1]['instructions']
		# store the rest
		if not ret[1]['ok']:
			return -1, 'error: checking client: %r' % ret[1]
		del ret[1]['ok']
		keys_filename = os.path.join(level_path, KEYS_FILENAME)
		with open(keys_filename, 'w') as f:
			f.write(PYTHON_HEADER)
			f.write(autopep8.fix_code('kwargs = %s' % ret[1],
					options={'aggressive': 1}))
		print 'keys filename: %s' % keys_filename
		return 0, ''
Exemplo n.º 20
0
def format_file(path):
    '''
    The function, using 'for' loop scans all the directories as well
    as sub-directories of the folder for python file and checks
    for pep8 format.
    '''
    p = ""
    for dirpath, _, filenames in os.walk(path):
        if os.path.exists(os.path.join(dirpath, 'bin/activate')):
            p = dirpath
        for file_ in filenames:
            if file_.endswith('.py'):
                file_ = os.path.abspath(os.path.join(dirpath, file_))
                if (not bool(p)) and os.access(file_, os.W_OK):
                    try:
                        temp_file = open(file_, 'r')
                        source_code = temp_file.read()
                        temp_file.seek(0)
                        temp_file = open(file_, 'w')
                        temp_file.truncate()
                        temp_file.write(autopep8.fix_code(source_code))
                        temp_file.close()
                    except:
                        print(file_)
                        print(sys.exc_info()[0])
Exemplo n.º 21
0
def analyze_repo(repo):
    global number
    for file_text in get_files_text(repo, ".py"):
        origin = file_text.decoded_content.decode()
        new = autopep8.fix_code(origin)
        if new != origin:
            print("COMMIT fix to file", file_text.path)
Exemplo n.º 22
0
 def burn_disc(self):
     """Burn the disc with songs."""
     self.ensure_one()
     content = self.dj_render_template()
     # make sure PEP8 is safe
     content = autopep8.fix_code(content)
     return self.disc_full_path(), to_str(content)
Exemplo n.º 23
0
    def generate_fuzzer(self,
                        grammars,
                        *,
                        encoding='utf-8',
                        lib_dir=None,
                        actions=True,
                        pep8=False):
        """
        Generates fuzzers from grammars.

        :param grammars: List of grammar files to generate from.
        :param encoding: Grammar file encoding.
        :param lib_dir: Alternative directory to look for imports.
        :param actions: Boolean to enable or disable grammar actions.
        :param pep8: Boolean to enable pep8 to beautify the generated fuzzer source.
        """
        lexer_root, parser_root = None, None

        for grammar in grammars:
            root = self._parse(grammar, encoding, lib_dir)
            # Lexer and/or combined grammars are processed first to evaluate TOKEN_REF-s.
            if root.grammarType().LEXER() or not root.grammarType().PARSER():
                lexer_root = root
            else:
                parser_root = root

        fuzzer_generator = FuzzerGenerator(self.antlr_parser_cls, actions)
        for name, src in fuzzer_generator.generate(lexer_root, parser_root):
            with open(join(self.work_dir, name + '.py'), 'w') as f:
                if pep8:
                    src = autopep8.fix_code(src)
                f.write(src)
Exemplo n.º 24
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={'aggressive': 2})

    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)
Exemplo n.º 25
0
def normalize_python(code):
    # TODO: we should really be comparing the tree between a, b but this
    # works for now

    if code is None:
        return None

    code = _delete_python_comments(code)

    if not autopep8 or not parso:
        raise ImportError('autopep8 and parso are required for normalizing '
                          'Python code: pip install autopep8 parso')

    node = parso.parse(code).children[0]

    if node.type == 'decorated':
        node = node.children[-1]

    try:
        doc_node = node.get_doc_node()
    except Exception:
        # function without docstring...
        doc_node = None

    if doc_node is not None:
        code = code.replace('\n' + doc_node.get_code(), '')

    code = autopep8.fix_code(code)

    return code
Exemplo n.º 26
0
    def generate_fuzzer(self, grammars, *, options=None, encoding='utf-8', lib_dir=None, actions=True, pep8=False):
        """
        Generates fuzzers from grammars.

        :param grammars: List of grammar files to generate from.
        :param options: Dictionary of options that override/extend those set in the grammar.
        :param encoding: Grammar file encoding.
        :param lib_dir: Alternative directory to look for imports.
        :param actions: Boolean to enable or disable grammar actions.
        :param pep8: Boolean to enable pep8 to beautify the generated fuzzer source.
        """
        lexer_root, parser_root = None, None

        for grammar in grammars:
            root = self._parse(grammar, encoding, lib_dir)
            # Lexer and/or combined grammars are processed first to evaluate TOKEN_REF-s.
            if root.grammarType().LEXER() or not root.grammarType().PARSER():
                lexer_root = root
            else:
                parser_root = root

        graph = build_graph(self.antlr_parser_cls, actions, lexer_root, parser_root)
        graph.options.update(options or {})

        src = self.template.render(graph=graph, version=__version__).lstrip()
        with open(join(self.work_dir, graph.name + '.' + self.lang), 'w') as f:
            if pep8:
                src = autopep8.fix_code(src)
            f.write(src)
def _format(config, document, line_range=None):
    options = _autopep8_config(config)
    if line_range:
        options['line_range'] = list(line_range)

    new_source = fix_code(document.source, options=options)

    if new_source == document.source:
        return []

    # I'm too lazy at the moment to parse diffs into TextEdit items
    # So let's just return the entire file...
    return [{
        'range': {
            'start': {
                'line': 0,
                'character': 0
            },
            # End char 0 of the line after our document
            'end': {
                'line': len(document.lines),
                'character': 0
            }
        },
        'newText': new_source
    }]
Exemplo n.º 28
0
def fix_code(code):
    """Formats Python code to conform to the PEP 8 style guide.

    """
    if not autopep8:
        raise Fault("autopep8 not installed, cannot fix code.", code=400)
    return autopep8.fix_code(code, apply_config=True)
Exemplo n.º 29
0
    def run(self, filename, file,
            max_line_length: int=79,
            indent_size: int=SpacingHelper.DEFAULT_TAB_WIDTH,
            pep_ignore: typed_list(str)=(),
            pep_select: typed_list(str)=(),
            local_pep8_config: bool=False):
        """
        Detects and fixes PEP8 incompliant code. This bear will not change
        functionality of the code in any way.

        :param max_line_length:   Maximum number of characters for a line.
        :param indent_size:       Number of spaces per indentation level.
        :param pep_ignore:        A list of errors/warnings to ignore.
        :param pep_select:        A list of errors/warnings to exclusively
                                  apply.
        :param local_pep8_config: Set to true if autopep8 should use a config
                                  file as if run normally from this directory.
        """
        options = {'ignore': pep_ignore,
                   'select': pep_select,
                   'max_line_length': max_line_length,
                   'indent_size': indent_size}

        corrected = autopep8.fix_code(''.join(file),
                                      apply_config=local_pep8_config,
                                      options=options).splitlines(True)

        diffs = Diff.from_string_arrays(file, corrected).split_diff()

        for diff in diffs:
            yield Result(self,
                         'The code does not comply to PEP8.',
                         affected_code=(diff.range(filename),),
                         diffs={filename: diff})
Exemplo n.º 30
0
 def init_forms(self, section_name):
     forms_text = autopep8.fix_code(
         self.forms_string(self.sections[section_name].resources))
     forms_path = os.path.join(self.app_directory, self.app_name,
                               section_name, "forms.py")
     with open(forms_path, "w") as forms_file:
         forms_file.write(forms_text)
Exemplo n.º 31
0
def normalize_python(code):
    # TODO: we should really be comparing the tree between a, b but this
    # works for now

    if code is None:
        return None

    code = _delete_python_comments(code)

    if not autopep8 or not parso:
        raise ImportError('autopep8 and parso are required for normalizing '
                          'Python code: pip install autopep8 parso')

    try:
        doc_node = parso.parse(code).children[0].get_doc_node()
    except Exception as e:
        warnings.warn(
            'Could not remove docstring from Python code: {}'.format(e))
    else:
        if doc_node is not None:
            code = code.replace(doc_node.get_code(), '')

    code = autopep8.fix_code(code)

    return code
Exemplo n.º 32
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
Exemplo n.º 33
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={'aggressive': 2})

    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)
Exemplo n.º 34
0
 def do_show(self, line):
     """Show me the code!
     """
     code = autopep8.fix_code("".join(self._generate_workflow_code()))
     if self.options.no_highlight:
         print code
     else:
         print highlight(code, PythonLexer(), TerminalFormatter())
Exemplo n.º 35
0
 def do_gen(self, line):
     if os.path.exists(self.file_name):
         prompt = u"确定要生成代码?之前的代码文件 '{}' 将被覆盖(y/n)".format(
             self.file_name).encode("utf-8")
         if raw_input(prompt) != 'y':
             return
     with open(self.file_name, "w") as f:
         f.write(autopep8.fix_code("".join(self._generate_workflow_code())))
Exemplo n.º 36
0
 def init_init(self):
     init_text = autopep8.fix_code(self.init_string(),
                                   options={"ignore": ["E402"]
                                            })  # fixing code will break
     init_path = os.path.join(self.app_directory, self.app_name,
                              "__init__.py")
     with open(init_path, "w") as init_file:
         init_file.write(init_text)
Exemplo n.º 37
0
 def do_show(self, line):
     """Show me the code!
     """
     code = autopep8.fix_code("".join(self._generate_workflow_code()))
     if self.options.no_highlight:
         print code
     else:
         print highlight(code, PythonLexer(), TerminalFormatter())
Exemplo n.º 38
0
 def export_code(self):
     action_code = "from imgaug import augmenters as iaa\n\n"
     action_code += 'seq=' + self.sequential.to_code()
     action_code = autopep8.fix_code(action_code)
     save_code_path = QFileDialog.getSaveFileName(self, "保存为", r'D:\\', '*.py')[0]
     if save_code_path != '':
         with codecs.open(save_code_path, 'wb', encoding='utf-8') as save_file:
             save_file.write(action_code)
Exemplo n.º 39
0
 def do_gen(self, line):
     if os.path.exists(self.file_name):
         prompt = u"确定要生成代码?之前的代码文件 '{}' 将被覆盖(y/n)".format(
             self.file_name).encode("utf-8")
         if raw_input(prompt) != 'y':
             return
     with open(self.file_name, "w") as f:
         f.write(autopep8.fix_code("".join(self._generate_workflow_code())))
Exemplo n.º 40
0
 def validate_code(self):
     try:
         validated_code = pep8.fix_code(self, options={'verbose': 1})
         return validated_code
     except AttributeError:
         print("The input " + self + " is not a valid attribute")
     except Exception as e:
         (print(e))
Exemplo n.º 41
0
def tree_to_str(tree: AST) -> str:

    fix_missing_locations(tree)
    return autopep8.fix_code(ast.unparse(tree),
                             options={
                                 "aggressive": 1,
                                 "max_line_length": 120
                             })
Exemplo n.º 42
0
def save_code():
    if request.method == "POST":
        code_text = request.form["orig_code"]
        code_file = gen_text_file(autopep8.fix_code(code_text))
        attachment_filename = ''.join(('code_', '.py'))
        return send_file(code_file,
                         mimetype="application/x-python",
                         as_attachment=True,
                         attachment_filename=attachment_filename)
Exemplo n.º 43
0
    def _to_pep8(dd):
        # dir_backup = Dir_Root("__backup")

        for fn in dd.filenames():
            if fn.endswith(".py"):
                try:
                    content = autopep8.fix_code(to_unicode(dd.get(fn)))
                    dd.set(fn, to_utf8(to_unicode(content)))
                except Exception as e:
                    print e
                    dd.set(fn, DIR_MAIN.get(fn))
Exemplo n.º 44
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
Exemplo n.º 45
0
 def formatted(self):
     now = datetime.datetime.now()
     history = "# {}\n# version: {}\n\n".format(now.isoformat(), Env.hc().version)
     history += "from hail import *\n\n"
     for stmt in self.statements:
         history += (stmt + "\n\n")
     history += ("(" + self.expr + ")")
     try:
         import autopep8
         return autopep8.fix_code(history)
     except ImportError:
         return history
Exemplo n.º 46
0
def create_network_py(config):
    context = {
        "meta": config["meta"],
        "data": config["data"],
        "network": config["network"],
        "loss": config["loss"],
    }

    with open(os.path.join(config["path"], config["name"]), 'w') as f:
        py = render_template('network.tpl', context)
        py = autopep8.fix_code(py, options={'aggressive': 1})
        f.write(py)
Exemplo n.º 47
0
 def run(self, edit):
     syntax = self.view.settings().get('syntax')
     if syntax.lower().find('python') == -1:
         return
     replace_region = self.view.line(
         sublime.Region(0, self.view.size()))
     source = self.view.substr(replace_region)
     fixed = autopep8.fix_code(source, options=PPA.get_options())
     is_dirty, err = MergeUtils.merge_code(self.view, edit, source, fixed)
     if err:
         sublime.error_message(
             "%s: Merge failure: '%s'" % (PLUGIN_NAME, err))
         raise
Exemplo n.º 48
0
    def run_autopep(file,
                    pep_ignore,
                    pep_select,
                    max_line_length,
                    indent_size):
        new_file = autopep8.fix_code(
            ''.join(file),
            options={'ignore': pep_ignore,
                     'select': pep_select,
                     'max_line_length': max_line_length,
                     'indent_size': indent_size})

        return new_file.splitlines(True), []
Exemplo n.º 49
0
def fix_code(code, directory):
    """Formats Python code to conform to the PEP 8 style guide.

    """
    if not autopep8:
        raise Fault('autopep8 not installed, cannot fix code.',
                    code=400)
    old_dir = os.getcwd()
    try:
        os.chdir(directory)
        return autopep8.fix_code(code, apply_config=True)
    finally:
        os.chdir(old_dir)
Exemplo n.º 50
0
def create_trainer_py(config):
    context = {
        "meta": config["meta"],
        "data": config["data"],
        "network": config["network"],
        "loss": config["loss"],
        "optimizer": config["loss"]
    }

    with open(os.path.join(config["path"], "trainer.py"), 'w') as f:
        py = render_template('trainer.tpl', context)
        py = autopep8.fix_code(py, options={'aggressive': 1})
        f.write(py)
def build_pants_target(file_info):
    def _wrap_quotes(string):
        return "'%s'" % string

    name = _wrap_quotes(file_info['name'])
    sources = ", ".join([_wrap_quotes(source)
                         for source in file_info['sources']])
    dependencies = "\n".join(file_info['dependencies'])

    return autopep8.fix_code(PANTS_TARGET_TEMPLATE.substitute(
        name=name,
        sources=sources,
        dependencies=dependencies
    ))
Exemplo n.º 52
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
Exemplo n.º 53
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)
Exemplo n.º 54
0
def beautify_with_autopep8_yapf_isort(path):
  contents = get_file_contents(path)

  autopep8ed_contents = autopep8.fix_code(contents, apply_config=True)
  try:
    yapfed_contents, _ = FormatCode(
        autopep8ed_contents, filename=path, style_config='setup.cfg')
  except SyntaxError as e:
    print(e)
    return False
  isorted_contents = SortImports(file_contents=yapfed_contents).output

  if contents == isorted_contents:
    return False
  put_file_contents(path, isorted_contents)
  return True
Exemplo n.º 55
0
    def on_autopep8_activate(self):
        doc = self.window.get_active_document()
        if not doc:
            return

        try:
            encoding = doc.get_encoding().get_charset()
        except Exception as err:
            encoding = 'utf-8'
            print('Encoding err', err)

        start, end = doc.get_bounds()
        doc.set_text(autopep8.fix_code(
            doc.get_text(start, end,
                         include_hidden_chars=True).encode(encoding),
            encoding=encoding))
Exemplo n.º 56
0
def to_pep8(dir_path=CUR_PATH):
    import autopep8

    for root, dirs, files in os.walk(dir_path):
        for filename in files:
            if filename.endswith(".py") and "Utils_" not in filename:
                filepath = PATH_JOIN(root, filename)

                try:
                    with open(filepath, "r") as F:
                        content = autopep8.fix_code(to_unicode(F.read()))

                    with open(filepath, "w") as F:
                        F.write(to_utf8(to_unicode(content)))

                except Exception as e:
                    print e
    def generate_code(self, app_list, **options):
        t = loader.get_template(
            'django_extension_commands/view/{0}/{1}.tpl'.format(
                'fbv' if self.function_based else 'cbv',
                self.view_type
            ))
        if not isinstance(t, Template) and not (hasattr(t, 'template') and isinstance(t.template, Template)):
            raise Exception("Default Django template loader isn't used. "
                            "This can lead to the incorrect template rendering. "
                            "Please, check the settings.")

        c = Context({
            'app_list': app_list,
        })
        code = t.render(c)
        code = autopep8.fix_code(code)
        return code
Exemplo n.º 58
0
    def autopep8_line_range(self, f, start, end):
        """Apply autopep8 between start and end of file f xcasxz."""
        self.options.line_range = [start, end]
        fixed = autopep8.fix_code(f,   self.options)

        if self.options.docformatter:
            fixed = docformatter.format_code(
                fixed,
                summary_wrap_length=self.options.max_line_length - 1,
                description_wrap_length=(self.options.max_line_length
                                         - 2 * self.options.indent_size),
                pre_summary_newline=self.options.pre_summary_newline,
                post_description_blank=self.options.post_description_blank,
                force_wrap=self.options.force_wrap,
                line_range=[start, end])

        return fixed
Exemplo n.º 59
0
def create_spiders(spiders, schemas, extractors, items, selector='css'):
    """Create all spiders from slybot spiders."""
    item_classes = ''
    if items:
        item_classes = '\nfrom ..items import {}'.format(
            ', '.join((v().__class__.__name__ for v in items.values()))
        )
    spider_data = []
    for name, (spider, spec) in spiders.items():
        log.info('Creating spider "%s"' % spider.name)
        spider = create_spider(name, spider, spec, schemas, extractors, items,
                               selector)
        cleaned_name = _clean(name)
        filename = 'spiders/{}.py'.format(cleaned_name)
        data = '\n'.join((SPIDER_FILE(item_classes=item_classes),
                          spider.strip()))
        code = fix_code(to_unicode(data), OPTIONS)
        spider_data.append((filename, code))
    return spider_data
Exemplo n.º 60
0
    def run(self, filename, file,
            max_line_length: int = 79,
            indent_size: int = SpacingHelper.DEFAULT_TAB_WIDTH,
            pep_ignore: typed_list(str) = (),
            pep_select: typed_list(str) = (),
            local_pep8_config: bool = False,
            ):
        """
        Detects and fixes PEP8 incompliant code. This bear will not change
        functionality of the code in any way.

        :param max_line_length:
            Maximum number of characters for a line.
            When set to 0 allows infinite line length.
        :param indent_size:
            Number of spaces per indentation level.
        :param pep_ignore:
            A list of errors/warnings to ignore.
        :param pep_select:
            A list of errors/warnings to exclusively apply.
        :param local_pep8_config:
            Set to true if autopep8 should use a config file as if run normally
            from this directory.
        """
        if not max_line_length:
            max_line_length = sys.maxsize

        options = {'ignore': pep_ignore,
                   'select': pep_select,
                   'max_line_length': max_line_length,
                   'indent_size': indent_size}

        corrected = autopep8.fix_code(''.join(file),
                                      apply_config=local_pep8_config,
                                      options=options).splitlines(True)

        diffs = Diff.from_string_arrays(file, corrected).split_diff()

        for diff in diffs:
            yield Result(self,
                         'The code does not comply to PEP8.',
                         affected_code=(diff.range(filename),),
                         diffs={filename: diff})