1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-11-05 07:20:13 +01:00
LibreTranslate/app/manage.py

50 lines
1.5 KiB
Python
Raw Normal View History

import argparse
from app.api_keys import Database
2021-05-18 05:41:02 +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")
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(
"req_limit", type=int, help="Request Limits (per second)"
)
keys_add_parser.add_argument(
"--key", type=str, default="auto", required=False, help="API Key"
)
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")
args = parser.parse_args()
2021-05-18 05:41:02 +02:00
if args.command == "keys":
db = Database()
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:
print("%s: %s" % item)
2021-05-18 05:41:02 +02:00
elif args.sub_command == "add":
print(db.add(args.req_limit, args.key)[0])
2021-05-18 05:41:02 +02:00
elif args.sub_command == "remove":
print(db.remove(args.key))
else:
parser.print_help()
2021-05-18 05:41:02 +02:00
exit(1)