Example #1
0
    def testLinesRangeRemoveSome(self):
        unformatted_code = textwrap.dedent(u"""\
        def A():
          pass




        def B():  # 7
          pass  # 8




        def C():
          pass
        """)
        expected_formatted_code = textwrap.dedent(u"""\
        def A():
          pass



        def B():  # 7
          pass  # 8



        def C():
          pass
        """)
        code, changed = yapf_api.FormatCode(unformatted_code, lines=[(6, 9)])
        self.assertCodeEqual(expected_formatted_code, code)
        self.assertTrue(changed)
 def format(self, script):
     pythonReformatted = yapf_api.FormatCode(
         script, style_config=self.pythonStyle)[0]
     tokens = self.tokenizer.tokenize(
         pythonReformatted
     )  # get all strings passed to spark.sql() in the .py script
     return self.get_formatted_script_from_tokens(pythonReformatted, tokens)
Example #3
0
    def testLinesRangeBoundaryNotOutside(self):
        unformatted_code = textwrap.dedent(u"""\
        def A():
          pass



        def B():  # 6
          pass  # 7



        def C():
          pass
        """)
        expected_formatted_code = textwrap.dedent(u"""\
        def A():
          pass



        def B():  # 6
          pass  # 7



        def C():
          pass
        """)
        code, changed = yapf_api.FormatCode(unformatted_code, lines=[(6, 7)])
        self.assertCodeEqual(expected_formatted_code, code)
        self.assertFalse(changed)
Example #4
0
def index():
    source = SOURCE_CODE
    style_config = 'pep8'

    if request.method == 'POST':
        source = request.form['source']
        style_config = request.form['style_config']

    error = ''
    error_class = ''

    try:
        formatted, _ = yapf_api.FormatCode(source, style_config=style_config)
    except Exception as e:
        formatted = ''
        error = e
        error_class = e.__class__.__name__

    data = {
        'source': source,
        'formatted': formatted,
        'style_config': style_config,
        'error': error,
        'error_class': error_class,
        'yapf_version': yapf.__version__
    }

    return render_template('index.html', **data)
Example #5
0
    def export_mapping(self, plugin_name, folder):
        res = {
            "mapping": {
                "status": None,
                "file": None,
                "message": None,
                "origin": None
            }
        }
        # first check if plugin defines a custom mapping in manifest
        # if that's the case, we don't need to export mapping there
        # as it'll be exported with "uploader" code
        plugindoc = get_data_plugin().find_one({"_id": plugin_name})
        assert plugindoc, "Can't find plugin named '%s'" % plugin_name
        plugin_folder = plugindoc.get("download", {}).get("data_folder")
        assert plugin_folder, "Can't find plugin folder for '%s'" % plugin_name
        try:
            manifest = json.load(
                open(os.path.join(plugin_folder, "manifest.json")))
            if "mapping" in manifest.get("uploader", {}):
                res["mapping"][
                    "message"] = "Custom mapping included in uploader export"
                res["mapping"]["status"] = "warning"
                res["mapping"]["origin"] = "custom"
                return res
        except Exception as e:
            self.logger.error("Can't read manifest while exporting code: %s" %
                              e)
        # try to export mapping from src_master (official)
        doc = get_src_master().find_one({"_id": plugin_name})
        if doc:
            mapping = doc.get("mapping")
            res["mapping"]["origin"] = "registered"
        else:
            doc = get_src_dump().find_one({"_id": plugin_name})
            mapping = doc and doc.get("inspect", {}).get("jobs", {}).get(plugin_name, {}).get("inspect", {}).\
                get("results", {}).get("mapping")
            res["mapping"]["origin"] = "inspection"
        if not mapping:
            res["mapping"]["origin"] = None
            res["mapping"]["status"] = "warning"
            res["mapping"][
                "message"] = "Can't find registered or generated (inspection) mapping"
            return res
        else:
            ufile = os.path.join(folder, "upload.py")
            strmap, _ = yapf_api.FormatCode(pprint.pformat(mapping))
            with open(ufile, "a") as fout:
                fout.write("""
    @classmethod
    def get_mapping(klass):
        return %s\n""" % textwrap.indent((strmap), prefix="    " * 2))

        res["mapping"]["file"] = ufile
        res["mapping"]["status"] = "ok"

        return res
