-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.py
136 lines (116 loc) · 3.83 KB
/
tools.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
import os
import copy
import nulldate
#just some of my standard functions that i use everywhere
def clone(var):
return copy.deepcopy(var)
#just some of my standard functions that i use everywhere
def inp(msg, lower=False):
usr = ''
print (msg)
while True:
txt = input()
if txt == '':
break
usr += txt
if usr:
while usr[-1] == ' ':
usr = usr[:-1]
if lower:
usr = usr.lower()
return usr
#create and write to a file
def createFile(path, filename, contents=None):
#create directory if not exists
if not os.path.exists(path):
os.makedirs(path)
#add the path together
fPath = path + '/' + filename
#create the file. (if it already exists append an integer to the end of file name)
i = 2
if os.path.exists(fPath + ".txt"):
fPath += str(i)
while os.path.exists(fPath):
i += 1
fPath = path + '/' + filename + str(i)
file = open(fPath + ".txt", 'a')
#write to file if needed
if contents:
for c in contents:
file.write(c + "\n")
print ("Your citation has been generated to " + fPath + ".txt")
file.close()
#get author function (it lives here because I originally wanted to do APA as well as MLA but i ran out of time)
def getAuthors(cFormat):
authors = []
#if the format is mla which it always is :/
if cFormat == "mla":
#while loop to make sure a correct input is returned
while True:
numAuthors = inp("How many authors are there? ")
try:
numAuthors = int(numAuthors)
except Exception:
numAuthors = -1
if numAuthors == 0:
authors.append('')
return authors
elif numAuthors == 1:
a = inp("Enter author name")
authors.append(a)
return authors
elif numAuthors == 2:
a = inp("Enter primary author name")
authors.append(a)
a = inp("Enter secondary author name")
authors.append(a)
return authors
elif numAuthors > 2:
a = inp ("Enter primary author name")
authors.append(a)
authors.append("et al")
return authors
else:
print ("Please enter a valid number of authors")
#get date function returns a nulldate.Date data type
def getDate(cFormat, endPunc):
if cFormat == "mla":
while True:
usrDate = inp("Enter date (enter 'nd' or 'no date' if date is missing)")
date = nulldate.parser(usrDate)
if date.year or date.nodate:
break
else:
print ("Missing year")
strDate = ""
if not date.nodate:
if date.year:
strDate = str(date.year) + endPunc + ' '
if date.month:
strDate = date.strMonth + ' ' + strDate
if date.day:
strDate = str(date.day) + ' ' + strDate
return strDate
#get pages. returns a list of starting and ending page
def getPages():
#get starting page
while True:
pgs = inp("Enter start page (enter 'np' or 'no page' if page numbers are missing)")
if pgs == "np" or pgs == "no page":
return None
try:
pgs = int(pgs)
break
except:
print ("Enter a valid number")
#get ending page
while True:
pge = inp("Enter end page (enter 'np' or 'no page' if page numbers are missing)")
if pge == "np" or pge == "no page":
return None
try:
pge = int(pge)
break
except:
print ("Enter a valid number")
return [pgs, pge]