Merge pull request #2643 from hermannschwaerzlerUIBK/token_from_user_config

Get token from "account" section of user configuration file (if available)
This commit is contained in:
feiniks 2024-04-15 10:29:58 +08:00 committed by GitHub
commit 5cd98b7b9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -319,6 +319,38 @@ def get_repo_download_info(url, token):
repo_info = urlopen(url, headers=headers)
return json.loads(repo_info.decode('utf8'))
def get_value_from_user_config(args, section, key):
user_config = args.C
if not user_config:
user_config = DEFAULT_USER_CONF_DIR
else:
user_config = abspath(user_config)
if _user_config_valid(user_config):
try:
from configparser import ConfigParser
from configparser import NoOptionError
except ImportError:
from ConfigParser import ConfigParser
from ConfigParser import NoOptionError
cfg = ConfigParser()
cfg.read(user_config)
if len(cfg.sections()) < 1 or cfg.sections()[0] != 'account':
return None
try:
token = cfg.get(section, key)
return token
except NoOptionError:
return None
else:
return None
def get_token_from_args_or_config(args):
if args.token:
return args.token
else:
return get_value_from_user_config(args, 'account', 'token')
def seaf_init(args):
''' Initialize config directories'''
@ -414,14 +446,8 @@ def seaf_list_remote(args):
conf_dir = _conf_dir(args)
server_from_config, user_from_config = None, None
user_config_dir = args.C
if not user_config_dir:
user_config_dir = DEFAULT_USER_CONF_DIR
else:
user_config_dir = abspath(user_config_dir)
if _user_config_valid(user_config_dir):
server_from_config, user_from_config = _parse_user_config(user_config_dir)
server_from_config = get_value_from_user_config(args, 'account', 'server')
user_from_config = get_value_from_user_config(args, 'account', 'user')
url = args.server
if not url and server_from_config:
@ -438,7 +464,7 @@ def seaf_list_remote(args):
if not username:
username = input("Enter username: ")
token = args.token
token = get_token_from_args_or_config(args)
password = None
tfa = args.tfa
if not token:
@ -491,14 +517,8 @@ def seaf_download(args):
print("Library id is required")
sys.exit(1)
server_from_config, user_from_config = None, None
user_config_dir = args.C
if not user_config_dir:
user_config_dir = DEFAULT_USER_CONF_DIR
else:
user_config_dir = abspath(user_config_dir)
if _user_config_valid(user_config_dir):
server_from_config, user_from_config = _parse_user_config(user_config_dir)
server_from_config = get_value_from_user_config(args, 'account', 'server')
user_from_config = get_value_from_user_config(args, 'account', 'user')
url = args.server
if not url and server_from_config:
@ -519,7 +539,7 @@ def seaf_download(args):
username = user_from_config
if not username:
username = input("Enter username: ")
token = args.token
token = get_token_from_args_or_config(args)
password = None
tfa = args.tfa
if not token:
@ -584,14 +604,8 @@ def seaf_download_by_name(args):
print("Library name is required")
sys.exit(1)
server_from_config, user_from_config = None, None
user_config_dir = args.C
if not user_config_dir:
user_config_dir = DEFAULT_USER_CONF_DIR
else:
user_config_dir = abspath(user_config_dir)
if _user_config_valid(user_config_dir):
server_from_config, user_from_config = _parse_user_config(user_config_dir)
server_from_config = get_value_from_user_config(args, 'account', 'server')
user_from_config = get_value_from_user_config(args, 'account', 'user')
url = args.server
if not url and server_from_config:
@ -609,7 +623,7 @@ def seaf_download_by_name(args):
username = input("Enter username: ")
args.username = username
token = args.token
token = get_token_from_args_or_config(args)
password = None
tfa = args.tfa
if not token:
@ -644,14 +658,8 @@ def seaf_sync(args):
print("Library id is required")
sys.exit(1)
server_from_config, user_from_config = None, None
user_config_dir = args.C
if not user_config_dir:
user_config_dir = DEFAULT_USER_CONF_DIR
else:
user_config_dir = abspath(user_config_dir)
if _user_config_valid(user_config_dir):
server_from_config, user_from_config = _parse_user_config(user_config_dir)
server_from_config = get_value_from_user_config(args, 'account', 'server')
user_from_config = get_value_from_user_config(args, 'account', 'user')
url = args.server
if not url and server_from_config:
@ -679,7 +687,7 @@ def seaf_sync(args):
username = input("Enter username: ")
password = None
token = args.token
token = get_token_from_args_or_config(args)
tfa = args.tfa
if not token:
password = args.password
@ -860,14 +868,8 @@ def seaf_create(args):
'''Create a library'''
conf_dir = _conf_dir(args)
server_from_config, user_from_config = None, None
user_config_dir = args.C
if not user_config_dir:
user_config_dir = DEFAULT_USER_CONF_DIR
else:
user_config_dir = abspath(user_config_dir)
if _user_config_valid(user_config_dir):
server_from_config, user_from_config = _parse_user_config(user_config_dir)
server_from_config = get_value_from_user_config(args, 'account', 'server')
user_from_config = get_value_from_user_config(args, 'account', 'user')
# check username and password
username = args.username
@ -883,7 +885,7 @@ def seaf_create(args):
print("Seafile server url need to be presented")
sys.exit(1)
token = args.token
token = get_token_from_args_or_config(args)
password = None
tfa = args.tfa
if not token: