Simplified options extraction.
This commit is contained in:
parent
a6d6f62d11
commit
bb36d42a54
|
@ -212,11 +212,35 @@ class TextoutOptions:
|
|||
self._s_aliases = {}
|
||||
|
||||
if default:
|
||||
self.import_options(_get_builtin_module())
|
||||
self.add(_get_builtin_module())
|
||||
for mod in modules:
|
||||
self.import_options(mod)
|
||||
self.add(mod)
|
||||
|
||||
def __extract_from(self, module):
|
||||
def add(self, element):
|
||||
""" Add an option. """
|
||||
|
||||
if isinstance(element, str):
|
||||
element = str(element)
|
||||
element = _importmod(element,
|
||||
_getouterframes(_currentframe(), 1)[0].name)
|
||||
|
||||
if _ismod(element):
|
||||
self.__extract(element)
|
||||
return True
|
||||
|
||||
if _isclass(element) and issubclass(element, TextoutTag):
|
||||
for alias in element.aliases:
|
||||
self._aliases[alias] = element
|
||||
return True
|
||||
|
||||
if _isclass(element) and issubclass(element, TextoutSmiley):
|
||||
for alias in element.aliases:
|
||||
self._s_aliases[alias] = element
|
||||
|
||||
self._htmlsm = None
|
||||
return True
|
||||
|
||||
def __extract(self, module):
|
||||
""" Extract options from a module. """
|
||||
|
||||
tags = []
|
||||
|
@ -235,44 +259,13 @@ class TextoutOptions:
|
|||
for submodule in (obj for name, obj in ((nm, getattr(module, nm)) \
|
||||
for nm in ds) if (name == '__init__' or name[0] != '_') \
|
||||
and _ismod(obj)):
|
||||
ob_tags, ob_smileys = self.__extract_tags(submodule)
|
||||
tags += [tag for tag in ob_tags \
|
||||
if not any(tag is x for x in tags)]
|
||||
smileys += [sm for sm in ob_smileys \
|
||||
if not any(sm is x for x in smileys)]
|
||||
del obtained
|
||||
self.__extract(submodule)
|
||||
|
||||
# Extract the tags from the current module.
|
||||
|
||||
for obj in (obj for name, obj in ((nm, getattr(module, nm)) \
|
||||
for nm in ds) if name[0] != '_'):
|
||||
if _isclass(obj) and issubclass(obj, TextoutTag):
|
||||
tags += [tag for tag in (obj,) \
|
||||
if not any(tag is x for x in tags)]
|
||||
if _isclass(obj) and issubclass(obj, TextoutSmiley):
|
||||
smileys += [sm for sm in (obj,) \
|
||||
if not any(sm is x for x in smileys)]
|
||||
|
||||
return tags, smileys
|
||||
|
||||
def import_options(self, module):
|
||||
""" Import tags and smileys from a dedicated module. """
|
||||
|
||||
if isinstance(module, str):
|
||||
module = str(module)
|
||||
module = _importmod(module,
|
||||
_getouterframes(_currentframe(), 1)[0].name)
|
||||
if not _ismod(module):
|
||||
raise ValueError(f"expected a module, got: {repr(module)}")
|
||||
|
||||
tags, smileys = self.__extract_from(module)
|
||||
for tag in tags:
|
||||
for alias in tag.aliases:
|
||||
self._aliases[alias] = tag
|
||||
for sm in smileys:
|
||||
for alias in sm.aliases:
|
||||
self._s_aliases[alias] = sm
|
||||
self._htmlsm = None
|
||||
self.add(obj)
|
||||
|
||||
def get_smileys(self):
|
||||
""" Get the smileys dictionary. """
|
||||
|
|
Loading…
Reference in New Issue