Skip to content

Commit df804b8

Browse files
authored
Merge pull request #4 from NACLab/configUpdates
Config updates
2 parents 94d24af + b03baff commit df804b8

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

ngcsimlib/__init__.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,32 @@
1-
21
from . import utils
32
from . import controller
43
from . import commands
54
from . import logger
65

7-
import argparse, os, warnings, json, logging
6+
import argparse, os, warnings, json
87
from types import SimpleNamespace
98
from pathlib import Path
109
from sys import argv
1110
from importlib import import_module
12-
from ngcsimlib.configManager import GlobalConfig
11+
from ngcsimlib.configManager import init_config, get_config
1312

1413
from pkg_resources import get_distribution
1514

1615
__version__ = get_distribution('ngcsimlib').version ## set software version
1716

1817

1918
###### Preload Modules
20-
def preload():
21-
parser = argparse.ArgumentParser(description='Build and run a model using ngclearn')
22-
parser.add_argument("--modules", type=str, help='location of modules.json file')
23-
24-
## ngc-sim-lib only cares about --modules argument
25-
args, unknown = parser.parse_known_args() # args = parser.parse_args()
26-
try:
27-
module_path = args.modules
28-
except:
29-
module_path = None
30-
31-
if module_path is None:
32-
module_config = GlobalConfig.get_config("modules")
33-
if module_config is None:
34-
module_path = "json_files/modules.json"
35-
else:
36-
module_path = module_config.get("module_path", "json_files/modules.json")
19+
def preload_modules():
20+
module_config = get_config("modules")
21+
if module_config is None:
22+
module_path = "json_files/modules.json"
23+
else:
24+
module_path = module_config.get("module_path", "json_files/modules.json")
3725

3826
if not os.path.isfile(module_path):
3927
warnings.warn("\nMissing file to preload modules from. Attempted to locate file at \"" + str(module_path) +
4028
"\". No modules will be preloaded. "
41-
"\nSee https://ngc-learn.readthedocs.io/en/latest/tutorials/model_basics/json_modules.html for additional information")
29+
"\nSee https://ngc-learn.readthedocs.io/en/latest/tutorials/model_basics/json_modules.html for additional information")
4230
return
4331

4432
with open(module_path, 'r') as file:
@@ -76,18 +64,16 @@ def configure():
7664
if not os.path.isfile(config_path):
7765
warnings.warn("\nMissing configuration file. Attempted to locate file at \"" + str(config_path) +
7866
"\". Default Config will be used. "
79-
"\nSee PUT LINK HERE for additional information")
67+
"\nSee https://ngc-learn.readthedocs.io/en/latest/tutorials/model_basics/configuration.html for "
68+
"additional information")
8069
return
8170

82-
GlobalConfig.init_config(config_path)
83-
8471

72+
init_config(config_path)
8573

8674

8775
if not Path(argv[0]).name == "sphinx-build" or Path(argv[0]).name == "build.py":
8876
if "readthedocs" not in argv[0]: ## prevent readthedocs execution of preload
8977
configure()
9078
logger.init_logging()
91-
92-
preload()
93-
79+
preload_modules()

ngcsimlib/configManager.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""
22
ngc libraries have a need for global level configuration which will all be
33
handled here. It will also be possible to add custom configuration sections to
4-
this file with no issues or additional setup. However the main purpose of these
4+
this file with no issues or additional setup. However, the main purpose of these
55
configurations are not control specific parts of the model but control ngc
66
libraries at a high level.
77
"""
88
import json
9+
from types import SimpleNamespace
910

1011

1112
class ConfigManager:
@@ -25,5 +26,24 @@ def get_config(self, name):
2526

2627
return self.loadedConfig.get(name, None)
2728

29+
def provide_namespace(self, configName):
30+
config = self.get_config(configName)
31+
if config is None:
32+
return None
33+
else:
34+
return SimpleNamespace(**config)
35+
36+
37+
_GlobalConfig = ConfigManager()
38+
39+
40+
def init_config(path):
41+
_GlobalConfig.init_config(path)
42+
43+
44+
def get_config(configName):
45+
return _GlobalConfig.get_config(configName)
46+
2847

29-
GlobalConfig = ConfigManager()
48+
def provide_namespace(configName):
49+
return _GlobalConfig.provide_namespace(configName)

ngcsimlib/logger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import sys
3-
from ngcsimlib.configManager import GlobalConfig
3+
from ngcsimlib.configManager import get_config
44

55
def _concatArgs(*args, **kwargs):
66
"""Internal Decorator for concatenating arguments into a single string"""
@@ -19,7 +19,7 @@ def wrapped(*wargs, sep=" ", end="", **kwargs):
1919

2020

2121
def init_logging():
22-
loggingConfig = GlobalConfig.get_config("logging")
22+
loggingConfig = get_config("logging")
2323
if loggingConfig is None:
2424
loggingConfig = {"logging_file": None,
2525
"logging_level": logging.WARNING,

0 commit comments

Comments
 (0)