Example #6
0
def test_wrong_indent():
    code = (
        u'    very_long_variable_name = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)'
    )
    correct_code = u"""\
    very_long_variable_name = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
                               15, 16)"""
    formatted_code, _ = yapf_api.FormatCode(code, style_config='pep8')
    assert correct_code == formatted_code
Example #7
0
    def Format(self, code: str) -> str:
        """Formats the code.

    Args:
      code (str): the code to format

    Returns:
      str: the formatted code
    """
        return yapf_api.FormatCode(code, style_config=self.yapf_path)
Example #8
0
    def get_code(self, f):
        code = f.to_python(namespace=self.namespace)
        if self.unformatted:
            return code

        try:
            from yapf.yapflib import yapf_api
        except ModuleNotFoundError:
            return code
        code, changed = yapf_api.FormatCode(code)
        return code
Example #9
0
def fix_code(code):
    """Formats Python code to conform to the PEP 8 style guide.

    """
    if not yapf_api:
        raise Fault('yapf not installed', code=400)
    style_config = file_resources.GetDefaultStyleForDir(os.getcwd())
    reformatted_source, _ = yapf_api.FormatCode(code,
                                                filename='<stdin>',
                                                style_config=style_config,
                                                verify=False)
    return reformatted_source
Example #10
0
def yapf_reformat_handler(text_before_cursor):
    from yapf.yapflib import file_resources
    from yapf.yapflib import yapf_api

    style_config = file_resources.GetDefaultStyleForDir(os.getcwd())
    formatted_text, was_formatted = yapf_api.FormatCode(
        text_before_cursor, style_config=style_config
    )
    if was_formatted:
        if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"):
            formatted_text = formatted_text[:-1]
        return formatted_text
    else:
        return text_before_cursor
Example #11
0
def fix_code(code, directory):
    """Formats Python code to conform to the PEP 8 style guide.

    """
    if not yapf_api:
        raise Fault('yapf not installed', code=400)
    style_config = file_resources.GetDefaultStyleForDir(directory
                                                        or os.getcwd())
    try:
        reformatted_source, _ = yapf_api.FormatCode(code,
                                                    filename='<stdin>',
                                                    style_config=style_config,
                                                    verify=False)
        return reformatted_source
    except Exception as e:
        raise Fault("Error during formatting: {}".format(e), code=400)
Example #12
0
def main():
    if not _YAPF_IMPORTED:
        return

    encoding = vim.eval('&encoding')
    buf = vim.current.buffer
    unicode_buf = [unicode(s, encoding) for s in buf]
    text = '\n'.join(unicode_buf)
    buf_range = (vim.current.range.start, vim.current.range.end)
    lines_range = [pos + 1 for pos in buf_range]
    style_config = _get_style()
    vim.command('let l:used_style = "{}"'.format(style_config))
    try:
        formatted = yapf_api.FormatCode(text,
                                        filename='<stdin>',
                                        style_config=style_config,
                                        lines=[lines_range],
                                        verify=False)
    except (SyntaxError, IndentationError) as err:
        vim.command('let l:error_type = "{}"'.format(type(err).__name__))
        vim.command('let l:error_position = [{}, {}]'.format(
            err.lineno, err.offset))
        return

    if isinstance(formatted, tuple):
        formatted = formatted[0]

    lines = formatted.rstrip('\n').split('\n')
    sequence = difflib.SequenceMatcher(None, unicode_buf, lines)

    allow_out_of_range = vim.eval("l:allow_out_of_range") != "0"

    for op in reversed(sequence.get_opcodes()):
        if op[0] == 'equal':
            continue
        # buf_range is [closed, closed], and op is [closed,open)
        # so we must offset buf_range[1]
        in_range = max(buf_range[0], op[1]) <= min(buf_range[1] + 1, op[2])
        if in_range or allow_out_of_range:
            vim.current.buffer[op[1]:op[2]] = [
                l.encode(encoding) for l in lines[op[3]:op[4]]
            ]
Example #13
0
    def render_one_to_python(self, order: 'Order', testing=False) -> str:
        """
        Render one order to a Python trace file.

        Return the name of the file written
        """
        if testing:
            py_lines = ["from arkimapslib.unittest import mock_macro as macro"]
        else:
            py_lines = ["import os"]
            for k, v in self.env_overrides.items():
                py_lines.append(f"os.environ[{k!r}] = {v!r}")
            py_lines.append("from Magics import macro")

        output_pathname = os.path.join(self.workdir, order.relpath,
                                       order.basename)
        order_args = "".join(
            [f", {k}={v!r}" for k, v in order.output_options.items()])
        py_lines.append(
            f"parts = [macro.output(output_formats=['png'], output_name={output_pathname!r},"
            f" output_name_first_page_number='off'{order_args})]", )

        for step in order.order_steps:
            name, parms = step.as_magics_macro()
            py_parms = []
            for k, v in parms.items():
                py_parms.append(f"{k}={v!r}")
            py_lines.append(
                f"parts.append(macro.{name}({', '.join(py_parms)}))")

        py_lines.append("macro.plot(*parts)")

        code = "\n".join(py_lines)
        try:
            from yapf.yapflib import yapf_api
            code, changed = yapf_api.FormatCode(code)
        except ModuleNotFoundError:
            pass

        return code
Example #14
0
    def export_dumper(self, plugin_name, folder):
        res = {
            "dumper": {
                "status": None,
                "file": None,
                "class": None,
                "message": None
            }
        }
        try:
            dclass = self.dumper_manager[plugin_name]
        except KeyError:
            res["dumper"]["status"] = "warning"
            res["dumper"][
                "message"] = "No dumper found for plugin '%s'" % plugin_name
        try:
            dumper_name = plugin_name.capitalize() + "Dumper"
            self.logger.debug("Exporting dumper %s" % dumper_name)
            assert len(dclass) == 1, "More than one dumper found: %s" % dclass
            dclass = dclass[0]
            assert hasattr(dclass, "python_code"), "No generated code found"
            dinit = os.path.join(folder, "__init__.py")
            dfile = os.path.join(folder, "dump.py")
            # clear init, we'll append code
            # we use yapf (from Google) as autopep8 (for instance) doesn't give
            # good results in term in indentation (input_type list for keylookup for instance)
            beauty, _ = yapf_api.FormatCode(dclass.python_code)
            with open(dfile, "w") as fout:
                fout.write(beauty)
            with open(dinit, "a") as fout:
                fout.write("from .dump import %s\n" % dumper_name)
            res["dumper"]["status"] = "ok"
            res["dumper"]["file"] = dfile
            res["dumper"]["class"] = dumper_name
        except Exception as e:
            res["dumper"]["status"] = "error"
            res["dumper"]["message"] = "Error exporting dumper: %s" % e
            return res

        return res
Example #15
0
    def testLinesOnRangeBoundary(self):
        unformatted_code = textwrap.dedent(u"""\
        def A():
          pass

        def B():  # 4
          pass  # 5

        def C():
          pass
        def D():  # 9
          pass  # 10
        def E():
          pass
        """)
        expected_formatted_code = textwrap.dedent(u"""\
        def A():
          pass


        def B():  # 4
          pass  # 5


        def C():
          pass


        def D():  # 9
          pass  # 10


        def E():
          pass
        """)
        code, changed = yapf_api.FormatCode(unformatted_code,
                                            lines=[(4, 5), (9, 10)])
        self.assertCodeEqual(expected_formatted_code, code)
        self.assertTrue(changed)
    def get_formatted_script_from_tokens(self, script, tokens):
        '''
        Format the given script.

        Parameters
        scirpt: string
            The script to format.
        
        Return: string
            The formatted script.
        '''
        formattedScript = ''
        for token in tokens:
            formattedScript += script[self.pointer:token.start]
            formattedQuery = sparksqlformatter_api.format_query(
                token.value, self.sparksqlStyle
            )  # will get rid of starting/trailling blank spaces

            formattedQuery = Formatter.indent_query(formattedQuery,
                                                    token.indent)
            if not script[(token.start - 3):token.start] in [
                    "'''", '"""'
            ]:  # handle queries quoted by '' or "" that are possibly formatted to multiline
                if '\n' in formattedQuery:  # if query is multiline
                    formattedScript = formattedScript[:-1] + "'''\n"  # remove starting ' or " and replace with triple single quotes
                    formattedScript += formattedQuery
                    formattedScript += '\n' + token.indent + "'''"  # add ending triple quotes on a separate line
                    self.pointer = token.end + 1  # skip pointer over ending ' or "
                else:  # if query is single line
                    formattedScript += formattedQuery.lstrip(
                    )  # remove added indent
                    self.pointer = token.end
            else:
                formattedScript += '\n' + formattedQuery + '\n' + token.indent  # properly format between triple quotes
                self.pointer = token.end
        formattedScript += script[self.pointer:]
        self.reset()
        return yapf_api.FormatCode(formattedScript,
                                   style_config=self.pythonStyle)[0]
