def __setup_import_splitter(self, enable):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(
             f'{{based_on_style: huawei, '
             f'insert_space_after_hash_char: {enable}}}'))
Exemple #2
0
 def testErrorNoStyleFile(self):
   with self.assertRaisesRegexp(style.StyleConfigError,
                                'is not a valid style or file path'):
     style.CreateStyleFromConfig('/8822/xyznosuchfile')
Exemple #3
0
 def testPEP8ByName(self):
   for pep8_name in ('PEP8', 'pep8', 'Pep8'):
     cfg = style.CreateStyleFromConfig(pep8_name)
     self.assertTrue(_LooksLikePEP8Style(cfg))
Exemple #4
0
 def testChromiumByName(self):
     for chromium_name in ('chromium', 'Chromium', 'CHROMIUM'):
         cfg = style.CreateStyleFromConfig(chromium_name)
         self.assertTrue(_LooksLikeChromiumStyle(cfg))
    def testNoSplitBeforeDictValue(self):
        try:
            style.SetGlobalStyle(
                style.CreateStyleFromConfig(
                    '{based_on_style: pep8, '
                    'allow_split_before_dict_value: false, '
                    'coalesce_brackets: true, '
                    'dedent_closing_brackets: true, '
                    'each_dict_entry_on_separate_line: true, '
                    'split_before_logical_operator: true}'))

            unformatted_code = textwrap.dedent("""\
          some_dict = {
              'title': _("I am example data"),
              'description': _("Lorem ipsum dolor met sit amet elit, si vis pacem para bellum "
                               "elites nihi very long string."),
          }
          """)
            expected_formatted_code = textwrap.dedent("""\
          some_dict = {
              'title': _("I am example data"),
              'description': _(
                  "Lorem ipsum dolor met sit amet elit, si vis pacem para bellum "
                  "elites nihi very long string."
              ),
          }
          """)
            uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
            self.assertCodeEqual(expected_formatted_code,
                                 reformatter.Reformat(uwlines))

            unformatted_code = textwrap.dedent("""\
          X = {'a': 1, 'b': 2, 'key': this_is_a_function_call_that_goes_over_the_column_limit_im_pretty_sure()}
          """)
            expected_formatted_code = textwrap.dedent("""\
          X = {
              'a': 1,
              'b': 2,
              'key': this_is_a_function_call_that_goes_over_the_column_limit_im_pretty_sure()
          }
          """)
            uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
            self.assertCodeEqual(expected_formatted_code,
                                 reformatter.Reformat(uwlines))

            unformatted_code = textwrap.dedent("""\
          attrs = {
              'category': category,
              'role': forms.ModelChoiceField(label=_("Role"), required=False, queryset=category_roles, initial=selected_role, empty_label=_("No access"),),
          }
          """)
            expected_formatted_code = textwrap.dedent("""\
          attrs = {
              'category': category,
              'role': forms.ModelChoiceField(
                  label=_("Role"),
                  required=False,
                  queryset=category_roles,
                  initial=selected_role,
                  empty_label=_("No access"),
              ),
          }
          """)
            uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
            self.assertCodeEqual(expected_formatted_code,
                                 reformatter.Reformat(uwlines))

            unformatted_code = textwrap.dedent("""\
          css_class = forms.CharField(
              label=_("CSS class"),
              required=False,
              help_text=_("Optional CSS class used to customize this category appearance from templates."),
          )
          """)
            expected_formatted_code = textwrap.dedent("""\
          css_class = forms.CharField(
              label=_("CSS class"),
              required=False,
              help_text=_(
                  "Optional CSS class used to customize this category appearance from templates."
              ),
          )
          """)
            uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
            self.assertCodeEqual(expected_formatted_code,
                                 reformatter.Reformat(uwlines))
        finally:
            style.SetGlobalStyle(style.CreatePEP8Style())
Exemple #6
0
def FormatCode(unformatted_source,
               filename='<unknown>',
               style_config=None,
               lines=None,
               print_diff=False,
               verify=False):
  """Format a string of Python code.

  This provides an alternative entry point to YAPF.

  Arguments:
    unformatted_source: (unicode) The code to format.
    filename: (unicode) The name of the file being reformatted.
    remaining arguments: see comment at the top of this module.

  Returns:
    Tuple of (reformatted_source, changed). reformatted_source conforms to the
    desired formatting style. changed is True if the source changed.
  """
  _CheckPythonVersion()
  style.SetGlobalStyle(style.CreateStyleFromConfig(style_config))
  if not unformatted_source.endswith('\n'):
    unformatted_source += '\n'

  try:
    tree = pytree_utils.ParseCodeToTree(unformatted_source)
  except parse.ParseError as e:
    e.msg = filename + ': ' + e.msg
    raise

  # disable lines outside the --lines range
  lines = _LineRangesToSet(lines)

  # Run passes on the tree, modifying it in place.
  comment_splicer.SpliceComments(tree)
  continuation_splicer.SpliceContinuations(tree)
  subtype_assigner.AssignSubtypes(tree)
  identify_container.IdentifyContainers(tree)
  split_penalty.ComputeSplitPenalties(tree)
  blank_line_calculator.CalculateBlankLines(tree)
  long_lines_splitter.SplitLongLines(tree, lines)

  uwlines = pytree_unwrapper.UnwrapPyTree(tree)

  # make all ordering of code (imports/comments/variables declarations/e.t.c.)
  _OrderCode(uwlines, style)

  for uwl in uwlines:
    uwl.CalculateFormattingInformation()

  _MarkLinesToFormat(uwlines, lines)

  uwlines = import_list_splitter.split_import_lists(uwlines)
  uwlines = comment_formatter.format_comments(uwlines)
  uwlines = _SplitSemicolons(uwlines)

  reformatted_source = reformatter.Reformat(uwlines, filename, verify, lines)

  if unformatted_source == reformatted_source:
    return '' if print_diff else reformatted_source, False

  code_diff = _GetUnifiedDiff(
      unformatted_source, reformatted_source, filename=filename)

  if print_diff:
    return code_diff, code_diff.strip() != ''  # pylint: disable=g-explicit-bool-comparison

  return reformatted_source, True
 def testDefaultBasedOnExplicitlyUnicodeTypeString(self):
   cfg = style.CreateStyleFromConfig(u'{}')
   self.assertIsInstance(cfg, dict)
Exemple #8
0
 def __setup(self, name):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(
             f'{{based_on_style: pep8, '
             f'check_module_naming_style: {name}}}'))
