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

Revamped module loading, addresses #202 issue #203

Merged
merged 1 commit into from Aug 27, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,29 @@ def setup(self):
self.variables = {}

filenames = []
if not hasattr(self.config, 'enable'):
for fn in os.listdir(os.path.join(os.getcwd(), 'modules')):
if fn.endswith('.py') and not fn.startswith('_'):
filenames.append(os.path.join(home, 'modules', fn))
else:
for fn in self.config.enable:
filenames.append(os.path.join(home, 'modules', fn + '.py'))

if hasattr(self.config, 'extra'):
for fn in self.config.extra:
if os.path.isfile(fn):
filenames.append(fn)
elif os.path.isdir(fn):
for n in os.listdir(fn):
if n.endswith('.py') and not n.startswith('_'):
filenames.append(os.path.join(fn, n))

# Default module folder + extra folders
module_folders = [os.path.join(home, 'modules')]
module_folders.extend(getattr(self.config, 'extra', []))

excluded = getattr(self.config, 'exclude', [])
enabled = getattr(self.config, 'enable', [])

for folder in module_folders:
if os.path.isfile(folder):
filenames.append(folder)
elif os.path.isdir(folder):
for fn in os.listdir(folder):
if fn.endswith('.py') and not fn.startswith('_'):
name = os.path.basename(fn)[:-3]
# If whitelist is present only include whitelisted
# Never include blacklisted items
if name in enabled or not enabled and name not in excluded:
Copy link
Contributor

Choose a reason for hiding this comment

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

You could make this clearer

Copy link
Author

Choose a reason for hiding this comment

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

I feel it's a lot more clearer than it was before.

The excluded and enabled variables aren't called "excluded_modules" and "enabled_modules" because it makes the lines really long, but if that's not a problem I can gladly rename them.

fn can probably be renamed to file, or filename.

Besides that I sadly can't think of a way to improve the code.

filenames.append(os.path.join(folder, fn))

modules = []
excluded_modules = getattr(self.config, 'exclude', [])
for filename in filenames:
name = os.path.basename(filename)[:-3]
if name in excluded_modules: continue
# if name in sys.modules:
# del sys.modules[name]
try: module = imp.load_source(name, filename)
Expand Down