/
filesystem.py
123 lines (90 loc) · 2.47 KB
/
filesystem.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
import os
import sys
import shutil
import inspect
import glob
import __main__
class FileReport(object):
def __init__(self, file_name):
self.file_name = file_name
self.handle = open(file_name, 'w', 1)
def add(self, *args):
text = '{} ' * len(args) + '\n'
self.handle.write(text.format(*args))
def close(self):
self.handle.close()
def tailNumber(text):
st = ""
for c in text[::-1]:
if c.isdigit():
st = c + st
else:
break
return st, text[:-len(st)]
def incrementName(path):
number, raw_name = tailNumber(path)
if len(number):
new_name = raw_name + str(int(number) + 1)
else:
new_name = path + '_0'
return new_name
def mainFileName(full=False):
path = __main__.__file__
path = os.path.split(path)[1]
if not full:
path = os.path.splitext(path)[0]
return path
def mainFilePath():
import __main__
return os.path.split(__main__.__file__)[0]
def mkd(path, increment=True):
if path[-1] == '/':
path = path[:-1]
if increment:
temp_path = path
i = 0
while os.path.exists(temp_path) and len(os.listdir(temp_path)) > 0:
# path = incrementName(path + path_2)
temp_path = path + "_%i" % i
i += 1
path = temp_path
mkd(path, increment=False)
elif not os.path.exists(path):
os.makedirs(path)
return path
def rmd(path):
shutil.rmtree(path)
def rm(path):
os.remove(path)
def here():
frame = inspect.stack()
module = inspect.getmodule(frame[1][0])
return os.path.dirname(os.path.realpath(module.__file__))
def ls(dir, extention='*.*'):
return glob.glob(os.path.join(dir, extention))
class Arguments(object):
def __init__(self):
self.flags = {}
self.options = {}
def flag(self, *args, **kwargs):
self.flag.update(kwargs)
for arg in args:
self.flags[arg] = False
def options(self, *args, **kwargs):
self.options.update(kwargs)
for arg in args:
self.options[arg] = 0
def parse(self):
self.argv = sys.argv[1:]
def __call__(self, arg):
if arg in self.options:
return self.options[arg]
elif arg in self.flags:
return self.flags[arg]
if __name__ == "__main__":
# print(here())
# print(ls(here()))
a = Arguments()
a.flag('verbose')
print(a.flags)
a.parse()