]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/setuptools/command/setopt.py
6358c0451b2d0036e3821d897fb6f7ab436ee4a9
1 from distutils
.util
import convert_path
2 from distutils
import log
3 from distutils
.errors
import DistutilsOptionError
8 from setuptools
import Command
10 __all__
= ['config_file', 'edit_config', 'option_base', 'setopt']
13 def config_file(kind
="local"):
14 """Get the filename of the distutils, local, global, or per-user config
16 `kind` must be one of "local", "global", or "user"
22 os
.path
.dirname(distutils
.__file
__), 'distutils.cfg'
25 dot
= os
.name
== 'posix' and '.' or ''
26 return os
.path
.expanduser(convert_path("~/%spydistutils.cfg" % dot
))
28 "config_file() type must be 'local', 'global', or 'user'", kind
32 def edit_config(filename
, settings
, dry_run
=False):
33 """Edit a configuration file to include `settings`
35 `settings` is a dictionary of dictionaries or ``None`` values, keyed by
36 command/section name. A ``None`` value means to delete the entire section,
37 while a dictionary lists settings to be changed or deleted in that section.
38 A setting of ``None`` means to delete that setting.
40 log
.debug("Reading configuration from %s", filename
)
41 opts
= configparser
.RawConfigParser()
42 opts
.optionxform
= lambda x
: x
44 for section
, options
in settings
.items():
46 log
.info("Deleting section [%s] from %s", section
, filename
)
47 opts
.remove_section(section
)
49 if not opts
.has_section(section
):
50 log
.debug("Adding new section [%s] to %s", section
, filename
)
51 opts
.add_section(section
)
52 for option
, value
in options
.items():
55 "Deleting %s.%s from %s",
56 section
, option
, filename
58 opts
.remove_option(section
, option
)
59 if not opts
.options(section
):
60 log
.info("Deleting empty [%s] section from %s",
62 opts
.remove_section(section
)
65 "Setting %s.%s to %r in %s",
66 section
, option
, value
, filename
68 opts
.set(section
, option
, value
)
70 log
.info("Writing %s", filename
)
72 with
open(filename
, 'w') as f
:
76 class option_base(Command
):
77 """Abstract base class for commands that mess with config files"""
80 ('global-config', 'g',
81 "save options to the site-wide distutils.cfg file"),
83 "save options to the current user's pydistutils.cfg file"),
85 "configuration file to use (default=setup.cfg)"),
89 'global-config', 'user-config',
92 def initialize_options(self
):
93 self
.global_config
= None
94 self
.user_config
= None
97 def finalize_options(self
):
99 if self
.global_config
:
100 filenames
.append(config_file('global'))
102 filenames
.append(config_file('user'))
103 if self
.filename
is not None:
104 filenames
.append(self
.filename
)
106 filenames
.append(config_file('local'))
107 if len(filenames
) > 1:
108 raise DistutilsOptionError(
109 "Must specify only one configuration file option",
112 self
.filename
, = filenames
115 class setopt(option_base
):
116 """Save command-line options to a file"""
118 description
= "set an option in setup.cfg or another config file"
121 ('command=', 'c', 'command to set an option for'),
122 ('option=', 'o', 'option to set'),
123 ('set-value=', 's', 'value of the option'),
124 ('remove', 'r', 'remove (unset) the value'),
125 ] + option_base
.user_options
127 boolean_options
= option_base
.boolean_options
+ ['remove']
129 def initialize_options(self
):
130 option_base
.initialize_options(self
)
133 self
.set_value
= None
136 def finalize_options(self
):
137 option_base
.finalize_options(self
)
138 if self
.command
is None or self
.option
is None:
139 raise DistutilsOptionError("Must specify --command *and* --option")
140 if self
.set_value
is None and not self
.remove
:
141 raise DistutilsOptionError("Must specify --set-value or --remove")
146 self
.command
: {self
.option
.replace('-', '_'): self
.set_value
}