summaryrefslogtreecommitdiffstats
path: root/silpa/modules/fortune/fortune.py
blob: cf31aad38e2fec745440d25c02a1a11286366409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Fortune
# -*- coding: utf-8 -*-
#
#  Copyright © 2008  Santhosh Thottingal
#  Released under the GPLV3+ license

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):
		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 = """
		<h2>Fortune Malayalam</h2></hr>
		<p>Enter the text for getting a random quote with the given string in the below text area.
		</p>
		<form action="" method="post">
		<input type="text" cols='100' name='input_text' id='id1' value="%s"/>
		<input  type="submit" id="Fortune" value="Fortune"  name="action" style="width:12em;"/>
		</br>
		</form>
		"""
		if(form.has_key('input_text')):
			text = form['input_text'].value	.decode('utf-8')
		else:
			text=""	
		response=response % text
		result = self.fortune_ml(text)
		response = response+"<h2>Random Quote</h2></hr>"
		response = response+"<b>"+result+"</b>"
		return response
	def get_module_name(self):
		return "Fortune Malayalam"
	def get_info(self):
		return 	"Get/Search a random Malayalam quote "
def getInstance():
	return Fortune()