Skip to content

halibot: add optional config, configfile argument to core#113

Closed
richteer wants to merge 1 commit into
masterfrom
allow-supplied-config
Closed

halibot: add optional config, configfile argument to core#113
richteer wants to merge 1 commit into
masterfrom
allow-supplied-config

Conversation

@richteer

@richteer richteer commented Oct 1, 2018

Copy link
Copy Markdown
Contributor

With the eventual intent on moving some main.py functionality into modules, it will be useful to reuse some of core's logic for temporary cli use. Thus, we will need to spin up temporary instances of Halibot to load config, load modules, etc.

This adds an optional keyword config to Halibot, which sets a config dict as supplied. By default, this value is empty, as it was previously.

This also adds an optional keyword configfile to Halibot, to specify an alternate configuration file to load on ._load_config(). Therefore, this method has also been updated to accept an optional parameter configfile parameter of its own, in the event some external code wants to load an
alternate code without running the full start process.

The default config file remains config.json. In theory, no changes to other code is necessary.

NOTE: With this change, config is no longer a static class variable, and is now purely an instance variable. This probably shouldn't affect anything, but multiple Halibot objects will no longer have the same config dict object.

This should resolve #84

@richteer
richteer requested a review from sjrct October 1, 2018 18:19
Comment thread halibot/halibot.py
if self.use_config:
self._load_config()
if not self.config:
self._load_config(configfile=self.configfile)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Forgot to mention this in the description, but I have it here skip loading the config if it was already defined. We may want to change this behavior, but I figure if someone is calling .start(), they can also call ._load_config as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Follow up TODO item: we should really consider config.json validation.

Comment thread halibot/halibot.py Outdated
self.use_auth = kwargs.get("use_auth", True)
self.workdir = kwargs.get("workdir", ".")
self.config = kwargs.get("config", {})
self.configfile = kwards.get("configfile", "config.json")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Typo here, will fix in next edit.

Comment thread halibot/halibot.py
self.use_config = kwargs.get("use_config", True)
self.use_auth = kwargs.get("use_auth", True)
self.workdir = kwargs.get("workdir", ".")
self.config = kwargs.get("config", {})

@richteer richteer Oct 1, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Issue here: When we load a config from a file via ._load_config, we update halibot.packages.__path__. If a config is supplied, that step is skipped. Will fix in next edit as well, just need to check if we are supplied a config and update the path accordingly.

update: should be fixed now

With the eventual intent on moving some `main.py` functionality into modules,
it will be useful to reuse some of core's logic for temporary cli use. Thus,
we will need to spin up temporary instances of `Halibot` to load config, load
modules, etc.

This adds an optional keyword `config` to `Halibot`, which sets a config dict
as supplied. By default, this value is empty, as it was previously.

This also adds an optional keyword `configfile` to `Halibot`, to specify an
alternate configuration file to load on `._load_config()`. Therefore, this
method has also been updated to accept an optional parameter `configfile`
parameter of its own, in the event some external code wants to load an
alternate code without running the full start process.

The default config file remains `config.json`. In theory, no changes to
other code is necessary.

**NOTE:** With this change, `config` is no longer a static class variable,
and is now purely an instance variable. This probably shouldn't affect
anything, but multiple `Halibot` objects will no longer have the same
config dict object.
@richteer
richteer force-pushed the allow-supplied-config branch from 7e0afe9 to ba150da Compare October 1, 2018 19:20
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.2%) to 94.536% when pulling ba150da on allow-supplied-config into e033e85 on master.

@sjrct sjrct left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There will be some conflicts between this and PR #126, probably easier to merge that before this?

Moving _load_config and _write_config into the Config class defined in that PR as discussed in IRC implies moving configfile into that class as well. I suspect we should make them public methods too,

Should probably also add an argument for specifying the system config path and an argument for ignoring the system config altogether.

Comment thread halibot/halibot.py
self.workdir = kwargs.get("workdir", ".")
self.config = kwargs.get("config", {})
if self.config:
halibot.packages.__path__ = self.config.get("package-path", [])

@sjrct sjrct Feb 6, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With #126 lines 46-47 should not be needed as that assignment is done automatically

@richteer

richteer commented Feb 6, 2019

Copy link
Copy Markdown
Contributor Author

There will be some conflicts between this and PR #126, probably easier to merge that before this?

I agree, #126 is more interesting than this one. In fact, a lot of this PR could probably be loaded into the Config class.

Moving _load_config and _write_config into the Config class defined in that PR as discussed in IRC implies moving configfile into that class as well. I suspect we should make them public methods too,

Oh yeah, that.

Should probably also add an argument for specifying the system config path and an argument for ignoring the system config altogether.

Agreed. Should also we consider default paths or environment variables for this?

@sjrct

sjrct commented Feb 6, 2019

Copy link
Copy Markdown
Member

Agreed. Should also we consider default paths or environment variables for this?

The default right now is to look alongside the halibot source code, which is inferred from the __file__, which seems fine. I think it would be more useful to have an environment variable for that top-level halibot directory, HALDIR, which defaults to inferring from __file__ if not set.

@richteer

richteer commented Mar 5, 2019

Copy link
Copy Markdown
Contributor Author

Closing this since these functions should be on the Config class now. #84 still needs to be resolved.

@richteer richteer closed this Mar 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Halibot core should accept "temporary" configs

3 participants