Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: sitescripts/oauth2dl/bin/oauth2dl.py

Issue 29833582: Issue 4954 - Implement a downloader script supporting OAuth2 authentication/authorization (Closed)
Left Patch Set: Created July 18, 2018, 1:41 p.m.
Right Patch Set: removed .Python from .gitignore and added .DS_Store to .hgignore Created Aug. 2, 2018, 9:24 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/oauth2dl/bin/constants.py ('k') | sitescripts/oauth2dl/test/__init__.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH 2 # Copyright (C) 2006-present eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 16
17 import argparse 17 import argparse
Vasily Kuznetsov 2018/07/20 21:00:01 PEP8 recommends breaking the imports into three gr
Tudor Avram 2018/07/23 19:29:03 Acknowledged.
Tudor Avram 2018/07/31 09:20:16 Done.
18 import codecs 18 import io
19 import os 19 import os
20 import sys 20 import sys
21 import json
22
21 from httplib2 import Http 23 from httplib2 import Http
22 from oauth2client.service_account import ServiceAccountCredentials 24 from oauth2client.service_account import ServiceAccountCredentials
23 import json
24 25
25 try: 26 from sitescripts.oauth2dl.bin import constants as cnts
Vasily Kuznetsov 2018/07/20 21:00:00 I think we can assume that the root of sitescripts
Tudor Avram 2018/07/23 19:29:03 That was to allow it to run both as `python -m ...
Tudor Avram 2018/07/31 09:20:17 Done.
26 from sitescripts.oauth2dl.bin import constants as cnts
27 except ImportError:
28 import constants as cnts
29
30
31 def write_to_file(content, path):
Vasily Kuznetsov 2018/07/20 21:00:00 I'm not sure if it really makes sense to have this
Tudor Avram 2018/07/23 19:29:03 Acknowledged.
Tudor Avram 2018/07/31 09:20:16 Done.
32 """Write data to file.
33
34 Parameters
35 ----------
36 content: str
37 The data to write.
38 path: str
39 Path to the file we write to.
40 """
41 with codecs.open(path, encoding='utf-8', mode='wb') as f:
42 f.write(content)
43 27
44 28
45 def download_file(url, key_file, scope): 29 def download_file(url, key_file, scope):
46 """Download a file using Oauth2. 30 """Download a file using Oauth2.
47 31
48 Parameters 32 Parameters
49 ---------- 33 ----------
50 url: str 34 url: str
51 The url of the file we want to download 35 The url of the file we want to download
52 key_file: str 36 key_file: str
53 Path/ url to key file used in Oauth2 37 Path/ url to key file used in Oauth2
54 scope: str 38 scope: str
55 The scope used in Oauth2 39 The scope used in Oauth2
56 40
57 Returns 41 Returns
58 ------- 42 -------
59 dict 43 dict
60 Headers resulted from the HTTP request. 44 Headers resulted from the HTTP request.
61 str 45 str
62 The content of the file we want to download/ 46 Content of the file we want to download/ error message if unsuccessful.
63 error message if it was unsuccessful. 47
64 """ 48 """
65 credentials = ServiceAccountCredentials.from_json_keyfile_name(key_file, sco pes=[scope]) 49 credentials = ServiceAccountCredentials.from_json_keyfile_name(
Vasily Kuznetsov 2018/07/20 21:00:00 PEP8 recommends keeping the lines under 80 charact
Tudor Avram 2018/07/23 19:29:03 Yeah, I know. that's why I added the flake8 except
Tudor Avram 2018/07/31 09:20:16 Done.
50 key_file,
51 scopes=[scope],
52 )
66 53
67 http_auth = credentials.authorize(Http()) 54 http_auth = credentials.authorize(Http())
68 55
69 headers, content = http_auth.request(url) 56 headers, content = http_auth.request(url)
70 try: 57 try:
71 content = content.decode('utf-8') 58 content = content.decode('utf-8')
72 finally: 59 finally:
73 return headers, content 60 return headers, content
74 61
75 62
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 sys.exit(cnts.GOOGLE_OAUTH_ERROR.format( 96 sys.exit(cnts.GOOGLE_OAUTH_ERROR.format(
110 str(error_json['error']['code']), 97 str(error_json['error']['code']),
111 str(error_json['error']['message']), 98 str(error_json['error']['message']),
112 )) 99 ))
113 except ValueError: 100 except ValueError:
114 sys.exit(content) 101 sys.exit(content)
115 102
116 if args.o is None: 103 if args.o is None:
117 sys.stdout.write(content) 104 sys.stdout.write(content)
118 else: 105 else:
119 write_to_file(content, args.o) 106 with io.open(args.o, encoding='utf-8', mode='wb') as f:
107 f.write(content)
120 108
121 109
122 if __name__ == '__main__': 110 if __name__ == '__main__':
123 main() 111 main()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld