2021-02-15 20:30:28 +02:00
|
|
|
import argparse
|
2022-07-15 13:22:04 +02:00
|
|
|
import os
|
2021-05-18 05:51:33 +02:00
|
|
|
|
2022-12-30 01:44:53 +02:00
|
|
|
from libretranslate.api_keys import Database
|
|
|
|
from libretranslate.default_values import DEFAULT_ARGUMENTS as DEFARGS
|
2021-02-15 20:30:28 +02:00
|
|
|
|
2021-05-18 05:41:02 +02:00
|
|
|
|
2021-02-15 20:30:28 +02:00
|
|
|
def manage():
|
2021-05-18 05:41:02 +02:00
|
|
|
parser = argparse.ArgumentParser(description="LibreTranslate Manage Tools")
|
|
|
|
subparsers = parser.add_subparsers(
|
|
|
|
help="", dest="command", required=True, title="Command List"
|
|
|
|
)
|
|
|
|
|
|
|
|
keys_parser = subparsers.add_parser("keys", help="Manage API keys database")
|
2022-07-15 13:22:04 +02:00
|
|
|
keys_parser.add_argument(
|
|
|
|
"--api-keys-db-path",
|
|
|
|
default=DEFARGS['API_KEYS_DB_PATH'],
|
|
|
|
type=str,
|
|
|
|
help="Use a specific path inside the container for the local database",
|
|
|
|
)
|
2021-05-18 05:41:02 +02:00
|
|
|
keys_subparser = keys_parser.add_subparsers(
|
|
|
|
help="", dest="sub_command", title="Command List"
|
|
|
|
)
|
|
|
|
|
|
|
|
keys_add_parser = keys_subparser.add_parser("add", help="Add API keys to database")
|
|
|
|
keys_add_parser.add_argument(
|
2023-04-03 17:06:46 +02:00
|
|
|
"req_limit", type=int, help="Request Limits (per minute)"
|
2021-05-18 05:41:02 +02:00
|
|
|
)
|
|
|
|
keys_add_parser.add_argument(
|
|
|
|
"--key", type=str, default="auto", required=False, help="API Key"
|
|
|
|
)
|
2024-01-21 19:48:42 +02:00
|
|
|
keys_add_parser.add_argument(
|
|
|
|
"--char-limit", type=int, default=0, required=False, help="Character limit"
|
|
|
|
)
|
2021-05-18 05:41:02 +02:00
|
|
|
|
|
|
|
keys_remove_parser = keys_subparser.add_parser(
|
|
|
|
"remove", help="Remove API keys to database"
|
|
|
|
)
|
|
|
|
keys_remove_parser.add_argument("key", type=str, help="API Key")
|
2021-02-15 20:30:28 +02:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
2021-05-18 05:41:02 +02:00
|
|
|
|
|
|
|
if args.command == "keys":
|
2022-07-15 13:22:04 +02:00
|
|
|
if not os.path.exists(args.api_keys_db_path):
|
|
|
|
print("No such database: %s" % args.api_keys_db_path)
|
|
|
|
exit(1)
|
|
|
|
db = Database(args.api_keys_db_path)
|
2021-02-15 20:30:28 +02:00
|
|
|
if args.sub_command is None:
|
|
|
|
# Print keys
|
|
|
|
keys = db.all()
|
|
|
|
if not keys:
|
|
|
|
print("There are no API keys")
|
|
|
|
else:
|
|
|
|
for item in keys:
|
2023-07-09 12:29:11 +02:00
|
|
|
print("{}: {}".format(*item))
|
2021-02-15 20:30:28 +02:00
|
|
|
|
2021-05-18 05:41:02 +02:00
|
|
|
elif args.sub_command == "add":
|
2024-01-21 19:48:42 +02:00
|
|
|
print(db.add(args.req_limit, args.key, args.char_limit)[0])
|
2021-05-18 05:41:02 +02:00
|
|
|
elif args.sub_command == "remove":
|
2021-02-15 20:30:28 +02:00
|
|
|
print(db.remove(args.key))
|
|
|
|
else:
|
|
|
|
parser.print_help()
|
2021-05-18 05:41:02 +02:00
|
|
|
exit(1)
|