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/`.
if sys.version_info.major < 3:
timerbase = threading._Timer
+ stringbase = basestring
else:
+ import tempfile
timerbase = threading.Timer
+ stringbase = str
try:
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)
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):
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]]
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:
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])
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]
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():
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:
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: