From 0270be43b9a24c039f518c684b788f0a52aa50a0 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 5 Apr 2013 21:34:52 +0200 Subject: Minor refactoring of importing osmoappdesc --- osmopy/osmotestconfig.py | 8 ++------ osmopy/osmotestvty.py | 9 +++------ osmopy/osmoutil.py | 16 ++++++++++------ 3 files changed, 15 insertions(+), 18 deletions(-) mode change 100755 => 100644 osmopy/osmotestvty.py diff --git a/osmopy/osmotestconfig.py b/osmopy/osmotestconfig.py index 8a18f84..f9ebde0 100644 --- a/osmopy/osmotestconfig.py +++ b/osmopy/osmotestconfig.py @@ -190,12 +190,8 @@ if __name__ == '__main__': if args.w: workdir = args.w - osmoappdesc = None - try: - osmoappdesc = osmoutil.importappconf(confpath, "osmoappdesc") - except ImportError as e: - print >> sys.stderr, "osmoappdesc not found, set searchpath with -p" - sys.exit(1) + osmoappdesc = osmoutil.importappconf_or_quit(confpath, "osmoappdesc", + args.p) apps = osmoappdesc.apps configs = osmoappdesc.app_configs diff --git a/osmopy/osmotestvty.py b/osmopy/osmotestvty.py old mode 100755 new mode 100644 index f02c610..8d9f3c4 --- a/osmopy/osmotestvty.py +++ b/osmopy/osmotestvty.py @@ -27,6 +27,7 @@ confpath = '.' class TestVTY(unittest.TestCase): + def setUp(self): osmo_vty_cmd = osmoappdesc.vty_command[:] config_index = osmo_vty_cmd.index('-c') @@ -90,12 +91,8 @@ if __name__ == '__main__': if args.p: confpath = args.p - osmoappdesc = None - try: - osmoappdesc = osmoutil.importappconf(confpath, "osmoappdesc") - except ImportError as e: - print >> sys.stderr, "osmoappdesc not found, set searchpath with -p" - sys.exit(1) + osmoappdesc = osmoutil.importappconf_or_quit(confpath, "osmoappdesc", + args.p) print "confpath %s, workdir %s" % (confpath, workdir) os.chdir(workdir) diff --git a/osmopy/osmoutil.py b/osmopy/osmoutil.py index 791506d..78465d9 100755 --- a/osmopy/osmoutil.py +++ b/osmopy/osmoutil.py @@ -39,12 +39,16 @@ def end_proc(proc): proc.wait() -"""Add a directory to sys.path, try to import a config file. +"""Add a directory to sys.path, try to import a config file.""" -This may throw ImportError if the config file is not found.""" - - -def importappconf(dirname, confname): +def importappconf_or_quit(dirname, confname, p_set): if dirname not in sys.path: sys.path.append(dirname) - return importlib.import_module(confname) + try: + return importlib.import_module(confname) + except ImportError as e: + if p_set: + print >> sys.stderr, "osmoappdesc not found in %s" % dirname + else: + print >> sys.stderr, "set osmoappdesc location with -p " + sys.exit(1) -- cgit v1.2.3