-
Notifications
You must be signed in to change notification settings - Fork 160
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
Add config loader from config file #146
base: master
Are you sure you want to change the base?
Conversation
…iable With this config we load configuration variables from a config file and from the environment. If a variable is in both (config and env) the env variable is used. This makes easy to quick change a particular variable between restarts (the debug level e.g.).
Just to add to the idea of loading tokens/vars from a config file.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left detailed notes on the implementation, but I think I'm still not sold on the why to add this. Can you make the case for having a config file again?
In my mind, you could always just write a tiny bash script that, on startup, dumped config variables from a file into the environment. Why would this be better than that?
limbo/config.py
Outdated
@@ -0,0 +1,50 @@ | |||
import os | |||
from backports import configparser as confparser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does backports come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gives some backports of features from newer versions of Python to previews: https://pypi.python.org/pypi/configparser
limbo/config.py
Outdated
from backports import configparser as confparser | ||
|
||
|
||
class Config: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be class Config(object)
limbo/config.py
Outdated
|
||
|
||
class Config: | ||
env_config = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an instance variable, not a class-level variable. Later on, when you say self.env_config
, you're not referring to this variable but creating a new instance variable; this one is available as Config.env_config
but is never used
limbo/config.py
Outdated
|
||
class Config: | ||
env_config = {} | ||
file_config = None # type: confparser.ConfigParser |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
limbo/config.py
Outdated
self.file_config = confparser.ConfigParser() | ||
config_file = self.env_config.get("config_location", "config.ini") | ||
# check if config exists | ||
import os |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import this at the top of the file, not in here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
limbo/config.py
Outdated
# check if config exists | ||
import os | ||
if not os.path.exists(config_file): | ||
raise IOError("File for configuration not found") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we just log it and move on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it can be.
limbo/config.py
Outdated
|
||
def load_config_file(self): | ||
self.file_config = confparser.ConfigParser() | ||
config_file = self.env_config.get("config_location", "config.ini") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this really need to be configurable? Couldn't we just say that the config file is named config.ini
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes testing easier, to change between mock files. Also adds easier configuration.
limbo/config.py
Outdated
self.load_config_file() | ||
|
||
def load_config_file(self): | ||
self.file_config = confparser.ConfigParser() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why store file config separately from environment config? If we allow that we should accept both, shouldn't they go into the same object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future we can easy add option to extensions to write to config file, not just read.
For that we should keep the ConfigParser object.
requirements.txt
Outdated
@@ -35,3 +35,4 @@ virtualenv==15.1.0 | |||
webencodings==0.5.1 | |||
websocket-client==0.44.0 | |||
wrapt==1.10.10 | |||
configparser==3.5.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why add a third-party lib when python has this available in all the versions we use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, was my mistake. The IDE kept saying it wasn't installed and I thought it was needed for Python 2.
Will remove in future commit then.
# Conflicts: # limbo/limbo.py
With this config we load configuration variables from a config file and from the environment.
If a variable is present in both (config and env) the env variable is used.
This makes easy to quick change a particular variable between restarts (the debug level e.g.).