Move hashflag parsing to Utils.get_hashflags
This commit is contained in:
parent
5cd1936af9
commit
0f7a122a84
2 changed files with 38 additions and 27 deletions
|
@ -1,4 +1,5 @@
|
||||||
import glob, imp, io, inspect, os, sys, uuid
|
import glob, imp, io, inspect, os, sys, uuid
|
||||||
|
from src import Utils
|
||||||
|
|
||||||
BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
|
BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
|
||||||
BITBOT_EXPORTS_MAGIC = "__bitbot_exports"
|
BITBOT_EXPORTS_MAGIC = "__bitbot_exports"
|
||||||
|
@ -53,31 +54,24 @@ class ModuleManager(object):
|
||||||
def _load_module(self, bot, name):
|
def _load_module(self, bot, name):
|
||||||
path = self._module_path(name)
|
path = self._module_path(name)
|
||||||
|
|
||||||
with io.open(path, mode="r", encoding="utf8") as module_file:
|
for hashflag, value in Utils.get_hashflags(path):
|
||||||
while True:
|
if hashflag == "ignore":
|
||||||
line = module_file.readline().strip()
|
|
||||||
line_split = line.split(" ")
|
|
||||||
if line and line.startswith("#--"):
|
|
||||||
# this is a hashflag
|
|
||||||
if line == "#--ignore":
|
|
||||||
# nope, ignore this module.
|
# nope, ignore this module.
|
||||||
raise ModuleNotLoadedWarning("module ignored")
|
raise ModuleNotLoadedWarning("module ignored")
|
||||||
elif line_split[0] == "#--require-config" and len(
|
|
||||||
line_split) > 1:
|
elif hashflag == "require-config" and value:
|
||||||
if not self.config.get(line_split[1].lower(), None):
|
if not self.config.get(value.lower(), None):
|
||||||
# nope, required config option not present.
|
# nope, required config option not present.
|
||||||
raise ModuleNotLoadedWarning(
|
raise ModuleNotLoadedWarning("required config not present")
|
||||||
"required config not present")
|
|
||||||
elif line_split[0] == "#--require-module" and len(
|
elif hashflag == "require-module" and value:
|
||||||
line_split) > 1:
|
requirement = value.lower()
|
||||||
if not "bitbot_%s" % line_split[1].lower() in sys.modules:
|
if not requirement in self.modules:
|
||||||
if not line_split[1].lower() in self.waiting_requirement:
|
if not requirement in self.waiting_requirement:
|
||||||
self.waiting_requirement[line_split[1].lower()] = set([])
|
self.waiting_requirement[requirement] = set([])
|
||||||
self.waiting_requirement[line_split[1].lower()].add(path)
|
self.waiting_requirement[requirement].add(path)
|
||||||
raise ModuleNotLoadedWarning(
|
raise ModuleNotLoadedWarning("waiting for requirement")
|
||||||
"waiting for requirement")
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
module = imp.load_source(self._import_name(name), path)
|
module = imp.load_source(self._import_name(name), path)
|
||||||
|
|
||||||
if not hasattr(module, "Module"):
|
if not hasattr(module, "Module"):
|
||||||
|
|
21
src/Utils.py
21
src/Utils.py
|
@ -1,5 +1,5 @@
|
||||||
import json, re, traceback, urllib.request, urllib.parse, urllib.error, ssl
|
import io, json, re, traceback, urllib.request, urllib.parse, urllib.error
|
||||||
import string
|
import ssl, string
|
||||||
import bs4
|
import bs4
|
||||||
from . import ModuleManager
|
from . import ModuleManager
|
||||||
|
|
||||||
|
@ -296,3 +296,20 @@ def export(setting, value):
|
||||||
|
|
||||||
def strip_html(s):
|
def strip_html(s):
|
||||||
return bs4.BeautifulSoup(s, "lxml").get_text()
|
return bs4.BeautifulSoup(s, "lxml").get_text()
|
||||||
|
|
||||||
|
def get_hashflags(filename):
|
||||||
|
hashflags = {}
|
||||||
|
with io.open(filename, mode="r", encoding="utf8") as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip("\n")
|
||||||
|
if not line.startswith("#"):
|
||||||
|
break
|
||||||
|
elif line.startswith("#--"):
|
||||||
|
line_split = line.split(" ", 1)
|
||||||
|
hashflag = line_split[0][3:]
|
||||||
|
value = None
|
||||||
|
|
||||||
|
if len(line_split) > 1:
|
||||||
|
value = line_split[1]
|
||||||
|
hashflags[hashflag] = value
|
||||||
|
return hashflags.items()
|
||||||
|
|
Loading…
Reference in a new issue