From b99198b41db5040c9c3d903f7a8ef5a77134228a Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Wed, 10 Sep 2025 15:51:17 +0330 Subject: [PATCH] chore: Improve script import mechanism and remove boilerplate --- core/scripts/hysteria2/add_user.py | 2 +- core/scripts/hysteria2/bulk_users.py | 3 +-- core/scripts/hysteria2/edit_user.py | 3 +-- core/scripts/hysteria2/get_user.py | 3 +-- core/scripts/hysteria2/kick.py | 3 +-- core/scripts/hysteria2/list_users.py | 11 ++++------- core/scripts/hysteria2/remove_user.py | 3 +-- core/scripts/hysteria2/reset_user.py | 3 +-- core/scripts/hysteria2/show_user_uri.py | 2 +- core/scripts/hysteria2/wrapper_uri.py | 3 +-- 10 files changed, 13 insertions(+), 23 deletions(-) diff --git a/core/scripts/hysteria2/add_user.py b/core/scripts/hysteria2/add_user.py index f66301c..3a3fd6a 100644 --- a/core/scripts/hysteria2/add_user.py +++ b/core/scripts/hysteria2/add_user.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 +import init_paths import sys import os import subprocess import re from datetime import datetime -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from db.database import db def add_user(username, traffic_gb, expiration_days, password=None, creation_date=None, unlimited_user=False): diff --git a/core/scripts/hysteria2/bulk_users.py b/core/scripts/hysteria2/bulk_users.py index 592c1a4..45051ff 100644 --- a/core/scripts/hysteria2/bulk_users.py +++ b/core/scripts/hysteria2/bulk_users.py @@ -1,12 +1,11 @@ #!/usr/bin/env python3 +import init_paths import sys import os import subprocess import argparse import re - -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from db.database import db def add_bulk_users(traffic_gb, expiration_days, count, prefix, start_number, unlimited_user): diff --git a/core/scripts/hysteria2/edit_user.py b/core/scripts/hysteria2/edit_user.py index 8b27f40..1573913 100644 --- a/core/scripts/hysteria2/edit_user.py +++ b/core/scripts/hysteria2/edit_user.py @@ -1,12 +1,11 @@ #!/usr/bin/env python3 +import init_paths import sys import os import argparse import re from datetime import datetime - -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from db.database import db def edit_user(username, new_username=None, new_password=None, traffic_gb=None, expiration_days=None, creation_date=None, blocked=None, unlimited_user=None): diff --git a/core/scripts/hysteria2/get_user.py b/core/scripts/hysteria2/get_user.py index 80968d9..9a56aed 100644 --- a/core/scripts/hysteria2/get_user.py +++ b/core/scripts/hysteria2/get_user.py @@ -1,11 +1,10 @@ #!/usr/bin/env python3 +import init_paths import json import sys import os import getopt - -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from db.database import db def get_user_info(username): diff --git a/core/scripts/hysteria2/kick.py b/core/scripts/hysteria2/kick.py index 9fb9c80..031c3f8 100644 --- a/core/scripts/hysteria2/kick.py +++ b/core/scripts/hysteria2/kick.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import init_paths import os import sys import json @@ -7,8 +8,6 @@ import fcntl import datetime import logging from concurrent.futures import ThreadPoolExecutor - -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from db.database import db from hysteria2_api import Hysteria2Client from paths import CONFIG_FILE diff --git a/core/scripts/hysteria2/list_users.py b/core/scripts/hysteria2/list_users.py index 61e28e5..2d1e45a 100644 --- a/core/scripts/hysteria2/list_users.py +++ b/core/scripts/hysteria2/list_users.py @@ -1,13 +1,10 @@ +#!/usr/bin/env python3 + +import init_paths import sys import json from pathlib import Path - -try: - from hysteria2_api import Hysteria2Client -except ImportError: - sys.exit("Error: hysteria2_api library not found. Please install it.") - -sys.path.append(str(Path(__file__).resolve().parent.parent)) +from hysteria2_api import Hysteria2Client from db.database import db from paths import CONFIG_FILE, API_BASE_URL diff --git a/core/scripts/hysteria2/remove_user.py b/core/scripts/hysteria2/remove_user.py index 94195b9..d5a32a3 100644 --- a/core/scripts/hysteria2/remove_user.py +++ b/core/scripts/hysteria2/remove_user.py @@ -1,9 +1,8 @@ #!/usr/bin/env python3 +import init_paths import sys import os - -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from db.database import db def remove_user(username): diff --git a/core/scripts/hysteria2/reset_user.py b/core/scripts/hysteria2/reset_user.py index 8274b77..91516af 100644 --- a/core/scripts/hysteria2/reset_user.py +++ b/core/scripts/hysteria2/reset_user.py @@ -1,10 +1,9 @@ #!/usr/bin/env python3 +import init_paths import sys import os from datetime import date - -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from db.database import db def reset_user(username): diff --git a/core/scripts/hysteria2/show_user_uri.py b/core/scripts/hysteria2/show_user_uri.py index 5f355a4..8fd9661 100644 --- a/core/scripts/hysteria2/show_user_uri.py +++ b/core/scripts/hysteria2/show_user_uri.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import init_paths import os import sys import json @@ -9,7 +10,6 @@ import re import qrcode from io import StringIO from typing import Tuple, Optional, Dict, List, Any -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from db.database import db from paths import * diff --git a/core/scripts/hysteria2/wrapper_uri.py b/core/scripts/hysteria2/wrapper_uri.py index 1590ee6..8d1b71e 100644 --- a/core/scripts/hysteria2/wrapper_uri.py +++ b/core/scripts/hysteria2/wrapper_uri.py @@ -1,13 +1,12 @@ #!/usr/bin/env python3 +import init_paths import os import sys import json import argparse from functools import lru_cache from typing import Dict, List, Any - -sys.path.append(os.path.join(os.path.dirname(__file__), '..')) from db.database import db from paths import *