From e8da744744da6eab0aaddb3bb8dd820b15ce4c79 Mon Sep 17 00:00:00 2001 From: caryoscelus Date: Mon, 8 Apr 2024 13:30:49 +0000 Subject: [PATCH] WIP --- src/User/User.py | 5 +++-- src/User/UserManager.py | 6 +++--- src/main.py | 9 ++++++++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/User/User.py b/src/User/User.py index dbcfc56f..89571146 100644 --- a/src/User/User.py +++ b/src/User/User.py @@ -35,8 +35,9 @@ class User(object): # Save to data/users.json @util.Noparallel(queue=True, ignore_class=True) def save(self): + users_json = f'{config.private_dir}/users.json' s = time.time() - users = json.load(open("%s/users.json" % config.data_dir)) + users = json.load(open(users_json)) if self.master_address not in users: users[self.master_address] = {} # Create if not exist user_data = users[self.master_address] @@ -45,7 +46,7 @@ class User(object): user_data["sites"] = self.sites user_data["certs"] = self.certs user_data["settings"] = self.settings - helper.atomicWrite("%s/users.json" % config.data_dir, helper.jsonDumps(users).encode("utf8")) + helper.atomicWrite(users_json, helper.jsonDumps(users).encode("utf8")) self.log.debug("Saved in %.3fs" % (time.time() - s)) self.delayed_save_thread = None diff --git a/src/User/UserManager.py b/src/User/UserManager.py index 067734a6..a2afc295 100644 --- a/src/User/UserManager.py +++ b/src/User/UserManager.py @@ -15,7 +15,7 @@ class UserManager(object): self.users = {} self.log = logging.getLogger("UserManager") - # Load all user from data/users.json + # Load all user from users.json def load(self): if not self.users: self.users = {} @@ -25,7 +25,7 @@ class UserManager(object): s = time.time() # Load new users try: - json_path = "%s/users.json" % config.data_dir + json_path = f'{config.private_dir}/users.json' data = json.load(open(json_path)) except Exception as err: raise Exception("Unable to load %s: %s" % (json_path, err)) @@ -57,7 +57,7 @@ class UserManager(object): user.saveDelayed() return user - # List all users from data/users.json + # List all users # Return: {"usermasteraddr": User} def list(self): if self.users == {}: # Not loaded yet diff --git a/src/main.py b/src/main.py index b25e4a81..2fff005f 100644 --- a/src/main.py +++ b/src/main.py @@ -72,6 +72,13 @@ def init_dirs(): and not config.offline and (not data_dir.is_dir() or not (data_dir / 'sites.json').is_file())) + old_users_json = data_dir / 'users.json' + if old_users_json.is_file(): + print('Migrating existing users.json file to private/') + old_sites_json = data_dir / 'sites.json' + if old_sites_json.is_file(): + print('Migrating existing sites.json file to private/') + if not data_dir.is_dir(): data_dir.mkdir(parents=True, exist_ok=True) @@ -94,7 +101,7 @@ def init_dirs(): if not os.path.isfile(sites_json): with open(sites_json, "w") as f: f.write("{}") - users_json = f"{data_dir}/users.json" + users_json = f"{private_dir_dir}/users.json" if not os.path.isfile(users_json): with open(users_json, "w") as f: f.write("{}")