]> jfr.im git - erebus.git/commitdiff
more py3 compat
authorzonidjan <redacted>
Wed, 11 Apr 2018 02:16:29 +0000 (21:16 -0500)
committerzonidjan <redacted>
Wed, 11 Apr 2018 02:16:29 +0000 (21:16 -0500)
ctlmod.py
modlib.py
modules/foo.py
modules/weather.py

index 4d12d214a2e7f261f462f5ec4bcb1be9b00ed2ad..09ce8fa51022b66d024306f2602ae68d458efdf0 100644 (file)
--- a/ctlmod.py
+++ b/ctlmod.py
@@ -3,11 +3,13 @@
 
 from __future__ import print_function
 
-import sys, time
+import sys, time, importlib
 import modlib
 
 if sys.version_info.major >= 3:
        from importlib import reload
+else:
+       importlib.invalidate_caches = lambda: None
 
 modules = {}
 dependents = {}
@@ -17,7 +19,7 @@ def isloaded(modname): return modname in modules
 def modhas(modname, attname): return getattr(modules[modname], attname, None) is not None
 
 def load(parent, modname, dependent=False):
-       #wrapper to call _load and print return
+       """Wrapper to call _load and print the return value."""
        if dependent:
                print("(Loading dependency %s..." % (modname), end=' ')
        else:
@@ -41,11 +43,12 @@ def load(parent, modname, dependent=False):
        return modstatus
 
 def _load(parent, modname, dependent=False):
+       """Load and return the new status of the module."""
        successstatus = []
        if not isloaded(modname):
+               importlib.invalidate_caches()
                try:
-                       mod = __import__('modules.'+modname, globals(), locals(), ['*'], 0)
-                       # ^ fromlist doesn't actually do anything(?) but it means we don't have to worry about this returning the top-level "modules" object
+                       mod = importlib.import_module('modules.'+modname)
                        reload(mod) #in case it's been previously loaded.
                except Exception as e:
                        return modlib.error(e)
index e4686a2546aa1044018e4fa330562f2573f701f8..e5c8d81bc2964e0648bbf71e5f9f64c230883909 100644 (file)
--- a/modlib.py
+++ b/modlib.py
@@ -10,6 +10,7 @@ else:
        stringbase = str
 
 class error(object):
+       """Used to return an error to the bot core."""
        def __init__(self, desc):
                self.errormsg = desc
        def __nonzero__(self):
index 0171182cffd7e22010a75cea5455985cdf14596c..ea91a9f77285df703043f01334ab83a3c058a8de 100644 (file)
@@ -43,6 +43,7 @@ def needchan(bot, user, chan, realtarget, *args):
 
 @lib.hook(needchan=False, wantchan=True)
 @lib.help(None, 'a command which will consume a channel if given')
+def wantchan(bot, user, chan, realtarget, *args):
        if chan is not None:
                bot.msg(user, "Channel provided: %s" % (chan))
        else:
index 27fc9013d7423ad9a1968fb39e005265fc7136f4..aafe1299cc2a24d94f21fa487f66dc17c9313fd6 100644 (file)
@@ -18,9 +18,14 @@ modstart = lib.modstart
 modstop = lib.modstop
 
 # module code
-import json, urllib, time
+import json, time, sys
 from email.utils import parsedate
 
+if sys.version_info.major < 3:
+       from urllib import urlopen
+else:
+       from urllib.request import urlopen
+
 def location(person, default=None): return lib.mod('userinfo')._get(person, 'location', default=None)
 
 def _dayofweek(dayname):
@@ -28,7 +33,7 @@ def _dayofweek(dayname):
 
 def _weather(place):
        if place is not None:
-               weather = json.load(urllib.urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place)))
+               weather = json.load(urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place)))
                if lib.parent.cfg.getboolean('debug', 'weather'):
                        lib.parent.log('*', "?", repr(weather))
                if 'response' in weather:
@@ -38,14 +43,15 @@ def _weather(place):
                                return "That search term is ambiguous. Please be more specific."
 
                current = weather['current_observation']
-               measuredat = list(parsedate(current['observation_time_rfc822']))
+               measuredat = list(parsedate(current['observation_time_rfc822'])) # we have to turn this into a list so that we can assign to it.
                measuredat[6] = _dayofweek(current['observation_time_rfc822'][0:3])
                measuredatTZ = current['local_tz_short']
                loc = current['observation_location']
                if loc['city'] == "" or loc['state'] == "": loc = current['display_location']
                return u"Weather in %(location)s: As of %(time)s %(tz)s, %(conditions)s, %(cel)s\u00B0C (%(far)s\u00B0F) (feels like %(flcel)s\u00B0C (%(flfar)s\u00B0F)). Wind %(wind)s. %(link)s" % {
                        'location': loc['full'],
-                       'time': time.strftime("%a %H:%M", measuredat), 'tz': measuredatTZ,
+                       'time': time.strftime("%a %H:%M", tuple(measuredat)), # now we have to turn it back into a tuple because Py3's time.strftime requires it.
+                       'tz': measuredatTZ,
                        'conditions': current['weather'],
                        'cel': current['temp_c'], 'far': current['temp_f'],
                        'flcel': current['feelslike_c'], 'flfar': current['feelslike_f'],