Skip to content

Commit 1596deb

Browse files
committed
improved help text a lot
1 parent ffc2210 commit 1596deb

2 files changed

Lines changed: 70 additions & 13 deletions

File tree

flickr_download/filename_handlers.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,23 @@
88
from __future__ import unicode_literals
99
from collections import defaultdict
1010

11+
DEFAULT_HANDLER = 'title'
12+
"""The default handler if none is specified"""
13+
14+
15+
def _get_short_docstring(docstring):
16+
"""
17+
Given a docstring return the first sentence of it.
18+
19+
@param: docstring: str, the docstring to parsde
20+
@return: str, the short docstring
21+
"""
22+
return docstring.split('.')[0].strip()
23+
1124

1225
def title(pset, photo, suffix):
1326
"""
14-
Name file after title. Falls back to photo id.
27+
Name file after title (falls back to photo id).
1528
1629
@param pset: Flickr.Photoset, the photoset
1730
@param photo: Flickr.Photo, the photo
@@ -26,7 +39,7 @@ def title(pset, photo, suffix):
2639

2740
def idd(pset, photo, suffix):
2841
"""
29-
Name file after id
42+
Name file after photo id.
3043
3144
@param pset: Flickr.Photoset, the photoset
3245
@param photo: Flickr.Photo, the photo
@@ -38,7 +51,7 @@ def idd(pset, photo, suffix):
3851

3952
def title_and_id(pset, photo, suffix):
4053
"""
41-
Name file after title and photo id
54+
Name file after title and photo id.
4255
4356
@param pset: Flickr.Photoset, the photoset
4457
@param photo: Flickr.Photo, the photo
@@ -57,7 +70,7 @@ def title_and_id(pset, photo, suffix):
5770

5871
def title_increment(pset, photo, suffix):
5972
"""
60-
Name file after photo title, but add an incrementing counter on duplicates
73+
Name file after photo title, but add an incrementing counter on duplicates.
6174
6275
@param pset: Flickr.Photoset, the photoset
6376
@param photo: Flickr.Photo, the photo
@@ -83,10 +96,25 @@ def title_increment(pset, photo, suffix):
8396
}
8497

8598

86-
def get_filename_handler(name='title'):
99+
def get_filename_handler(name):
87100
"""
88101
Returns the given filename handler as a function
89102
@param name: str, name of the handler to return
90103
@return: Function, handler
91104
"""
92-
return HANDLERS[name]
105+
return HANDLERS[name or DEFAULT_HANDLER]
106+
107+
108+
def get_filename_handler_help():
109+
"""
110+
Returns a description of each handler to be used for help output.
111+
112+
@return: str, help text
113+
"""
114+
ret = []
115+
for name, func in HANDLERS.iteritems():
116+
ret.append(' {handler} - {doc}{default}'
117+
.format(handler=name,
118+
doc=_get_short_docstring(func.__doc__),
119+
default=(' (DEFAULT)' if name == DEFAULT_HANDLER else '')))
120+
return 'Naming modes:\n' + '\n'.join(ret)

flickr_download/flick_download.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import yaml
2121

2222
from flickr_download.filename_handlers import get_filename_handler
23+
from flickr_download.filename_handlers import get_filename_handler_help
2324
from flickr_download.utils import get_full_path
2425

2526
CONFIG_FILE = "~/.flickr_download"
@@ -154,7 +155,26 @@ def print_sets(username):
154155

155156

156157
def main():
157-
parser = argparse.ArgumentParser('Download a Flickr Set')
158+
parser = argparse.ArgumentParser(
159+
formatter_class=argparse.RawTextHelpFormatter,
160+
description='Downloads one or more Flickr photo sets.\n'
161+
'\n'
162+
'To use it you need to get your own Flickr API key here:\n'
163+
'https://www.flickr.com/services/api/misc.api_keys.html\n'
164+
'\n'
165+
'For more information see:\n'
166+
'https://github.com/beaufour/flickr-download',
167+
epilog='examples:\n'
168+
' list all sets for a user:\n'
169+
' > {app} -k <api_key> -s <api_secret> -l beaufour\n'
170+
'\n'
171+
' download a given set:\n'
172+
' > {app} -k <api_key> -s <api_secret> -d 72157622764287329\n'
173+
'\n'
174+
' download a given set, keeping duplicate names:\n'
175+
' > {app} -k <api_key> -s <api_secret> -d 72157622764287329 -n title_increment\n'
176+
.format(app=sys.argv[0])
177+
)
158178
parser.add_argument('-k', '--api_key', type=str,
159179
help='Flickr API key')
160180
parser.add_argument('-s', '--api_secret', type=str,
@@ -170,11 +190,17 @@ def main():
170190
parser.add_argument('-q', '--quality', type=str, metavar='SIZE_LABEL',
171191
default=None, help='Quality of the picture')
172192
parser.add_argument('-n', '--naming', type=str, metavar='NAMING_MODE',
173-
default='title', help='Photo naming mode')
193+
help='Photo naming mode')
194+
parser.add_argument('-m', '--list_naming', action='store_true',
195+
help='List naming modes')
174196
parser.set_defaults(**_load_defaults())
175197

176198
args = parser.parse_args()
177199

200+
if args.list_naming:
201+
print(get_filename_handler_help())
202+
return 1
203+
178204
if not args.api_key or not args.api_secret:
179205
print ('You need to pass in both "api_key" and "api_secret" arguments', file=sys.stderr)
180206
return 1
@@ -185,7 +211,9 @@ def main():
185211

186212
if args.list:
187213
print_sets(args.list)
188-
elif args.download or args.download_user:
214+
return 0
215+
216+
if args.download or args.download_user:
189217
try:
190218
get_filename = get_filename_handler(args.naming)
191219
if args.download:
@@ -194,10 +222,11 @@ def main():
194222
download_user(args.download_user, get_filename, args.quality)
195223
except KeyboardInterrupt:
196224
print('Forcefully aborting. Last photo download might be partial :(', file=sys.stderr)
197-
else:
198-
print('ERROR: Must pass either --list or --download\n', file=sys.stderr)
199-
parser.print_help()
200-
return 1
225+
return 0
226+
227+
print('ERROR: Must pass either --list or --download\n', file=sys.stderr)
228+
parser.print_help()
229+
return 1
201230

202231
if __name__ == '__main__':
203232
sys.exit(main())

0 commit comments

Comments
 (0)