Created
February 18, 2021 13:39
-
-
Save tetebueno/5800504133709c9844bcd1779019d390 to your computer and use it in GitHub Desktop.
Revisions
-
tetebueno renamed this gist
Feb 18, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
tetebueno created this gist
Feb 18, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ import re as _re import json as _json JSON_EXTENSION = '.json' def find_json_for_file(file: Path): try: if file.with_name(file.name + JSON_EXTENSION).is_file(): # file.jpg -> file.jpg.json the_json_path = file.with_name(file.name + JSON_EXTENSION) elif file.with_name(file.name.replace(file.suffix, JSON_EXTENSION)).is_file(): # file.jpg -> file.json the_json_path = file.with_name(file.name.replace(file.suffix.lower(), JSON_EXTENSION)) elif len(file.name) >= 47: # fileee...eee.jpg -> fileee...eee..json the_json_path = file.with_name(file.name[0:46] + JSON_EXTENSION) elif bool(_re.search(r'^(.+)(\(\d+\))(\..+)$', file.name)): weird_search = _re.search(r'^(.+)(\(\d+\))(\..+)$', file.name) if file.with_name(weird_search.group(1) + JSON_EXTENSION).is_file(): # file(1).jpg -> file.json the_json_path = file.with_name(weird_search.group(1) + JSON_EXTENSION) else: # file(1).jpg -> file.jpg(1).json the_json_path = file.with_name(weird_search.group(1) + weird_search.group(3) + weird_search.group(2) + JSON_EXTENSION) #print('Using ' + the_json_path.name + ' for ' + file.name) with open(the_json_path, 'r') as f: json_dict = _json.load(f) return json_dict except: raise FileNotFoundError(f'Couldn\'t find json for file: {file}')