Skip to content

Instantly share code, notes, and snippets.

@danni
Created July 25, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danni/9a0a35f8f419f60fef42 to your computer and use it in GitHub Desktop.
Save danni/9a0a35f8f419f60fef42 to your computer and use it in GitHub Desktop.
diff --git a/aloe/fs.py b/aloe/fs.py
index e2c99f8..51c7ac7 100644
--- a/aloe/fs.py
+++ b/aloe/fs.py
@@ -37,7 +37,6 @@ except NameError:
# pylint:disable=no-name-in-module,redefined-builtin
from importlib import reload
# pylint:enable=no-name-in-module,redefined-builtin
-from os.path import join, dirname
class FeatureLoader(object):
@@ -52,12 +51,10 @@ class FeatureLoader(object):
for path, _, files in os.walk(dir_):
for filename in fnmatch.filter(files, '*.py'):
- root = dirname(join(path, filename))
- sys.path.insert(0, root)
- to_load = cls.filename(filename)
+ package, to_load = \
+ cls.module_path(os.path.join(path, filename))
module = import_module(to_load)
reload(module) # Make sure steps and hooks are registered
- sys.path.remove(root)
@classmethod
def find_feature_directories(cls, dir_):
@@ -79,18 +76,41 @@ class FeatureLoader(object):
if path in packages:
# Does this package have a feature directory?
if 'features' in dirs:
- yield join(path, 'features')
+ yield os.path.join(path, 'features')
else:
# This is not a package, prune search
dirs[:] = []
@classmethod
- def filename(cls, path):
+ def split_path(cls, path):
"""
- Return only the filename from a full path, without extension.
+ Recursively split the path
"""
- fname = os.path.split(path)[1]
- fname = os.path.splitext(fname)[0]
- return fname
+ head, tail = os.path.split(path)
+
+ if not head:
+ return (tail,)
+ else:
+ return cls.split_path(head) + (tail,)
+
+ @classmethod
+ def module_path(cls, path):
+ """
+ Returns a module path from a full path
+
+ Returns: a tuple of (package, module)
+ """
+ for module_path in sys.path:
+ module = cls.split_path(os.path.relpath(path, start=module_path))
+
+ if module[0] != '..':
+
+ package = '.'.join(module[:-1])
+ module = os.path.splitext(module[-1])[0]
+
+ return (package, module)
+
+ else:
+ raise ImportError("No sys.path to %s", path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment