-
Notifications
You must be signed in to change notification settings - Fork 43
/
Filist.py
159 lines (127 loc) · 4.75 KB
/
Filist.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
A class for reading the contents of a file, modifying it in
memory, and writing it back.
A Filist is a list of strings that contains the contents of
a file. All the usual list operations can be applied to a Filist.
Because this class loads the entire file into memory, it uses
more space than it might need to, and it is limited to working with
files that fit into memory. The advantage is that it is easy to
implement algorithms that need random access to the contents of the
file, and it is convenient to debug because all the data is
available all the time.
Some of the methods I included are here because of specific
operations I was interested in.
Copyright 2012 Allen B. Downey
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see http://www.gnu.org/licenses/gpl.html
or write to the Free Software Foundation, Inc., 51 Franklin St,
Fifth Floor, Boston, MA 02110-1301 USA
"""
import sys
import re
class Filist(list):
def __init__(self, filename=None, t=None):
""" if a filename is provided, read the contents """
if filename:
self.filename = filename
lines = open(filename).readlines()
self.extend(lines)
# add any lines that are provided
if t:
self.extend(t)
def __str__(self):
return ''.join(self)
def join(self):
"""collapse the list of strings into a single long string"""
self[:] = [str(self)]
def search_lines(self, pattern):
"""traverse lines in order until one of them matches,
return the index and the match object
"""
pat = re.compile(pattern)
for i in range(0, len(self)):
match = pat.search(self[i])
if match:
return i, match
def sub_lines(self, pattern, replace, start=0):
"""
"""
pat = re.compile(pattern)
for i, line in enumerate(self):
if i < start:
continue
line, n = pat.subn(replace, self[i])
if n:
self[i] = line
def move_indexterms(self):
"""Moves index entries from section headings into a paragraph."""
i = 0
while i < len(self):
if not (self[i].startswith('\\section') or
self[i].startswith('\\subsection') or
self[i].startswith('\\begin{exercise}')):
i += 1
continue
i += 1
while self[i].startswith('\\label'):
i += 1
begin = i
while self[i].startswith('\\index'):
i += 1
end = i
if begin == end:
continue
# shift the lines
j = end
while j > begin:
self[j] = self[j-1]
j -= 1
self[begin] = '\n'
def decapitate(self, pattern='<BODY >\n'):
"""find the first line that matches the pattern;
remove it and all the previous lines
"""
i = self.index(pattern)
del self[:i+1]
def depeditate(self, pattern='</BODY>\n'):
"""find the first line that matches the pattern;
remove it and all the following lines
"""
i = self.index(pattern)
del self[i:]
def prefile(self, filename):
"""prepend the contents of the given file"""
ft = Filist(filename)
self.insert(0, ft)
def suffile(self, filename):
"""append the contents of the given file"""
ft = Filist(filename)
self.extend(ft)
def writeto(self, filename):
"""write the contents of the Filist to a file"""
fp = open(filename, 'w')
for line in self:
fp.write(line)
def writeback(self):
"""write the contents of the Filist back to the file it came from"""
self.writeto(self.filename)
def change_suffix(filename, suffix):
"""return a new filename that is the same as the given name
with the file extension replaced with the given suffix
"""
filename.split('.')
t[-1] = suffix
return '.'.join(t)
def cp(source, dest):
"""copy a file from source to destination, returning a Filist"""
ft = Filist(source)
ft.writeto(dest)
return ft