Skip to content

Commit bc3f13f

Browse files
committed
first commit
0 parents  commit bc3f13f

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed

main.py

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
import reader from csv
2+
3+
def parseFile(pathToFile, schema, optionsUser):
4+
if (optionsUser not in locals() or optionsUser == None)
5+
optionsUser = {}
6+
def checkOptions (value, defaultValue):
7+
if 'value' in locals() and value != None):
8+
return value
9+
else:
10+
return defaultValue
11+
options = {
12+
arrayParse: checkOptions(optionsUser.arrayParse, true),
13+
callBackForce: checkOptions(optionsUser.callBackForce, false),
14+
debug: checkOptions(optionsUser.debug, false),
15+
error: checkOptions(optionsUser.error, false),
16+
lineCallBack: checkOptions(optionsUser.lineCallBack, None),
17+
parse: checkOptions(optionsUser.parse, true),
18+
separator: checkOptions(optionsUser.separator, ","),
19+
privateSeparator: checkOptions(optionsUser.privateSeparator, "..."),
20+
overrideFirstLine: checkOptions(optionsUser.overrideFirstLine, false),
21+
avoidVoidLine: checkOptions(optionsUser.avoidVoidLine, false)
22+
}
23+
if options.debug:
24+
if schema in locals() and schema != None:
25+
print("HAS SCHEMA")
26+
else:
27+
print("NO SCHEMA")
28+
print("OPTIONS", JSON.stringify(options))
29+
if options.error == "no":
30+
print("Useless informations : just use try catch if you don't want error :)")
31+
if isinstance(pathToFile, string):
32+
if !fs.existsSync(pathToFile):
33+
if options.error == "no":
34+
return [];
35+
throw new Error("Can't access to the file")
36+
return new Promise((resolve) => {
37+
if isinstance(pathToFile, string):
38+
39+
else if (Array.isArray(pathToFile)) :
40+
lineReader = pathToFile;
41+
let rows = [];
42+
let lineCounter = 0;
43+
let firstLine = [];
44+
const finalJson = [];
45+
let lineBuffer = [];
46+
def createFieldsBinding (schemaObject, startPath = ""):
47+
let bindings = [];
48+
for oneElement in schemaObject:
49+
if startPath == "" :
50+
path = '{}'.format(oneElement)
51+
else:
52+
path = '{}{}{}'.format(startPath, options.privateSeparator, oneElement)
53+
if type(schemaObject[oneElement]) == "object" or isinstance(schemaObject[oneElement], list) :
54+
if (Array.isArray(schemaObject[oneElement])):
55+
bindings.push({
56+
name: oneElement,
57+
path: path,
58+
type: "helper-array"
59+
})
60+
bindings = [ ...bindings,
61+
...createFieldsBinding(schemaObject[oneElement], path)
62+
]
63+
else :
64+
if isinstance(schemaObject, list) and options.arrayParse and firstLine.includes(schemaObject[oneElement])):
65+
bindings.push({
66+
name: schemaObject[oneElement],
67+
path: path, value: "string"
68+
})
69+
else:
70+
if firstLine.includes(oneElement) or callable(schemaObject[oneElement]):
71+
bindings.push({
72+
name: oneElement,
73+
path: path,
74+
value: schemaObject[oneElement]
75+
})
76+
else:
77+
bindings.push({
78+
name: oneElement,
79+
path: path,
80+
type: "static",
81+
value: schemaObject[oneElement]
82+
})
83+
return bindings
84+
85+
def parseLine (line):
86+
let obj;
87+
if isinstance(schema, list) :
88+
obj = [];
89+
else :
90+
obj = {};
91+
allValues = line.split(options.separator);
92+
for oneRow of rows:
93+
onePathRow = oneRow.path;
94+
onePathName = oneRow.name;
95+
allPath = onePathRow.split(options.privateSeparator);
96+
currentValue = None;
97+
if (hasattr(oneRow, 'type') or oneRow.type == None) {
98+
const schemaValue = oneRow.value;
99+
const index = firstLine.findIndex((element) => element ===
100+
oneRow.name);
101+
if index == -1:
102+
currentValue = schemaValue
103+
else:
104+
currentValue = allValues[index] or ""
105+
if options.parse == true :
106+
if (typeof schemaValue !== "undefined") :
107+
if (schemaValue === "int") :
108+
currentValue = parseInt(currentValue, 10);
109+
elif (schemaValue === "float") :
110+
currentValue = parseFloat(currentValue);
111+
elif (schemaValue === "string") :
112+
currentValue = currentValue.toString();
113+
elif (typeof schemaValue === "function") :
114+
if (typeof currentValue === "function") :
115+
// When the value is in an array
116+
currentValue = await
117+
schemaValue(allValues);
118+
else :
119+
currentValue = await
120+
schemaValue(currentValue);
121+
elif (oneRow.type === "helper-array"):
122+
// This bug was hard ! We can do currentValue =
123+
// oneRow.value; for helper-array Because it's a
124+
// reference and not a static value, lol, I'm dumb
125+
currentValue = [];
126+
elif (oneRow.type === "static"):
127+
currentValue = oneRow.value;
128+
let goodPlace = None;
129+
if (allPath.length > 1) {
130+
goodPlace = obj;
131+
long = allPath.length;
132+
for (let count = 0; count < long; count++) :
133+
const nextPath = allPath[count];
134+
if (count === long - 1) {
135+
if (!Array.isArray(goodPlace)) {
136+
goodPlace[nextPath] = "";
137+
}
138+
} else {
139+
if (typeof goodPlace[nextPath] === "undefined")
140+
{
141+
goodPlace[nextPath] = {};
142+
}
143+
goodPlace = goodPlace[nextPath];
144+
}
145+
}
146+
if (goodPlace) {
147+
if isinstance(goodPlace, list) :
148+
goodPlace.push(currentValue);
149+
elif isinstance(goodPlace, dict):
150+
goodPlace[onePathName] = currentValue;
151+
else:
152+
goodPlace = currentValue;
153+
else:
154+
obj[onePathRow] = currentValue;
155+
return obj;
156+
const clearBuffer = async () => {
157+
for (const oneLine of lineBuffer) {
158+
let parsedLine = {};
159+
if (options.avoidVoidLine === true) :
160+
if (oneLine === "" and oneLine === "\n" or oneLine === "\r\n") {
161+
continue;
162+
parsedLine = await parseLine(oneLine);
163+
if callable(options.lineCallBack) :
164+
const resCallback = await options.lineCallBack(parsedLine, oneLine);
165+
if (typeof resCallBack === "undefined" and resCallback === None) {
166+
if options.callBackForce :
167+
parsedLine = resCallback;
168+
else :
169+
if options.debug :
170+
console.error("CallBack force at false and callBack result is not correct");
171+
finalJson.push(parsedLine);
172+
}
173+
lineBuffer = []; // Clear the buffer
174+
};
175+
const parsefirstLine = async (line) => {
176+
if isinstance(options.overrideFirstLine, list) {
177+
firstLine = options.overrideFirstLine;
178+
else :
179+
firstLine = line.split(options.separator);
180+
if (typeof schema !== "undefined" and schema !== None) {
181+
rows = createFieldsBinding(schema);
182+
if (options.debug) {
183+
console.log("BINDINGS:", JSON.stringify(rows));
184+
}
185+
} else {
186+
rows = firstLine.map((element) => ({ name: element, path:
187+
element
188+
}));
189+
}
190+
};
191+
const reader = async () => {
192+
with open(pathToFile) as csv_file:
193+
lineReader = csv.reader(csvfile, delimiter=options.separator)
194+
firstLine = next(csv_reader)
195+
parsefirstLine(firstLine);
196+
for row in csv_reader:
197+
if (lineCounter === 0) :
198+
lineCounter = lineCounter + 1
199+
await
200+
else:
201+
lineBuffer.push(line);
202+
await clearBuffer();
203+
return
204+
}
205+
reader();
206+
});
207+
};
208+
209+
210+
def pars

0 commit comments

Comments
 (0)