Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed imports.

Removed sys.path.append() hack. Problem stemmed from a
lack of understanding regarding the __import__ function.
  • Loading branch information...
commit fc50a16cb41ce003f35c33204baea14c3bdf05b4 1 parent aae7c5e
Brian Gesiak authored

Showing 1 changed file with 11 additions and 13 deletions. Show diff stats Hide diff stats

  1. +11 13 pyhoe/main/command.py
24 pyhoe/main/command.py
... ... @@ -1,17 +1,19 @@
1 1 #!/usr/bin/env python
2 2
3 3 import sys
4   -from os.path import dirname, join, abspath
5 4 import argparse
6 5
7   -# FIXME - Imports don't seem to work unless I add package
8   -# directories to my path. Surely this can't be the right
9   -# way to do this.
10   -PACKAGE_DIRECTORY = dirname(dirname(__file__))
11   -sys.path.append(abspath(PACKAGE_DIRECTORY))
  6 +
12 7 COMMANDS = ("sow",)
13   -for cmd in COMMANDS:
14   - sys.path.append(join(PACKAGE_DIRECTORY, cmd))
  8 +
  9 +
  10 +def import_child(module_name):
  11 + """Dynamically imports child of specified module."""
  12 + module = __import__(module_name)
  13 + for layer in module_name.split('.')[1:]:
  14 + module = getattr(module, layer)
  15 + return module
  16 +
15 17
16 18 def main():
17 19 # Instantiate argument parser, add arguments.
@@ -44,15 +46,11 @@ def main():
44 46 sys.exit(0)
45 47
46 48 # Pass remaining args to appropriate function.
47   - # FIXME - Hacky.
48 49 args = vars(parser.parse_args(sys.argv[1:2]))
49 50 for c in COMMANDS:
50 51 if args["action"] == c:
51 52 # FIXME - pyhoe shouldn't be hard-coded here.
52   - module = "pyhoe.%s.command" % (c)
53   - __import__(
54   - module, fromlist=[PACKAGE_DIRECTORY]
55   - ).execute(sys.argv[2:])
  53 + import_child("pyhoe.%s.command" % c).execute(sys.argv[2:])
56 54
57 55
58 56 if __name__ == "__main__":

0 comments on commit fc50a16

Please sign in to comment.
Something went wrong with that request. Please try again.