Example #17
0
    def export_uploader(self, plugin_name, folder):
        res = {
            "uploader": {
                "status": None,
                "file": None,
                "class": None,
                "message": None
            }
        }
        try:
            uclass = self.uploader_manager[plugin_name]
        except KeyError:
            res["uploader"]["status"] = "warning"
            res["uploader"][
                "message"] = "No uploader found for plugin '%s'" % plugin_name
            return res
        try:
            uploader_name = plugin_name.capitalize() + "Uploader"
            self.logger.debug("Exporting uploader %s" % uploader_name)
            assert len(
                uclass) == 1, "More than one uploader found: %s" % uclass
            uclass = uclass[0]
            assert hasattr(uclass, "python_code"), "No generated code found"
            dinit = os.path.join(folder, "__init__.py")
            ufile = os.path.join(folder, "upload.py")
            beauty, _ = yapf_api.FormatCode(uclass.python_code)
            with open(ufile, "w") as fout:
                fout.write(beauty)
            with open(dinit, "a") as fout:
                fout.write("from .upload import %s\n" % uploader_name)
            res["uploader"]["status"] = "ok"
            res["uploader"]["file"] = ufile
            res["uploader"]["class"] = uploader_name
        except Exception as e:
            res["uploader"]["status"] = "error"
            res["uploader"]["message"] = "Error exporting uploader: %s" % e
            return res

        return res
Example #18
0
    def export_mapping(self, plugin_name, folder):
        res = {
            "mapping": {
                "status": None,
                "file": None,
                "message": None,
                "origin": None
            }
        }
        # first try to export mapping from src_master (official)
        doc = get_src_master().find_one({"_id": plugin_name})
        if doc:
            mapping = doc.get("mapping")
            res["mapping"]["origin"] = "registered"
        else:
            doc = get_src_dump().find_one({"_id": plugin_name})
            mapping = doc and doc.get("inspect",{}).get("jobs",{}).get(plugin_name,{}).get("inspect",{}).\
                          get("results",{}).get("mapping")
            res["mapping"]["origin"] = "inspection"
        if not mapping:
            res["mapping"]["origin"] = None
            res["mapping"]["status"] = "warning"
            res["mapping"][
                "message"] = "Can't find registered or generated (inspection) mapping"
            return res
        else:
            ufile = os.path.join(folder, "upload.py")
            strmap, _ = yapf_api.FormatCode(pprint.pformat(mapping))
            with open(ufile, "a") as fout:
                fout.write("""
    @classmethod
    def get_mapping(klass):
        return %s\n""" % textwrap.indent((strmap), prefix="    " * 2))

        res["mapping"]["file"] = ufile
        res["mapping"]["status"] = "ok"

        return res
Example #19
0
 def slot_code_format(self):
     """
     格式化代码
     注意,格式化之前需要保存光标的位置,格式化之后再将光标设置回当前的位置。
     :return:
     """
     text = self.text().strip()
     prev_pos = self.textEdit.getCursorPosition()
     if not text:
         return
     text = py3compat.removeBOM(text)
     try:
         reformatted_source, _ = yapf_api.FormatCode(
             text,
             filename=self.filename(),
             # print_diff=True,
             style_config=os.path.join(os.path.dirname(__file__), 'config',
                                       '.style.yapf'))
         self.set_text(reformatted_source)
         self.textEdit.setCursorPosition(*prev_pos)
     except Exception as e:
         logger.warning(str(e))
         lines = re.findall(r'line (\d+)\)', str(e))
         row = -1
         if lines:
             # 跳转到指定行
             row = int(lines[0])
             row = row - 1 if row else 0
             col = self.textEdit.lineLength(row)
             self.textEdit.setCursorPosition(row, col - 1)
             # 标记波浪线
             self.textEdit.fillIndicatorRange(row, 0, row, col,
                                              self._indicator_error2)
         QMessageBox.critical(self, self.tr('Error'), str(e))
         if row > -1:
             # 清除被标记波浪线
             self.textEdit.clearIndicatorRange(row, 0, row, col,
                                               self._indicator_error2)
