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

Move Package Management Logic to a Module #114

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

Conversation

richteer
Copy link
Contributor

@richteer richteer commented Oct 1, 2018

THIS IS A VERY RFC PATCH, DO NOT MERGE

Moral of this PR: Why do things in main.py, when we can share that logic with a module?

This is a crazy hacky RFC to demonstrate a method of implementing logic that is meant to be shared between the CLI and an actual running instance. Due to halibot-extra/irc#3 , I'm not able to test the runtime portion yet, but surprisingly the CLI portion does.

General idea:

Add a new -instances list to config.json called cli-instances, which are only ever loaded from main.py. Only modules that implement .cli() (final name TBD) and .cli_receive() (also needs fixing) should be ever listed there. The .cli() call takes in the command subparser from main.py, and adds whatever commands it supports there. The .cli_receive() makes the actual call.

Why.

Well, doing it this way meant I only needed to change how to print out information. Plus, we don't need to depend on routing or any other weird stuff yet to figure out when the CLI commands need to show up at all. For now, this will lead to a much simpler and smaller main.py, and the module side of things isn't any more complex than it was before.

Look forward to a !config module using the similar pattern coming soon, probably based off of this branch.

Other notes:

  • This PR somewhat depends on halibot: add optional config, configfile argument to core #113 , so please check and merge that one long before caring about this one.
  • I'm ignoring Travis again, since this will probably break all the tests yet again.
  • This will most assuredly require a version bump, I suggest we either merge this into an for-0.3.0 branch, or bump version shortly after

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
Copy link
Contributor Author

richteer commented Oct 3, 2018

Pushed a couple of small fixes:

  • no longer errors if cli-instances is not defined in the config
  • fixed import of halibot so new and old code agree

self.config = json.loads(f.read())
halibot.packages.__path__ = self.config.get("package-path", [])

def _write_config(self):
with open(os.path.join(self.workdir, "config.json"), "w") as f:
def _write_config(self, configfile="config.json"):
Copy link
Member

Choose a reason for hiding this comment

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

Should we pull out the "config.json" string and make it a constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Late to the party here, but I'll update this PR when #126, #113, etc are merged. This line probably won't apply with those changes.

"of": "core:PackageManager"
}
}
}
Copy link
Member

@sjrct sjrct Jan 31, 2019

Choose a reason for hiding this comment

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

I think we should make this a file and add an option for init to specify the default config. I was considering doing that as part of a fix for #122 and #121, so there may be a conflict here.

@sjrct
Copy link
Member

sjrct commented Jan 31, 2019

What do you think about having the CLI work as a halibot instance, where there is one agent that receives the commands from the CLI and and then the various default cli-instances would just live as module-instances within that config? We can still have the cli-instances for extensions to the command line interface that people want to add.

main.py becomes very simple in this case, all it does is spin up the cli halibot instance.

@richteer
Copy link
Contributor Author

richteer commented Feb 6, 2019

What do you think about having the CLI work as a halibot instance, where there is one agent that receives the commands from the CLI and and then the various default cli-instances would just live as module-instances within that config?

That was sort of what I was trying to work towards with this PR. The obvious exception would be a main.py run, which would have to run the real instance.

I think all we would have to do is have the dummy cli-agent ".receive()" the rest of argv.

I need to refresh myself a bit first on this PR to be honest.

We can still have the cli-instances for extensions to the command line interface that people want to add.

I went back and forth on that one debating if it should be a different file like config-cli.json, but I think it's cleaner and more portable to keep that in the main config.json.

main.py becomes very simple in this case, all it does is spin up the cli halibot instance.

That's the goal. I'd like little-to-no logic in main.py anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants