From fa8590e197025784b0606a42ae2eb58a4cf7b962 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Wed, 27 Nov 2019 17:24:04 -0600 Subject: [PATCH] reformated code --- compare_json.py | 44 ++++++++++++++++++++------------------------ dir_to_json.py | 20 +++++++++++--------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/compare_json.py b/compare_json.py index 77c1d15..cf4b2b4 100644 --- a/compare_json.py +++ b/compare_json.py @@ -5,42 +5,38 @@ import json from dir_to_json import get_json + def compare_json(json1, json2): - bckup1, bckup2 = json1[:], json2[:] items1 = list(enumerate(json1)) items2 = list(enumerate(json2)) for i, item1 in items1: for j, item2 in items2: - if item1["name"] == item2["name"]: - if item1["is_file"] == True == item2["is_file"]: - if item1["checksum"] == item2["checksum"]: - json1[i] = None - json2[j] = None - ''' - else: - json1[i]["tag"] = "update" - json2[j] = None - ''' - elif item1["is_file"] == False == item2["is_file"]: - new_json1, new_json2 = compare_json(item1["content"], item2["content"]) - if len(new_json1) == 0: - json1[i] = None - else: - json1[i]["content"] = new_json1 - if len(new_json2) == 0: - json2[j] = None - else: - json2[j]["content"] = new_json2 - elif item1["is_file"] != item2["is_file"]:##### Caso hipotetico imposible ##### - json1[i]["tag"] == "delete" + if item1["name"] == item2["name"]: + if all([item1["is_file"], item2["is_file"]]): + if item1["checksum"] == item2["checksum"]: + json1[i] = None + json2[j] = None + elif all([not item1["is_file"], not item2["is_file"]]): + new_json1, new_json2 = compare_json( + item1["content"], item2["content"]) + if len(new_json1) == 0: + json1[i] = None + else: + json1[i]["content"] = new_json1 + if len(new_json2) == 0: + json2[j] = None + else: + json2[j]["content"] = new_json2 json1 = list(filter(None, json1)) json2 = list(filter(None, json2)) return json1, json2 + + if __name__ == "__main__": try: json1 = get_json("/home/kirbylife/Proyectos/munyal_test/original") json2 = get_json("/home/kirbylife/Proyectos/munyal_test/copy") - except: + except Exception: print("error outside") json1, json2 = compare_json(json1, json2) #print(len(json1), len(json2)) diff --git a/dir_to_json.py b/dir_to_json.py index 5f59b1a..16d3299 100644 --- a/dir_to_json.py +++ b/dir_to_json.py @@ -1,10 +1,9 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from hashlib import md5 - -import os import json +import os +from hashlib import md5 def md5sum(filename): @@ -14,18 +13,17 @@ def md5sum(filename): for chunk in iter(lambda: f.read(128 * hash.block_size), b""): hash.update(chunk) return hash.hexdigest() - except: + except Exception: return None + def get_json(path): out = [] items = os.listdir(path) try: for item in items: if item[0] != ".": - item_json = { - "name": item - } + item_json = {"name": item} route = os.path.join(path, item) if os.path.isdir(route): item_json["is_file"] = False @@ -40,11 +38,15 @@ def get_json(path): item_json["checksum"] = checksum else: item = None + else: + continue out.append(item_json) - except: + except Exception: return get_json(path) return out + if __name__ == "__main__": - output = get_json("/media/kirbylife/DATOS/Proyectos/PyCharmProjects/Munyal/folder_test") + output = get_json( + "/media/kirbylife/DATOS/Proyectos/PyCharmProjects/Munyal/folder_test") print(json.dumps(output, indent=4))