-
Notifications
You must be signed in to change notification settings - Fork 0
/
stoicumipsum.py
142 lines (120 loc) · 4.93 KB
/
stoicumipsum.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
import random, sys, subprocess, platform
try:
import pyperclip
except:
print("You need to install pyperclip to run this program")
subprocess.check_call([sys.executable, "-m", "pip3", "install", "pyperclip"])
import pyperclip
#config
#Minimal length of stoicum ipsum text
min_txt_lngth = 600
inputfile = "meditationes"
#booleans
linenumbererror = False
generalerror = False
def giveipsum(linenumber):
#to make the line number the same as its position in the list
linenumber-=1
#error handling
if linenumber < 0:
linenumbererror = True
raise Exception("Please use a line number over 0")
elif linenumber >= numberoflines:
linenumber = numberoflines-1
with open(inputfile, "r") as f:
lines = f.readlines()
return lines[linenumber]
txt = ""
#counts line number
with open(inputfile, "r") as f:
numberoflines = sum(1 for lines in f)
#checks if a line number was given
try:
option = sys.argv[1]
i = 1
temp = []
queue = []
#merge
if option == "-m" or option == "-ls" or option == "-w" or option == "-l" or option == "-r":
try:
while not (sys.argv[-i] == "-m" or sys.argv[-i] == "-ls" or sys.argv[-i] == "-w" or sys.argv[-i] == "-l" or sys.argv[-i] == "-r"):
temp.append(int(sys.argv[-i])); i+=1
for x in range(len(temp), 0, -1): queue.append(temp[x-1]);
#merge
if option == "-m":
for line in queue:
txt += giveipsum(line)
elif option == "-w":
wordcount = 0
if len(queue) == 1:
txt += giveipsum(random.randint(0, numberoflines))
elif len(queue) == 2:
txt += giveipsum(queue[1])
else:
raise Exception("You used one argument too much with the -w command")
for x, i in enumerate(txt):
if i == " " or i ==", " or i=="; " or i==". " or i==": ":
wordcount+=1
if wordcount == queue[0]:
txt = txt[:x]
#enlarge to or more than the minimum length
elif option == "-ls":
if len(queue) == 1:
x = 0
while len(txt) <= min_txt_lngth:
txt += giveipsum(queue[0]+x); x+=1
#in case you want a random line to have a minimum size
elif len(queue) == 0:
x = 0
linenumber = random.randint(0, numberoflines)
while len(txt) <= min_txt_lngth:
txt += giveipsum(linenumber+x); x+=1;
else:
raise Exception("You used one argument too much with the -ln command")
elif option == "-l":
if len(queue) == 1:
x = 0
min_txt_lngth = queue[0]
while len(txt) <= min_txt_lngth:
txt += giveipsum(random.randint(0, numberoflines)+x); x+=1
#in case you want a random line to have a minimum size
elif len(queue) == 0:
raise Exception("You have to specify the length")
else:
raise Exception("You used one argument too much with the -l command")
#range/merge of many ranges
elif option == "-r":
if len(queue) % 2 == 0:
x = len(queue)
y = 0
while x//2!=0:
x/=2;
if queue[y] < queue[y+1]:
for z in range(queue[y], queue[y+1]+1, 1): txt += giveipsum(z);
elif queue[y] > queue[y+1]:
for z in range(queue[y], queue[y-1]-1, -1): txt += giveipsum(z);
else: txt += giveipsum(queue[y]);
y+=2
else:
raise Exception("You specified a range with an odd number")
except:
if linenumbererror:
print("Please use a line number over 0")
else:
print("There was an error, you used the syntax of the command in the wrong way")
#standard/ only one line will be displayed
else:
#check if passed argument is a digit
if not sys.argv[1].isdigit():
print("There was an error, you used the syntax of the command in the wrong way")
generalerror = True
if not generalerror == True:
linenumber = int(sys.argv[1])
txt += giveipsum(linenumber)
except:
linenumber = random.randint(0, numberoflines)
txt += giveipsum(linenumber)
#output
if not generalerror == True:
print(txt)
pyperclip.copy(txt)