source: imaps-frontend/node_modules/flatted/python/flatted.py

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.8 KB
Line 
1# ISC License
2#
3# Copyright (c) 2018-2021, Andrea Giammarchi, @WebReflection
4#
5# Permission to use, copy, modify, and/or distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15# PERFORMANCE OF THIS SOFTWARE.
16
17import json as _json
18
19class _Known:
20 def __init__(self):
21 self.key = []
22 self.value = []
23
24class _String:
25 def __init__(self, value):
26 self.value = value
27
28
29def _array_keys(value):
30 keys = []
31 i = 0
32 for _ in value:
33 keys.append(i)
34 i += 1
35 return keys
36
37def _object_keys(value):
38 keys = []
39 for key in value:
40 keys.append(key)
41 return keys
42
43def _is_array(value):
44 return isinstance(value, list) or isinstance(value, tuple)
45
46def _is_object(value):
47 return isinstance(value, dict)
48
49def _is_string(value):
50 return isinstance(value, str)
51
52def _index(known, input, value):
53 input.append(value)
54 index = str(len(input) - 1)
55 known.key.append(value)
56 known.value.append(index)
57 return index
58
59def _loop(keys, input, known, output):
60 for key in keys:
61 value = output[key]
62 if isinstance(value, _String):
63 _ref(key, input[int(value.value)], input, known, output)
64
65 return output
66
67def _ref(key, value, input, known, output):
68 if _is_array(value) and not value in known:
69 known.append(value)
70 value = _loop(_array_keys(value), input, known, value)
71 elif _is_object(value) and not value in known:
72 known.append(value)
73 value = _loop(_object_keys(value), input, known, value)
74
75 output[key] = value
76
77def _relate(known, input, value):
78 if _is_string(value) or _is_array(value) or _is_object(value):
79 try:
80 return known.value[known.key.index(value)]
81 except:
82 return _index(known, input, value)
83
84 return value
85
86def _transform(known, input, value):
87 if _is_array(value):
88 output = []
89 for val in value:
90 output.append(_relate(known, input, val))
91 return output
92
93 if _is_object(value):
94 obj = {}
95 for key in value:
96 obj[key] = _relate(known, input, value[key])
97 return obj
98
99 return value
100
101def _wrap(value):
102 if _is_string(value):
103 return _String(value)
104
105 if _is_array(value):
106 i = 0
107 for val in value:
108 value[i] = _wrap(val)
109 i += 1
110
111 elif _is_object(value):
112 for key in value:
113 value[key] = _wrap(value[key])
114
115 return value
116
117def parse(value, *args, **kwargs):
118 json = _json.loads(value, *args, **kwargs)
119 wrapped = []
120 for value in json:
121 wrapped.append(_wrap(value))
122
123 input = []
124 for value in wrapped:
125 if isinstance(value, _String):
126 input.append(value.value)
127 else:
128 input.append(value)
129
130 value = input[0]
131
132 if _is_array(value):
133 return _loop(_array_keys(value), input, [value], value)
134
135 if _is_object(value):
136 return _loop(_object_keys(value), input, [value], value)
137
138 return value
139
140
141def stringify(value, *args, **kwargs):
142 known = _Known()
143 input = []
144 output = []
145 i = int(_index(known, input, value))
146 while i < len(input):
147 output.append(_transform(known, input, input[i]))
148 i += 1
149 return _json.dumps(output, *args, **kwargs)
Note: See TracBrowser for help on using the repository browser.