Skip to content

Commit

Permalink
Fixed imports.
Browse files Browse the repository at this point in the history
Removed sys.path.append() hack. Problem stemmed from a
lack of understanding regarding the __import__ function.
  • Loading branch information
modocache committed Feb 22, 2012
1 parent aae7c5e commit fc50a16
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions pyhoe/main/command.py
@@ -1,17 +1,19 @@
#!/usr/bin/env python #!/usr/bin/env python


import sys import sys
from os.path import dirname, join, abspath
import argparse import argparse


# FIXME - Imports don't seem to work unless I add package
# directories to my path. Surely this can't be the right
# way to do this.
PACKAGE_DIRECTORY = dirname(dirname(__file__))
sys.path.append(abspath(PACKAGE_DIRECTORY))
COMMANDS = ("sow",) COMMANDS = ("sow",)
for cmd in COMMANDS:
sys.path.append(join(PACKAGE_DIRECTORY, cmd))
def import_child(module_name):
"""Dynamically imports child of specified module."""
module = __import__(module_name)
for layer in module_name.split('.')[1:]:
module = getattr(module, layer)
return module



def main(): def main():
# Instantiate argument parser, add arguments. # Instantiate argument parser, add arguments.
Expand Down Expand Up @@ -44,15 +46,11 @@ def main():
sys.exit(0) sys.exit(0)


# Pass remaining args to appropriate function. # Pass remaining args to appropriate function.
# FIXME - Hacky.
args = vars(parser.parse_args(sys.argv[1:2])) args = vars(parser.parse_args(sys.argv[1:2]))
for c in COMMANDS: for c in COMMANDS:
if args["action"] == c: if args["action"] == c:
# FIXME - pyhoe shouldn't be hard-coded here. # FIXME - pyhoe shouldn't be hard-coded here.
module = "pyhoe.%s.command" % (c) import_child("pyhoe.%s.command" % c).execute(sys.argv[2:])
__import__(
module, fromlist=[PACKAGE_DIRECTORY]
).execute(sys.argv[2:])




if __name__ == "__main__": if __name__ == "__main__":
Expand Down

0 comments on commit fc50a16

Please sign in to comment.