-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.py
executable file
·144 lines (128 loc) · 5.01 KB
/
options.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 16 11:30:39 2015
@author: ardavan
"""
from misc import alphabet,finder
class Options(list):
#Creates and manages an option list
def __init__(self,lang="english",heading="",footing=""):
self.lang = lang
self.heading = heading
self.footing = footing
def add_Option(self,content,title=""):
ab = alphabet(self.lang);alpha= ab.retstr()
letter = alpha[len(self)]
line = [letter,title,content.decode('utf-8')]
self.append(line)
def print_Options(self):
strn="";
for i in self:
if i[1]:
strn+=i[0] + ") " + i[1] + " " + i[2] + "\n"
else:
strn+=i[0] + ") " + i[2]+ "\n"
return strn
def select_Option(self,option):
for j in range(len(self)):
if unicode(option.lower()) == self[j][0]:
return j
return None
def __list__(self):
return self
class Chunks(str):
def __init__(self,text):
self.size = 140
self.text = text
self.chunk_list=[]
self.position=0
def __str__(self):
if len(self.chunk_list)==0:
return ""
else:
return self.chunk_list[self.position%len(self.chunk_list)]
def create_Chunks(self,footing="",heading=""):
if len(heading)>0:
heading = heading+"\n"
if len(footing)>0:
footing = "\n"+footing
content=self.text; chunks = [];added = 0;i=0
if (len(self.text)+len(heading)+len(footing)) <= self.size:
self.chunk_list = [heading+content+footing]
return None
else:
chunksize = self.size - (len(footing)+9)
maxpos = (len(self.text)+len(heading))/chunksize
lastsize = (len(self.text)+len(heading))%chunksize
temp = heading + content[:(chunksize-len(heading))]
if len(temp)>(chunksize-len(heading)-20):
k = temp.find("\n",-14);finspa = len(temp.split()[-1])+1
if k > 0:
added +=(chunksize-k)
temp = temp[:k]
if len(temp) + 3 + len(footing)<=self.size:
chunks.append(temp+"..."+footing)
else:
chunks.append(temp+footing)
content = content[(k-len(heading)):]
elif temp.find(" ",-10) != -1:
added += (finspa)
temp = temp[:-(finspa)]
chunks.append(temp+footing)
content = content[(chunksize-finspa):]
elif len(temp)!=0:
chunks.append(temp+footing);content = content[chunksize:]
else:
chunks.append(temp+footing);content = content[(chunksize-len(heading)):]
if lastsize+added>chunksize:
maxpos = (len(self.text)+len(heading)+added)/chunksize
lastsize = (len(self.text)+len(heading)+added)%chunksize
added = 0
while i < maxpos+1:
temp = content[:chunksize]
if len(temp)>(chunksize-20):
k = temp.find("\n",-14);finspa = len(temp.split()[-1])+1
if k > 0:
added +=(chunksize-k)
temp = temp[:k]
if len(temp) + 3 + len(footing)<=self.size:
chunks.append(temp+"..."+footing)
else:
chunks.append(temp+footing)
content = content[k:]
elif temp.find(" ",-10) != -1:
added += (finspa)
temp = temp[:-(finspa)]
chunks.append(temp+footing)
content = content[(chunksize-finspa):]
elif len(temp)!=0:
chunks.append(temp+footing);content = content[chunksize:]
elif len(temp)!=0:
chunks.append(temp+footing);content = content[chunksize:]
if lastsize+added>chunksize:
maxpos = (len(self.text)+len(heading)+added)/chunksize
lastsize = (len(self.text)+len(heading)+added)%chunksize
added = 0
i+=1
counter=1;count = len(chunks)
for i, chunk in enumerate(chunks):
chunks[i] = chunk+"\n"+'%02d of %02d' % (counter, count)
counter = counter + 1
self.chunk_list = chunks
def find_Chunks(self,word):
chunks=[]
for i in range(1,len(self.chunk_list)):
findword = finder(r'{0}'.format(word),unicode(self.chunk_list[i]))
if findword.found():
chunks.append(self.chunk_list[i])
if len(chunks) > 0:
return chunks
else:
return False
def next_Chunk(self):
self.position+=1; return self
def goto_Chunk(self,number=1):
if 1 <= number <= len(self.chunk_list)+1:
self.position = number-1; return self
else:
return "Number out of range."