Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
141 lines (128 loc) · 4.29 KB
/
Copy pathutils.py
File metadata and controls
141 lines (128 loc) · 4.29 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
139
140
141
from colorama import init, Fore, Back, Style
init(autoreset = True)
def pretty_print(data, depth = 0, indent = 2, sdepth = False, comma = False):
if isinstance(data, list):
if not sdepth:
print(depth * (" " * indent), end = "")
print(Fore.BLUE + Style.BRIGHT + "[" + Style.RESET_ALL)
for i in data:
if comma:
print(",")
else:
comma = True
if isinstance(i, bool) or i is None:
print((depth + 1) * (" " * indent), end = "")
print(Fore.BLUE + str(i), end = "")
elif isinstance(i, int) or isinstance(i, float):
print((depth + 1) * (" " * indent), end = "")
print(Fore.CYAN + str(i) + Style.RESET_ALL, end = "")
elif isinstance(i, str):
print((depth + 1) * (" " * indent), end = "")
print(Fore.MAGENTA + f'\"{i}\"', end = "")
else:
pretty_print(i, depth + 1)
print("\n" + (depth * (" " * indent)), end = "")
print(Fore.BLUE + Style.BRIGHT + "]" + Style.RESET_ALL, end = "")
elif isinstance(data, dict):
if not sdepth:
print(depth * (" " * indent), end = "")
print(Fore.BLUE + Style.BRIGHT + "{" + Style.RESET_ALL)
for k, v in zip(data.keys(), data.values()):
if comma:
print(",")
else:
comma = True
print((depth + 1) * (" " * indent) , end = "")
if isinstance(k, int) or isinstance(k, float):
print(Fore.YELLOW + Style.DIM + str(k) + Style.RESET_ALL + ": ", end = "")
else:
print(Fore.YELLOW + f'\"{k}\"' + Fore.RESET + ": ", end = "")
if isinstance(v, bool) or v is None:
print(Fore.BLUE + str(v), end = "")
elif isinstance(v, int) or isinstance(v, float):
print(Fore.CYAN + str(v) + Style.RESET_ALL, end = "")
elif isinstance(v, str):
print(Fore.MAGENTA + f'\"{v}\"', end = "")
else:
pretty_print(v, depth + 1, sdepth = True)
print("\n" + (depth * (" " * indent)), end = "")
print(Fore.BLUE + Style.BRIGHT + "}" + Style.RESET_ALL, end = "")
if depth == 0:
print("")
db_fields = ["*"]
placeholder = [" "]
values = []
elements = []
def query_generator(query, fields = {}, OR = False, kwd = "="):
def placeholder_maker(kwd, isand = True):
if kwd == "$in":
placeholder[-1] += f" WHERE EXISTS (SELECT 1 FROM json_each(data.{".".join(elements)}) WHERE value = ?)"
placeholder[-1] += f" WHERE EXISTS (SELECT 1 FROM json_each(data.{".".join(elements)}) WHERE value = ?)"
elif kwd == "$and":
placeholder[-1] += f" json_extract(data, $.{".".join(elements)}) = ?"
isand = False
elif kwd in keywords:
placeholder[-1] += f" json_extract(data, $.{".".join(elements)} {keyword_converter(kwd)} ?"
else:
placeholder[-1] += f" json_extract(data, $.{".".join(elements)}) = ?"
if isand:
placeholder[-1] += " AND "
def keyword_converter(kwd):
if kwd == "$eq":
return "="
elif kwd == "$gt":
return ">"
elif kwd == "$lt":
return "<"
elif kwd == "$le":
return "<="
elif kwd == "$ge":
return ">="
keywords = ["$gt", "$lt", "$ge", "$le", "$eq", "$and", "$in"]
print("\n", query, "\n")
for k, v in zip(query.keys(), query.values()):
if k in keywords:
if k == "$and":
if isinstance(v, list):
for i in v:
if OR:
placeholder[-1] += " OR "
else:
OR = True
query_generator(i, kwd = k)
else:
print(Fore.WHITE + Style.RED + "Invalid Value...\nUse Array datatype with '$in'..." + Style.RESET_ALL)
return 1
elif k == "$in":
if isinstance(v, list):
values.append(tuple(v))
else:
print(Fore.WHITE + Style.RED + "Invalid Value...\nUse Array datatype with '$in'..." + Style.RESET_ALL)
return 1
else:
if isinstance(v, dict):
query_generator(v, kwd = k)
else:
placeholder[-1] = placeholder[-1].rstrip("= ?") + " " + keyword_converter(k) + " ?"
values.append(v)
else:
elements.append(k)
placeholder_maker(kwd)
if isinstance(v, dict):
query_generator(v)
else:
values.append(v)
elements.pop()
if len(fields) > 0:
db_fields.clear()
if len(fields) == 1 or len(fields) == 2 and "_id" in fields.keys():
for k, v in zip(fields.keys(), fields.values()):
if isinstance(v, int) and v:
db_fields.append(f" json_extract(data, $.{k})")
else:
print("Invalid Placeholder...")
return 1
print("\n", ",".join(db_fields))
print("\n", " AND ".join(placeholder))
print("\n", tuple(values))
return ",".join(db_fields), " AND ".join(placeholder), tuple(values)