Exemple #9
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 errors, non-zero otherwise.
  """
    parser = argparse.ArgumentParser(description='Formatter for Python code.')
    parser.add_argument('--version',
                        action='store_true',
                        help='show version number and exit')
    parser.add_argument('--style-help',
                        action='store_true',
                        help='show style settings and exit')
    parser.add_argument(
        '--style',
        action='store',
        default='pep8',
        help=(
            'specify formatting style: either a style name (for example "pep8" '
            'or "google"), or the name of a file with style settings. pep8 is '
            'the default.'))
    parser.add_argument('--verify',
                        action='store_true',
                        help='try to verify refomatted code for syntax errors')
    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='*')
    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))
        for option, docstring in sorted(style.Help().items()):
            print(option, "=", style.Get(option), sep='')
            for line in docstring.splitlines():
                print('  ', line)
            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
        sys.stdout.write(
            yapf_api.FormatCode(py3compat.unicode('\n'.join(original_source) +
                                                  '\n'),
                                filename='<stdin>',
                                style_config=args.style,
                                lines=lines,
                                verify=args.verify))
        return 0

    files = file_resources.GetCommandLineFiles(args.files, args.recursive)
    if not files:
        raise YapfError('Input filenames did not match any python files')
    FormatFiles(files,
                lines,
                style_config=args.style,
                in_place=args.in_place,
                print_diff=args.diff,
                verify=args.verify)
    return 0
Exemple #10
0
def FormatCode(unformatted_source,
               filename='<unknown>',
               style_config=None,
               lines=None,
               print_diff=False,
               verify=True):
    """Format a string of Python code.

  This provides an alternative entry point to YAPF.

  Arguments:
    unformatted_source: (unicode) The code to format.
    filename: (unicode) The name of the file being reformatted.
    remaining arguments: see comment at the top of this module.

  Returns:
    Tuple of (reformatted_source, changed). reformatted_source conforms to the
    desired formatting style. changed is True if the source changed.
  """
    _CheckPythonVersion()
    if not unformatted_source.endswith('\n'):
        unformatted_source += '\n'

    fst_newline = unformatted_source.find('\n')

    indent_match = re.match('^(\s+)', unformatted_source[:fst_newline])
    st = style.CreateStyleFromConfig(style_config)
    if indent_match:
        unformatted_source = dedent(unformatted_source)
        original_indent = indent_match.group(0)
        original_len = len(original_indent)
        offset = original_len - original_len % st['INDENT_WIDTH']
        probable_indent = original_indent[:offset]
        st['COLUMN_LIMIT'] -= len(probable_indent)
    style.SetGlobalStyle(st)
    tree = pytree_utils.ParseCodeToTree(unformatted_source)

    # Run passes on the tree, modifying it in place.
    comment_splicer.SpliceComments(tree)
    continuation_splicer.SpliceContinuations(tree)
    subtype_assigner.AssignSubtypes(tree)
    split_penalty.ComputeSplitPenalties(tree)
    blank_line_calculator.CalculateBlankLines(tree)

    uwlines = pytree_unwrapper.UnwrapPyTree(tree)
    for uwl in uwlines:
        uwl.CalculateFormattingInformation()

    _MarkLinesToFormat(uwlines, lines)
    reformatted_source = reformatter.Reformat(uwlines, verify)
    if indent_match:
        reformatted_source = indent(reformatted_source, probable_indent)

    if unformatted_source == reformatted_source:
        return '' if print_diff else reformatted_source, False

    code_diff = _GetUnifiedDiff(unformatted_source,
                                reformatted_source,
                                filename=filename)

    if print_diff:
        return code_diff, code_diff != ''

    return reformatted_source, True
 def __setup_import_splitter(self, enable):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(
             f'{{based_on_style: huawei, '
             f'split_single_line_imports: {enable}}}'))
 def __setup(self, enable):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(
             f'{{based_on_style: pep8, '
             f'disable_splitting_by_semicolon: {enable}}}'))
 def __setup(self, value):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(
             f'{{based_on_style: pep8, '
             f'save_initial_indents_formatting: {value}}}'))
     style.Set('JOIN_MULTIPLE_LINES', True)
 def __setup(self, enable):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(
             f'{{based_on_style: pep8, '
             f'warn_lost_exceptions: {enable}}}'))
 def __setup_import_splitter(self, enable):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(
             f'{{based_on_style: pep8 '
             f'warn_not_commented_global_vars: {enable}}}'))
 def __setup(self, name):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(f'{{based_on_style: huawei, '
                                     f'check_func_naming_style: {name}}}'))
Exemple #17
0
 def testYapfByName(self):
     for yapf_name in ('yapf', 'YAPF'):
         cfg = style.CreateStyleFromConfig(yapf_name)
         self.assertTrue(_LooksLikeYapfStyle(cfg))
Exemple #18
0
 def __setup(self, enabled, pattern):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(f'{{based_on_style: pep8, '
                                     f'warn_missing_copyright: {enabled} '
                                     f'copyright_pattern: {pattern}}}'))
Exemple #19
0
 def __setup(self, enable):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(f'{{based_on_style: pep8, '
                                     f'disable_all_warnings: {enable}}}'))
 def __setup(self, enable):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(
             f'{{based_on_style: pep8, '
             f'warn_misplaced_bare_raise: {enable}}}'))
 def testDefaultBasedOnDetaultTypeString(self):
   cfg = style.CreateStyleFromConfig('{}')
   self.assertIsInstance(cfg, dict)
Exemple #22
0
    def testSplittingArguments(self):
        if sys.version_info[1] < 5:
            return

        unformatted_code = """\
async def open_file(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
    pass

async def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):
    pass

def open_file(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None):
    pass

def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):
    pass
"""
        expected_formatted_code = """\
async def open_file(
    file,
    mode='r',
    buffering=-1,
    encoding=None,
    errors=None,
    newline=None,
    closefd=True,
    opener=None
):
    pass


async def run_sync_in_worker_thread(
    sync_fn, *args, cancellable=False, limiter=None
):
    pass


def open_file(
    file,
    mode='r',
    buffering=-1,
    encoding=None,
    errors=None,
    newline=None,
    closefd=True,
    opener=None
):
    pass


def run_sync_in_worker_thread(sync_fn, *args, cancellable=False, limiter=None):
    pass
