]> jfr.im git - erebus.git/commitdiff
py3 updates
authorzonidjan <redacted>
Sat, 10 Apr 2021 04:08:40 +0000 (23:08 -0500)
committerzonidjan <redacted>
Sat, 10 Apr 2021 04:08:40 +0000 (23:08 -0500)
README.md
modules/trivia.py
modules/userinfo.py
modules/weatherstack_weather.py

index 0838f322ecd22dd9ab85e6f7db44c11e2b993d88..414f9f3a13884fb96c733cbaaa8f18f2d5876281 100644 (file)
--- a/README.md
+++ b/README.md
@@ -15,8 +15,8 @@ To suppress croncheck.sh from restarting the bot without removing from crontab,
 
 Output will be placed in `logfile`, which is rotated to `oldlogs/`. (I strongly recommend `rm oldlogs/*` as a weekly crontab entry. `@weekly find /path/to/erebus/oldlogs/ -mtime 7 -execdir rm '{}' +`)
 
-The bot targets both Python 2 and 3. However, it is generally only actively tested on Python 2.
-If it's not working on Python 3 (or an included module isn't working on Python 3), please raise a bug.
+The bot targets both Python 2 and 3. It has recently switched to being primarily tested on Python 3.
+Python 2 support will slowly erode due to a lack of testing.
 
 Some modules require additional supporting materials, which can be found in `modules/contrib/`.
 
index 251588c39281b39aea57d0754841e207cf5a33ce..8abc9ff7230467d09e15a48575424786760ab7b9 100644 (file)
@@ -35,8 +35,11 @@ import json, random, threading, re, time, datetime, os, sys
 
 if sys.version_info.major < 3:
        timerbase = threading._Timer
+       stringbase = basestring
 else:
+       import tempfile
        timerbase = threading.Timer
+       stringbase = str
 
 
 try:
@@ -130,11 +133,16 @@ class TriviaState(object):
                if json is not None and json.dump is not None:
 #                      json.dump(self.db, open(self.questionfile, "w"))#, indent=4, separators=(',', ': '))
                        dbjson = json.dumps(self.db)
+                       # we have to do a dance to save the file to make the update atomic, and also because Py3 doesn't have os.tempnam in the misguided pursuit of "security"
                        if len(dbjson) > 0:
                                os.rename(self.questionfile, self.questionfile+".auto.bak")
-                               tmpfn = os.tempnam('.', 'trivia')
                                try:
-                                       f = open(tmpfn, "w")
+                                       if sys.version_info.major < 3:
+                                               tmpfn = os.tempnam('.', 'trivia')
+                                               f = open(tmpfn, "w")
+                                       else:
+                                               fd, tmpfn = tempfile.mkstemp(dir='.', prefix='trivia')
+                                               f = open(fd, "w")
                                        f.write(dbjson)
                                        f.close()
                                        os.rename(tmpfn, self.questionfile)
@@ -145,7 +153,9 @@ class TriviaState(object):
                                                os.unlink(tmpfn)
                                        except OSError: # temp file is already gone
                                                pass
-                                       raise # we may be better off just swallowing exceptions?
+                                       except UnboundLocalError:
+                                               pass
+                                       raise
                return False
 
        def getchan(self):
@@ -332,7 +342,7 @@ class TriviaState(object):
                else:
                        nextq.append(time.time())
 
-               if isinstance(nextq[1], basestring):
+               if isinstance(nextq[1], stringbase):
                        nextq[1] = nextq[1].lower()
                else:
                        nextq[1] = [s.lower() for s in nextq[1]]
@@ -343,7 +353,7 @@ class TriviaState(object):
                qtext += " "
                for qword in qary:
                        spacer = random.choice(
-                               range(0x61,0x7A) + ([0x20]*4)
+                               list(range(0x61,0x7A)) + ([0x20]*4)
                        )
                        qtext += "\00304,01"+qword+"\00301,01"+chr(spacer) #a-z
                if not self.getbot().fastmsg(self.chan, qtext): #if message is too long:
@@ -355,7 +365,7 @@ class TriviaState(object):
                self.curq = nextq
                self.curqid = nextqid
 
-               if isinstance(self.curq[1], basestring): self.hintanswer = self.curq[1]
+               if isinstance(self.curq[1], stringbase): self.hintanswer = self.curq[1]
                else: self.hintanswer = random.choice(self.curq[1])
 
                self.steptimer = MyTimer(self.db['hinttimer'], self.nexthint, args=[1])
@@ -364,7 +374,7 @@ class TriviaState(object):
        def checkanswer(self, answer):
                if self.curq is None:
                        return False
-               elif isinstance(self.curq[1], basestring):
+               elif isinstance(self.curq[1], stringbase):
                        return answer.lower() == self.curq[1]
                else: # assume it's a list or something.
                        return answer.lower() in self.curq[1]
index ab093656a05bdab81baa921365424a263a411c02..c4281dd65c127d090bd2ebb80b8a4cf39beb0c72 100644 (file)
@@ -25,7 +25,13 @@ def modstop(*args, **kwargs):
        return lib.modstop(*args, **kwargs)
 
 # module code
-import json, __builtin__
+import json, sys
+if sys.version_info.major >= 3:
+       import builtins as __builtin__
+       stringbase = str
+else:
+       import __builtin__
+       stringbase = basestring
 
 #setup
 def gotParent():
@@ -44,7 +50,7 @@ def _getauth(thing):
        if isinstance(thing, parent.User):
                if thing.auth is not None:
                        return "#"+thing.auth
-       elif isinstance(thing, basestring):
+       elif isinstance(thing, stringbase):
                if thing.startswith("#"):
                        return thing
                else:
index a79ff78d115fc1fa5a2822e5933f4c0efc3e634f..937f1dc19b905a7e85799cd63c43a9a59c18f3dc 100644 (file)
@@ -53,7 +53,10 @@ def _weather(place):
                return "Weather is not enabled - please set the API key in the config file"
 
        if place is not None:
-               weather = json.load(urlopen(('http://api.weatherstack.com/current?access_key=%s&query=%s' % (lib.parent.cfg.get('weatherstack_weather', 'key'), quote_plus(place))).encode('utf8')))
+               url = 'http://api.weatherstack.com/current?access_key=%s&query=%s' % (lib.parent.cfg.get('weatherstack_weather', 'key'), quote_plus(place))
+               if sys.version_info.major < 3:
+                       url = url.encode('utf8')
+               weather = json.load(urlopen(url))
                if lib.parent.cfg.getboolean('debug', 'weather'):
                        lib.parent.log('*', "?", repr(weather))
                if 'error' in weather: