# HG changeset patch # User Paul Boddie # Date 1532988049 -7200 # Node ID da161661f733e13b5b3664adb1af91bc39edd36d # Parent b89502ed29bed2e6f30ffcbb3ef1e449b5b30043 Moved redundant manifest-related code into the imports module. diff -r b89502ed29be -r da161661f733 moinformat/imports.py --- a/moinformat/imports.py Mon Jul 30 23:19:51 2018 +0200 +++ b/moinformat/imports.py Tue Jul 31 00:00:49 2018 +0200 @@ -19,10 +19,12 @@ this program. If not, see . """ -from os.path import isdir, join, splitext +from os.path import isdir, join, split, splitext from os import listdir from importlib import import_module +reserved = ["__init__", "common", "manifest"] + def get_extensions(dirname, modname, stores, reserved, prefix=None): "Import extensions inside 'dirname'." @@ -46,4 +48,37 @@ store_name = prefix and "%s.%s" % (prefix, leafname) or leafname stores[store_name] = import_module("%s.%s" % (modname, leafname)) +def get_mapping(modules, get_key, get_value): + + """ + Using the 'modules' mapping, employ 'get_key' and 'get_value' to register + objects provided by the modules in a mapping. + """ + + mapping = {} + + # Use the callables to obtain the keys and values from modules. + + for name, module in modules.items(): + mapping[get_key(name, module)] = get_value(module) + + return mapping + +def get_modules(module_file, module_name): + + """ + Obtain details of the indicated module's package using 'module_file', + corresponding to a __file__ attribute, and 'module_name', corresponding to + a __name__ attribute. + """ + + dirname = split(module_file)[0] + package = module_name.rsplit(".", 1)[0] + + # Define an attribute mapping names to modules. + + modules = {} + get_extensions(dirname, package, modules, reserved) + return modules + # vim: tabstop=4 expandtab shiftwidth=4 diff -r b89502ed29be -r da161661f733 moinformat/input/manifest.py --- a/moinformat/input/manifest.py Mon Jul 30 23:19:51 2018 +0200 +++ b/moinformat/input/manifest.py Tue Jul 31 00:00:49 2018 +0200 @@ -19,29 +19,17 @@ this program. If not, see . """ -from moinformat.imports import get_extensions -from os.path import split - -reserved = ["__init__", "common", "manifest"] - -# Obtain details of this module's package. - -dirname = split(__file__)[0] -package = __name__.rsplit(".", 1)[0] +from moinformat.imports import get_extensions, get_mapping, get_modules # Define an attribute mapping names to modules. -modules = {} -get_extensions(dirname, package, modules, reserved) +modules = get_modules(__file__, __name__) # Obtain all input contexts. -inputs = {} - -# Use names declared in each class to register the handlers: +# Use names declared in each class to register the contexts: # input.name -> input -for module in modules.values(): - inputs[module.input.name] = module.input +inputs = get_mapping(modules, lambda n, m: m.input.name, lambda m: m.input) # vim: tabstop=4 expandtab shiftwidth=4 diff -r b89502ed29be -r da161661f733 moinformat/links/manifest.py --- a/moinformat/links/manifest.py Mon Jul 30 23:19:51 2018 +0200 +++ b/moinformat/links/manifest.py Tue Jul 31 00:00:49 2018 +0200 @@ -19,29 +19,17 @@ this program. If not, see . """ -from moinformat.imports import get_extensions -from os.path import split - -reserved = ["__init__", "common", "manifest"] - -# Obtain details of this module's package. - -dirname = split(__file__)[0] -package = __name__.rsplit(".", 1)[0] +from moinformat.imports import get_extensions, get_mapping, get_modules # Define an attribute mapping names to modules. -modules = {} -get_extensions(dirname, package, modules, reserved) +modules = get_modules(__file__, __name__) # Obtain all linkers. -linkers = {} - -# Use names declared in each handler to register the handlers: +# Use names declared in each class to register the linkers: # linker.name -> linker -for module in modules.values(): - linkers[module.linker.name] = module.linker +linkers = get_mapping(modules, lambda n, m: m.linker.name, lambda m: m.linker) # vim: tabstop=4 expandtab shiftwidth=4 diff -r b89502ed29be -r da161661f733 moinformat/macros/manifest.py --- a/moinformat/macros/manifest.py Mon Jul 30 23:19:51 2018 +0200 +++ b/moinformat/macros/manifest.py Tue Jul 31 00:00:49 2018 +0200 @@ -19,29 +19,17 @@ this program. If not, see . """ -from moinformat.imports import get_extensions -from os.path import split - -reserved = ["__init__", "common", "manifest"] - -# Obtain details of this module's package. - -dirname = split(__file__)[0] -package = __name__.rsplit(".", 1)[0] +from moinformat.imports import get_extensions, get_mapping, get_modules # Define an attribute mapping names to modules. -modules = {} -get_extensions(dirname, package, modules, reserved) +modules = get_modules(__file__, __name__) # Obtain all macros. -macros = {} - -# Use names declared in each handler to register the handlers: +# Use names declared in each class to register the handlers: # macro.name -> macro -for module in modules.values(): - macros[module.macro.name] = module.macro +macros = get_mapping(modules, lambda n, m: m.macro.name, lambda m: m.macro) # vim: tabstop=4 expandtab shiftwidth=4 diff -r b89502ed29be -r da161661f733 moinformat/output/manifest.py --- a/moinformat/output/manifest.py Mon Jul 30 23:19:51 2018 +0200 +++ b/moinformat/output/manifest.py Tue Jul 31 00:00:49 2018 +0200 @@ -19,29 +19,17 @@ this program. If not, see . """ -from moinformat.imports import get_extensions -from os.path import split - -reserved = ["__init__", "common", "manifest"] - -# Obtain details of this module's package. - -dirname = split(__file__)[0] -package = __name__.rsplit(".", 1)[0] +from moinformat.imports import get_extensions, get_mapping, get_modules # Define an attribute mapping names to modules. -modules = {} -get_extensions(dirname, package, modules, reserved) +modules = get_modules(__file__, __name__) # Obtain all output contexts. -outputs = {} - -# Use names declared in each class to register the handlers: +# Use names declared in each class to register the contexts: # output.name -> output -for module in modules.values(): - outputs[module.output.name] = module.output +outputs = get_mapping(modules, lambda n, m: m.output.name, lambda m: m.output) # vim: tabstop=4 expandtab shiftwidth=4 diff -r b89502ed29be -r da161661f733 moinformat/parsers/graphviz.py --- a/moinformat/parsers/graphviz.py Mon Jul 30 23:19:51 2018 +0200 +++ b/moinformat/parsers/graphviz.py Tue Jul 31 00:00:49 2018 +0200 @@ -33,6 +33,8 @@ "A parser for Graphviz content, identifying format directives." + format = "graphviz" + # Parser handler methods. def parse_directive(self, region): diff -r b89502ed29be -r da161661f733 moinformat/parsers/manifest.py --- a/moinformat/parsers/manifest.py Mon Jul 30 23:19:51 2018 +0200 +++ b/moinformat/parsers/manifest.py Tue Jul 31 00:00:49 2018 +0200 @@ -3,7 +3,7 @@ """ Moin wiki parser manifest. -Copyright (C) 2017 Paul Boddie +Copyright (C) 2017, 2018 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -19,26 +19,17 @@ this program. If not, see . """ -from moinformat.imports import get_extensions -from os.path import split - -reserved = ["__init__", "common", "manifest"] - -# Obtain details of this module's package. - -dirname = split(__file__)[0] -package = __name__.rsplit(".", 1)[0] +from moinformat.imports import get_extensions, get_mapping, get_modules # Define an attribute mapping names to modules. -modules = {} -get_extensions(dirname, package, modules, reserved) +modules = get_modules(__file__, __name__) # Obtain all parsers. -parsers = {} +# Use names declared in each class to register the parsers: +# parser.format -> parser -for module_name, module in modules.items(): - parsers[module_name] = module.parser +parsers = get_mapping(modules, lambda n, m: m.parser.format, lambda m: m.parser) # vim: tabstop=4 expandtab shiftwidth=4 diff -r b89502ed29be -r da161661f733 moinformat/parsers/moin.py --- a/moinformat/parsers/moin.py Mon Jul 30 23:19:51 2018 +0200 +++ b/moinformat/parsers/moin.py Tue Jul 31 00:00:49 2018 +0200 @@ -48,6 +48,8 @@ "A wiki region parser." + format = "moin" + def __init__(self, formats=None, root=None): """ diff -r b89502ed29be -r da161661f733 moinformat/parsers/table.py --- a/moinformat/parsers/table.py Mon Jul 30 23:19:51 2018 +0200 +++ b/moinformat/parsers/table.py Tue Jul 31 00:00:49 2018 +0200 @@ -33,6 +33,8 @@ "A parser for improved table syntax." + format = "table" + # Principal parser methods. def parse_region_content(self, items, region): diff -r b89502ed29be -r da161661f733 moinformat/serialisers/manifest.py --- a/moinformat/serialisers/manifest.py Mon Jul 30 23:19:51 2018 +0200 +++ b/moinformat/serialisers/manifest.py Tue Jul 31 00:00:49 2018 +0200 @@ -3,7 +3,7 @@ """ Moin wiki serialiser manifest. -Copyright (C) 2017 Paul Boddie +Copyright (C) 2017, 2018 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -19,26 +19,17 @@ this program. If not, see . """ -from moinformat.imports import get_extensions -from os.path import split - -reserved = ["__init__", "common", "manifest"] - -# Obtain details of this module's package. - -dirname = split(__file__)[0] -package = __name__.rsplit(".", 1)[0] +from moinformat.imports import get_extensions, get_mapping, get_modules # Define an attribute mapping names to modules. -modules = {} -get_extensions(dirname, package, modules, reserved) +modules = get_modules(__file__, __name__) # Obtain all serialisers. -serialisers = {} +# Use names declared in each class to register the handlers: +# serialiser.format -> serialiser -for module_name, module in modules.items(): - serialisers[module_name] = module.serialiser +serialisers = get_mapping(modules, lambda n, m: n, lambda m: m.serialiser) # vim: tabstop=4 expandtab shiftwidth=4