Skip to content

Commit ef5b018

Browse files
authored
Merge pull request #1 from NACLab/logger
Logger
2 parents 7121051 + 9eb9d29 commit ef5b018

File tree

14 files changed

+264
-110
lines changed

14 files changed

+264
-110
lines changed

ngcsimlib/__init__.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
12
from . import utils
23
from . import controller
34
from . import commands
5+
from . import logger
46

5-
import argparse, os, warnings, json
7+
import argparse, os, warnings, json, logging
68
from types import SimpleNamespace
79
from pathlib import Path
810
from sys import argv
911
from importlib import import_module
12+
from ngcsimlib.configManager import GlobalConfig
1013

1114
from pkg_resources import get_distribution
1215

@@ -26,7 +29,11 @@ def preload():
2629
module_path = None
2730

2831
if module_path is None:
29-
module_path = "json_files/modules.json"
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")
3037

3138
if not os.path.isfile(module_path):
3239
warnings.warn("\nMissing file to preload modules from. Attempted to locate file at \"" + str(module_path) +
@@ -51,6 +58,36 @@ def preload():
5158
utils._Loaded_Attributes[keyword] = atr
5259

5360

61+
###### Initialize Config
62+
def configure():
63+
parser = argparse.ArgumentParser(description='Build and run a model using ngclearn')
64+
parser.add_argument("--config", type=str, help='location of config.json file')
65+
66+
## ngc-sim-lib only cares about --config argument
67+
args, unknown = parser.parse_known_args() # args = parser.parse_args()
68+
try:
69+
config_path = args.modules
70+
except:
71+
config_path = None
72+
73+
if config_path is None:
74+
config_path = "json_files/config.json"
75+
76+
if not os.path.isfile(config_path):
77+
warnings.warn("\nMissing configuration file. Attempted to locate file at \"" + str(config_path) +
78+
"\". Default Config will be used. "
79+
"\nSee PUT LINK HERE for additional information")
80+
return
81+
82+
GlobalConfig.init_config(config_path)
83+
84+
85+
86+
5487
if not Path(argv[0]).name == "sphinx-build" or Path(argv[0]).name == "build.py":
5588
if "readthedocs" not in argv[0]: ## prevent readthedocs execution of preload
89+
configure()
90+
logger.init_logging()
91+
5692
preload()
93+

ngcsimlib/commands/clamp.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import warnings
2-
31
from ngcsimlib.commands.command import Command
42
from ngcsimlib.utils import extract_args
5-
from ngcsimlib.component_utils import find_compartment
6-
3+
from ngcsimlib.componentUtils import find_compartment
4+
from ngcsimlib.logger import warn, error
75

86
class Clamp(Command):
97
"""
@@ -33,11 +31,9 @@ def __init__(self, components=None, compartment=None, clamp_name=None,
3331
super().__init__(components=components, command_name=command_name,
3432
required_calls=['clamp'])
3533
if compartment is None:
36-
raise RuntimeError(
37-
self.name + " requires a \'compartment\' to clamp to for construction")
34+
error(self.name, "requires a \'compartment\' to clamp to for construction")
3835
if clamp_name is None:
39-
raise RuntimeError(
40-
self.name + " requires a \'clamp_name\' to bind to for construction")
36+
error(self.name, "requires a \'clamp_name\' to bind to for construction")
4137

4238
self.clamp_name = clamp_name
4339
self.compartment = compartment
@@ -46,23 +42,20 @@ def __init__(self, components=None, compartment=None, clamp_name=None,
4642
_, mapped_name = find_compartment(component.compartments, component.__class__, self.compartment)
4743

4844
if mapped_name is None:
49-
raise RuntimeError(self.name +
50-
" is attempting to initialize clamp to non-existent compartment \"" + self.compartment
51-
+ "\" on " + name)
45+
error(self.name, " is attempting to initialize clamp to non-existent compartment \"",
46+
self.compartment, "\" on ", name, sep=" ")
5247

5348
if mapped_name != self.compartment:
54-
warnings.warn(self.name +
55-
" is attempting to initialize clamp to a property name, not a compartment of " +
56-
name + ". If using a verboseDict and autoMap this will work, otherwise this clamp will "
57-
"have unknown behavior")
49+
warn(self.name, " is attempting to initialize clamp to a property name, not a compartment of ", name,
50+
". If using a verboseDict and autoMap this will work, otherwise this clamp will have unknown "
51+
"behavior", sep=" ")
5852

5953
def __call__(self, *args, **kwargs):
6054
try:
6155
vals = extract_args([self.clamp_name], *args, **kwargs)
6256
except RuntimeError:
63-
raise RuntimeError(self.name + ", " + str(
64-
self.clamp_name) + " is missing from keyword arguments or a positional "
65-
"arguments can be provided")
57+
error(self.name, ",", self.clamp_name,
58+
"is missing from keyword arguments or a positional arguments can be provided")
6659

6760
for component in self.components:
6861
self.components[component].clamp(self.compartment, vals[self.clamp_name])

ngcsimlib/commands/compound.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from ngcsimlib.commands import Command
22
from ngcsimlib.utils import check_attributes
3-
import warnings
3+
from ngcsimlib.logger import warn, error
44

55
class Compound(Command):
66
"""
@@ -25,9 +25,9 @@ def __init__(self, components=None, command_name=None, command_list=None,
2525
"""
2626
super().__init__(components=components, command_name=command_name)
2727
if controller is None:
28-
raise RuntimeError("The controller is needed to build a compound command (This should be passed in by default)")
28+
error("The controller is needed to build a compound command (This should be passed in by default)")
2929
if command_list is None or len(command_list) == 0:
30-
warnings.warn("The command list for command " + self.name + " is None or empty")
30+
warn("The command list for command", self.name, "is None or empty")
3131

3232
self.command_list = command_list
3333
self.controller = controller

ngcsimlib/commands/multiclamp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from ngcsimlib.commands.command import Command
22
from ngcsimlib.utils import extract_args, check_attributes
3-
3+
from ngcsimlib.logger import error
44

55
class Multiclamp(Command):
66
"""
@@ -25,16 +25,16 @@ def __init__(self, components=None, clamp_name=None, command_name=None,
2525
super().__init__(components=components, command_name=command_name,
2626
required_calls=['clamp'])
2727
if clamp_name is None:
28-
raise RuntimeError(self.name + " requires a \'clamp_name\' to bind to for construction")
28+
error(self.name, "requires a \'clamp_name\' to bind to for construction")
2929

3030
self.clamp_name = clamp_name
3131

3232
def __call__(self, *args, **kwargs):
3333
try:
3434
vals = extract_args([self.clamp_name], *args, **kwargs)
3535
except RuntimeError:
36-
raise RuntimeError(self.name + ", " + str(self.clamp_name) + " is missing from keyword arguments or a "
37-
"positional arguments can be provided")
36+
error(self.name, ",", self.clamp_name,
37+
"is missing from keyword arguments or a positional arguments can be provided")
3838

3939
for compartment, value in vals[self.clamp_name].items():
4040
for component in self.components:

ngcsimlib/commands/reset.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from ngcsimlib.commands import Command
22
from ngcsimlib.utils import extract_args
3-
import warnings
4-
3+
from ngcsimlib.logger import warn, error
54
class Reset(Command):
65
"""
76
In every model/system, there is a need to reset components back to some
@@ -24,15 +23,15 @@ def __init__(self, components=None, reset_name=None, command_name=None,
2423
super().__init__(components=components, command_name=command_name,
2524
required_calls=['reset'])
2625
if reset_name is None:
27-
raise RuntimeError(self.name + " requires a \'reset_name\' to bind to for construction")
26+
error(self.name, "requires a \'reset_name\' to bind to for construction")
2827
self.reset_name = reset_name
2928

3029
def __call__(self, *args, **kwargs):
3130
try:
3231
vals = extract_args([self.reset_name], *args, **kwargs)
3332
except RuntimeError:
34-
warnings.warn(self.name + ", " + str(self.reset_name) + " is missing from keyword arguments and no "
35-
"positional arguments were provided", stacklevel=6)
33+
warn(self.name, ",", self.reset_name,
34+
"is missing from keyword arguments and no positional arguments were provided")
3635
return
3736

3837
if vals[self.reset_name]:

ngcsimlib/commands/save.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ngcsimlib.commands import Command
22
from ngcsimlib.utils import extract_args
3-
import warnings
3+
from ngcsimlib.logger import warn, error
4+
45

56
class Save(Command):
67
"""
@@ -30,15 +31,16 @@ def __init__(self, components=None, directory_flag=None, command_name=None,
3031
super().__init__(components=components, command_name=command_name,
3132
required_calls=['save'])
3233
if directory_flag is None:
33-
raise RuntimeError(self.name + " requires a \'directory_flag\' to bind to for construction")
34+
error(self.name, "requires a \'directory_flag\' to bind to for construction")
35+
3436
self.directory_flag = directory_flag
3537

3638
def __call__(self, *args, **kwargs):
3739
try:
3840
vals = extract_args([self.directory_flag], *args, **kwargs)
3941
except RuntimeError:
40-
warnings.warn(self.name + ", " + str(self.directory_flag) + " is missing from keyword arguments and no "
41-
"positional arguments were provided", stacklevel=6)
42+
warn(self.name + ",", self.directory_flag,
43+
"is missing from keyword arguments and no positional arguments were provided")
4244
return
4345

4446
if vals[self.directory_flag]:

ngcsimlib/commands/seed.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from ngcsimlib.commands.command import Command
22
from ngcsimlib.utils import extract_args
3-
3+
from ngcsimlib.logger import error
44

55
class Seed(Command):
66
"""
@@ -27,18 +27,16 @@ def __init__(self, components=None, seed_name=None,
2727
super().__init__(components=components, command_name=command_name,
2828
required_calls=['seed'])
2929
if seed_name is None:
30-
raise RuntimeError(
31-
self.name + " requires a \'seed_name\' to bind to for construction")
30+
error(self.name, "requires a \'seed_name\' to bind to for construction")
3231

3332
self.seed_name = seed_name
3433

3534
def __call__(self, *args, **kwargs):
3635
try:
3736
vals = extract_args([self.seed_name], *args, **kwargs)
3837
except RuntimeError:
39-
raise RuntimeError(self.name + ", " + str(
40-
self.seed_name) + " is missing from keyword arguments or a positional "
41-
"arguments can be provided")
38+
error(self.name, ",", self.seed_name,
39+
"is missing from keyword arguments or a positional arguments can be provided")
4240

4341
for component in self.components:
4442
self.components[component].seed(vals[self.seed_name])

ngcsimlib/commands/track.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from ngcsimlib.commands import Command
22
from ngcsimlib.utils import extract_args
3-
import warnings
3+
from ngcsimlib.logger import warn, error
44

55
class Track(Command):
66
"""
@@ -28,9 +28,9 @@ def __init__(self, components=None, compartment=None, tracker=None,
2828
"""
2929
super().__init__(components=components, command_name=command_name)
3030
if compartment is None:
31-
raise RuntimeError(self.name + " requires a \'compartment\' to clamp to for construction")
31+
error(self.name, "requires a \'compartment\' to clamp to for construction")
3232
if tracker is None:
33-
raise RuntimeError(self.name + " requires a \'tracker\' to bind to for construction")
33+
error(self.name,"requires a \'tracker\' to bind to for construction")
3434

3535
self.tracker = tracker
3636
self.compartment = compartment
@@ -40,8 +40,8 @@ def __call__(self, *args, **kwargs):
4040
try:
4141
vals = extract_args([self.tracker], *args, **kwargs)
4242
except RuntimeError:
43-
warnings.warn(self.name + ", " + str(self.tracker) + " is missing from keyword arguments and no "
44-
"positional arguments were provided", stacklevel=6)
43+
warn(self.name, ",", self.tracker,
44+
"is missing from keyword arguments and no positional arguments were provided")
4545
return
4646

4747
v = []

ngcsimlib/component.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from abc import ABC, abstractmethod
2-
import warnings
32
import ngcsimlib.utils as utils
4-
from ngcsimlib.component_utils import VerboseDict, ComponentMetadata
3+
from ngcsimlib.componentUtils import VerboseDict, ComponentMetadata
54

65

76

0 commit comments

Comments
 (0)