summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsand <daniel@spatof.org>2013-05-13 16:20:53 (GMT)
committer sand <daniel@spatof.org>2013-05-13 16:20:53 (GMT)
commit60bf9dccbabb162ee30931c018f83385434d113d (patch)
tree36c3d99808dff6418a41ef0f169b32aab0cfd815
parent03946d6cc2853217cc6c7830627fd285775ef0cf (diff)
Refactoring prcd, pulizia
-rw-r--r--pinolo/prcd.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/pinolo/prcd.py b/pinolo/prcd.py
index 4707262..40f1206 100644
--- a/pinolo/prcd.py
+++ b/pinolo/prcd.py
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
-
import codecs
import pkg_resources
import logging
import random
-logger = logging.getLogger('pinolo.prcd')
+
+log = logging.getLogger('pinolo.prcd')
PRCD_FILES = [
'prcd_cri.txt',
@@ -17,6 +17,7 @@ PRCD_FILES = [
'prcd_vsf.txt',
]
+
def read_prcd_files():
"""
Legge i file PRCD e popola un `dict`.
@@ -24,24 +25,27 @@ def read_prcd_files():
operativo in uso.
"""
moccoli = {}
-
- logger.debug("Reading PRCD files")
for filename in PRCD_FILES:
category = filename[filename.index('_')+1:filename.index('.')]
category = unicode(category, 'utf-8', 'replace')
# fd = pkg_resources.resource_stream(__name__, "data/prcd/%s" % filename)
path = pkg_resources.resource_filename(__name__, "data/prcd/%s" % filename)
- fd = codecs.open(path, encoding='utf-8')
- lines = fd.readlines()
- moccoli[category] = lines
- fd.close()
+ try:
+ with codecs.open(path, "rb", encoding="utf-8") as fd:
+ lines = fd.readlines()
+ moccoli[category] = lines
+ except IOError:
+ pass
return moccoli
+
moccoli = read_prcd_files()
prcd_categories = moccoli.keys()
+
def moccolo_random(category=None):
if not category:
category = random.choice(moccoli.keys())
- if not category in moccoli: return (None, None)
+ if not category in moccoli:
+ return (None, None)
return (category, random.choice(moccoli[category]))