Look into 'setup.cfg' to see if it has config info

Closes #192
pull/207/head
Bill Wendling 7 years ago
parent 57ffbbb8db
commit 6e8ca7b93e
  1. 4
      CHANGELOG
  2. 22
      README.rst
  3. 1
      setup.cfg
  4. 13
      yapf/__init__.py
  5. 13
      yapf/yapflib/file_resources.py
  6. 24
      yapf/yapflib/style.py

@ -3,6 +3,10 @@
# This project adheres to [Semantic Versioning](http://semver.org/).
## [0.6.2] UNRELEASED
### Changed
- Look at the 'setup.cfg' file to see if it contains style information for
YAPF.
### Fixed
- Make lists that can fit on one line more likely to stay together.

@ -47,7 +47,9 @@ some of the drudgery of maintaining your code.
Installation
============
To install YAPF from PyPI::
To install YAPF from PyPI:
.. code-block:: shell
$ pip install yapf
@ -58,7 +60,9 @@ is to clone this repository.
Note that if you intend to use YAPF as a command-line tool rather than as a
library, installation is not necessary. YAPF supports being run as a directory
by the Python interpreter. If you cloned/unzipped YAPF into ``DIR``, it's
possible to run::
possible to run:
.. code-block:: shell
$ PYTHONPATH=DIR python DIR/yapf [options] ...
@ -101,9 +105,9 @@ Options::
--style STYLE 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
.style.yapf file located in one of the parent
directories of the source file (or current directory
for stdin)
.style.yapf or setup.cfg file located in one of the
parent directories of the source file (or current
directory for stdin)
--style-help show style settings and exit
--no-local-style don't search for local style definition (.style.yapf)
--verify try to verify reformatted code for syntax errors
@ -121,7 +125,9 @@ the predefined styles (e.g., ``pep8`` or ``google``), a path to a configuration
file that specifies the desired style, or a dictionary of key/value pairs.
The config file is a simple listing of (case-insensitive) ``key = value`` pairs
with a ``[style]`` heading. For example::
with a ``[style]`` heading. For example:
.. code-block:: guess
[style]
based_on_style = pep8
@ -132,7 +138,9 @@ The ``based_on_style`` setting determines which of the predefined styles this
custom style is based on (think of it like subclassing).
It's also possible to do the same on the command line with a dictionary. For
example::
example:
.. code-block:: guess
--style='{based_on_style: chromium, indent_width: 4}'

@ -1,3 +1,2 @@
[bdist_wheel]
universal = 1

@ -94,16 +94,15 @@ def main(argv):
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 file located in one of the parent '
'directories of the source file (or current directory for '
'stdin)' % style.LOCAL_STYLE))
'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')
parser.add_argument(
'--no-local-style',
action='store_true',
help=("don't search for local style definition (%s)" % style.LOCAL_STYLE))
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='try to verify reformatted code for syntax errors')

@ -31,7 +31,7 @@ from yapf.yapflib import style
def GetDefaultStyleForDir(dirname):
"""Return default style name for a given directory.
Looks for .style.yapf in the parent directories.
Looks for .style.yapf or setup.cfg in the parent directories.
Arguments:
dirname: (unicode) The name of the directory.
@ -41,9 +41,20 @@ def GetDefaultStyleForDir(dirname):
"""
dirname = os.path.abspath(dirname)
while True:
# See if we have a .style.yapf file.
style_file = os.path.join(dirname, style.LOCAL_STYLE)
if os.path.exists(style_file):
return style_file
# See if we have a setup.cfg file with a '[yapf]' section.
config_file = os.path.join(dirname, style.SETUP_CONFIG)
if os.path.exists(config_file):
with open(config_file) as fd:
config = py3compat.ConfigParser()
config.read_file(fd)
if config.has_section('yapf'):
return config_file
dirname = os.path.dirname(dirname)
if (not dirname or not os.path.basename(dirname) or
dirname == os.path.abspath(os.path.sep)):

@ -301,9 +301,14 @@ def _CreateConfigParserFromConfigFile(config_filename):
with open(config_filename) as style_file:
config = py3compat.ConfigParser()
config.read_file(style_file)
if not config.has_section('style'):
raise StyleConfigError('Unable to find section [style] in {0}'.format(
config_filename))
if config_filename.endswith(SETUP_CONFIG):
if not config.has_section('yapf'):
raise StyleConfigError('Unable to find section [yapf] in {0}'.format(
config_filename))
else:
if not config.has_section('style'):
raise StyleConfigError('Unable to find section [style] in {0}'.format(
config_filename))
return config
@ -320,13 +325,19 @@ def _CreateStyleFromConfigParser(config):
StyleConfigError: if an unknown style option was encountered.
"""
# Initialize the base style.
section = 'style'
if config.has_option('style', 'based_on_style'):
based_on = config.get('style', 'based_on_style').lower()
base_style = _STYLE_NAME_TO_FACTORY[based_on]()
elif config.has_option('yapf', 'based_on_style'):
based_on = config.get('yapf', 'based_on_style').lower()
base_style = _STYLE_NAME_TO_FACTORY[based_on]()
section = 'yapf'
else:
base_style = DEFAULT_STYLE_FACTORY()
# Read all options specified in the file and update the style.
for option, value in config.items('style'):
for option, value in config.items(section):
if option.lower() == 'based_on_style':
# Now skip this one - we've already handled it and it's not one of the
# recognized style options.
@ -346,9 +357,12 @@ def _CreateStyleFromConfigParser(config):
DEFAULT_STYLE = 'pep8'
DEFAULT_STYLE_FACTORY = CreatePEP8Style
# The name of the file to use for directory-local style defintion.
# The name of the file to use for directory-local style definition.
LOCAL_STYLE = '.style.yapf'
# The name of the config file for directory-local style definition.
SETUP_CONFIG = 'setup.cfg'
# TODO(eliben): For now we're preserving the global presence of a style dict.
# Refactor this so that the style is passed around through yapf rather than
# being global.

Loading…
Cancel
Save