Эх сурвалжийг харах

Dateien zurück-kombinieren

robert 1 жил өмнө
parent
commit
7e22fcf67b

+ 83 - 27
webservice/planner_combine_files.py

@@ -7,33 +7,89 @@ import re
 from pathlib import Path
 
 
-def combine_file(year, version, timestamp, username):
-    base_dir = "save/"
-
-    data_file = f"data_{year}_{version}_{timestamp}.csv"
-    data_search = rf"data_{year}_{version}_\d+\.csv"
-    user_search = rf"user_{username}_\d+\.json"
-    options_search = rf"options_{year}_{version}_\d+\.json"
-    struct_search = rf"struct_{year}_{version}_\d+\.json"
-
-    file_list = [f.name for f in Path(base_dir).glob("*.*") if f.is_file()]
-    file_list.sort()
-
-    if not Path(base_dir + data_file).exists():
-        data_files = [f for f in file_list if re.search(data_search, f)]
-        if len(data_files) > 0:
-            data_file = data_files[-1]
-
-    with open(base_dir + data_file, "r") as frh:
-        csv_reader = csv.reader(frh, delimiter="\t")
-        header = next(csv_reader)
-        values2 = defaultdict(dict)
-        for row in csv_reader:
-            id = row.pop(0)
-            department = row.pop(0)
-            values2[id][department] = row
-    print(len(values2.keys()))
+class PlannerCombineFiles:
+    def __init__(self, year, version, timestamp, username):
+        self.year = year
+        self.version = version
+        self.timestamp = timestamp
+        self.username = username
+
+        self.base_dir = "save/"
+
+        self.file_list = [f.name for f in Path(self.base_dir).glob("*.*") if f.is_file()]
+        self.file_list.sort()
+
+    @property
+    def data_file(self):
+        file = f"data_{self.year}_{self.version}_{self.timestamp}.csv"
+        file_search = rf"data_{self.year}_{self.version}_\d+\.csv"
+        return self.get_filename(file, file_search)
+
+    @property
+    def user_file(self):
+        file = f"user_{self.timestamp}.csv"
+        file_search = rf"user_{self.username}_\d+\.json"
+        return self.get_filename(file, file_search)
+
+    @property
+    def options_file(self):
+        file = f"options_{self.year}_{self.version}_{self.timestamp}.json"
+        file_search = rf"options_{self.year}_{self.version}_\d+\.json"
+        return self.get_filename(file, file_search)
+
+    @property
+    def struct_file(self):
+        file = f"struct_{self.year}_{self.version}_{self.timestamp}.json"
+        file_search = rf"struct_{self.year}_{self.version}_\d+\.json"
+        return self.get_filename(file, file_search)
+
+    def get_filename(self, file, file_search):
+        if not Path(self.base_dir + file).exists():
+            data_files = [f for f in self.file_list if re.search(file_search, f)]
+            if len(data_files) > 0:
+                file = data_files[-1]
+        return self.base_dir + file
+
+    def __call__(self):
+        self.struct = self.load_scaffold(self.struct_file)
+        self.add_data_file(self.data_file)
+        self.add_user_file(self.user_file)
+        self.add_options_file(self.options_file)
+        return self.struct
+
+    def load_scaffold(self, struct_file):
+        with open(struct_file, "r") as frh:
+            res = json.load(frh)
+        self.scaffold = dict([(s["id"], s) for s in res])
+        return res
+
+    def add_data_file(self, data_file):
+        with open(data_file, "r") as frh:
+            csv_reader = csv.reader(frh, delimiter="\t")
+            _ = next(csv_reader)
+            values2 = defaultdict(dict)
+            for row in csv_reader:
+                id = row.pop(0)
+                department = row.pop(0)
+                values2[id][department] = row
+            for id, v2 in values2.items():
+                self.scaffold[id]["values2"] = v2
+
+    def add_user_file(self, user_file):
+        with open(user_file, "r") as frh:
+            res = json.load(frh)
+        for id, s in self.scaffold.items():
+            s["drilldown"] = res["drilldown"].get(id, False)
+
+    def add_options_file(self, options_file):
+        with open(options_file, "r") as frh:
+            res = json.load(frh)
+        for id, s in self.scaffold.items():
+            if id in res:
+                s["options"] = res[id]
 
 
 if __name__ == "__main__":
-    combine_file("2023", "V1", "current", "global")
+    pcf = PlannerCombineFiles("2023", "V1", "current", "global")
+    res = pcf()
+    assert res is not None