mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-08 04:34:16 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -1,16 +1,18 @@
|
|||
from distutils.util import convert_path
|
||||
from __future__ import annotations
|
||||
|
||||
import configparser
|
||||
import os
|
||||
|
||||
import distutils
|
||||
from distutils import log
|
||||
from distutils.errors import DistutilsOptionError
|
||||
import distutils
|
||||
import os
|
||||
import configparser
|
||||
|
||||
from distutils.util import convert_path
|
||||
from setuptools import Command
|
||||
|
||||
__all__ = ['config_file', 'edit_config', 'option_base', 'setopt']
|
||||
|
||||
|
||||
def config_file(kind="local"):
|
||||
def config_file(kind='local'):
|
||||
"""Get the filename of the distutils, local, global, or per-user config
|
||||
|
||||
`kind` must be one of "local", "global", or "user"
|
||||
|
|
@ -19,13 +21,13 @@ def config_file(kind="local"):
|
|||
return 'setup.cfg'
|
||||
if kind == 'global':
|
||||
return os.path.join(
|
||||
os.path.dirname(distutils.__file__), 'distutils.cfg'
|
||||
os.path.dirname(distutils.__file__), 'distutils.cfg',
|
||||
)
|
||||
if kind == 'user':
|
||||
dot = os.name == 'posix' and '.' or ''
|
||||
return os.path.expanduser(convert_path("~/%spydistutils.cfg" % dot))
|
||||
return os.path.expanduser(convert_path('~/%spydistutils.cfg' % dot))
|
||||
raise ValueError(
|
||||
"config_file() type must be 'local', 'global', or 'user'", kind
|
||||
"config_file() type must be 'local', 'global', or 'user'", kind,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -37,37 +39,39 @@ def edit_config(filename, settings, dry_run=False):
|
|||
while a dictionary lists settings to be changed or deleted in that section.
|
||||
A setting of ``None`` means to delete that setting.
|
||||
"""
|
||||
log.debug("Reading configuration from %s", filename)
|
||||
log.debug('Reading configuration from %s', filename)
|
||||
opts = configparser.RawConfigParser()
|
||||
opts.optionxform = lambda x: x
|
||||
opts.read([filename])
|
||||
for section, options in settings.items():
|
||||
if options is None:
|
||||
log.info("Deleting section [%s] from %s", section, filename)
|
||||
log.info('Deleting section [%s] from %s', section, filename)
|
||||
opts.remove_section(section)
|
||||
else:
|
||||
if not opts.has_section(section):
|
||||
log.debug("Adding new section [%s] to %s", section, filename)
|
||||
log.debug('Adding new section [%s] to %s', section, filename)
|
||||
opts.add_section(section)
|
||||
for option, value in options.items():
|
||||
if value is None:
|
||||
log.debug(
|
||||
"Deleting %s.%s from %s",
|
||||
section, option, filename
|
||||
'Deleting %s.%s from %s',
|
||||
section, option, filename,
|
||||
)
|
||||
opts.remove_option(section, option)
|
||||
if not opts.options(section):
|
||||
log.info("Deleting empty [%s] section from %s",
|
||||
section, filename)
|
||||
log.info(
|
||||
'Deleting empty [%s] section from %s',
|
||||
section, filename,
|
||||
)
|
||||
opts.remove_section(section)
|
||||
else:
|
||||
log.debug(
|
||||
"Setting %s.%s to %r in %s",
|
||||
section, option, value, filename
|
||||
'Setting %s.%s to %r in %s',
|
||||
section, option, value, filename,
|
||||
)
|
||||
opts.set(section, option, value)
|
||||
|
||||
log.info("Writing %s", filename)
|
||||
log.info('Writing %s', filename)
|
||||
if not dry_run:
|
||||
with open(filename, 'w') as f:
|
||||
opts.write(f)
|
||||
|
|
@ -77,12 +81,18 @@ class option_base(Command):
|
|||
"""Abstract base class for commands that mess with config files"""
|
||||
|
||||
user_options = [
|
||||
('global-config', 'g',
|
||||
"save options to the site-wide distutils.cfg file"),
|
||||
('user-config', 'u',
|
||||
"save options to the current user's pydistutils.cfg file"),
|
||||
('filename=', 'f',
|
||||
"configuration file to use (default=setup.cfg)"),
|
||||
(
|
||||
'global-config', 'g',
|
||||
'save options to the site-wide distutils.cfg file',
|
||||
),
|
||||
(
|
||||
'user-config', 'u',
|
||||
"save options to the current user's pydistutils.cfg file",
|
||||
),
|
||||
(
|
||||
'filename=', 'f',
|
||||
'configuration file to use (default=setup.cfg)',
|
||||
),
|
||||
]
|
||||
|
||||
boolean_options = [
|
||||
|
|
@ -106,8 +116,8 @@ class option_base(Command):
|
|||
filenames.append(config_file('local'))
|
||||
if len(filenames) > 1:
|
||||
raise DistutilsOptionError(
|
||||
"Must specify only one configuration file option",
|
||||
filenames
|
||||
'Must specify only one configuration file option',
|
||||
filenames,
|
||||
)
|
||||
self.filename, = filenames
|
||||
|
||||
|
|
@ -115,7 +125,7 @@ class option_base(Command):
|
|||
class setopt(option_base):
|
||||
"""Save command-line options to a file"""
|
||||
|
||||
description = "set an option in setup.cfg or another config file"
|
||||
description = 'set an option in setup.cfg or another config file'
|
||||
|
||||
user_options = [
|
||||
('command=', 'c', 'command to set an option for'),
|
||||
|
|
@ -136,14 +146,14 @@ class setopt(option_base):
|
|||
def finalize_options(self):
|
||||
option_base.finalize_options(self)
|
||||
if self.command is None or self.option is None:
|
||||
raise DistutilsOptionError("Must specify --command *and* --option")
|
||||
raise DistutilsOptionError('Must specify --command *and* --option')
|
||||
if self.set_value is None and not self.remove:
|
||||
raise DistutilsOptionError("Must specify --set-value or --remove")
|
||||
raise DistutilsOptionError('Must specify --set-value or --remove')
|
||||
|
||||
def run(self):
|
||||
edit_config(
|
||||
self.filename, {
|
||||
self.command: {self.option.replace('-', '_'): self.set_value}
|
||||
self.command: {self.option.replace('-', '_'): self.set_value},
|
||||
},
|
||||
self.dry_run
|
||||
self.dry_run,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue