import argparse
import io
+import json
import os.path
import shutil
import sys
return parser.error(f'{path} does not exist!')
return path
-def fernet_key(parser, path):
- if not os.path.isfile(path):
- return parser.error(f'{path} does not exist!')
- with open(path, 'rb') as f:
- return Fernet(f.read())
+def fernet_key(parser, path_or_string):
+ if os.path.isfile(path_or_string):
+ with open(path_or_string, 'rb') as f:
+ return Fernet(f.read())
+ return Fernet(path_or_string)
-def get_drive_client(authentication_token):
- creds = Credentials.from_authorized_user_file(authentication_token, SCOPES)
+def auth_token(parser, path_or_string):
+ creds = None
+ if os.path.isfile(path_or_string):
+ creds = Credentials.from_authorized_user_file(path_or_string, SCOPES)
+ else:
+ as_json = json.loads(path_or_string)
+ creds = Credentials(
+ token=as_json['token'],
+ refresh_token=as_json['refresh_token'],
+ token_uri=as_json['token_uri'],
+ client_id=as_json['client_id'],
+ client_secret=as_json['client_secret'],
+ scopes=as_json['scopes'],
+ )
if not creds or not creds.valid:
- print(f'{authentication_token} is not valid. Use `auth` to obtain a new '
- 'one.')
+ print(f'Token is not valid. Use `auth` to obtain a new one.')
sys.exit(1)
-
- return build('drive', 'v3', credentials=creds)
+ return creds
def get_path_on_drive(file_path):
if os.path.isabs(file_path):
print('Authentication successful.')
def list(args):
- drive = get_drive_client(args.token)
+ drive = build('drive', 'v3', credentials=args.token)
files_on_drive = drive.files().list(q='trashed=false',
fields='files(id, originalFilename)').execute()
print(file_on_drive['originalFilename'])
def download(args):
- drive = get_drive_client(args.token)
+ drive = build('drive', 'v3', credentials=args.token)
maybe_id = get_file_id(drive, args.path)
if not maybe_id:
print(f'Removed {path_in_tmp}.')
def upload(args):
- drive = get_drive_client(args.token)
+ drive = build('drive', 'v3', credentials=args.token)
path = args.name if args.name else args.file
path_in_tmp = get_path_on_drive(path)
print(f'Removed {path_in_tmp}.')
def delete(args):
- drive = get_drive_client(args.token)
+ drive = build('drive', 'v3', credentials=args.token)
maybe_id = get_file_id(drive, args.path)
if not maybe_id:
list_parser = subparsers.add_parser('list', help='List files.')
list_parser.add_argument('-t', '--token', required=True,
- type=lambda x : file_path(parser, x), help='File with the '
+ type=lambda x : auth_token(parser, x), help='File with the '
'authentication token.')
list_parser.set_defaults(func=list)
type=lambda x : fernet_key(parser, x), help='File with the decryption '
'key.')
download_parser.add_argument('-t', '--token', required=True,
- type=lambda x : file_path(parser, x), help='File with the '
+ type=lambda x : auth_token(parser, x), help='File with the '
'authentication token.')
download_parser.add_argument('--leave-as-archive', action='store_true',
help='Skip unarchiving for directories.')
type=lambda x : fernet_key(parser, x), help='File with the decryption '
'key.')
upload_parser.add_argument('-t', '--token', required=True,
- type=lambda x : file_path(parser, x), help='File with the '
+ type=lambda x : auth_token(parser, x), help='File with the '
'authentication token.')
upload_parser.set_defaults(func=upload)
delete_parser = subparsers.add_parser('delete', help='Delete a file.')
delete_parser.add_argument('path', help='File to delete.')
delete_parser.add_argument('-t', '--token', required=True,
- type=lambda x : file_path(parser, x), help='File with the '
+ type=lambda x : auth_token(parser, x), help='File with the '
'authentication token.')
delete_parser.set_defaults(func=delete)