2
0
Fork 0
textout/textoutpc/tags/__init__.py

74 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
""" Tag helpers for the translate utilities.
As we ought to be able to make separate tag modules, this module
does not hardcode the imports and makes it possible to import any
custom module to isolate some tags from the others and make the
`textoutpc` a generic module for BBcode.
"""
from inspect import ismodule as _ismod, isclass as _isclass
from .base import TextoutTag as _TextoutTag, TextoutBlockTag, \
TextoutInlineTag, TextoutParagraphTag
__all__ = ["TextoutParagraphTag", "TextoutBlockTag", "TextoutInlineTag",
"get_tag"]
# ---
# Gathering of the tags.
# ---
def _extract_tags(module):
""" Load all tags from a module. """
tags = []
# Obtain the list of properties from the module.
try:
ds = module.__all__
except:
ds = dir(module)
# Get the submodules from the module (usually different files in the
# tags module folder).
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)):
obtained = _extract_tags(submodule)
tags += [tag for tag in obtained if not any(tag is x for x in tags)]
del obtained
# Extract the tags from the current module.
for tag in (obj for name, obj in ((nm, getattr(module, nm)) for nm in ds) \
if name[0] != '_' and _isclass(obj) and issubclass(obj, _TextoutTag)):
tags.append(tag)
return tags
_tags = _extract_tags(__import__("builtin", \
{'__name__': __name__ + '.__init__'}, level=1))
_aliases = {alias: tag for alias, tag in \
[(alias, tag) for tag in _tags for alias in tag.aliases]}
# ---
# Function to get a tag.
# ---
def get_tag(name, value, output_type = 'html', tweaks = {}):
""" Find a tag using its name. """
try:
als = _aliases[name]
als = als(name, value, output_type, tweaks)
return als
except:
return None
# End of file.