-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversion.py
More file actions
138 lines (109 loc) · 3.35 KB
/
conversion.py
File metadata and controls
138 lines (109 loc) · 3.35 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
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
import utils
import commands
class filewrapper:
def __init__(self, string: str):
self.string = string
self.charptr = 0
self.char = string[0]
def checkcharptr(self):
if self.charptr == len(self.string):
return False
return True
def nextchar(self):
if self.checkcharptr():
self.charptr += 1
self.char = self.string[self.charptr]
return self.string[self.charptr]
return None
def currchar(self):
return self.string[self.charptr]
def lastchar(self):
return self.string[self.charptr - 1]
class Node:
def __init__(self, text="", alias="", childs=None):
if childs is None:
childs = []
self.childs = childs
self.text = text
self.alias = alias
def reset(self):
self.childs = []
self.text = ""
self.alias = ""
def has_alias(self):
if (self.alias == "") or (not isinstance(self.alias, str)):
return False
else:
return True
def parsenode(file: filewrapper):
node = Node()
props = ("a", "t", "c")
while True:
if file.nextchar() in props:
if file.currchar() == "a":
node.alias = parseproperty(file)
elif file.currchar() == "t":
node.text = parseproperty(file)
elif file.currchar() == "c":
node.childs = parseproperty(file)
elif file.currchar() == "}":
return node
def parsearray(file: filewrapper):
array = []
while True:
if file.nextchar() == "]":
return array
elif file.currchar() == "{":
array.append(parsenode(file))
def parsestring(file: filewrapper):
string = ""
while True:
file.nextchar()
if (file.currchar() == '"') and (file.lastchar() != '\ '[0]):
return string
string += file.currchar()
def parseproperty(file: filewrapper):
while True:
if file.nextchar() == '"':
return parsestring(file)
elif file.currchar() == '[':
return parsearray(file)
def descision(prompt: str, options=[["Y", "y"], ["N", "n"]]):
while True:
choice = input(prompt)
for option in options:
if isinstance(option, list):
for version in option:
if version == choice:
return option
elif isinstance(option, str):
if option == choice:
return option
def load(args):
"""load <filename>
used to load a note tree from storage, if no argument is given it will try to use the last file name used by
save, csave, or load"""
recent_file = args
try:
with open(recent_file, "rt") as f:
file = filewrapper(f.read())
except:
print(f"Error while reading {recent_file}")
return
newnode = parsenode(file)
print(newnode)
return newnode
tag_text = "text".encode("UTF-8")
tag_name = "name".encode("UTF-8")
def old_to_note(old: Node, father=None):
note = utils.Note(
name=old.alias,
text=old.text,
father=father
)
note.subnotes = [old_to_note(sub, father) for sub in old.childs]
return note
old = load("notes.trnts")
new = old_to_note(old)
commands.current = new
commands.save("new.trnts")