-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_files.py
More file actions
198 lines (166 loc) · 5.72 KB
/
read_files.py
File metadata and controls
198 lines (166 loc) · 5.72 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
YES = frozenset({"y", "Y", "yes", "Yes", "YES"})
def main():
dirty = False
items = []
filename, items = choose_file()
if not filename:
print("Cancelled")
return
while True:
print("\nList Keeper\n")
print_list(items)
choice = get_choice(items, dirty)
if choice in "Aa":
dirty = add_item(items, dirty)
elif choice in "Dd":
dirty = delete_item(items, dirty)
elif choice in "Ss":
dirty = save_list(filename, items)
elif choice in "Qq":
if (dirty and (get_string("Save unsaved changes (y/n)",
"yes/no", "y") in YES)):
save_list(filename, items, True)
break
def choose_file():
enter_filename = False
print("\nList Keeper\n")
files = [x for x in os.listdir(".") if x.endswith(".lst")]
if not files:
enter_filename = True
if not enter_filename:
print_list(files)
index = get_integer("Specify file's number (or 0 to create "
"a new one)", "number", maximum=len(files),
allow_zero=True)
if index == 0:
enter_filename = True
else:
filename = files[index - 1]
items = load_list(filename)
if enter_filename:
filename = get_string("Choose filename", "filename")
if not filename.endswith(".lst"):
filename += ".lst"
items = []
return filename, items
def print_list(items):
if not items:
print("-- no items are in the list --")
else:
width = 1 if len(items) < 10 else 2 if len(items) < 100 else 3
for i, item in enumerate(items):
print("{0:{width}}: {item}".format(i + 1, **locals()))
print()
def get_choice(items, dirty):
while True:
if items:
if dirty:
menu = "[A]dd [D]elete [S]ave [Q]uit"
valid_choices = "AaDdSsQq"
else:
menu = "[A]dd [D]elete [Q]uit"
valid_choices = "AaDdQq"
else:
menu = "[A]dd [Q]uit"
valid_choices = "AaQq"
choice = get_string(menu, "choice", "a")
if choice not in valid_choices:
print("ERROR: invalid choice--enter one of '{0}'".format(
valid_choices))
input("Press Enter to continue...")
else:
return choice
def add_item(items, dirty):
item = get_string("Add item", "item")
if item:
items.append(item)
items.sort(key=str.lower)
return True
return dirty
def delete_item(items, dirty):
index = get_integer("Delete item number (or 0 to cancel)",
"number", maximum=len(items),
allow_zero=True)
if index != 0:
del items[index - 1]
return True
return dirty
def load_list(filename):
items = []
fh = None
try:
for line in open(filename, encoding="utf8"):
items.append(line.rstrip())
except EnvironmentError as err:
print("ERROR: failed to load {0}: {1}".format(filename, err))
return []
finally:
if fh is not None:
fh.close()
return items
def save_list(filename, items, terminating=False):
fh = None
try:
fh = open(filename, "w", encoding="utf8")
fh.write("\n".join(items))
fh.write("\n")
except EnvironmentError as err:
print("ERROR: failed to save {0}: {1}".format(filename, err))
return True
else:
print("Saved {0} item{1} to {2}".format(len(items),
("s" if len(items) != 1 else ""), filename))
if not terminating:
input("Press Enter to continue...")
return False
finally:
if fh is not None:
fh.close()
def get_string(message, name="string", default=None,
minimum_length=0, maximum_length=80):
message += ": " if default is None else " [{0}]: ".format(default)
while True:
try:
line = input(message)
if not line:
if default is not None:
return default
if minimum_length == 0:
return ""
else:
raise ValueError("{0} may not be empty".format(
name))
if not (minimum_length <= len(line) <= maximum_length):
raise ValueError("{name} must have at least "
"{minimum_length} and at most "
"{maximum_length} characters".format(
**locals()))
return line
except ValueError as err:
print("ERROR", err)
def get_integer(message, name="integer", default=None, minimum=0,
maximum=100, allow_zero=True):
class RangeError(Exception): pass
message += ": " if default is None else " [{0}]: ".format(default)
while True:
try:
line = input(message)
if not line and default is not None:
return default
i = int(line)
if i == 0:
if allow_zero:
return i
else:
raise RangeError("{0} may not be 0".format(name))
if not (minimum <= i <= maximum):
raise RangeError("{name} must be between {minimum} "
"and {maximum} inclusive{0}".format(
" (or 0)" if allow_zero else "", **locals()))
return i
except RangeError as err:
print("ERROR", err)
except ValueError as err:
print("ERROR {0} must be an integer".format(name))
main()