Example #20
0
    def write_file(self, f: Model, file: TextIO):
        code = f.to_python(namespace=self.namespace)

        if not self.unformatted:
            try:
                from yapf.yapflib import yapf_api
            except ModuleNotFoundError:
                pass
            else:
                code, changed = yapf_api.FormatCode(code)

        if self.loadable:
            print("import datetime", file=file)
            print("from decimal import Decimal", file=file)
            if self.namespace:
                print("import", self.namespace, file=file)
            elif self.namespace is False:
                print("from a38.fattura import *", file=file)
            else:
                print("import a38", file=file)
            print(file=file)
            print("fattura = ", file=file, end="")
        print(code, file=file)
Example #21
0
def test_wrong_indent(num_of_spaces, correct_num_of_spaces):
    stmt = 'a = 1\n'
    code = ' ' * num_of_spaces + stmt
    formatted_code, _ = yapf_api.FormatCode(code, style_config='pep8')
    spaces = len(match('^(\s+)', formatted_code).group(0))
    assert spaces == correct_num_of_spaces
Example #22
0
 def _Check(self, unformatted_code, expected_formatted_code):
     formatted_code, _ = yapf_api.FormatCode(unformatted_code,
                                             style_config='chromium')
     self.assertEqual(expected_formatted_code, formatted_code)
Example #23
0
 def _Check(self, unformatted_code, expected_formatted_code):
     formatted_code, _ = yapf_api.FormatCode(
         unformatted_code,
         style_config=style.SetGlobalStyle(self._OwnStyle()))
     self.assertEqual(expected_formatted_code, formatted_code)
Example #24
0
def main(argv):
  """Main program.

  Arguments:
    argv: (Positional arguments) A list of files to reformat.

  Returns:
    0 if there were no errors, non-zero otherwise.
  """
  parser = argparse.ArgumentParser(description='Formatter for Python code.')
  parser.add_argument(
      '--style', action='store', default=None,
      help=('specify formatting style: either a style name (for example "pep8" '
            'or "google"), or the name of a file with style settings'))
  diff_inplace_group = parser.add_mutually_exclusive_group()
  diff_inplace_group.add_argument(
      '-d', '--diff', action='store_true',
      help='print the diff for the fixed source')
  diff_inplace_group.add_argument(
      '-i', '--in-place', action='store_true',
      help='make changes to files in place')

  lines_recursive_group = parser.add_mutually_exclusive_group()
  lines_recursive_group.add_argument(
      '-l', '--lines', metavar='START-END', action='append', default=None,
      help='range of lines to reformat, one-based')
  lines_recursive_group.add_argument(
      '-r', '--recursive', action='store_true',
      help='run recursively over directories')

  parser.add_argument('files', nargs=argparse.REMAINDER)
  args = parser.parse_args()

  if args.lines and len(args.files) > 1:
    parser.error('cannot use -l/--lines with more than one file')

  lines = _GetLines(args.lines) if args.lines is not None else None
  files = file_resources.GetCommandLineFiles(argv[1:], args.recursive)
  if not files:
    # No arguments specified. Read code from stdin.
    if args.in_place or args.diff:
      parser.error('cannot use --in_place or --diff flags when reading '
                   'from stdin')

    original_source = []
    while True:
      try:
        # Use 'raw_input' instead of 'sys.stdin.read', because otherwise the
        # user will need to hit 'Ctrl-D' more than once if they're inputting
        # the program by hand. 'raw_input' throws an EOFError exception if
        # 'Ctrl-D' is pressed, which makes it easy to bail out of this loop.
        original_source.append(py3compat.raw_input())
      except EOFError:
        break
    sys.stdout.write(yapf_api.FormatCode(
        py3compat.unicode('\n'.join(original_source) + '\n'),
        filename='<stdin>',
        style_config=args.style,
        lines=lines))
    return 0

  FormatFiles(files, lines, style_config=args.style, in_place=args.in_place,
              print_diff=args.diff)
  return 0
Example #25
0
def main(argv):
  """Main program.

  Arguments:
    argv: command-line arguments, such as sys.argv (including the program name
      in argv[0]).

  Returns:
    0 if there were no changes, non-zero otherwise.

  Raises:
    YapfError: if none of the supplied files were Python files.
  """
  parser = argparse.ArgumentParser(description='Formatter for Python code.')
  parser.add_argument(
      '-v',
      '--version',
      action='store_true',
      help='show version number and exit')

  diff_inplace_group = parser.add_mutually_exclusive_group()
  diff_inplace_group.add_argument(
      '-d',
      '--diff',
      action='store_true',
      help='print the diff for the fixed source')
  diff_inplace_group.add_argument(
      '-i',
      '--in-place',
      action='store_true',
      help='make changes to files in place')

  lines_recursive_group = parser.add_mutually_exclusive_group()
  lines_recursive_group.add_argument(
      '-r',
      '--recursive',
      action='store_true',
      help='run recursively over directories')
  lines_recursive_group.add_argument(
      '-l',
      '--lines',
      metavar='START-END',
      action='append',
      default=None,
      help='range of lines to reformat, one-based')

  parser.add_argument(
      '-e',
      '--exclude',
      metavar='PATTERN',
      action='append',
      default=None,
      help='patterns for files to exclude from formatting')
  parser.add_argument(
      '--style',
      action='store',
      help=('specify formatting style: either a style name (for example "pep8" '
            'or "google"), or the name of a file with style settings. The '
            'default is pep8 unless a %s or %s file located in one of the '
            'parent directories of the source file (or current directory for '
            'stdin)' % (style.LOCAL_STYLE, style.SETUP_CONFIG)))
  parser.add_argument(
      '--style-help',
      action='store_true',
      help=('show style settings and exit; this output can be '
            'saved to .style.yapf to make your settings '
            'permanent'))
  parser.add_argument(
      '--no-local-style',
      action='store_true',
      help="don't search for local style definition")
  parser.add_argument('--verify', action='store_true', help=argparse.SUPPRESS)
  parser.add_argument(
      '-p',
      '--parallel',
      action='store_true',
      help=('Run yapf in parallel when formatting multiple files. Requires '
            'concurrent.futures in Python 2.X'))

  parser.add_argument('files', nargs='*')
  args = parser.parse_args(argv[1:])

  if args.version:
    print('yapf {}'.format(__version__))
    return 0

  if args.style_help:
    style.SetGlobalStyle(style.CreateStyleFromConfig(args.style))
    print('[style]')
    for option, docstring in sorted(style.Help().items()):
      for line in docstring.splitlines():
        print('#', line and ' ' or '', line, sep='')
      print(option.lower(), '=', style.Get(option), sep='')
      print()
    return 0

  if args.lines and len(args.files) > 1:
    parser.error('cannot use -l/--lines with more than one file')

  lines = _GetLines(args.lines) if args.lines is not None else None
  if not args.files:
    # No arguments specified. Read code from stdin.
    if args.in_place or args.diff:
      parser.error('cannot use --in-place or --diff flags when reading '
                   'from stdin')

    original_source = []
    while True:
      try:
        # Use 'raw_input' instead of 'sys.stdin.read', because otherwise the
        # user will need to hit 'Ctrl-D' more than once if they're inputting
        # the program by hand. 'raw_input' throws an EOFError exception if
        # 'Ctrl-D' is pressed, which makes it easy to bail out of this loop.
        original_source.append(py3compat.raw_input())
      except EOFError:
        break

    style_config = args.style
    if style_config is None and not args.no_local_style:
      style_config = file_resources.GetDefaultStyleForDir(os.getcwd())

    source = [line.rstrip() for line in original_source]
    reformatted_source, _ = yapf_api.FormatCode(
        py3compat.unicode('\n'.join(source) + '\n'),
        filename='<stdin>',
        style_config=style_config,
        lines=lines,
        verify=args.verify)
    file_resources.WriteReformattedCode('<stdout>', reformatted_source)
    return 0

  files = file_resources.GetCommandLineFiles(args.files, args.recursive,
                                             args.exclude)
  if not files:
    raise errors.YapfError('Input filenames did not match any python files')

  FormatFiles(
      files,
      lines,
      style_config=args.style,
      no_local_style=args.no_local_style,
      in_place=args.in_place,
      print_diff=args.diff,
      verify=args.verify,
      parallel=args.parallel)
  return 0
Example #26
0
def main(argv):
    """Main program.

  Arguments:
    argv: command-line arguments, such as sys.argv (including the program name
      in argv[0]).

  Returns:
    Zero on successful program termination, non-zero otherwise.
    With --diff: zero if there were no changes, non-zero otherwise.

  Raises:
    YapfError: if none of the supplied files were Python files.
  """
    args = _ParseArguments(argv)
    if args.version:
        print('yapf {}'.format(__version__))
        return 0

    style_config = args.style

    if args.style_help:
        print_help(args)
        return 0

    if args.lines and len(args.files) > 1:
        parser.error('cannot use -l/--lines with more than one file')

    lines = _GetLines(args.lines) if args.lines is not None else None
    if not args.files:
        # No arguments specified. Read code from stdin.
        if args.in_place or args.diff:
            parser.error('cannot use --in-place or --diff flags when reading '
                         'from stdin')

        original_source = []
        while True:
            if sys.stdin.closed:
                break
            try:
                # Use 'raw_input' instead of 'sys.stdin.read', because otherwise the
                # user will need to hit 'Ctrl-D' more than once if they're inputting
                # the program by hand. 'raw_input' throws an EOFError exception if
                # 'Ctrl-D' is pressed, which makes it easy to bail out of this loop.
                original_source.append(py3compat.raw_input())
            except EOFError:
                break
            except KeyboardInterrupt:
                return 1

        if style_config is None and not args.no_local_style:
            style_config = file_resources.GetDefaultStyleForDir(os.getcwd())

        source = [line.rstrip() for line in original_source]
        source[0] = py3compat.removeBOM(source[0])

        try:
            reformatted_source, _ = yapf_api.FormatCode(
                py3compat.unicode('\n'.join(source) + '\n'),
                filename='<stdin>',
                style_config=style_config,
                lines=lines,
                verify=args.verify)
        except tokenize.TokenError as e:
            raise errors.YapfError('%s:%s' % (e.args[1][0], e.args[0]))

        file_resources.WriteReformattedCode('<stdout>', reformatted_source)
        return 0

    # Get additional exclude patterns from ignorefile
    exclude_patterns_from_ignore_file = file_resources.GetExcludePatternsForDir(
        os.getcwd())

    files = file_resources.GetCommandLineFiles(
        args.files, args.recursive,
        (args.exclude or []) + exclude_patterns_from_ignore_file)
    if not files:
        raise errors.YapfError(
            'Input filenames did not match any python files')

    changed = FormatFiles(files,
                          lines,
                          style_config=args.style,
                          no_local_style=args.no_local_style,
                          in_place=args.in_place,
                          print_diff=args.diff,
                          verify=args.verify,
                          parallel=args.parallel,
                          quiet=args.quiet,
                          verbose=args.verbose)
    return 1 if changed and (args.diff or args.quiet) else 0
Example #27
0
            src = src_file.read()

        if 'autopep8' in opts:
            old_src = src
            src = autopep8.fix_code(src,
                                    options={
                                        'aggressive': args.autopep8_agression,
                                        'ignore': ['E1', 'W1', 'E501']
                                    })
            changed |= (old_src != src)
            del old_src

        if 'quotes' in opts:
            old_src = src
            try:
                src = fixers_api.Pre2to3FixerRun(src, {'fixers': ['quotes']})
            except:
                import traceback
                print("Error trying to fix quotes: " + traceback.format_exc())
            else:
                changed |= (old_src != src)
                del old_src

        if 'yapf' in opts:
            src, _changed = yapf_api.FormatCode(src, file, 'yapf')
            changed |= _changed

        if changed:
            with open(file, 'w') as src_file:
                src_file.write(src)
Example #28
0
 def testBadSyntax(self):
     code = '  a = 1\n'
     yapf_api.FormatCode(code)
Example #29
0
def main(argv):
  """Main program.

  Arguments:
    argv: command-line arguments, such as sys.argv (including the program name
      in argv[0]).

  Returns:
    Zero on successful program termination, non-zero otherwise.
    With --diff: zero if there were no changes, non-zero otherwise.

  Raises:
    YapfError: if none of the supplied files were Python files.
  """
  parser = argparse.ArgumentParser(description='Formatter for Python code.')
  parser.add_argument(
      '-v',
      '--version',
      action='store_true',
      help='show version number and exit')

  diff_inplace_quiet_group = parser.add_mutually_exclusive_group()
  diff_inplace_quiet_group.add_argument(
      '-d',
      '--diff',
      action='store_true',
      help='print the diff for the fixed source')
  diff_inplace_quiet_group.add_argument(
      '-i',
      '--in-place',
      action='store_true',
      help='make changes to files in place')
  diff_inplace_quiet_group.add_argument(
      '-q',
      '--quiet',
      action='store_true',
      help='output nothing and set return value')

  lines_recursive_group = parser.add_mutually_exclusive_group()
  lines_recursive_group.add_argument(
      '-r',
      '--recursive',
      action='store_true',
      help='run recursively over directories')
  lines_recursive_group.add_argument(
      '-l',
      '--lines',
      metavar='START-END',
      action='append',
      default=None,
      help='range of lines to reformat, one-based')

  parser.add_argument(
      '-e',
      '--exclude',
      metavar='PATTERN',
      action='append',
      default=None,
      help='patterns for files to exclude from formatting')
  parser.add_argument(
      '--style',
      action='store',
      help=('specify formatting style: either a style name (for example "pep8" '
            'or "google"), or the name of a file with style settings. The '
            'default is pep8 unless a %s or %s file located in the same '
            'directory as the source or one of its parent directories '
            '(for stdin, the current directory is used).' %
            (style.LOCAL_STYLE, style.SETUP_CONFIG)))
  parser.add_argument(
      '--style-help',
      action='store_true',
      help=('show style settings and exit; this output can be '
            'saved to .style.yapf to make your settings '
            'permanent'))
  parser.add_argument(
      '--no-local-style',
      action='store_true',
      help="don't search for local style definition")
  parser.add_argument('--verify', action='store_true', help=argparse.SUPPRESS)
  parser.add_argument(
      '-p',
      '--parallel',
      action='store_true',
      help=('run yapf in parallel when formatting multiple files. Requires '
            'concurrent.futures in Python 2.X'))
  parser.add_argument(
      '-vv',
      '--verbose',
      action='store_true',
      help='print out file names while processing')

  parser.add_argument(
      'files', nargs='*', help='reads from stdin when no files are specified.')
  args = parser.parse_args(argv[1:])

  if args.version:
    print('yapf {}'.format(__version__))
    return 0

  style_config = args.style

  if args.style_help:
    print_help(args)
    return 0

  if args.lines and len(args.files) > 1:
    parser.error('cannot use -l/--lines with more than one file')

  lines = _GetLines(args.lines) if args.lines is not None else None
  if not args.files:
    # No arguments specified. Read code from stdin.
    if args.in_place or args.diff:
      parser.error('cannot use --in-place or --diff flags when reading '
                   'from stdin')

    original_source = []
    while True:
      if sys.stdin.closed:
        break
      try:
        # Use 'raw_input' instead of 'sys.stdin.read', because otherwise the
        # user will need to hit 'Ctrl-D' more than once if they're inputting
        # the program by hand. 'raw_input' throws an EOFError exception if
        # 'Ctrl-D' is pressed, which makes it easy to bail out of this loop.
        original_source.append(py3compat.raw_input())
      except EOFError:
        break
      except KeyboardInterrupt:
        return 1

    if style_config is None and not args.no_local_style:
      style_config = file_resources.GetDefaultStyleForDir(os.getcwd())

    source = [line.rstrip() for line in original_source]
    source[0] = py3compat.removeBOM(source[0])

    try:
      reformatted_source, _ = yapf_api.FormatCode(
          py3compat.unicode('\n'.join(source) + '\n'),
          filename='<stdin>',
          style_config=style_config,
          lines=lines,
          verify=args.verify)
    except tokenize.TokenError as e:
      raise errors.YapfError('%s:%s' % (e.args[1][0], e.args[0]))

    file_resources.WriteReformattedCode('<stdout>', reformatted_source)
    return 0

  # Get additional exclude patterns from ignorefile
  exclude_patterns_from_ignore_file = file_resources.GetExcludePatternsForDir(
      os.getcwd())

  files = file_resources.GetCommandLineFiles(args.files, args.recursive,
                                             (args.exclude or []) +
                                             exclude_patterns_from_ignore_file)
  if not files:
    raise errors.YapfError('Input filenames did not match any python files')

  changed = FormatFiles(
      files,
      lines,
      style_config=args.style,
      no_local_style=args.no_local_style,
      in_place=args.in_place,
      print_diff=args.diff,
      verify=args.verify,
      parallel=args.parallel,
      quiet=args.quiet,
      verbose=args.verbose)
  return 1 if changed and (args.diff or args.quiet) else 0
Example #30
0
 def _Check(self, unformatted_code, expected_formatted_code):
     style.SetGlobalStyle(style.CreateGoogleStyle())
     formatted_code = yapf_api.FormatCode(unformatted_code)
     self.assertEqual(expected_formatted_code, formatted_code)