From fc685f9e3b3572574d2d80a5efb3f1648ea0b4eb Mon Sep 17 00:00:00 2001 From: Eric Fried Date: Tue, 8 Oct 2019 13:19:34 -0500 Subject: [PATCH] Privatize and reorganize base package methods On the theory that they aren't and shouldn't be used, the following methods are removed from the public interface of the base os_traits package: - symbolize (deleted, unused internally) - import_submodules (deleted, replaced with appropriate call to _walk_submodules) This commit also reorganizes the module to put private methods first, public methods last; and alphabetize within those groupings. Sem-Ver: api-break Change-Id: Idb5613bc016743ba689c6996d39399aae774daa9 --- os_traits/__init__.py | 69 ++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/os_traits/__init__.py b/os_traits/__init__.py index ca529f3..09c0764 100644 --- a/os_traits/__init__.py +++ b/os_traits/__init__.py @@ -30,13 +30,6 @@ __version__ = pbr.version.VersionInfo(THIS_NAME).version_string() CUSTOM_NAMESPACE = 'CUSTOM_' -# TODO(efried): This is unused internally; it exists for backward compatibility -# in case some consumer was actually calling it, which would be weird. -# Consider removing it (and privatizing all the helpers in this module). -def symbolize(mod_name, name): - _symbolize(mod_name, [name]) - - def _symbolize(mod_name, props): """Given a reference to a Python module object and an iterable of short string names for traits, registers symbols in the module corresponding to @@ -50,6 +43,21 @@ def _symbolize(mod_name, props): setattr(leaf_mod, prop, value) # os_traits.hw.cpu.x86.SSE +def _visualize(mod_name, props, seen=None): + if mod_name in seen: + return + seen.add(mod_name) + components = mod_name.split('.') + tab = ' ' + # Print the module name + indent = tab * (len(components) - 1) + print('%s%s:' % (indent, components[-1].upper())) + # Print the properties + indent = tab * len(components) + if props: + print('%s%s' % (indent, ', '.join(props))) + + def _walk_submodules(package, recursive, callback, **kwargs): """Recursively walk the repository's submodules and invoke a callback for each module with the list of short trait names found therein. @@ -81,42 +89,9 @@ def _walk_submodules(package, recursive, callback, **kwargs): _walk_submodules(mod_name, recursive, callback, **kwargs) -def _visualize(mod_name, props, seen=None): - if mod_name in seen: - return - seen.add(mod_name) - components = mod_name.split('.') - tab = ' ' - # Print the module name - indent = tab * (len(components) - 1) - print('%s%s:' % (indent, components[-1].upper())) - # Print the properties - indent = tab * len(components) - if props: - print('%s%s' % (indent, ', '.join(props))) - - -def print_tree(): - """Print (to stdout) a visual representation of all the namespaces and the - (short) trait names defined therein. - - """ - _walk_submodules(sys.modules.get(__name__), True, _visualize, seen=set()) - - -def import_submodules(package, recursive=True): - """Import all submodules of a module, recursively, including subpackages - - :param package: package (name or actual module) - :type package: str | module - :param recursive: Walk all submodules recursively? - :type recursive: bool - """ - _walk_submodules(package, recursive, _symbolize) - - -# This is where the names defined in submodules are imported -import_submodules(sys.modules.get(__name__)) +# This is where the names defined in submodules are imported by recursively +# importing all submodules/subpackages and symbolizing their TRAITS +_walk_submodules(sys.modules.get(__name__), True, _symbolize) def get_traits(prefix=None, suffix=None): @@ -180,3 +155,11 @@ def normalize_name(name): norm_name = re.sub('[^0-9A-Za-z]+', '_', name) # Bug #1762789: Do .upper after replacing non alphanumerics. return CUSTOM_NAMESPACE + norm_name.upper() + + +def print_tree(): + """Print (to stdout) a visual representation of all the namespaces and the + (short) trait names defined therein. + + """ + _walk_submodules(sys.modules.get(__name__), True, _visualize, seen=set())