Skip to content
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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

eSoares
Copy link
Contributor

@eSoares eSoares commented Nov 15, 2017

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.).

…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.).
@eSoares
Copy link
Contributor Author

eSoares commented Nov 16, 2017

Just to add to the idea of loading tokens/vars from a config file.
Each plugin could have it's own section with their parameters, something like:

.....
[plugin_weather]
token = ....
default_city = ...

Copy link
Owner

@llimllib llimllib left a 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
Copy link
Owner

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?

Copy link
Contributor Author

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:
Copy link
Owner

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 = {}
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

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

Copy link
Contributor Author

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")
Copy link
Owner

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?

Copy link
Contributor Author

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")
Copy link
Owner

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?

Copy link
Contributor Author

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()
Copy link
Owner

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?

Copy link
Contributor Author

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
Copy link
Owner

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?

Copy link
Contributor Author

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.

@eSoares eSoares mentioned this pull request Jan 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants