Skip to content

Instantly share code, notes, and snippets.

@IvanaGyro
Created July 20, 2019 08:28
Show Gist options
  • Save IvanaGyro/c55680c70be80e07fc00cd5ef3fab6b3 to your computer and use it in GitHub Desktop.
Save IvanaGyro/c55680c70be80e07fc00cd5ef3fab6b3 to your computer and use it in GitHub Desktop.
Fix MongoDB's dumped JSONs which save indices information.
#!python3
import argparse
import json
import os
import shutil
from pathlib import Path
def convert(obj):
if type(obj) == list:
return [convert(item) for item in obj]
elif type(obj) == dict:
if '$numberInt' in obj:
return int(obj['$numberInt'])
elif '$numberDouble' in obj:
return float(obj['$numberDouble'])
else:
return {key: convert(item) for key, item in obj.items()}
else:
return obj
assert False
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Fix MongoDB's dumped JSONs which save indices information.")
parser.add_argument(
'path',
help='Path of the target file or the folder keep the JSONs.')
parser.add_argument(
'-r', '--restore',
action='store_true',
help='Restore from the backup files')
args = parser.parse_args()
if args.restore:
if args.path.endswith('.json.bak'):
is_file = True
file_path = Path(args.path[:-4])
backup_path = Path(args.path)
elif args.path.endswith('.json'):
is_file = True
file_path = Path(args.path)
backup_path = Path(args.path + '.bak')
else:
is_file = False
path = Path(args.path)
if not path.is_dir():
print('Cannot find the backup files.')
exit(1)
if is_file:
if not backup_path.is_file():
print('Cannot find the backup files.')
exit(1)
else:
shutil.move(backup_path, file_path)
else:
for filename in os.listdir(path):
if filename.endswith('.json.bak'):
file_path = Path(path, filename[:-4])
backup_path = Path(path, filename)
shutil.move(backup_path, file_path)
else: # convert JSONs
path = Path(args.path)
if path.is_file() and path.suffix == '.json':
backup_path = Path(str(path) + '.bak')
shutil.copy2(path, backup_path)
with open(path) as fp:
obj = json.load(fp)
with open(path, 'w') as fp:
json.dump(convert(obj), fp)
elif path.is_dir():
for filename in os.listdir(path):
if filename.endswith('.json'):
file_path = Path(path, filename)
backup_path = Path(path, filename + '.bak')
shutil.copy2(file_path, backup_path)
with open(file_path) as fp:
obj = json.load(fp)
with open(file_path, 'w') as fp:
json.dump(convert(obj), fp)
else:
print('Cannot find the JSON files.')
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment