| 282 | # Update json file to db |
| 283 | # Return: True if matched |
| 284 | def updateJson(self, file_path, file=None, cur=None): |
| 285 | if not file_path.startswith(self.db_dir): |
| 286 | return False # Not from the db dir: Skipping |
| 287 | relative_path = file_path[len(self.db_dir):] # File path realative to db file |
| 288 | |
| 289 | # Check if filename matches any of mappings in schema |
| 290 | matched_maps = [] |
| 291 | for match, map_settings in self.schema["maps"].items(): |
| 292 | try: |
| 293 | if SafeRe.match(match, relative_path): |
| 294 | matched_maps.append(map_settings) |
| 295 | except SafeRe.UnsafePatternError as err: |
| 296 | self.log.error(err) |
| 297 | |
| 298 | # No match found for the file |
| 299 | if not matched_maps: |
| 300 | return False |
| 301 | |
| 302 | # Load the json file |
| 303 | try: |
| 304 | if file is None: # Open file is not file object passed |
| 305 | file = open(file_path, "rb") |
| 306 | |
| 307 | if file is False: # File deleted |
| 308 | data = {} |
| 309 | else: |
| 310 | if file_path.endswith("json.gz"): |
| 311 | file = helper.limitedGzipFile(fileobj=file) |
| 312 | |
| 313 | if sys.version_info.major == 3 and sys.version_info.minor < 6: |
| 314 | data = json.loads(file.read().decode("utf8")) |
| 315 | else: |
| 316 | data = json.load(file) |
| 317 | except Exception as err: |
| 318 | self.log.debug("Json file %s load error: %s" % (file_path, err)) |
| 319 | data = {} |
| 320 | |
| 321 | # No cursor specificed |
| 322 | if not cur: |
| 323 | cur = self.getSharedCursor() |
| 324 | cur.logging = False |
| 325 | |
| 326 | # Row for current json file if required |
| 327 | if not data or [dbmap for dbmap in matched_maps if "to_keyvalue" in dbmap or "to_table" in dbmap]: |
| 328 | json_row = cur.getJsonRow(relative_path) |
| 329 | |
| 330 | # Check matched mappings in schema |
| 331 | for dbmap in matched_maps: |
| 332 | # Insert non-relational key values |
| 333 | if dbmap.get("to_keyvalue"): |
| 334 | # Get current values |
| 335 | res = cur.execute("SELECT * FROM keyvalue WHERE json_id = ?", (json_row["json_id"],)) |
| 336 | current_keyvalue = {} |
| 337 | current_keyvalue_id = {} |
| 338 | for row in res: |
| 339 | current_keyvalue[row["key"]] = row["value"] |
| 340 | current_keyvalue_id[row["key"]] = row["keyvalue_id"] |
| 341 | |