3 # © Copyright 2021-2022, Scott Gasch
5 """Global configuration driven by commandline arguments, environment variables,
6 saved configuration files, and zookeeper-based dynamic configurations. This
7 works across several modules.
15 parser = config.add_commandline_args(
17 "Args related to module doing the thing.",
20 "--module_do_the_thing",
23 help="Should the module do the thing?"
30 parser = config.add_commandline_args(
32 "A program that does the thing.",
38 help="Should we really do the thing?"
42 config.parse() # Very important, this must be invoked!
44 If you set this up and remember to invoke config.parse(), all commandline
45 arguments will play nicely together. This is done automatically for you
46 if you're using the :meth:`bootstrap.initialize` decorator on
47 your program's entry point. See :meth:`python_modules.bootstrap.initialize`
56 if __name__ == '__main__':
59 Either way, you'll get this behavior from the commandline::
63 [--module_do_the_thing MODULE_DO_THE_THING]
67 Args related to module doing the thing.
69 --module_do_the_thing MODULE_DO_THE_THING
70 Should the module do the thing?
73 A program that does the thing
76 Should we really do the thing?
78 Arguments themselves should be accessed via
79 :code:`config.config['arg_name']`. e.g.::
81 if not config.config['dry_run']:
91 from typing import Any, Dict, List, Optional, Tuple
93 # This module is commonly used by others in here and should avoid
94 # taking any unnecessary dependencies back on them.
96 # Make a copy of the original program arguments immediately upon module load.
97 PROGRAM_NAME: str = os.path.basename(sys.argv[0])
98 ORIG_ARGV: List[str] = sys.argv.copy()
101 class OptionalRawFormatter(argparse.HelpFormatter):
102 """This formatter has the same bahavior as the normal argparse text
103 formatter except when the help text of an argument begins with
104 "RAW|". In that case, the line breaks are preserved and the text
107 Use this, for example, when you need the helptext of an argument
108 to have its spacing preserved exactly, e.g.::
114 choices=['CHEAT', 'AUTOPLAY', 'SELFTEST', 'PRECOMPUTE', 'PLAY'],
116 help='''RAW|Our mode of operation. One of:
118 PLAY = play wordle with me! Pick a random solution or
119 specify a solution with --template.
121 CHEAT = given a --template and, optionally, --letters_in_word
122 and/or --letters_to_avoid, return the best guess word;
124 AUTOPLAY = given a complete word in --template, guess it step
125 by step showing work;
127 SELFTEST = autoplay every possible solution keeping track of
128 wins/losses and average number of guesses;
130 PRECOMPUTE = populate hash table with optimal guesses.
135 def _split_lines(self, text, width):
136 if text.startswith('RAW|'):
137 return text[4:].splitlines()
138 return argparse.HelpFormatter._split_lines(self, text, width)
141 # A global argparser that we will collect arguments in. Each module (including
142 # us) will add arguments to a separate argument group.
143 ARGS = argparse.ArgumentParser(
145 formatter_class=OptionalRawFormatter,
146 fromfile_prefix_chars="@",
147 epilog=f'{PROGRAM_NAME} uses config.py ({__file__}) for global, cross-module configuration setup and parsing.',
148 # I don't fully understand why but when loaded by sphinx sometimes
149 # the same module is loaded many times causing any arguments it
150 # registers via module-level code to be redefined. Work around
151 # this iff the program is 'sphinx-build'
152 conflict_handler='resolve' if PROGRAM_NAME == 'sphinx-build' else 'error',
155 # Arguments specific to config.py. Other users should get their own group by
156 # invoking config.add_commandline_args.
157 GROUP = ARGS.add_argument_group(
158 f'Global Config ({__file__})',
159 'Args that control the global config itself; how meta!',
165 help='Config file (populated via --config_savefile) from which to read args in lieu or in addition to those passed via the commandline. Note that if the given path begins with "zk:" then it is interpreted as a zookeeper path instead of as a filesystem path. When loading config from zookeeper, any argument with the string "dynamic" in the name (e.g. --module_dynamic_url) may be modified at runtime by changes made to zookeeper (using --config_savefile=zk:path). You should therefore either write your code to handle dynamic argument changes or avoid naming arguments "dynamic" if you use zookeeper configuration paths.',
171 help='Display the global configuration (possibly derived from multiple sources) on STDERR at program startup time.',
178 help='Populate a config file (compatible with --config_loadfile) with the given path for later use. If the given path begins with "zk:" it is interpreted as a zookeeper path instead of a filesystem path. When updating zookeeper-based configs, all running programs that read their configuration from zookeeper (via --config_loadfile=zk:path) at startup time will see their configuration dynamically updated; flags with "dynamic" in their names (e.g. --my_dynamic_flag) may have their values changed. You should therefore either write your code to handle dynamic argument changes or avoid naming arguments "dynamic" if you use zookeeper configuration paths.',
181 '--config_rejects_unrecognized_arguments',
184 help='If present, config will raise an exception if it doesn\'t recognize an argument. The default behavior is to ignore unknown arguments so as to allow interoperability with programs that want to use their own argparse calls to parse their own, separate commandline args.',
187 '--config_exit_after_parse',
190 help='If present, halt the program after parsing config. Useful, for example, to write a --config_savefile and then terminate.',
196 Everything in the config module used to be module-level functions and
197 variables but it made the code ugly and harder to maintain. Now, this
198 class does the heavy lifting. We still rely on some globals, though:
200 ARGS and GROUP to interface with argparse
201 PROGRAM_NAME stores argv[0] close to program invocation
202 ORIG_ARGV stores the original argv list close to program invocation
203 CONFIG and config: hold the (singleton) instance of this class.
208 # Has our parse() method been invoked yet?
209 self.config_parse_called = False
211 # A configuration dictionary that will contain parsed
212 # arguments. This is the data that is most interesting to our
213 # callers as it will hold the configuration result.
214 self.config: Dict[str, Any] = {}
216 # Defer logging messages until later when logging has been
218 self.saved_messages: List[str] = []
220 # A zookeeper client that is lazily created so as to not incur
221 # the latency of connecting to zookeeper for programs that are
222 # not reading or writing their config data into zookeeper.
223 self.zk: Optional[Any] = None
225 # Per known zk file, what is the max version we have seen?
226 self.max_version: Dict[str, int] = {}
228 def __getitem__(self, key: str) -> Optional[Any]:
229 """If someone uses []'s on us, pass it onto self.config."""
230 return self.config.get(key, None)
232 def __setitem__(self, key: str, value: Any) -> None:
233 self.config[key] = value
235 def __contains__(self, key: str) -> bool:
236 return key in self.config
238 def get(self, key: str, default: Any = None) -> Optional[Any]:
239 return self.config.get(key, default)
242 def add_commandline_args(title: str, description: str = "") -> argparse._ArgumentGroup:
243 """Create a new context for arguments and return a handle.
246 title: A title for your module's commandline arguments group.
247 description: A helpful description of your module.
250 An argparse._ArgumentGroup to be populated by the caller.
252 return ARGS.add_argument_group(title, description)
255 def overwrite_argparse_epilog(msg: str) -> None:
256 """Allows your code to override the default epilog created by
260 msg: The epilog message to substitute for the default.
265 def is_flag_already_in_argv(var: str) -> bool:
266 """Returns true if a particular flag is passed on the commandline
270 var: The flag to search for.
278 def print_usage() -> None:
279 """Prints the normal help usage message out."""
286 program usage help text as a string.
288 return ARGS.format_usage()
291 def _reorder_arg_action_groups_before_help(entry_module: Optional[str]):
292 """Internal. Used to reorder the arguments before dumping out a
293 generated help string such that the main program's arguments come
297 reordered_action_groups = []
298 for grp in ARGS._action_groups:
299 if entry_module is not None and entry_module in grp.title: # type: ignore
300 reordered_action_groups.append(grp)
301 elif PROGRAM_NAME in GROUP.title: # type: ignore
302 reordered_action_groups.append(grp)
304 reordered_action_groups.insert(0, grp)
305 return reordered_action_groups
308 def _parse_arg_into_env(arg: str) -> Optional[Tuple[str, str, List[str]]]:
309 """Internal helper to parse commandling args into environment vars."""
311 if not arg.startswith('['):
314 if not arg.endswith(']'):
324 # Environment vars the same as flag names without
325 # the initial -'s and in UPPERCASE.
329 return var, env, chunks
332 def _to_bool(in_str: str) -> bool:
335 in_str: the string to convert to boolean
338 A boolean equivalent of the original string based on its contents.
339 All conversion is case insensitive. A positive boolean (True) is
340 returned if the string value is any of the following:
349 Otherwise False is returned.
369 return in_str.lower() in ("true", "1", "yes", "y", "t", "on")
371 def _augment_sys_argv_from_environment_variables(self):
372 """Internal. Look at the system environment for variables that match
373 commandline arg names. This is done via some munging such that:
375 :code:`--argument_to_match`
379 :code:`ARGUMENT_TO_MATCH`
381 This allows users to set args via shell environment variables
382 in lieu of passing them on the cmdline.
385 usage_message = Config.usage()
389 # Foreach valid optional commandline option (chunk) generate
390 # its analogous environment variable.
391 for chunk in usage_message.split():
398 _ = Config._parse_arg_into_env(arg)
401 if env in os.environ:
402 if not Config.is_flag_already_in_argv(var):
403 value = os.environ[env]
404 self.saved_messages.append(
405 f'Initialized from environment: {var} = {value}'
407 if len(chunks) == 1 and Config._to_bool(value):
409 elif len(chunks) > 1:
411 sys.argv.append(value)
414 def _process_dynamic_args(self, event):
416 logger = logging.getLogger(__name__)
417 contents, meta = self.zk.get(event.path, watch=self._process_dynamic_args)
418 logger.debug('Update for %s at version=%d.', event.path, meta.version)
420 'Max known version for %s is %d.', event.path, self.max_version.get(event.path, 0)
422 if meta.version > self.max_version.get(event.path, 0):
423 self.max_version[event.path] = meta.version
424 contents = contents.decode()
426 for arg in contents.split():
428 temp_argv.append(arg)
429 logger.info("Updating %s from zookeeper async config change.", arg)
430 if len(temp_argv) > 0:
433 known, _ = ARGS.parse_known_args()
435 self.config.update(vars(known))
437 def _augment_sys_argv_from_loadfile(self):
438 """Internal. Augment with arguments persisted in a saved file."""
441 saw_other_args = False
442 grab_next_arg = False
443 for arg in sys.argv[1:]:
444 if 'config_loadfile' in arg:
445 pieces = arg.split('=')
453 saw_other_args = True
455 if loadfile is not None:
457 if loadfile[:3] == 'zk:':
458 from kazoo.client import KazooClient
465 self.zk = KazooClient(
466 hosts=scott_secrets.ZOOKEEPER_NODES,
469 keyfile=scott_secrets.ZOOKEEPER_CLIENT_CERT,
470 keyfile_password=scott_secrets.ZOOKEEPER_CLIENT_PASS,
471 certfile=scott_secrets.ZOOKEEPER_CLIENT_CERT,
474 zkpath = loadfile[3:]
475 if not zkpath.startswith('/config/'):
476 zkpath = '/config/' + zkpath
477 zkpath = re.sub(r'//+', '/', zkpath)
478 if not self.zk.exists(zkpath):
480 f'ERROR: --config_loadfile argument must be a file, {loadfile} not found (in zookeeper)'
482 except Exception as e:
484 f'ERROR: Error talking with zookeeper while looking for {loadfile}'
486 elif not os.path.exists(loadfile):
488 f'ERROR: --config_loadfile argument must be a file, {loadfile} not found.'
492 msg = f'Augmenting commandline arguments with those from {loadfile}.'
494 msg = f'Reading commandline arguments from {loadfile}.'
495 print(msg, file=sys.stderr)
496 self.saved_messages.append(msg)
502 contents, meta = self.zk.get(zkpath, watch=self._process_dynamic_args)
503 contents = contents.decode()
506 for arg in contents.split('\n')
507 if 'config_savefile' not in arg
509 self.saved_messages.append(f'Setting {zkpath}\'s max_version to {meta.version}')
510 self.max_version[zkpath] = meta.version
511 except Exception as e:
512 raise Exception(f'Error reading {zkpath} from zookeeper.') from e
513 self.saved_messages.append(f'Loaded config from zookeeper from {zkpath}')
515 with open(loadfile, 'r') as rf:
516 newargs = rf.readlines()
517 newargs = [arg.strip('\n') for arg in newargs if 'config_savefile' not in arg]
520 def dump_config(self):
521 """Print the current config to stdout."""
522 print("Global Configuration:", file=sys.stderr)
523 pprint.pprint(self.config, stream=sys.stderr)
526 def parse(self, entry_module: Optional[str]) -> Dict[str, Any]:
527 """Main program should call this early in main(). Note that the
528 :code:`bootstrap.initialize` wrapper takes care of this automatically.
529 This should only be called once per program invocation.
532 if self.config_parse_called:
535 # If we're about to do the usage message dump, put the main
536 # module's argument group last in the list (if possible) so that
537 # when the user passes -h or --help, it will be visible on the
538 # screen w/o scrolling.
540 if arg in ('--help', '-h'):
541 if entry_module is not None:
542 entry_module = os.path.basename(entry_module)
543 ARGS._action_groups = Config._reorder_arg_action_groups_before_help(entry_module)
545 # Examine the environment for variables that match known flags.
546 # For a flag called --example_flag the corresponding environment
547 # variable would be called EXAMPLE_FLAG. If found, hackily add
548 # these into sys.argv to be parsed.
549 self._augment_sys_argv_from_environment_variables()
551 # Look for loadfile and read/parse it if present. This also
552 # works by jamming these values onto sys.argv.
553 self._augment_sys_argv_from_loadfile()
555 # Parse (possibly augmented, possibly completely overwritten)
556 # commandline args with argparse normally and populate config.
557 known, unknown = ARGS.parse_known_args()
558 self.config.update(vars(known))
560 # Reconstruct the argv with unrecognized flags for the benefit of
561 # future argument parsers. For example, unittest_main in python
562 # has some of its own flags. If we didn't recognize it, maybe
565 if config['config_rejects_unrecognized_arguments']:
567 f'Encountered unrecognized config argument(s) {unknown} with --config_rejects_unrecognized_arguments enabled; halting.'
569 self.saved_messages.append(
570 f'Config encountered unrecognized commandline arguments: {unknown}'
572 sys.argv = sys.argv[:1] + unknown
574 # Check for savefile and populate it if requested.
575 savefile = config['config_savefile']
576 if savefile and len(savefile) > 0:
577 data = '\n'.join(ORIG_ARGV[1:])
578 if savefile[:3] == 'zk:':
579 zkpath = savefile[3:]
580 if not zkpath.startswith('/config/'):
581 zkpath = '/config/' + zkpath
582 zkpath = re.sub(r'//+', '/', zkpath)
585 from kazoo.client import KazooClient
589 self.zk = KazooClient(
590 hosts=scott_secrets.ZOOKEEPER_NODES,
593 keyfile=scott_secrets.ZOOKEEPER_CLIENT_CERT,
594 keyfile_password=scott_secrets.ZOOKEEPER_CLIENT_PASS,
595 certfile=scott_secrets.ZOOKEEPER_CLIENT_CERT,
599 if len(data) > 1024 * 1024:
600 raise Exception(f'Saved args are too large! ({len(data)} bytes)')
601 if not self.zk.exists(zkpath):
602 self.zk.create(zkpath, data)
603 self.saved_messages.append(
604 f'Just created {zkpath}; setting its max_version to 0'
606 self.max_version[zkpath] = 0
608 meta = self.zk.set(zkpath, data)
609 self.saved_messages.append(
610 f'Setting {zkpath}\'s max_version to {meta.version}'
612 self.max_version[zkpath] = meta.version
613 except Exception as e:
614 raise Exception(f'Failed to create zookeeper path {zkpath}') from e
615 self.saved_messages.append(f'Saved config to zookeeper in {zkpath}')
617 with open(savefile, 'w') as wf:
620 # Also dump the config on stderr if requested.
621 if config['config_dump']:
624 self.config_parse_called = True
625 if config['config_exit_after_parse']:
626 print("Exiting because of --config_exit_after_parse.")
632 def has_been_parsed(self) -> bool:
633 """Returns True iff the global config has already been parsed"""
634 return self.config_parse_called
636 def late_logging(self):
637 """Log messages saved earlier now that logging has been initialized."""
638 logger = logging.getLogger(__name__)
639 logger.debug('Original commandline was: %s', ORIG_ARGV)
640 for _ in self.saved_messages:
644 # A global singleton instance of the Config class.
647 # A lot of client code uses config.config['whatever'] to lookup
648 # configuration so to preserve this we make this, config.config, with
649 # a __getitem__ method on it.
652 # Config didn't use to be a class; it was a mess of module-level
653 # functions and data. The functions below preserve the old interface
654 # so that existing clients do not need to be changed. As you can see,
655 # they mostly just thunk into the config class.
658 def add_commandline_args(title: str, description: str = "") -> argparse._ArgumentGroup:
659 """Create a new context for arguments and return a handle. An alias
660 for config.config.add_commandline_args.
663 title: A title for your module's commandline arguments group.
664 description: A helpful description of your module.
667 An argparse._ArgumentGroup to be populated by the caller.
669 return CONFIG.add_commandline_args(title, description)
672 def parse(entry_module: Optional[str]) -> Dict[str, Any]:
673 """Main program should call this early in main(). Note that the
674 :code:`bootstrap.initialize` wrapper takes care of this automatically.
675 This should only be called once per program invocation. Subsequent
676 calls do not reparse the configuration settings but rather just
677 return the current state.
679 return CONFIG.parse(entry_module)
682 def has_been_parsed() -> bool:
683 """Returns True iff the global config has already been parsed"""
684 return CONFIG.has_been_parsed()
687 def late_logging() -> None:
688 """Log messages saved earlier now that logging has been initialized."""
689 CONFIG.late_logging()
692 def dump_config() -> None:
693 """Print the current config to stdout."""
697 def overwrite_argparse_epilog(msg: str) -> None:
698 """Allows your code to override the default epilog created by
702 msg: The epilog message to substitute for the default.
704 Config.overwrite_argparse_epilog(msg)
707 def is_flag_already_in_argv(var: str) -> bool:
708 """Returns true if a particular flag is passed on the commandline
712 var: The flag to search for.
714 return Config.is_flag_already_in_argv(var)
717 def print_usage() -> None:
718 """Prints the normal help usage message out."""
725 program usage help text as a string.
727 return Config.usage()