diff options
author | Leonard Richardson <leonard.richardson@canonical.com> | 2011-02-20 15:41:54 -0500 |
---|---|---|
committer | Leonard Richardson <leonard.richardson@canonical.com> | 2011-02-20 15:41:54 -0500 |
commit | 1eb6214cf6e87f552b7f21d62b56a302b1ae68c4 (patch) | |
tree | fc3e5de35f5c31f30f6057b42c7764579e84bb81 | |
parent | 963cff867aac9b02041474522d59f665a4c4a58d (diff) |
Fixed up the code to register builders from a module.
-rw-r--r-- | beautifulsoup/builder/__init__.py | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/beautifulsoup/builder/__init__.py b/beautifulsoup/builder/__init__.py index 2e8846b..3983d66 100644 --- a/beautifulsoup/builder/__init__.py +++ b/beautifulsoup/builder/__init__.py @@ -1,4 +1,5 @@ import re +import sys from beautifulsoup.element import Entities __all__ = [ @@ -164,11 +165,28 @@ class HTMLTreeBuilder(TreeBuilder): return False -def register_builders_from(module, add_to_all): - __import__(module.__name__, module.__all__) +def register_builders_from(module_name, add_to_all, + local_to_this_package=False): + # I'm sure this is not the best way to do this. + + # Import the module beautifulsoup.builder.foo or some.other.builder.foo + if local_to_this_package: + fqn = __package__ + '.' + module_name + else: + fqn = module_name + __import__(fqn) + + # Look up the module by its fully-qualified name. + module = sys.modules[fqn] + + # Copy everything mentioned in the builder module's __all__ into + # this module. + this_module = sys.modules[__package__] + for name in module.__all__: + setattr(this_module, name, getattr(module, name)) + + # Add all names from the builder module's __all__ to these names. add_to_all += module.__all__ -from _lxml import * -register_builders_from(_lxml, __all__) -from _html5lib import * -register_builders_from(_html5lib, __all__) +register_builders_from('_lxml', __all__, True) +register_builders_from('_html5lib', __all__, True) |