-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.py
More file actions
79 lines (71 loc) · 3.23 KB
/
delete.py
File metadata and controls
79 lines (71 loc) · 3.23 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
# coding=utf-8
import os
import re
import auxiliar
def op_delete(order, path):
regex = re.compile(r'^ *delete +(:?"(?P<name_file>.*)"|'
r'(?P<all_activo>all+(:? +where +'
r'(:?(:?name +is +"(?P<is_file>.+)"|'
r'name +contains +"(?P<contains_file>.+)")|'
r'tag +is +"(?P<tag_name>.+)"))?))'
r'(:? +in +/(?P<route>(.*))/*)? *$')
trash = []
default_route = os.getcwd() + '/'
if regex.match(order):
# Se verifica si se produjo un match, de
# lo contrario retorna comando incorrecto.
route = regex.match(order).group('route')
name = regex.match(order).group('name_file')
is_active = regex.match(order).group('is_file')
contains_active = regex.match(order).group('contains_file')
tag_active = regex.match(order).group('tag_name')
if route:
if not os.path.exists(path):
print "Error: This PATH does not exists"
return
if re.match(r'home/.+', route):
default_route = '/' + route + '/'
else:
default_route = default_route + route + '/'
if regex.match(order).group('all_activo'):
if is_active: # Trabaja unicamente cuando es invocado "is"
filelist = [f for f in os.listdir(default_route)
if f == is_active + ".lpy"]
for f in filelist:
os.remove(default_route + f)
trash.append(default_route + f)
elif contains_active:
# Trabaja unicamente cuando es invocado "contains_file"
filelist = [f for f in os.listdir(default_route)
if contains_active in
f.strip('.lpy').split() and f.endswith('.lpy')]
for f in filelist:
os.remove(default_route + f)
trash.append(default_route + f)
elif tag_active:
# Eliminara todos los archivos que
# contengan un tag en especial del directorio actual
metadata = open(path + '/.metadata')
for line in metadata:
aux = line.strip().split("|")
if tag_active in re.findall(r'"(\w+)"', aux[4]):
if default_route == aux[1] + '/':
os.remove(default_route + aux[0] + '.lpy')
trash.append(default_route + aux[0])
metadata.close()
else: # esto borra todos los archivos que terminan con .lpy
filelist = [f for f in os.listdir(default_route)
if f.endswith(".lpy")]
for f in filelist:
os.remove(default_route + f)
trash.append(default_route + f)
elif name: # Es el encargado de borrar unicamente un archivo
try:
os.remove(default_route + name + '.lpy')
trash.append(default_route + name)
except OSError:
pass
auxiliar.bd_edit(trash, path, "clear")
else:
print "Error: Incorrect command"
return