Skip to content

Commit

Permalink
first steps for settings interface (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjamesjoyce committed Aug 8, 2018
1 parent c211aaa commit 544c662
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions lcopt/__init__.py
Expand Up @@ -6,3 +6,4 @@
from lcopt.export_view import *
from lcopt.bw2_import import *
from lcopt.data_store import *
from lcopt.settings import settings
2 changes: 2 additions & 0 deletions lcopt/constants.py
Expand Up @@ -20,6 +20,8 @@

DEFAULT_CONFIG = {
'ecoinvent':{
'username': None,
'password': None,
'version': DEFAULT_ECOINVENT_VERSION,
'system_model': DEFAULT_ECOINVENT_SYSTEM_MODEL,
},
Expand Down
15 changes: 13 additions & 2 deletions lcopt/data_store.py
Expand Up @@ -41,8 +41,7 @@ def __init__(self):
# config
self.config_file = os.path.join(self.lcopt_dir, 'lcopt_config.yml')
if not os.path.exists(self.config_file):
with open(self.config_file, 'w') as cfg:
yaml.dump(DEFAULT_CONFIG, cfg, default_flow_style=False)
self.write_default_config()

self.config = self.load_config()

Expand Down Expand Up @@ -76,8 +75,20 @@ def __init__(self):
def load_config(self):
with open(self.config_file, 'r') as cf:
config = yaml.load(cf)
if config is None:
self.write_default_config()
config = DEFAULT_CONFIG
return config

def refresh(self):
with open(self.config_file, 'r') as cf:
self.config = yaml.load(cf)

def write_default_config(self):
with open(self.config_file, 'w') as cfg:
yaml.dump(DEFAULT_CONFIG, cfg, default_flow_style=False)


@property
def models(self):
models = glob.glob(os.path.join(self.model_dir, '*.lcopt'))
Expand Down
73 changes: 73 additions & 0 deletions lcopt/settings.py
@@ -0,0 +1,73 @@
from .data_store import storage
import yaml

class SettingsDict(object):
def __init__(self, contents):

for k, v in contents.items():
setattr(self, k, v)

def __getattr__(self, name, value=None):
"""edit __getattr__ to allow dot notation writing of attributes to a SettingsDict object"""
try:
ret_value = object.__getattribute__(self, name)

except AttributeError:
if value is not None:
setattr(self, name, value)

ret_value = object.__getattribute__(self, name)

return ret_value

def delete(self, attr):
try:
delattr(self, attr)
except AttributeError:
raise AttributeError('{} does not exist'.format(attr))

def as_dict(self):
d = {}
attributes = [a for a in dir(self) if not a.startswith('_') and not callable(getattr(self,a))]
for k in attributes:#self._keys:
d[k] = getattr(self, k)
return d

class LcoptSettings(object):

def __init__(self):

self.config = storage.load_config()

self._sections = []

for section, content in self.config.items():
setattr(self, section, SettingsDict(content))
self._sections.append(section)

def as_dict(self):
d = {}
for k in self._sections:
d[k] = getattr(self, k).as_dict()
return d

def __repr__(self):

string = ""

for section, content in self.as_dict().items():
string += "{}:\n".format(section)
for k, v in content.items():
string += "\t{}: {}\n".format(k,v)

return "Lcopt settings: \n\n{}".format(string)


def write(self):
"""write the current settings to the config file"""
with open(storage.config_file, 'w') as cfg:
yaml.dump(self.as_dict(), cfg, default_flow_style=False)

storage.refresh()

settings = LcoptSettings()

0 comments on commit 544c662

Please sign in to comment.