def _CreateConfigParserFromConfigFile(config_filename): """Read the file and return a ConfigParser object.""" if not os.path.exists(config_filename): # Provide a more meaningful error here. raise StyleConfigError( '"{0}" is not a valid style or file path'.format(config_filename)) with open(config_filename) as style_file: config = py3compat.ConfigParser() config.read_file(style_file) if config_filename.endswith(SETUP_CONFIG): if not config.has_section('yapf'): raise StyleConfigError( 'Unable to find section [yapf] in {0}'.format( config_filename)) elif config_filename.endswith(LOCAL_STYLE): if not config.has_section('style'): raise StyleConfigError( 'Unable to find section [style] 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
def _CreateConfigParserFromConfigString(config_string): """Given a config string from the command line, return a config parser.""" config = py3compat.ConfigParser() config.add_section('style') for key, value in re.findall(r'([a-zA-Z0-9_]*): *([a-zA-Z0-9_]+)', config_string): config.set('style', key, value) return config
def _CreateConfigParserFromConfigString(config_string): """Given a config string from the command line, return a config parser.""" if config_string[0] != '{' or config_string[-1] != '}': raise StyleConfigError( "Invalid style dict syntax: '{}'.".format(config_string)) config = py3compat.ConfigParser() config.add_section('style') for key, value in re.findall(r'([a-zA-Z0-9_]+)\s*[:=]\s*([a-zA-Z0-9_]+)', config_string): config.set('style', key, value) return config
def _CreateConfigParserFromConfigFile(config_filename): """Read the file and return a ConfigParser object.""" if not os.path.exists(config_filename): # Provide a more meaningful error here. raise StyleConfigError( '"{0}" is not a valid style or file path'.format(config_filename)) with open(config_filename) as style_file: config = py3compat.ConfigParser() if config_filename.endswith(PYPROJECT_TOML): try: import toml except ImportError: raise errors.YapfError( "toml package is needed for using pyproject.toml as a configuration file" ) pyproject_toml = toml.load(style_file) style_dict = pyproject_toml.get("tool", {}).get("yapf", None) if style_dict is None: raise StyleConfigError( 'Unable to find section [tool.yapf] in {0}'.format( config_filename)) config.add_section('style') for k, v in style_dict.items(): config.set('style', k, str(v)) return config config.read_file(style_file) if config_filename.endswith(SETUP_CONFIG): if not config.has_section('yapf'): raise StyleConfigError( 'Unable to find section [yapf] in {0}'.format( config_filename)) return config if config_filename.endswith(LOCAL_STYLE): if not config.has_section('style'): raise StyleConfigError( 'Unable to find section [style] in {0}'.format( config_filename)) return config if not config.has_section('style'): raise StyleConfigError( 'Unable to find section [style] in {0}'.format( config_filename)) return config
def GetDefaultStyleForDir(dirname, default_style=style.DEFAULT_STYLE): """Return default style name for a given directory. Looks for .style.yapf or setup.cfg in the parent directories. Arguments: dirname: (unicode) The name of the directory. default_style: The style to return if nothing is found. Defaults to the global default style ('pep8') unless otherwise specified. Returns: The filename if found, otherwise return the default style. """ 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) try: fd = open(config_file) except IOError: pass # It's okay if it's not there. else: with fd: config = py3compat.ConfigParser() config.read_file(fd) if config.has_section('yapf'): return config_file if (not dirname or not os.path.basename(dirname) or dirname == os.path.abspath(os.path.sep)): break dirname = os.path.dirname(dirname) global_file = os.path.expanduser(style.GLOBAL_STYLE) if os.path.exists(global_file): return global_file return default_style
def _CreateStyleFormConfigFile(config_filename): """Create a style dict from a configuration file. Arguments: config_filename: name of a config file. Returns: A style dict. Raises: StyleConfigError: if an unknown style option was encountered. """ if not os.path.exists(config_filename): # Provide a more meaningful error here. raise StyleConfigError('"{0}" is not a valid style or file path'.format( 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)) # Initialize the base 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]() else: base_style = DEFAULT_STYLE_FACTORY() # Read all options specified in the file and update the style. for option, value in config.items('style'): 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. continue option = option.upper() if option not in _STYLE_OPTION_VALUE_CONVERTER: raise StyleConfigError('Unknown style option "{0}"'.format(option)) base_style[option] = _STYLE_OPTION_VALUE_CONVERTER[option](value) return base_style
def GetDefaultStyleForDir(dirname): """Return default style name for a given directory. Looks for .style.yapf or setup.cfg in the parent directories. Arguments: dirname: (unicode) The name of the directory. Returns: The filename if found, otherwise return the global default (pep8). """ 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)): break global_file = os.path.expanduser(style.GLOBAL_STYLE) if os.path.exists(global_file): return global_file return style.DEFAULT_STYLE
def _CreateConfigParserFromConfigDict(config_dict): config = py3compat.ConfigParser() config.add_section('style') for key, value in config_dict.items(): config.set('style', key, str(value)) return config
def GetDefaultStyleForDir(dirname, default_style=style.DEFAULT_STYLE): """Return default style name for a given directory. Looks for .style.yapf or setup.cfg or pyproject.toml in the parent directories. Arguments: dirname: (unicode) The name of the directory. default_style: The style to return if nothing is found. Defaults to the global default style ('pep8') unless otherwise specified. Returns: The filename if found, otherwise return the default style. """ 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) try: fd = open(config_file) except IOError: pass # It's okay if it's not there. else: with fd: config = py3compat.ConfigParser() config.read_file(fd) if config.has_section('yapf'): return config_file # See if we have a pyproject.toml file with a '[tool.yapf]' section. config_file = os.path.join(dirname, style.PYPROJECT_TOML) try: fd = open(config_file) except IOError: pass # It's okay if it's not there. else: with fd: try: import toml except ImportError: raise errors.YapfError( "toml package is needed for using pyproject.toml as a configuration file" ) pyproject_toml = toml.load(config_file) style_dict = pyproject_toml.get('tool', {}).get('yapf', None) if style_dict is not None: return config_file if (not dirname or not os.path.basename(dirname) or dirname == os.path.abspath(os.path.sep)): break dirname = os.path.dirname(dirname) global_file = os.path.expanduser(style.GLOBAL_STYLE) if os.path.exists(global_file): return global_file return default_style