Optimize imports
This commit is contained in:
parent
2e7a4ad132
commit
e588c30044
29 changed files with 730 additions and 924 deletions
|
|
@ -16,10 +16,9 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import imp
|
||||
|
||||
|
||||
def reload():
|
||||
import imp
|
||||
|
||||
import nemubot.tools.config
|
||||
imp.reload(nemubot.tools.config)
|
||||
|
||||
|
|
|
|||
|
|
@ -17,9 +17,6 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
||||
from nemubot.tools.xmlparser import parse_file
|
||||
|
||||
logger = logging.getLogger("nemubot.tools.config")
|
||||
|
||||
|
|
@ -97,7 +94,11 @@ def load_file(filename, context):
|
|||
filename -- the path to the file to load
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
if os.path.isfile(filename):
|
||||
from nemubot.tools.xmlparser import parse_file
|
||||
|
||||
config = parse_file(filename)
|
||||
|
||||
# This is a true nemubot configuration file, load it!
|
||||
|
|
|
|||
|
|
@ -16,10 +16,6 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from datetime import datetime, timezone
|
||||
import time
|
||||
|
||||
|
||||
def countdown(delta, resolution=5):
|
||||
sec = delta.seconds
|
||||
hours, remainder = divmod(sec, 3600)
|
||||
|
|
@ -82,10 +78,15 @@ def countdown_format(date, msg_before, msg_after, tz=None):
|
|||
"""Replace in a text %s by a sentence incidated the remaining time
|
||||
before/after an event"""
|
||||
if tz is not None:
|
||||
import os
|
||||
oldtz = os.environ['TZ']
|
||||
os.environ['TZ'] = tz
|
||||
|
||||
import time
|
||||
time.tzset()
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
# Calculate time before the date
|
||||
try:
|
||||
if datetime.now(timezone.utc) > date:
|
||||
|
|
@ -103,6 +104,7 @@ def countdown_format(date, msg_before, msg_after, tz=None):
|
|||
delta = date - datetime.now()
|
||||
|
||||
if tz is not None:
|
||||
import os
|
||||
os.environ['TZ'] = oldtz
|
||||
|
||||
return sentence_c % countdown(delta)
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@
|
|||
|
||||
# Extraction/Format text
|
||||
|
||||
from datetime import datetime, date
|
||||
import re
|
||||
|
||||
xtrdt = re.compile(r'''^.*? (?P<day>[0-9]{1,4}) .+?
|
||||
|
|
@ -71,6 +70,7 @@ def extractDate(msg):
|
|||
second = result.group("second")
|
||||
|
||||
if year is None:
|
||||
from datetime import date
|
||||
year = date.today().year
|
||||
if hour is None:
|
||||
hour = 0
|
||||
|
|
@ -84,6 +84,7 @@ def extractDate(msg):
|
|||
minute = int(minute) + 1
|
||||
second = 0
|
||||
|
||||
from datetime import datetime
|
||||
return datetime(int(year), int(month), int(day),
|
||||
int(hour), int(minute), int(second))
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -16,18 +16,9 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from html.entities import name2codepoint
|
||||
import http.client
|
||||
import json
|
||||
import re
|
||||
import socket
|
||||
from urllib.parse import quote
|
||||
from urllib.parse import urlparse
|
||||
from urllib.request import urlopen
|
||||
|
||||
from nemubot import __version__
|
||||
from nemubot.exception import IRCException
|
||||
from nemubot.tools.xmlparser import parse_string
|
||||
|
||||
|
||||
def isURL(url):
|
||||
|
|
@ -70,11 +61,19 @@ def getPassword(url):
|
|||
# Get real pages
|
||||
|
||||
def getURLContent(url, timeout=15):
|
||||
"""Return page content corresponding to URL or None if any error occurs"""
|
||||
"""Return page content corresponding to URL or None if any error occurs
|
||||
|
||||
Arguments:
|
||||
url -- the URL to get
|
||||
timeout -- maximum number of seconds to wait before returning an exception
|
||||
"""
|
||||
|
||||
o = urlparse(url)
|
||||
if o.netloc == "":
|
||||
o = urlparse("http://" + url)
|
||||
|
||||
import http.client
|
||||
|
||||
if o.scheme == "http":
|
||||
conn = http.client.HTTPConnection(o.hostname, port=o.port,
|
||||
timeout=timeout)
|
||||
|
|
@ -85,7 +84,10 @@ def getURLContent(url, timeout=15):
|
|||
conn = http.client.HTTPConnection(o.hostname, port=80, timeout=timeout)
|
||||
else:
|
||||
return None
|
||||
|
||||
import socket
|
||||
try:
|
||||
from nemubot import __version__
|
||||
if o.query != '':
|
||||
conn.request("GET", o.path + "?" + o.query,
|
||||
None, {"User-agent": "Nemubot v%s" % __version__})
|
||||
|
|
@ -141,16 +143,31 @@ def getURLContent(url, timeout=15):
|
|||
|
||||
|
||||
def getXML(url, timeout=15):
|
||||
"""Get content page and return XML parsed content"""
|
||||
"""Get content page and return XML parsed content
|
||||
|
||||
Arguments:
|
||||
url -- the URL to get
|
||||
timeout -- maximum number of seconds to wait before returning an exception
|
||||
"""
|
||||
|
||||
cnt = getURLContent(url, timeout)
|
||||
if cnt is None:
|
||||
return None
|
||||
else:
|
||||
from nemubot.tools.xmlparser import parse_string
|
||||
return parse_string(cnt.encode())
|
||||
|
||||
|
||||
def getJSON(url, timeout=15):
|
||||
"""Get content page and return JSON content"""
|
||||
"""Get content page and return JSON content
|
||||
|
||||
Arguments:
|
||||
url -- the URL to get
|
||||
timeout -- maximum number of seconds to wait before returning an exception
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
cnt = getURLContent(url, timeout)
|
||||
if cnt is None:
|
||||
return None
|
||||
|
|
@ -161,13 +178,27 @@ def getJSON(url, timeout=15):
|
|||
# Other utils
|
||||
|
||||
def htmlentitydecode(s):
|
||||
"""Decode htmlentities"""
|
||||
"""Decode htmlentities
|
||||
|
||||
Argument:
|
||||
s -- The string to decode
|
||||
"""
|
||||
|
||||
import re
|
||||
from html.entities import name2codepoint
|
||||
|
||||
return re.sub('&(%s);' % '|'.join(name2codepoint),
|
||||
lambda m: chr(name2codepoint[m.group(1)]), s)
|
||||
|
||||
|
||||
def striphtml(data):
|
||||
"""Remove HTML tags from text"""
|
||||
"""Remove HTML tags from text
|
||||
|
||||
Argument:
|
||||
data -- the string to strip
|
||||
"""
|
||||
|
||||
import re
|
||||
p = re.compile(r'<.*?>')
|
||||
return htmlentitydecode(p.sub('', data)
|
||||
.replace("(", "/(")
|
||||
|
|
|
|||
|
|
@ -16,13 +16,10 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import logging
|
||||
import xml.sax
|
||||
|
||||
from nemubot.tools.xmlparser import node as module_state
|
||||
|
||||
logger = logging.getLogger("nemubot.tools.xmlparser")
|
||||
|
||||
|
||||
class ModuleStatesFile(xml.sax.ContentHandler):
|
||||
|
||||
|
|
|
|||
|
|
@ -16,10 +16,7 @@
|
|||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import calendar
|
||||
from datetime import datetime, timezone
|
||||
import logging
|
||||
import xml.sax
|
||||
|
||||
logger = logging.getLogger("nemubot.tools.xmlparser.node")
|
||||
|
||||
|
|
@ -78,14 +75,17 @@ class ModuleState:
|
|||
else:
|
||||
return None
|
||||
|
||||
from datetime import datetime
|
||||
if isinstance(source, datetime):
|
||||
return source
|
||||
else:
|
||||
from datetime import timezone
|
||||
try:
|
||||
return datetime.utcfromtimestamp(float(source)).replace(tzinfo=timezone.utc)
|
||||
except ValueError:
|
||||
while True:
|
||||
try:
|
||||
import time
|
||||
return time.strptime(source[:19], "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc)
|
||||
except ImportError:
|
||||
pass
|
||||
|
|
@ -140,6 +140,7 @@ class ModuleState:
|
|||
|
||||
def setAttribute(self, name, value):
|
||||
"""DOM like method"""
|
||||
from datetime import datetime
|
||||
if (isinstance(value, datetime) or isinstance(value, str) or
|
||||
isinstance(value, int) or isinstance(value, float)):
|
||||
self.attributes[name] = value
|
||||
|
|
@ -196,14 +197,17 @@ class ModuleState:
|
|||
|
||||
def save_node(self, gen):
|
||||
"""Serialize this node as a XML node"""
|
||||
from datetime import datetime
|
||||
attribs = {}
|
||||
for att in self.attributes.keys():
|
||||
if att[0] != "_": # Don't save attribute starting by _
|
||||
if isinstance(self.attributes[att], datetime):
|
||||
import calendar
|
||||
attribs[att] = str(calendar.timegm(
|
||||
self.attributes[att].timetuple()))
|
||||
else:
|
||||
attribs[att] = str(self.attributes[att])
|
||||
import xml.sax
|
||||
attrs = xml.sax.xmlreader.AttributesImpl(attribs)
|
||||
|
||||
try:
|
||||
|
|
@ -220,6 +224,7 @@ class ModuleState:
|
|||
def save(self, filename):
|
||||
"""Save the current node as root node in a XML file"""
|
||||
with open(filename, "w") as f:
|
||||
import xml.sax
|
||||
gen = xml.sax.saxutils.XMLGenerator(f, "utf-8")
|
||||
gen.startDocument()
|
||||
self.save_node(gen)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue