Skip to content

added the config system #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
app_name: OpenFDA
secret_key: a passphrase for peppering
base_dir: !PY base_dir
app_dir: !PY app_dir
data_dir: !PY data_dir
log_dir: !PY log_dir
fda_data_url: https://download.open.fda.gov/drug/ndc/drug-ndc-0001-of-0001.json.zip
server:
host: 0.0.0.0
sqllite_db_name: drugs.db
json_file_name: !PY json_file_name
58 changes: 58 additions & 0 deletions app/utils/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python
"""apps.utils.config: imports and finalize config.yml"""

from pathlib import Path
import glob
import yaml
import os

class PyVar(yaml.YAMLObject):
"""
a class to create a custom !PY tag in yaml
The !PY tag allows calling python code stored in a dictionary
"""
# this is a PYYAML requirment and is case sensitive
yaml_tag = u'!PY'

def __init__(self):
"""
starts with empty dictionary
"""
self.py_dict = {}

def get(self,key):
"""
retrieves a value from the dictionary
"""
return self.py_dict[key]

def add(self,key,value):
"""
adds a new key value pair to a dictionary
"""
self.py_dict[key] = value


# construct all they key value pairs that can be referenced by the yaml file
# creating a function like this is required for yaml.SafeLoader.add_constructor
def yaml_construct(loader,node):
c = PyVar()
## start adding python variables here
c.add('base_dir',Path(__file__).resolve(strict=True).parent.parent.parent.absolute())
c.add('app_dir', c.get('base_dir') / 'app')
c.add('data_dir', c.get('app_dir') / 'data')
c.add('log_dir', c.get('app_dir') / 'log')
c.add('json_file_name',os.path.basename(max(glob.glob(str(c.get('data_dir')) + '/*.json'), key=os.path.getctime)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a drugs-ndc.json in the data folder however the following error occurs:

File "app/utils/config.py", line 45, in yaml_construct
    c.add('json_file_name',os.path.basename(max(glob.glob(str(c.get('data_dir')) + '/*.json'), key=os.path.getctime)))
ValueError: max() arg is an empty sequence```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hl1 - Currently, the code does not use any of these variables. This is the first step, where you have created a script to capture the YAML information. Correct?

Also, please be sure to update the requirements with YAML and specify the latest version number.

Correct. this is an independent system that could be added to master without impacting anything. integration is coming.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a drugs-ndc.json in the data folder however the following error occurs:

File "app/utils/config.py", line 45, in yaml_construct
    c.add('json_file_name',os.path.basename(max(glob.glob(str(c.get('data_dir')) + '/*.json'), key=os.path.getctime)))
ValueError: max() arg is an empty sequence```

hmmm... is the data folder in /app/data or /data?

return c.get(node.value)

# Required for safe_load
yaml.SafeLoader.add_constructor('!PY', yaml_construct)

# config.yml location
configyml_path = Path(__file__).resolve(strict=True).parent.parent.absolute() / "config.yml"

# load config.yml
config = yaml.safe_load(open(configyml_path))

if __name__ == "__main__":
print(config)