Skip to content

Commit 17d4d65

Browse files
committed
Improve launcher
1 parent 669ab76 commit 17d4d65

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

example.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@
1313
import samples.tools
1414

1515
def run_all_samples():
16-
for _, sample_name, _ in pkgutil.walk_packages(samples.__path__):
17-
sample_module = importlib.import_module('samples.'+sample_name)
18-
subkey_env_name = getattr(sample_module, "SUBSCRIPTION_KEY_ENV_NAME", None)
19-
if not subkey_env_name:
16+
for _, section_name_name, ispkg in pkgutil.walk_packages(samples.__path__):
17+
if not ispkg:
2018
continue
21-
print("Executing sample from ", sample_name)
22-
sample_module = importlib.import_module('samples.'+sample_name)
23-
samples.tools.execute_samples(sample_module.__dict__, subkey_env_name)
19+
section_package_name = "samples."+section_name_name
20+
section_package = importlib.import_module(section_package_name)
21+
for _, sample_name, _ in pkgutil.iter_modules(section_package.__path__):
22+
sample_module = importlib.import_module(section_package_name+"."+sample_name)
23+
subkey_env_name = getattr(sample_module, "SUBSCRIPTION_KEY_ENV_NAME", None)
24+
if not subkey_env_name:
25+
continue
26+
print("Executing sample from ", sample_name)
27+
try:
28+
samples.tools.execute_samples(sample_module.__dict__, subkey_env_name)
29+
except samples.tools.SubscriptionKeyError as err:
30+
print("{}\n".format(err))
2431

2532
if __name__ == "__main__":
2633
run_all_samples()

samples/tools.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
import types
1212

1313

14+
class SubscriptionKeyError(Exception):
15+
pass
16+
17+
1418
def start_sample(func, subscription_key):
1519
"""Start the function and show its doc on output.
1620
"""
@@ -25,7 +29,7 @@ def execute_samples(module_globals, key_env_variable):
2529
try:
2630
subscription_key = sys.argv[1] if len(sys.argv) >= 2 else os.environ[key_env_variable]
2731
except KeyError:
28-
sys.exit("You need to either set the {} env variable or give it as env variable.".format(key_env_variable))
32+
raise SubscriptionKeyError("You need to either set the {} env variable.".format(key_env_variable))
2933

3034
for func in list(module_globals.values()):
3135
if not isinstance(func, types.FunctionType):

0 commit comments

Comments
 (0)