-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.py
162 lines (131 loc) · 3.9 KB
/
map.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
# TODO Utiliser prompt_toolkit et click pour gérer les commandes
import os
import re
running = True
nods_path = "/home/antharia/Dev/python/nod/"
nod_editor = "vim"
# CONFIG
# TODO read config file and update NOD variables
##############
# COMMANDS #
##############
# LIST
def list_nods():
'''
Returns a list of all nods in the nods directory
'''
nods_list = []
for root, dirs, files in os.walk(nods_path):
for file in files:
if (file.endswith(".nod")):
nods_list.append(file)
# print(file.split(".nod")[0])
return nods_list
def list_help():
'''
Print all arguments that can be used with list command
'''
print("list command accept those arguments :")
print("nods, links")
def list_links():
'''
Returns a dictionnary of all links used
'''
links_list = []
for root, dirs, files in os.walk(nods_path):
for file in files:
if (file.endswith(".nod")):
nod_file = open(os.path.join(nods_path, file), "rt")
while(True):
line = nod_file.readline()
if not line:
break
line = line.strip()
link_regex = '<[^<>\s]+>'
result = re.findall(link_regex, line)
if result:
links_list.append(result[0])
links_dict = list(dict.fromkeys(links_list))
for i in range(len(links_dict)):
print(links_list.count(links_dict[i]), ':', links_dict[i])
return links_dict
def list_links_by_nod(nod):
if not nod.endswith(".nod"):
nod = nod + ".nod"
nod_file = open(os.path.join(nods_path, nod), "rt")
nod_data = nod_file.read()
link_regex = '<[^<>\s]+>'
result = re.findall(link_regex, nod_data)
links_list = list(dict.fromkeys(result))
return links_list
def list_nods_by_link(link):
all_nods = list_nods()
nods_list = []
link = "<" + link + ">"
for n in all_nods:
links_list = list_links_by_nod(n)
if link in links_list:
nods_list.append(n)
return nods_list
# SHOW
def show_nod(nod):
nod = nod.lower()
nod = nod + ".nod"
nod_data = open(os.path.join(nods_path, nod), "rt")
while(True):
line = nod_data.readline()
if not line:
nod_data.close()
break
line = line.strip()
print(line)
nod_data.close()
# EDIT
def edit_nod(nod):
# nod = nod.lower()
nod = nod + ".nod"
nod_file = nods_path + nod
os.system(nod_editor + " " + nod_file)
# HELP
# TODO print the command list
#############
# CONSOLE #
#############
os.system("clear")
while (running):
command = input("MAP> ")
command = command.strip()
arguments = command.split(" ")
# EXIT command
if (arguments[0] == "exit"):
break
# LIST command
if (arguments[0] == "list"):
if len(arguments) == 1:
list_help()
else:
if (len(arguments) == 2):
if (arguments[1] in ("nods", "-n")):
all_nods = list_nods()
for n in all_nods:
print(n)
if (arguments[1] in ("links", "-l")):
list_links()
elif (len(arguments) == 3):
if (arguments[1] == "-n"):
links_list = list_links_by_nod(arguments[2])
for t in links_list:
print(t)
if (arguments[1] == "-l"):
nods_list = list_nods_by_link(arguments[2])
for n in nods_list:
print(n)
# SHOW command
if (arguments[0] == "show"):
if len(arguments) == 1:
pass
else:
show_nod(arguments[1])
# EDIT command
if (arguments[0] == "edit"):
edit_nod(arguments[1])