-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccd-src.py
executable file
·397 lines (349 loc) · 12.8 KB
/
ccd-src.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/python3
"""Advanced version of cd program. Use ccd --help."""
import configparser
import sys
from os import chdir
from os import environ
from os import execl
from os import listdir
from os.path import isdir
from os.path import join as osjoin
from re import findall as refindall
def helpshow():
"""Show The Help Page."""
helptext = """ CuteCD Python. Another chdir version written with Python.
Will find directory and make chdir to it for you.
If there will be more than one found dir, you will be able to chose.
SYNOPSIS:
\tccd [search name or pattern] [keys]
KEYS:
-h --help -? --? This help page
-d\t\tset start search point. Default -- $HOME value
-f\t\tmake chdir to the first found directory, will be no choice
-p\t\tinterpretate [name] as a regular search pattern.
\t\tDefault -- [name] must coincide with dir name.
-l\t\tcase unsensitive search. Default -- case sensitive
-a\t\tsearch in hidden directories. Default -- dont search in hidden
-r < value > \tset search depth limit.
\tDo not set big values. Default -- 100
-c\t\tuse specified config.
\t\tDefault config stored in $HOME/.config/ccd.conf
"""
print(helptext)
exit(0)
def configure(args, env, actual_version):
"""
Read config and apply program variables.
:param args: list of keys ans flags. Use sys.argv
:param env: dict of environment variables as dict. Use os.environ
:param actual_version: str config version. Hard-coded in main. Needs to
update config if needed
"""
start_point = env['HOME']
search_point = None
search_by_pattern = False
search_hidden = False
end_at_first = False
recursion_limit = 15
lowercase = False
excluded_dirs = "/dev:/run/udev:/proc:/sys"
exclude_pattern = r"/\.*cache/*|/s*bin/*"
config = ""
found_config = 0
config_exist = True
current_version = ''
if args.count('-c') == 0:
config = "{}/.config/ccd.conf".format(env['HOME'])
# default_config = True
found_config = 0
else:
try:
config = args[args.index('-c') + 1]
# default_config = False
found_config = 1
except IndexError:
print('Timid denial to read nameless config')
exit(1)
# try:
try:
config_exist = True
with open(config, 'r') as c:
current_version = c.readlines()[-1][:-1]
except FileNotFoundError:
config_exist = False
if (not config_exist and found_config == 0) or (current_version != actual_version):
print('Creating new config file')
# try:
# os.mkdir("{}/.config/ccd.conf".format(env['HOME']))
# except FileExistsError:
# pass
cfgp = configparser.ConfigParser()
cfgp.add_section("Defaults")
cfgp.set("Defaults", "start_point", start_point)
# cfgp.set("Defaults", "search_point", search_point)
cfgp.set("Defaults", "search_by_pattern", str(search_by_pattern))
cfgp.set("Defaults", "include_hidden_files", str(search_hidden))
cfgp.set("Defaults", "search_til_first", str(end_at_first))
cfgp.set("Defaults", "max_depth", str(recursion_limit))
cfgp.set("Defaults", "case_sensitive", str(not lowercase))
cfgp.add_section("Rules")
cfgp.set("Rules", "excluded_dirs", excluded_dirs)
cfgp.set("Rules", "exclude_pattern", exclude_pattern)
with open(config, 'w') as c:
cfgp.write(c)
c.write("\n{}\n".format(actual_version))
else:
cfgp = configparser.ConfigParser()
cfgp.read(config)
try:
start_point = cfgp.get("Defaults", "start_point")
# search_point = cfgp.get("Defaults", "search_point")
search_by_pattern = cfgp.get(
"Defaults", "search_by_pattern") == 'True'
search_hidden = cfgp.get(
"Defaults", "include_hidden_files") == 'True'
end_at_first = cfgp.get(
"Defaults", "search_til_first") == 'True'
recursion_limit = int(cfgp.get("Defaults", "max_depth"))
lowercase = cfgp.get("Defaults", "case_sensitive") == 'False'
excluded_dirs = cfgp.get("Rules", "excluded_dirs")
exclude_pattern = cfgp.get("Rules", "exclude_pattern")
except configparser.NoSectionError:
raise FileNotFoundError(
'Invalid config file {}'.format(config))
# exit(1)
# with open(config, 'r') as c:
# values = c.read()
# print(values)
# HERE I MUST EXEC CONFIG VALUES!
# except FileNotFoundError:
# print('{} was not found. use defaults'.format(config))
# found_config = 1
config = {}
config['stp'] = start_point
config['sep'] = search_point
config['sbp'] = search_by_pattern
config['seh'] = search_hidden
config['eaf'] = end_at_first
config['rel'] = recursion_limit
config['lwc'] = lowercase
config['exd'] = excluded_dirs
config['exp'] = exclude_pattern
config['fcf'] = found_config
return config
def parseargs(args, env, config):
"""
Parse cmd arguments stored in args.
:param args: list of keys and flags. Use os.argv
:param env: dict of environment variables as dict. Use os.environ
"""
if sum(args.count(t) for t in ['-h', '--help', '-?', '--?']) > 0:
return True, None, None, None, None, None, None, None, None, None
start_point = config['stp']
search_point = config['sep']
search_by_pattern = config['sbp']
search_hidden = config['seh']
end_at_first = config['eaf']
recursion_limit = config['rel']
lowercase = config['lwc']
excluded_dirs = config['exd']
exclude_pattern = config['exp']
found_config = config['fcf']
found_search = False
i = 0
while i < len(args):
arg = args[i]
try:
if arg == '-d':
start_point = args[i + 1]
i += 2
continue
elif arg == '-c':
if found_config == 0:
raise RuntimeError('все пошло нахрен!!!!!')
elif found_config < 2:
found_config += 1
i += 2
continue
else:
print('Config was been already specified by -c flag')
exit(1)
elif arg == '-f':
end_at_first = True
elif arg == '-p':
search_by_pattern = True
elif arg == '-l':
lowercase = not lowercase
elif arg == '-r':
try:
recursion_limit = int(args[i + 1])
i += 2
except ValueError:
print('Timid denial to use flag {} with value {}'.format(
args[i], args[i + 1]))
exit(1)
continue
elif arg == '-a':
search_hidden = True
elif found_search:
start_point = args[i]
if start_point[0] == '.':
search_hidden = True
else:
search_point = args[i]
found_search = True
if search_point[0] == '.':
search_hidden = True
except IndexError:
print('Timid remark: unused flag {}'.format(args[i]))
exit(1)
i += 1
if not found_search:
print('Timid denial to find a nameless folder')
exit(1)
return (False,
start_point,
search_point,
end_at_first,
search_by_pattern,
search_hidden,
recursion_limit,
lowercase,
excluded_dirs,
exclude_pattern)
def searchdepth(search_dir, search_point, end_at_first,
search_by_pattern, search_hidden, lowercase,
excluded_dirs, exclude_pattern, already_found, recursion):
"""
Recursive function to depth search.
:param search_dir: str destination directory name or pattern
:param search_point: str directory where to search at this iteration
:param end_at_first: bool will stop search at first found directory
:param search_by_pattern: bool interprete search_dir ad pattern
:param search_hidden: bool include also hidden files to search
:param lowercase: bool same as case-sensitive
:param already_found: internal variable. Some dirs were already found
:param recursion: internal variable. Need to set search depth limit
"""
if (end_at_first and already_found) or (recursion <= 0):
return []
e_d = excluded_dirs.split(':')
try:
dir_list = []
for f in listdir(path=search_dir):
path = osjoin(search_dir, f)
if isdir(path) and (path not in e_d) and (len(refindall(exclude_pattern, path)) == 0):
dir_list.append(f)
except PermissionError:
return []
if not search_hidden:
dir_list = [f for f in dir_list if f[0] != '.']
if search_by_pattern and lowercase:
found_list = [f for f in dir_list if len(
refindall(search_point.lower(), f.lower())) > 0]
elif search_by_pattern:
found_list = [f for f in dir_list if len(
refindall(search_point, f)) > 0]
elif lowercase:
found_list = [f for f in dir_list if f.lower() == search_point.lower()]
else:
found_list = [f for f in dir_list if f == search_point]
# if not search_hidden:
# found_list = [f for f in found_list if f[0] != '.']
if len(found_list) > 0 and end_at_first:
already_found = True
return [osjoin(search_dir, sorted(found_list)[0])]
elif len(found_list) > 0:
already_found = True
for d in dir_list:
found_list += searchdepth(
osjoin(search_dir, d),
search_point,
end_at_first,
search_by_pattern,
search_hidden,
lowercase,
excluded_dirs,
exclude_pattern,
already_found,
recursion - 1)
return [osjoin(search_dir, f) for f in found_list]
def request_chdir(dir_list, end_at_first, key):
"""
Make chdir to directories.
:param dir_list: list of found dirs to ask user in which to cd
:param end_at_first: bool auto make cd in first dir without asking
:param key: function sorting type
"""
if len(dir_list) == 0:
print('...nothing')
exit(1)
if end_at_first or len(dir_list) == 1:
chdir(dir_list[0])
execl(environ['SHELL'], environ['SHELL'].split('/')[-1])
exit(0)
dir_list = sorted(dir_list, key=key)
keys = list(range(len(dir_list)))
# requests = dict(zip(keys, dir_list))
for k in keys:
print("({}) {}".format(k, dir_list[k]))
while True:
seq = input('Which do you need? ({}): '.format(keys[-1]))
if seq == '':
seq = keys[-1]
else:
try:
seq = int(seq)
except ValueError:
if seq in ['q', 'Q']:
exit(0)
else:
continue
try:
chdir(dir_list[seq])
execl(environ['SHELL'], environ['SHELL'].split('/')[-1])
exit(0)
# except KeyError: # if use dictionary
# continue
except IndexError: # if use list
continue
def key_default_reversed(x):
"""
Sotring algorithm for request_chdir().
It sorts full directories paths for comfortable output, where
longer paths are moved into the beginning of the list.
"""
try:
hid_dep = len(x) - x.index('/.')
except ValueError:
hid_dep = 0
return (-1) * (x.count('/') + x.count('/.') * hid_dep)
try:
config = configure(sys.argv[1:], environ, 'ConfigVersion = v1.0')
(
helppage,
start_point,
search_point,
end_at_first,
search_by_pattern,
search_hidden,
recursion_limit,
lowercase,
excluded_dirs,
exclude_pattern
) = parseargs(sys.argv[1:], environ, config)
if helppage:
helpshow()
found_dirs = searchdepth(start_point,
search_point,
end_at_first,
search_by_pattern,
search_hidden,
lowercase,
excluded_dirs,
exclude_pattern,
False,
recursion_limit)
request_chdir(found_dirs, end_at_first, key_default_reversed)
except KeyboardInterrupt:
sys.exit(1)