Tidy loading facts and haikus.
authorCMDR furrycat <elite@furrycat.net>
Wed, 10 Oct 2018 08:33:01 +0000 (09:33 +0100)
committerCMDR furrycat <elite@furrycat.net>
Wed, 10 Oct 2018 08:33:01 +0000 (09:33 +0100)
plugin/facts/facts.py

index 3a3528d..00da32b 100644 (file)
@@ -1,25 +1,24 @@
 import asyncio
+import os
 import random
 
 from plugins import PluginCommand
 import bot
 
-
 class Facts(object):
   def __init__(self):
-    fFacts = open("catfacts.txt", "r")
-    self.catFacts = fFacts.readlines()
-    fFacts.close()
-    fHaiku = open("cat_haiku.txt", "r")
-    self.haikus = []
-    haiku = []
-    for line in fHaiku.readlines():
-      if line != "\n":
-        haiku.append(line)
-      else:
-        self.haikus.append(haiku)
-        haiku = []
-    fHaiku.close()
+    self.path = os.path.dirname(__file__)[(len(os.getcwd()) + 1):]
+    with open(os.sep.join([self.path, 'catfacts.txt']), 'r') as fd:
+      self.catFacts = fd.readlines()
+    with open(os.sep.join([self.path, 'cat_haiku.txt']), 'r') as fd:
+      self.haikus = []
+      haiku = []
+      for line in fd.readlines():
+        if line != "\n":
+          haiku.append(line.strip())
+        else:
+          self.haikus.append(haiku)
+          haiku = []
 
   def description(self):
     return 'Generates Cat Facts and Haikus'