-
-
Notifications
You must be signed in to change notification settings - Fork 101
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
Use YAML format for tanner configuration #371
Conversation
Since the old config was using staticmethod decorator I decided to use those
This has to be done so TravisCI can include a file ## To make a commit, type your commit message and press CTRL-ENTER. ## To cancel the commit, close the window. To sign off on the commit, ## press CTRL-S. ## ## You may also reference or close a GitHub issue with this commit. ## To do so, type `#` followed by the `tab` key. You will be shown a ## list of issues related to the current repo. You may also type ## `owner/repo#` plus the `tab` key to reference an issue in a ## different GitHub repo. setup.py | 3 ++- tanner/config.py | 4 ++-- tanner/{ => data}/config.yaml | 0 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index edcecad..01aa755 100644 --- a/setup.py +++ b/setup.py @@ -12,5 +12,6 @@ setup(name='Tanner', scripts=['bin/tanner', 'bin/tannerweb', 'bin/tannerapi'], data_files=[('/opt/tanner/db/', ['tanner/data/db_config.json', 'tanner/data/GeoLite2-City.mmdb']), ('/opt/tanner/data/', ['tanner/data/dorks.pickle', 'tanner/data/crawler_user_agents.txt', - 'tanner/files/engines/mako.py', 'tanner/files/engines/tornado.py'])] + 'tanner/files/engines/mako.py', 'tanner/files/engines/tornado.py', + 'tanner/data/config.yamls'])] ) diff --git a/tanner/config.py b/tanner/config.py index c5aa423..788a56e 100644 --- a/tanner/config.py +++ b/tanner/config.py @@ -18,7 +18,7 @@ class Meta(type): def parse_default_configs(path): return read_config(path) - default_config = parse_default_configs("/opt/tanner/config.yaml") + default_config = parse_default_configs("/opt/tanner/data/config.yaml") attribs.update({ 'default_config': default_config, 'parse_default_configs': parse_default_configs @@ -32,7 +32,7 @@ class TannerConfig(metaclass=Meta): @staticmethod def set_default_config(default_config_path): - TannerConfig.default_config = read_config("/opt/tanner/config.yaml") + TannerConfig.default_config = read_config(default_config_path) @staticmethod def set_config(config_path): diff --git a/tanner/config.yaml b/tanner/data/config.yaml similarity index 100% rename from tanner/config.yaml rename to tanner/data/config.yaml
Pull Request Test Coverage Report for Build 1233
💛 - Coveralls |
@afeena Finally all the tests are passing so please review it. |
config.ini
Outdated
@@ -0,0 +1,2 @@ | |||
[API] |
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 you need this file? Don't we want to move to yml?
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.
Sorry, this was by mistake. Since we are going to use YAML config file I've removed this from the source.
tanner/config.py
Outdated
return config_values | ||
|
||
|
||
class Meta(type): |
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 you need meta class? which problem does it solve?
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 was using Meta class to set the default value of the config within the class.
But now I realized it would be just fine if we can stick to the old model of config i.e using a GLOBAL variable to hold the default values.
tanner/config.py
Outdated
'SESSIONS': {"delete_timeout": 300} | ||
} | ||
|
||
def read_config(path): |
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.
you can make this function also a class function
tanner/config.py
Outdated
return config_values | ||
|
||
|
||
DEFAULT_CONFIG = read_config("/opt/tanner/data/config.yaml") |
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 do not read default while setting config?
tanner/config.py
Outdated
LOGGER.warning("Error in config, default value will be used. Section: %s Value: %s", section, value) | ||
res = config_template[section][value] | ||
|
||
res = TannerConfig.config[section][value] |
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.
instead of using if/try you can use noly try and catch also TypeError (case TannerConfig.config is None
)
or
you can merge together default config and user config
Looks good :) I tested it a little bit and it works well, just some minor comments :) |
@afeena The suggested changes have been made. |
If I try to set only one particular value through config, I get an error:
my config file:
|
tanner/config.py
Outdated
'SESSIONS': {"delete_timeout": 300} | ||
} | ||
|
||
class ReadConfig(): |
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.
maybe it's better to move read_config in tanner config class? Have a separate class for only one function which is logically connected with other general class seems redundant for me
Hey, won't this make the old config files obsolete? |
@afeena Yes, that is because in One solution is that we have a kind of check function, like if the section doesn't have all the values then the missing values will be added from the default config. @Parth1811 Yes, I think that was the idea. |
@mzfr my point is, that it would be nice to have this property, just e.g. I want to switch off one particular emulator, so I don't want to define al others as true. I suggest you merge two dict (might be a problem for nested structure), in this case, you can keep only one copy of config and don't do additional checks. Or maybe you have another good solution :) And as @Parth1811 mention, the old config will be not supported anymore, so it would be nice to catch the exception while trying to load yml config (in case someone wants to load ini file) |
Also added a check for old config format
@afeena I've made the changes that we discussed. Also, I have updated the config docs. Let me know if something is still missing out. |
tanner/config.py
Outdated
def read_config(path): | ||
config_values = {} | ||
with open(path, 'r') as f: | ||
config_values = yaml.load(f, Loader=yaml.FullLoader) |
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 think it would be nice to catch the exception instead of checking the ending of filename here https://github.com/mushorg/tanner/pull/371/files#diff-f428ee775d6702ef490b2120c5ef2ae5R23, because e.g. I've never used .ini for configs, I used .config
:)
'LOCALLOG': {'enabled': 'False', 'PATH': '/tmp/tanner_report.json'}, | ||
'CLEANLOG': {'enabled': 'False'} | ||
} | ||
DATA: |
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.
You can use comments to describe a little bit sections :)
I find this example quite good :)
https://opennmt.net/OpenNMT-tf/configuration.html
Also added some comment for config docs. And removed a comma from a config
@mzfr Awesome! It's ready to merge, since it contains breaking changes, what do you think if we merge it into develop first together with upcoming docker and dir changes and then prepare new release? |
@afeena yeah that would be a good thing to do. |
@mzfr could you please then change destination to develop branch? |
@afeena Done. And luckily I don't have to rebase :) |
@mzfr great, thanks! |
* Use YAML format for tanner configuration * Add pyyaml to requirements.txt * remove usage of self in TannerConfig class Since the old config was using staticmethod decorator I decided to use those * Fix the format of YAML config * Use metaclass to setup default values * Update config test * place default config file under tanner/data This has to be done so TravisCI can include a file ## To make a commit, type your commit message and press CTRL-ENTER. ## To cancel the commit, close the window. To sign off on the commit, ## press CTRL-S. ## ## You may also reference or close a GitHub issue with this commit. ## To do so, type `#` followed by the `tab` key. You will be shown a ## list of issues related to the current repo. You may also type ## `owner/repo#` plus the `tab` key to reference an issue in a ## different GitHub repo. setup.py | 3 ++- tanner/config.py | 4 ++-- tanner/{ => data}/config.yaml | 0 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index edcecad..01aa755 100644 --- a/setup.py +++ b/setup.py @@ -12,5 +12,6 @@ setup(name='Tanner', scripts=['bin/tanner', 'bin/tannerweb', 'bin/tannerapi'], data_files=[('/opt/tanner/db/', ['tanner/data/db_config.json', 'tanner/data/GeoLite2-City.mmdb']), ('/opt/tanner/data/', ['tanner/data/dorks.pickle', 'tanner/data/crawler_user_agents.txt', - 'tanner/files/engines/mako.py', 'tanner/files/engines/tornado.py'])] + 'tanner/files/engines/mako.py', 'tanner/files/engines/tornado.py', + 'tanner/data/config.yamls'])] ) diff --git a/tanner/config.py b/tanner/config.py index c5aa423..788a56e 100644 --- a/tanner/config.py +++ b/tanner/config.py @@ -18,7 +18,7 @@ class Meta(type): def parse_default_configs(path): return read_config(path) - default_config = parse_default_configs("/opt/tanner/config.yaml") + default_config = parse_default_configs("/opt/tanner/data/config.yaml") attribs.update({ 'default_config': default_config, 'parse_default_configs': parse_default_configs @@ -32,7 +32,7 @@ class TannerConfig(metaclass=Meta): @staticmethod def set_default_config(default_config_path): - TannerConfig.default_config = read_config("/opt/tanner/config.yaml") + TannerConfig.default_config = read_config(default_config_path) @staticmethod def set_config(config_path): diff --git a/tanner/config.yaml b/tanner/data/config.yaml similarity index 100% rename from tanner/config.yaml rename to tanner/data/config.yaml * Use global variable instead of Meta class * Make read_config function a static method under ReadConfig class * Move read_config function under TannerConfig class * removed get_section() and used get() in its place Also added a check for old config format * Update config docs * catch exception while reading the config file. Also added some comment for config docs. And removed a comma from a config
Fixes #366
Few things might fail but I am working on this. Made the PR so you(@afeena) can take a look and give your input about the way it's done.