1.1 --- a/moinformat/imports.py Mon Jul 30 23:19:51 2018 +0200
1.2 +++ b/moinformat/imports.py Tue Jul 31 00:00:49 2018 +0200
1.3 @@ -19,10 +19,12 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from os.path import isdir, join, splitext
1.8 +from os.path import isdir, join, split, splitext
1.9 from os import listdir
1.10 from importlib import import_module
1.11
1.12 +reserved = ["__init__", "common", "manifest"]
1.13 +
1.14 def get_extensions(dirname, modname, stores, reserved, prefix=None):
1.15
1.16 "Import extensions inside 'dirname'."
1.17 @@ -46,4 +48,37 @@
1.18 store_name = prefix and "%s.%s" % (prefix, leafname) or leafname
1.19 stores[store_name] = import_module("%s.%s" % (modname, leafname))
1.20
1.21 +def get_mapping(modules, get_key, get_value):
1.22 +
1.23 + """
1.24 + Using the 'modules' mapping, employ 'get_key' and 'get_value' to register
1.25 + objects provided by the modules in a mapping.
1.26 + """
1.27 +
1.28 + mapping = {}
1.29 +
1.30 + # Use the callables to obtain the keys and values from modules.
1.31 +
1.32 + for name, module in modules.items():
1.33 + mapping[get_key(name, module)] = get_value(module)
1.34 +
1.35 + return mapping
1.36 +
1.37 +def get_modules(module_file, module_name):
1.38 +
1.39 + """
1.40 + Obtain details of the indicated module's package using 'module_file',
1.41 + corresponding to a __file__ attribute, and 'module_name', corresponding to
1.42 + a __name__ attribute.
1.43 + """
1.44 +
1.45 + dirname = split(module_file)[0]
1.46 + package = module_name.rsplit(".", 1)[0]
1.47 +
1.48 + # Define an attribute mapping names to modules.
1.49 +
1.50 + modules = {}
1.51 + get_extensions(dirname, package, modules, reserved)
1.52 + return modules
1.53 +
1.54 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/moinformat/input/manifest.py Mon Jul 30 23:19:51 2018 +0200
2.2 +++ b/moinformat/input/manifest.py Tue Jul 31 00:00:49 2018 +0200
2.3 @@ -19,29 +19,17 @@
2.4 this program. If not, see <http://www.gnu.org/licenses/>.
2.5 """
2.6
2.7 -from moinformat.imports import get_extensions
2.8 -from os.path import split
2.9 -
2.10 -reserved = ["__init__", "common", "manifest"]
2.11 -
2.12 -# Obtain details of this module's package.
2.13 -
2.14 -dirname = split(__file__)[0]
2.15 -package = __name__.rsplit(".", 1)[0]
2.16 +from moinformat.imports import get_extensions, get_mapping, get_modules
2.17
2.18 # Define an attribute mapping names to modules.
2.19
2.20 -modules = {}
2.21 -get_extensions(dirname, package, modules, reserved)
2.22 +modules = get_modules(__file__, __name__)
2.23
2.24 # Obtain all input contexts.
2.25
2.26 -inputs = {}
2.27 -
2.28 -# Use names declared in each class to register the handlers:
2.29 +# Use names declared in each class to register the contexts:
2.30 # input.name -> input
2.31
2.32 -for module in modules.values():
2.33 - inputs[module.input.name] = module.input
2.34 +inputs = get_mapping(modules, lambda n, m: m.input.name, lambda m: m.input)
2.35
2.36 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/moinformat/links/manifest.py Mon Jul 30 23:19:51 2018 +0200
3.2 +++ b/moinformat/links/manifest.py Tue Jul 31 00:00:49 2018 +0200
3.3 @@ -19,29 +19,17 @@
3.4 this program. If not, see <http://www.gnu.org/licenses/>.
3.5 """
3.6
3.7 -from moinformat.imports import get_extensions
3.8 -from os.path import split
3.9 -
3.10 -reserved = ["__init__", "common", "manifest"]
3.11 -
3.12 -# Obtain details of this module's package.
3.13 -
3.14 -dirname = split(__file__)[0]
3.15 -package = __name__.rsplit(".", 1)[0]
3.16 +from moinformat.imports import get_extensions, get_mapping, get_modules
3.17
3.18 # Define an attribute mapping names to modules.
3.19
3.20 -modules = {}
3.21 -get_extensions(dirname, package, modules, reserved)
3.22 +modules = get_modules(__file__, __name__)
3.23
3.24 # Obtain all linkers.
3.25
3.26 -linkers = {}
3.27 -
3.28 -# Use names declared in each handler to register the handlers:
3.29 +# Use names declared in each class to register the linkers:
3.30 # linker.name -> linker
3.31
3.32 -for module in modules.values():
3.33 - linkers[module.linker.name] = module.linker
3.34 +linkers = get_mapping(modules, lambda n, m: m.linker.name, lambda m: m.linker)
3.35
3.36 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/moinformat/macros/manifest.py Mon Jul 30 23:19:51 2018 +0200
4.2 +++ b/moinformat/macros/manifest.py Tue Jul 31 00:00:49 2018 +0200
4.3 @@ -19,29 +19,17 @@
4.4 this program. If not, see <http://www.gnu.org/licenses/>.
4.5 """
4.6
4.7 -from moinformat.imports import get_extensions
4.8 -from os.path import split
4.9 -
4.10 -reserved = ["__init__", "common", "manifest"]
4.11 -
4.12 -# Obtain details of this module's package.
4.13 -
4.14 -dirname = split(__file__)[0]
4.15 -package = __name__.rsplit(".", 1)[0]
4.16 +from moinformat.imports import get_extensions, get_mapping, get_modules
4.17
4.18 # Define an attribute mapping names to modules.
4.19
4.20 -modules = {}
4.21 -get_extensions(dirname, package, modules, reserved)
4.22 +modules = get_modules(__file__, __name__)
4.23
4.24 # Obtain all macros.
4.25
4.26 -macros = {}
4.27 -
4.28 -# Use names declared in each handler to register the handlers:
4.29 +# Use names declared in each class to register the handlers:
4.30 # macro.name -> macro
4.31
4.32 -for module in modules.values():
4.33 - macros[module.macro.name] = module.macro
4.34 +macros = get_mapping(modules, lambda n, m: m.macro.name, lambda m: m.macro)
4.35
4.36 # vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/moinformat/output/manifest.py Mon Jul 30 23:19:51 2018 +0200
5.2 +++ b/moinformat/output/manifest.py Tue Jul 31 00:00:49 2018 +0200
5.3 @@ -19,29 +19,17 @@
5.4 this program. If not, see <http://www.gnu.org/licenses/>.
5.5 """
5.6
5.7 -from moinformat.imports import get_extensions
5.8 -from os.path import split
5.9 -
5.10 -reserved = ["__init__", "common", "manifest"]
5.11 -
5.12 -# Obtain details of this module's package.
5.13 -
5.14 -dirname = split(__file__)[0]
5.15 -package = __name__.rsplit(".", 1)[0]
5.16 +from moinformat.imports import get_extensions, get_mapping, get_modules
5.17
5.18 # Define an attribute mapping names to modules.
5.19
5.20 -modules = {}
5.21 -get_extensions(dirname, package, modules, reserved)
5.22 +modules = get_modules(__file__, __name__)
5.23
5.24 # Obtain all output contexts.
5.25
5.26 -outputs = {}
5.27 -
5.28 -# Use names declared in each class to register the handlers:
5.29 +# Use names declared in each class to register the contexts:
5.30 # output.name -> output
5.31
5.32 -for module in modules.values():
5.33 - outputs[module.output.name] = module.output
5.34 +outputs = get_mapping(modules, lambda n, m: m.output.name, lambda m: m.output)
5.35
5.36 # vim: tabstop=4 expandtab shiftwidth=4
6.1 --- a/moinformat/parsers/graphviz.py Mon Jul 30 23:19:51 2018 +0200
6.2 +++ b/moinformat/parsers/graphviz.py Tue Jul 31 00:00:49 2018 +0200
6.3 @@ -33,6 +33,8 @@
6.4
6.5 "A parser for Graphviz content, identifying format directives."
6.6
6.7 + format = "graphviz"
6.8 +
6.9 # Parser handler methods.
6.10
6.11 def parse_directive(self, region):
7.1 --- a/moinformat/parsers/manifest.py Mon Jul 30 23:19:51 2018 +0200
7.2 +++ b/moinformat/parsers/manifest.py Tue Jul 31 00:00:49 2018 +0200
7.3 @@ -3,7 +3,7 @@
7.4 """
7.5 Moin wiki parser manifest.
7.6
7.7 -Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk>
7.8 +Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>
7.9
7.10 This program is free software; you can redistribute it and/or modify it under
7.11 the terms of the GNU General Public License as published by the Free Software
7.12 @@ -19,26 +19,17 @@
7.13 this program. If not, see <http://www.gnu.org/licenses/>.
7.14 """
7.15
7.16 -from moinformat.imports import get_extensions
7.17 -from os.path import split
7.18 -
7.19 -reserved = ["__init__", "common", "manifest"]
7.20 -
7.21 -# Obtain details of this module's package.
7.22 -
7.23 -dirname = split(__file__)[0]
7.24 -package = __name__.rsplit(".", 1)[0]
7.25 +from moinformat.imports import get_extensions, get_mapping, get_modules
7.26
7.27 # Define an attribute mapping names to modules.
7.28
7.29 -modules = {}
7.30 -get_extensions(dirname, package, modules, reserved)
7.31 +modules = get_modules(__file__, __name__)
7.32
7.33 # Obtain all parsers.
7.34
7.35 -parsers = {}
7.36 +# Use names declared in each class to register the parsers:
7.37 +# parser.format -> parser
7.38
7.39 -for module_name, module in modules.items():
7.40 - parsers[module_name] = module.parser
7.41 +parsers = get_mapping(modules, lambda n, m: m.parser.format, lambda m: m.parser)
7.42
7.43 # vim: tabstop=4 expandtab shiftwidth=4
8.1 --- a/moinformat/parsers/moin.py Mon Jul 30 23:19:51 2018 +0200
8.2 +++ b/moinformat/parsers/moin.py Tue Jul 31 00:00:49 2018 +0200
8.3 @@ -48,6 +48,8 @@
8.4
8.5 "A wiki region parser."
8.6
8.7 + format = "moin"
8.8 +
8.9 def __init__(self, formats=None, root=None):
8.10
8.11 """
9.1 --- a/moinformat/parsers/table.py Mon Jul 30 23:19:51 2018 +0200
9.2 +++ b/moinformat/parsers/table.py Tue Jul 31 00:00:49 2018 +0200
9.3 @@ -33,6 +33,8 @@
9.4
9.5 "A parser for improved table syntax."
9.6
9.7 + format = "table"
9.8 +
9.9 # Principal parser methods.
9.10
9.11 def parse_region_content(self, items, region):
10.1 --- a/moinformat/serialisers/manifest.py Mon Jul 30 23:19:51 2018 +0200
10.2 +++ b/moinformat/serialisers/manifest.py Tue Jul 31 00:00:49 2018 +0200
10.3 @@ -3,7 +3,7 @@
10.4 """
10.5 Moin wiki serialiser manifest.
10.6
10.7 -Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk>
10.8 +Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>
10.9
10.10 This program is free software; you can redistribute it and/or modify it under
10.11 the terms of the GNU General Public License as published by the Free Software
10.12 @@ -19,26 +19,17 @@
10.13 this program. If not, see <http://www.gnu.org/licenses/>.
10.14 """
10.15
10.16 -from moinformat.imports import get_extensions
10.17 -from os.path import split
10.18 -
10.19 -reserved = ["__init__", "common", "manifest"]
10.20 -
10.21 -# Obtain details of this module's package.
10.22 -
10.23 -dirname = split(__file__)[0]
10.24 -package = __name__.rsplit(".", 1)[0]
10.25 +from moinformat.imports import get_extensions, get_mapping, get_modules
10.26
10.27 # Define an attribute mapping names to modules.
10.28
10.29 -modules = {}
10.30 -get_extensions(dirname, package, modules, reserved)
10.31 +modules = get_modules(__file__, __name__)
10.32
10.33 # Obtain all serialisers.
10.34
10.35 -serialisers = {}
10.36 +# Use names declared in each class to register the handlers:
10.37 +# serialiser.format -> serialiser
10.38
10.39 -for module_name, module in modules.items():
10.40 - serialisers[module_name] = module.serialiser
10.41 +serialisers = get_mapping(modules, lambda n, m: n, lambda m: m.serialiser)
10.42
10.43 # vim: tabstop=4 expandtab shiftwidth=4