Skip to content

Commit

Permalink
Make imports work on python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Aug 9, 2016
1 parent 7d59ad3 commit 99f9ca6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
6 changes: 3 additions & 3 deletions malcolm/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# make the import path nice
from malcolm.util import import_child_packages
from malcolm.util import import_methodmeta_decorated_classes

__all__ = import_child_packages(globals(), "controllers")
__all__ = import_methodmeta_decorated_classes(globals(), "controllers")

del import_child_packages
del import_methodmeta_decorated_classes
7 changes: 6 additions & 1 deletion malcolm/parts/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
import ca
# import subpackages
from malcolm.util import import_sub_packages

__all__ = import_sub_packages(globals(), "parts")

del import_sub_packages
6 changes: 3 additions & 3 deletions malcolm/parts/ca/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# make the import path nice
from malcolm.util import import_child_packages
from malcolm.util import import_methodmeta_decorated_classes

__all__ = import_child_packages(globals(), "parts", "ca")
__all__ = import_methodmeta_decorated_classes(globals(), "parts", "ca")

del import_child_packages
del import_methodmeta_decorated_classes
20 changes: 19 additions & 1 deletion malcolm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging


def import_child_packages(globals_d, *package_path):
def import_methodmeta_decorated_classes(globals_d, *package_path):
"""Prepare a package namespace by importing all subclasses following PEP8
rules that have @takes decorated functions"""
class_dict = {}
Expand All @@ -23,6 +23,24 @@ def import_child_packages(globals_d, *package_path):
return __all__


def import_sub_packages(globals_d, *package_path):
module_dict = {}
# this is the path to the package
package_fs_path = os.path.join(os.path.dirname(__file__), *package_path)
for f in os.listdir(package_fs_path):
if os.path.isdir(f):
# import it and add it to the list
import_name = "malcolm.%s" % (".".join(package_path),)
logging.debug("Importing %s" % import_name)
module = importlib.import_module(import_name)
module_dict[f] = module

globals_d.update(module_dict)
__all__ = list(module_dict)
return __all__



def find_decorated_classes(module):
for n in dir(module):
cls = getattr(module, n)
Expand Down
2 changes: 2 additions & 0 deletions tests/setup_malcolm_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
except:
# cothread doesn't work on python3 at the moment
cothread = MagicMock()
# Tell Mock not to have a MethodMeta, otherwise we will be decorated
del cothread.MethodMeta
def callback_result(f, *args, **kwargs):
return f(*args, **kwargs)
cothread.CallbackResult.side_effect = callback_result
Expand Down

0 comments on commit 99f9ca6

Please sign in to comment.