"""

        try:
            style.SetGlobalStyle(
                style.CreateStyleFromConfig(
                    '{based_on_style: pep8, '
                    'dedent_closing_brackets: true, '
                    'coalesce_brackets: false, '
                    'space_between_ending_comma_and_closing_bracket: false, '
                    'split_arguments_when_comma_terminated: true, '
                    'split_before_first_argument: true}'))

            uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
            self.assertCodeEqual(expected_formatted_code,
                                 reformatter.Reformat(uwlines))
        finally:
            style.SetGlobalStyle(style.CreatePEP8Style())
Exemple #23
0
 def testFacebookByName(self):
     for fb_name in ('facebook', 'FACEBOOK', 'Facebook'):
         cfg = style.CreateStyleFromConfig(fb_name)
         self.assertTrue(_LooksLikeFacebookStyle(cfg))
Exemple #24
0
def FormatCode(unformatted_source,
               filename='<unknown>',
               style_config=None,
               lines=None,
               print_diff=False,
               verify=False):
    """Format a string of Python code.

  This provides an alternative entry point to YAPF.

  Arguments:
    unformatted_source: (unicode) The code to format.
    filename: (unicode) The name of the file being reformatted.
    style_config: (string) Either a style name or a path to a file that contains
      formatting style settings. If None is specified, use the default style
      as set in style.DEFAULT_STYLE_FACTORY
    lines: (list of tuples of integers) A list of tuples of lines, [start, end],
      that we want to format. The lines are 1-based indexed. It can be used by
      third-party code (e.g., IDEs) when reformatting a snippet of code rather
      than a whole file.
    print_diff: (bool) Instead of returning the reformatted source, return a
      diff that turns the formatted source into reformatter source.
    verify: (bool) True if reformatted code should be verified for syntax.

  Returns:
    Tuple of (reformatted_source, changed). reformatted_source conforms to the
    desired formatting style. changed is True if the source changed.
  """
    _CheckPythonVersion()
    style.SetGlobalStyle(style.CreateStyleFromConfig(style_config))
    if not unformatted_source.endswith('\n'):
        unformatted_source += '\n'

    try:
        tree = pytree_utils.ParseCodeToTree(unformatted_source)
    except parse.ParseError as e:
        e.msg = filename + ': ' + e.msg
        raise

    # Run passes on the tree, modifying it in place.
    comment_splicer.SpliceComments(tree)
    continuation_splicer.SpliceContinuations(tree)
    subtype_assigner.AssignSubtypes(tree)
    identify_container.IdentifyContainers(tree)
    split_penalty.ComputeSplitPenalties(tree)
    blank_line_calculator.CalculateBlankLines(tree)

    uwlines = pytree_unwrapper.UnwrapPyTree(tree)
    for uwl in uwlines:
        uwl.CalculateFormattingInformation()

    lines = _LineRangesToSet(lines)
    _MarkLinesToFormat(uwlines, lines)
    reformatted_source = reformatter.Reformat(_SplitSemicolons(uwlines),
                                              verify, lines)

    if unformatted_source == reformatted_source:
        return '' if print_diff else reformatted_source, False

    code_diff = _GetUnifiedDiff(unformatted_source,
                                reformatted_source,
                                filename=filename)

    if print_diff:
        return code_diff, code_diff.strip() != ''  # pylint: disable=g-explicit-bool-comparison

    return reformatted_source, True
Exemple #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('-vv',
                        '--verbose',
                        action='store_true',
                        help='Print out file names while processing')

    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')

    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,
                          verbose=args.verbose)
    return 1 if changed and args.diff else 0
 def __setup(self, enable):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(
             f'{{based_on_style: pep8, '
             f'check_script_code_encapsulation: {enable}}}'))
Exemple #27
0
 def testDefault(self):
   # default is PEP8
   cfg = style.CreateStyleFromConfig(None)
   self.assertTrue(_LooksLikePEP8Style(cfg))
Exemple #28
0
 def __setup_import_splitter(self, enable, column_limit=80):
     style.SetGlobalStyle(
         style.CreateStyleFromConfig(
             f'{{based_on_style: pep8, '
             f'force_long_lines_wrapping: {enable}, '
             f'column_limit: {column_limit}}}'))
Exemple #29
0
 def testGoogleByName(self):
   for google_name in ('google', 'Google', 'GOOGLE'):
     cfg = style.CreateStyleFromConfig(google_name)
     self.assertTrue(_LooksLikeGoogleStyle(cfg))
Exemple #30
0
 def testHuaweiByName(self):
   for huawei_name in ('huawei', 'HUAWEI', 'Huawei'):
     cfg = style.CreateStyleFromConfig(huawei_name)
     self.assertTrue(_LooksLikeHuaweiStyle(cfg))