forked from ITHACA-FV/ITHACA-FV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.py
More file actions
executable file
·106 lines (82 loc) · 2.42 KB
/
files.py
File metadata and controls
executable file
·106 lines (82 loc) · 2.42 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
import os
import re
import shutil
from tempfile import mkstemp
def sed(what, pattern, replace, source, dest=None):
"""Reads a source file and writes the destination file.
In each line, replaces pattern with replace.
Args:
what (str): pattern of the line to be replaced
pattern (str): pattern to match (can be re.pattern)
replace (str): replacement str
source (str): input filename
dest (str): destination filename, if not given, source will be over written.
"""
fin = open(source, 'r')
if dest:
fout = open(dest, 'w')
else:
fd, name = mkstemp()
fout = open(name, 'w')
for line in fin:
if what in line:
out = re.sub(pattern, replace, line)
fout.write(out)
else:
fout.write(line)
try:
fout.writelines(fin.readlines())
except Exception as E:
raise E
fin.close()
fout.close()
fout.close()
if not dest:
shutil.move(name, source)
def sed_variable(what, source, new_value, dest=None):
"""Reads a source file and writes the destination file.
In each line, replaces pattern with replace.
Args:
what (str): pattern of the line to be replaced
source (str): input filename
new_value (str): replacement str
dest (str): destination filename, if not given, source will be over written.
"""
fin = open(source, 'r')
if dest:
fout = open(dest, 'w')
else:
fd, name = mkstemp()
fout = open(name, 'w')
for line in fin:
if what in line:
a = line.split();
a[1] = str(new_value)+";\n"
l = " ".join(str(x) for x in a)
fout.write(l)
else:
fout.write(line)
try:
fout.writelines(fin.readlines())
except Exception as E:
raise E
fin.close()
fout.close()
fout.close()
if not dest:
shutil.move(name, source)
def read_variable(what, source):
"""Reads a source file and returns the value of a variable.
Args:
what (str): pattern of the line to be returned
source (str): input filename.
"""
fin = open(source, 'r')
fd, name = mkstemp()
for line in fin:
if what in line:
a = line.split();
return(int(a[1][:-1]))
fin.close()
if not dest:
shutil.move(name, source)