summaryrefslogtreecommitdiffstats
path: root/silpa/modules/fortune
diff options
context:
space:
mode:
authoraashiks <aashiks@gmail.com>2009-04-30 12:40:23 +0530
committeraashiks <aashiks@gmail.com>2009-04-30 12:40:23 +0530
commit560d36aa534a458eee1663e56bf600286c802c6d (patch)
tree3d6ef34d17bf6fd87e9ab7f842a221415e341c9b /silpa/modules/fortune
parent0bc0f508ebba733e8fe8430cbd4aab9e74fdcb69 (diff)
parent101ae75439d5e33e20b11243426028956abe93ad (diff)
downloadRachana.git-560d36aa534a458eee1663e56bf600286c802c6d.tar.gz
Rachana.git-560d36aa534a458eee1663e56bf600286c802c6d.tar.xz
Rachana.git-560d36aa534a458eee1663e56bf600286c802c6d.zip
Merge branch 'master' of ssh://aashiks@git.sv.nongnu.org/srv/git/smc
Diffstat (limited to 'silpa/modules/fortune')
-rw-r--r--silpa/modules/fortune/fortune.py39
1 files changed, 27 insertions, 12 deletions
diff --git a/silpa/modules/fortune/fortune.py b/silpa/modules/fortune/fortune.py
index e7aac98..cf31aad 100644
--- a/silpa/modules/fortune/fortune.py
+++ b/silpa/modules/fortune/fortune.py
@@ -1,22 +1,37 @@
-# Spellchecker with language detection
-# coding: utf-8
+# Fortune
+# -*- coding: utf-8 -*-
#
# Copyright © 2008 Santhosh Thottingal
# Released under the GPLV3+ license
-import os
+import os,random
from common import *
class Fortune(SilpaModule):
+ def fortunes(self,infile,pattern=None):
+ """ Yield fortunes as lists of lines """
+ result = []
+ for line in infile:
+ line=line.decode("utf-8")
+ if line == "%\n":
+ yield result
+ result = []
+ else:
+ if(pattern==None):
+ result.append(line)
+ else:
+ if(line.find(pattern)==-1):
+ result.append(line)
+ if result:
+ yield result
+
def fortune_ml(self, word):
- if(word>""):
- command = "/usr/games/fortune -m " + word + " ./modules/fortune/database/fortune-ml"
- else:
- command = "/usr/games/fortune ./modules/fortune/database/fortune-ml"
- command=command.encode('utf-8')
- pipe = os.popen('{ ' + command + '; } 2>&1', 'r')
- text = pipe.read().decode('utf-8')
- pipe.close()
- return text
+ filename="./modules/fortune/database/fortune-ml"
+ """ Pick a random fortune from a file """
+ for index, fortune in enumerate(self.fortunes(file(filename),None)):
+ if random.random() < (1.0 / (index+1)):
+ chosen = fortune
+
+ return "".join(chosen)
def process(self, form):
response = """