-
Notifications
You must be signed in to change notification settings - Fork 14
/
gdrive-cli.py
executable file
·153 lines (113 loc) · 4.55 KB
/
gdrive-cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python
"""
Copyright 2012 Thomas Dignan <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
gdrive-cli.py command line google drive client.
Author: Tom Dignan <[email protected]>
Date: Fri Apr 27 16:00:35 EDT 2012
Official Docs: https://developers.google.com/drive/
"""
import argparse
from oauth import simple_cli
from gdrive import gdrive
from os import getenv
import pickle
from db import helper as dbhelper
from db import schema as dbschema
def get_stored_credentials_path():
return getenv("HOME") + "/.gdrive_oauth"
def get_service_object():
credentials = get_stored_credentials()
return gdrive.build_service(credentials)
def store_credentials():
credentials = simple_cli.authenticate()
pickled_creds_path = get_stored_credentials_path()
pickle.dump(credentials, open(pickled_creds_path, "wb"))
def authenticate():
store_credentials()
def get_stored_credentials():
pickled_creds_path = get_stored_credentials_path()
return pickle.load(open(pickled_creds_path, "rb"))
def make_argparser():
"""
ArgumentParser factory
"""
parser = argparse.ArgumentParser(description="gdrive-cli: google drive interface",
epilog="Author: Tom Dignan <[email protected]>")
parser.add_argument("--init-database", help="must be run after install once to initialize the user local database", action="store_true")
parser.add_argument("--authenticate", help="must be done before using other methods", action="store_true")
parser.add_argument("--show", help="show file metadata", metavar="<file_id>")
parser.add_argument("--list", help="list application's files (uses local database)", action="store_true")
parser.add_argument("--download", help="download file contents and print to stdout", metavar="<drive_file>")
parser.add_argument("--insert", help="insert new file", nargs=5,
metavar=("<title -- must include file ext>", "<description>", "<parent_id (if none, pass none)>", "<mime_type>", "<filename>"))
parser.add_argument("--rename", help="TODO: rename a file", nargs=2,
metavar=("<file_id>", "<new_title>"))
parser.add_argument("--update", help="TODO: update file", nargs=6,
metavar=("<file_id>", "<new_title>", "<new_description>", "<new_mime_type>",
"<new_filename>", "<new_revision>"))
return parser
def handle_args(args):
if args.authenticate is True:
handle_authenticate()
if args.list is True:
handle_list()
if args.show is not None:
handle_show(args.show)
elif args.download is not None:
handle_download(args.download)
elif args.insert is not None:
handle_insert(args.insert)
elif args.rename is not None:
handle_rename(args.rename)
elif args.update is not None:
handle_update(args.update)
elif args.init_database is True:
handle_init_database()
def handle_authenticate():
authenticate()
def handle_show(file_id):
service = get_service_object()
gdrive.print_file(service, file_id)
def handle_download(file_id):
service = get_service_object()
download = gdrive.download_file_by_id(service, file_id)
print download
def handle_insert(args):
service = get_service_object()
title = args[0]
description = args[1]
parent_id = args[2]
if parent_id == "none":
parent_id = None
mime_type = args[3]
filename = args[4]
file = gdrive.insert_file(service, title, description, parent_id, mime_type,
filename)
id = dbhelper.insert_file(file)
print "Inserted file ", id
def handle_list():
files = dbhelper.select_all_files()
print "filename\t\t\tid"
for f in files:
print "%(title)s\t\t%(id)s" % { "title" : f[0], "id" : f[1] }
def handle_rename(args):
print "not implemented"
def handle_update(args):
print "not implemented"
def handle_init_database():
print "Creating database..."
dbschema.create_schema()
print "done."
if __name__ == "__main__":
parser = make_argparser()
args = parser.parse_args()
handle_args(args)