-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
107 lines (87 loc) · 2.25 KB
/
utils.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
'''
My Utils
Written by Z. Duputel, September 2013
'''
# Externals
from os.path import exists,isdir
from numpy import ndarray
import shutil
import os
import re
def grep(chaine, file):
"""
Returns lines in file matching chaine
"""
out = [];
template = re.compile(chaine)
fid = open(file, 'r')
for line in fid:
if template.match(line):
out.append(line)
fid.close()
# All done
return(out)
def rm(ifiles):
'''
Remove file(s) or directory(ies)
Args:
ifiles: file, directory or list of files, directories
'''
if type(ifiles)!=list and type(ifiles)!=ndarray:
ifiles = [ifiles]
for ifile in ifiles:
if exists(ifile):
if isdir(ifile):
shutil.rmtree(ifile)
else:
os.remove(ifile)
# All done
return
def mkdir(idir,pflag=False):
'''
Create directory
Args:
idir: directory to be created
pflag: (optional, default=False)
False: will remove directory if exists
True: will NOT remove directory if exists
'''
if pflag and not exists(idir):
os.mkdir(idir)
else:
rm(idir)
os.mkdir(idir)
# All done
return
class parseConfigError(Exception):
"""
Raised if the config file is incorrect
"""
pass
def parseConfig(cfg_file):
'''
Parse my config files and returns a config dictionary
Args:
cfg_file: configuration filename
'''
# Check if cfg_file exists
assert exists(cfg_file), 'Cannot read %s: No such file'%(cfg_file)
# Fill the config dictionary
config = {}
try:
config_lines = open(cfg_file, 'r').readlines()
for line in config_lines:
if line.find('#')==0:
continue
if line.rstrip():
key,value = line.strip().split(':')
key = key.strip()
value = value.strip()
if key in config:
config[key].append(value)
else:
config[key]=value
except:
raise parseConfigError('Incorrect format in %s!\n'%cfg_file)
# All done
return config