44
55
66def parseFile (pathToFile = "" , schema = None , optionsUser = {}):
7+ global lineReader
78 def checkOptions (optionsUser , attr , defaultValue ):
89 if attr in optionsUser :
910 return optionsUser [attr ]
@@ -22,7 +23,7 @@ def checkOptions(optionsUser, attr, defaultValue):
2223 "avoidVoidLine" : checkOptions (optionsUser , "avoidVoidLine" , False )
2324 }
2425 if options ["debug" ]:
25- if schema in locals ( ) and schema != None :
26+ if ( isinstance ( schema , list ) or isinstance ( schema , dict ) ) and schema != None :
2627 print ("HAS SCHEMA" )
2728 else :
2829 print ("NO SCHEMA" )
@@ -35,178 +36,190 @@ def checkOptions(optionsUser, attr, defaultValue):
3536 return []
3637 # throw new Error("Can't access to the file")
3738
38- if isinstance (pathToFile , str ):
39- csvFile = open (pathToFile )
40- lineReader = CSVreader (csvFile , delimiter = options ["separator" ])
41- elif isinstance (pathToFile , list ):
42- lineReader = pathToFile
43- rows = []
44- firstLine = []
39+ if isinstance (pathToFile , str ):
40+ csvFile = open (pathToFile )
41+ lineReader = CSVreader (csvFile , delimiter = options ["separator" ])
42+ elif isinstance (pathToFile , list ):
43+ lineReader = pathToFile
44+ rows = []
45+ firstLine = []
4546
46- def createFieldsBinding (schemaObject , startPath = "" ):
47- global firstLine
48- bindings = []
49- for index , value in enumerate (schemaObject ):
50- if isinstance (schemaObject , list ):
51- oneElement = index
52- elif isinstance (schemaObject , dict ):
53- oneElement = value
54- if startPath == "" :
55- path = '{}' .format (oneElement )
47+ def createFieldsBinding (schemaObject , startPath = "" ):
48+ global firstLine
49+ bindings = []
50+ for index , value in enumerate (schemaObject ):
51+ if isinstance (schemaObject , list ):
52+ oneElement = index
53+ elif isinstance (schemaObject , dict ):
54+ oneElement = value
55+ if startPath == "" :
56+ path = '{}' .format (oneElement )
57+ else :
58+ path = '{}{}{}' .format (
59+ startPath , options ["privateSeparator" ], oneElement )
60+ if isinstance (schemaObject [oneElement ], dict ) or isinstance (schemaObject [oneElement ], list ):
61+ if isinstance (schemaObject [oneElement ], list ):
62+ bindings .append ({
63+ "name" : oneElement ,
64+ "path" : path ,
65+ "type" : "helper-array"
66+ })
67+ bindings = [
68+ * bindings , * createFieldsBinding (schemaObject [oneElement ], path )]
69+ else :
70+ if isinstance (schemaObject , list ) and options ["arrayParse" ] and schemaObject [oneElement ] in firstLine :
71+ bindings .append ({
72+ "name" : schemaObject [oneElement ],
73+ "path" : path ,
74+ "value" : "string"
75+ })
5676 else :
57- path = '{}{}{}' .format (
58- startPath , options ["privateSeparator" ], oneElement )
59- if isinstance (schemaObject [oneElement ], dict ) or isinstance (schemaObject [oneElement ], list ):
60- if isinstance (schemaObject [oneElement ], list ):
77+ if oneElement in firstLine or callable (schemaObject [oneElement ]):
6178 bindings .append ({
6279 "name" : oneElement ,
6380 "path" : path ,
64- "type " : "helper-array"
81+ "value " : schemaObject [ oneElement ]
6582 })
66- bindings = [
67- * bindings , * createFieldsBinding (schemaObject [oneElement ], path )]
68- else :
69- if isinstance (schemaObject , list ) and options ["arrayParse" ] and schemaObject [oneElement ] in firstLine :
83+ else :
7084 bindings .append ({
71- "name" : schemaObject [ oneElement ] ,
85+ "name" : oneElement ,
7286 "path" : path ,
73- "value" : "string"
87+ "type" : "static" ,
88+ "value" : schemaObject [oneElement ]
7489 })
75- else :
76- if oneElement in firstLine or callable (schemaObject [oneElement ]):
77- bindings .append ({
78- "name" : oneElement ,
79- "path" : path ,
80- "value" : schemaObject [oneElement ]
81- })
82- else :
83- bindings .append ({
84- "name" : oneElement ,
85- "path" : path ,
86- "type" : "static" ,
87- "value" : schemaObject [oneElement ]
88- })
89- return bindings
90+ return bindings
9091
91- def parseLine (line ):
92- global rows
93- global firstLine
94- if isinstance (schema , list ):
95- obj = []
96- else :
97- obj = {}
98- allValues = line
99- for oneRow in rows :
100- onePathRow = oneRow ["path" ]
101- onePathName = oneRow ["name" ]
102- allPath = onePathRow .split (options ["privateSeparator" ])
103- currentValue = None
104- if ('type' not in oneRow ) or ('type' in oneRow and oneRow ["type" ] == None ):
105- if 'value' not in oneRow :
106- schemaValue = None
107- else :
108- schemaValue = oneRow ["value" ]
92+ def parseLine (line ):
93+ global rows
94+ global firstLine
95+ if isinstance (schema , list ):
96+ obj = []
97+ else :
98+ obj = {}
99+ allValues = line
100+ for oneRow in rows :
101+ onePathRow = oneRow ["path" ]
102+ onePathName = oneRow ["name" ]
103+ allPath = onePathRow .split (options ["privateSeparator" ])
104+ currentValue = None
105+ if ('type' not in oneRow ) or ('type' in oneRow and oneRow ["type" ] == None ):
106+ if 'value' not in oneRow :
107+ schemaValue = None
108+ else :
109+ schemaValue = oneRow ["value" ]
110+ if oneRow ["name" ] in firstLine :
109111 index = firstLine .index (oneRow ["name" ])
110- if index == - 1 :
111- currentValue = schemaValue
112+ else :
113+ index = - 1
114+ if index == - 1 :
115+ currentValue = schemaValue
116+ else :
117+ currentValue = allValues [index ] or ""
118+ if options ["parse" ] == True :
119+ if schemaValue == "int" :
120+ currentValue = int (currentValue )
121+ elif schemaValue == "float" :
122+ currentValue = float (currentValue )
123+ elif schemaValue == "string" :
124+ currentValue = str (currentValue )
125+ elif callable (schemaValue ):
126+ if callable (currentValue ):
127+ # When the value is in an array
128+ currentValue = schemaValue (allValues )
129+ else :
130+ currentValue = schemaValue (currentValue )
131+ elif ('type' in oneRow and oneRow ["type" ] == "helper-array" ):
132+ currentValue = []
133+ elif ('type' in oneRow and oneRow ["type" ] == "static" ):
134+ currentValue = oneRow ["value" ]
135+ goodPlace = None
136+ if len (allPath ) > 1 :
137+ goodPlace = obj
138+ long = len (allPath )
139+ for count in range (0 , long ):
140+ nextPath = allPath [count ]
141+ if isinstance (goodPlace , list ):
142+ nextPathInt = int (nextPath )
143+ if count == (long - 1 ):
144+ if isinstance (goodPlace , dict ):
145+ goodPlace [nextPath ] = ""
112146 else :
113- currentValue = allValues [index ] or ""
114- if options ["parse" ] == True :
115- if schemaValue == "int" :
116- currentValue = int (currentValue )
117- elif schemaValue == "float" :
118- currentValue = float (currentValue )
119- elif schemaValue == "string" :
120- currentValue = str (currentValue )
121- elif callable (schemaValue ):
122- if callable (currentValue ):
123- # When the value is in an array
124- currentValue = schemaValue (allValues )
147+ if nextPath not in goodPlace :
148+ if isinstance (goodPlace , list ):
149+ if len (goodPlace ) < (nextPathInt + 1 ):
150+ # len() returns 0 and the first index of the list is 0 !
151+ goodPlace .insert (nextPathInt , {})
125152 else :
126- currentValue = schemaValue (currentValue )
127- elif ('type' in oneRow and oneRow ["type" ] == "helper-array" ):
128- currentValue = []
129- elif ('type' in oneRow and oneRow ["type" ] == "static" ):
130- currentValue = oneRow ["value" ]
131- goodPlace = None
132- if len (allPath ) > 1 :
133- goodPlace = obj
134- long = len (allPath )
135- for count in range (0 , long ):
136- nextPath = allPath [count ]
153+ goodPlace [nextPath ] = {}
137154 if isinstance (goodPlace , list ):
138- nextPathInt = int (nextPath )
139- if count == (long - 1 ):
140- if not isinstance (goodPlace , list ):
141- goodPlace [nextPath ] = ""
155+ goodPlace = goodPlace [nextPathInt ]
142156 else :
143- if nextPath not in goodPlace :
144- if isinstance (goodPlace , list ):
145- goodPlace .insert (nextPathInt , {})
146- else :
147- goodPlace [nextPath ] = {}
148- if isinstance (goodPlace , list ):
149- goodPlace = goodPlace [nextPathInt ]
150- else :
151- goodPlace = goodPlace [nextPath ]
152- if isinstance (goodPlace , list ):
153- goodPlace .append (currentValue )
154- elif isinstance (goodPlace , dict ):
155- goodPlace [onePathName ] = currentValue
156- else :
157- goodPlace = currentValue
157+ goodPlace = goodPlace [nextPath ]
158+ if isinstance (goodPlace , list ):
159+ goodPlace .append (currentValue )
160+ elif isinstance (goodPlace , dict ):
161+ goodPlace [onePathName ] = currentValue
158162 else :
159- if isinstance (obj , list ):
160- place = int (onePathRow )
161- obj .insert (place , currentValue )
162- elif isinstance (obj , dict ):
163- obj [onePathRow ] = currentValue
164- return obj
165-
166- def parsefirstLine ():
167- global firstLine
168- if isinstance (options ["overrideFirstLine" ], list ):
169- firstLine = options ["overrideFirstLine" ]
170- if schema != None :
171- # None is default value for schema
172- cols = createFieldsBinding (schema )
173- if options ["debug" ]:
174- print ("BINDINGS:" , JSONstringify (cols ))
163+ goodPlace = currentValue
175164 else :
176- def dupli (element ):
177- return {
178- "name" : element ,
179- "path" : element
180- }
181- cols = [dupli (x ) for x in firstLine ]
182- return cols
165+ if isinstance (obj , list ):
166+ place = int (onePathRow )
167+ obj .insert (place , currentValue )
168+ elif isinstance (obj , dict ):
169+ obj [onePathRow ] = currentValue
170+ return obj
171+
172+ def parsefirstLine ():
173+ global firstLine
174+ if isinstance (options ["overrideFirstLine" ], list ):
175+ firstLine = options ["overrideFirstLine" ]
176+ if schema != None :
177+ # None is default value for schema
178+ cols = createFieldsBinding (schema )
179+ if options ["debug" ]:
180+ print ("BINDINGS:" , JSONstringify (cols , default = lambda o : '<not serializable>' ))
181+ else :
182+ def dupli (element ):
183+ return {
184+ "name" : element ,
185+ "path" : element
186+ }
187+ cols = [dupli (x ) for x in firstLine ]
188+ return cols
183189
184- def reader ():
185- global rows
186- global firstLine
187- finalJson = []
190+ def reader ():
191+ global rows
192+ global firstLine
193+ global lineReader
194+ finalJson = []
195+ if isinstance (pathToFile , str ):
188196 firstLine = next (lineReader )
189- rows = parsefirstLine ()
190- for oneLine in lineReader :
191- parsedLine = {}
192- if options ["avoidVoidLine" ] == True :
193- if oneLine == "" and oneLine == "\n " or oneLine == "\r \n " :
194- continue
195- parsedLine = parseLine (oneLine )
196- if callable (options ["lineCallBack" ]):
197- resCallback = options ["lineCallBack" ](parsedLine , oneLine )
198- if resCallback == None :
199- if options ["callBackForce" ]:
200- parsedLine = resCallback
201- else :
202- if options ["debug" ]:
203- print (
204- "CallBack force at False and callBack result is not correct" )
205- else :
197+ elif isinstance (pathToFile , list ):
198+ firstLine = lineReader [0 ].split (options ["separator" ])
199+ lineReader = lineReader [1 :]
200+ rows = parsefirstLine ()
201+ for oneLine in lineReader :
202+ parsedLine = {}
203+ if isinstance (pathToFile , list ):
204+ oneLine = oneLine .split (options ["separator" ])
205+ if options ["avoidVoidLine" ] == True :
206+ if oneLine == "" and oneLine == "\n " or oneLine == "\r \n " :
207+ continue
208+ parsedLine = parseLine (oneLine )
209+ if callable (options ["lineCallBack" ]):
210+ resCallback = options ["lineCallBack" ](parsedLine , oneLine )
211+ if resCallback == None :
212+ if options ["callBackForce" ]:
206213 parsedLine = resCallback
207- finalJson .append (parsedLine )
208- return finalJson
209- converted = reader ()
210- if isinstance (pathToFile , str ):
211- csvFile .close ()
212- return converted
214+ else :
215+ if options ["debug" ]:
216+ print (
217+ "CallBack force at False and callBack result is not correct" )
218+ else :
219+ parsedLine = resCallback
220+ finalJson .append (parsedLine )
221+ return finalJson
222+ converted = reader ()
223+ if isinstance (pathToFile , str ):
224+ csvFile .close ()
225+ return converted
0 commit comments