Skip to content

Commit 58232a4

Browse files
committed
lots of minor fixes. Added __main__ to call the module direclty with options
1 parent 72d02c4 commit 58232a4

7 files changed

Lines changed: 541 additions & 32 deletions

File tree

bin/resolveimg.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
from __future__ import print_function
34
import sys
45
import imageresolver
56
import logging
@@ -10,18 +11,21 @@
1011
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
1112

1213
opts = OptionParser()
13-
opts.add_option("-u","--url",dest="url",help="A url to parse")
14-
opts.add_option("-d","--debug", action="store_true", dest="debug", help="enable debugging")
15-
opts.add_option("-r","--max-read", dest="max_read",help="Set the max read size")
16-
opts.add_option("-c","--chunk-size",dest="chunk_size",help="Chunk size to read on each pass")
17-
opts.add_option("-a","--read-all",dest="read_all",help="Read the entire image before checking size. Useful for some JPGs. Overrides --max-read")
18-
opts.add_option("-b","--adblock", action="store_true",dest="use_adblock_filters",help="Use adblock filters.")
19-
opts.add_option("-s","--no-ruleset", action="store_true",dest="use_js_ruleset",help="Use a custom ruleset for scoring.")
20-
opts.add_option("--benchmark", action="store_true",dest="benchmark",help="Benchmark the total time it takes for the script to return an image")
21-
opts.add_option("-n","--no-load-images", action="store_true",dest="load_images",help="Do not load images")
22-
opts.add_option("-p","--parser", dest="parser",help="Choose a parser to use")
23-
24-
(options,args) = opts.parse_args()
14+
opts.add_option("-u", "--url", dest="url",help="A url to parse")
15+
opts.add_option("-d", "--debug", action="store_true", dest="debug", help="enable debugging")
16+
opts.add_option("-r", "--max-read", dest="max_read", help="Set the max read size")
17+
opts.add_option("-c", "--chunk-size", dest="chunk_size", help="Chunk size to read on each pass")
18+
opts.add_option("-a", "--read-all", dest="read_all",
19+
help="Read the entire image before checking size. Useful for some JPGs. Overrides --max-read")
20+
opts.add_option("-b", "--adblock", action="store_true", dest="use_adblock_filters", help="Use adblock filters.")
21+
opts.add_option("-s", "--no-ruleset", action="store_true", dest="use_js_ruleset",
22+
help="Use a custom ruleset for scoring.")
23+
opts.add_option("--benchmark", action="store_true",dest="benchmark",
24+
help="Benchmark the total time it takes for the script to return an image")
25+
opts.add_option("-n", "--no-load-images", action="store_true", dest="load_images", help="Do not load images")
26+
opts.add_option("-p", "--parser", dest="parser", help="Choose a parser to use")
27+
28+
options, args = opts.parse_args()
2529

2630
kw_options = {}
2731

@@ -53,19 +57,21 @@
5357
url = options.url
5458

5559
if not url:
56-
print "URL required. Please use the url option or pass a url as the first argument"
60+
print("URL required. Please use the url option or pass a url as the first argument")
5761
sys.exit(-1)
5862

5963

6064
if options.benchmark:
6165
t1 = time.time()
66+
else:
67+
t1 = 0
6268

6369
i = imageresolver.ImageResolver(**kw_options)
6470
i.register(imageresolver.FileExtensionResolver())
6571
i.register(imageresolver.WebpageResolver(**kw_options))
6672

67-
print i.resolve(url)
73+
print(i.resolve(url))
6874

6975
if options.benchmark:
70-
print 'TOTAL TIME', time.time() - t1
76+
print('TOTAL TIME', time.time() - t1)
7177

imageresolver/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
https://github.com/mauricesvay/ImageResolver
1010
"""
1111

12-
from imageresolver.base import *
12+
from imageresolver.base import FileExtensionResolver, ImageResolver, WebpageResolver
13+
from imageresolver.getimageinfo import getImageInfo
1314
from imageresolver.version import __version__
1415

16+
__all__ = ['ImageResolver', 'FileExtensionResolver', 'WebpageResolver', '__version__']

imageresolver/__main__.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from __future__ import print_function
2+
import sys
3+
import logging
4+
import time
5+
from optparse import OptionParser
6+
from imageresolver import *
7+
8+
logger = logging.getLogger('ImageResolver')
9+
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
10+
11+
opts = OptionParser()
12+
opts.add_option("-u", "--url", dest="url", help="A url to parse")
13+
opts.add_option("-d", "--debug", action="store_true", dest="debug", help="enable debugging")
14+
opts.add_option("-r", "--max-read", dest="max_read", help="Set the max read size")
15+
opts.add_option("-c", "--chunk-size", dest="chunk_size", help="Chunk size to read on each pass")
16+
opts.add_option("-a", "--read-all", dest="read_all",
17+
help="Read the entire image before checking size. Useful for some JPGs. Overrides --max-read")
18+
opts.add_option("-b", "--adblock", action="store_true", dest="use_adblock_filters", help="Use adblock filters.")
19+
opts.add_option("-s", "--no-ruleset", action="store_true", dest="use_js_ruleset",
20+
help="Use a custom ruleset for scoring.")
21+
opts.add_option("--benchmark", action="store_true", dest="benchmark",
22+
help="Benchmark the total time it takes for the script to return an image")
23+
opts.add_option("-n", "--no-load-images", action="store_true", dest="load_images", help="Do not load images")
24+
opts.add_option("-p", "--parser", dest="parser", help="Choose a parser to use")
25+
26+
options, args = opts.parse_args()
27+
28+
kw_options = {}
29+
30+
if options.read_all:
31+
kw_options['read_all'] = True
32+
elif options.max_read:
33+
kw_options['max_read_size'] = int(options.max_read_size)
34+
35+
if options.chunk_size:
36+
kw_options['chunk_size'] = int(options.chunk_size)
37+
38+
if options.use_js_ruleset:
39+
kw_options['use_js_ruleset'] = False
40+
41+
if options.parser:
42+
kw_options['parser'] = options.parser
43+
44+
if options.load_images:
45+
kw_options['load_images'] = False
46+
47+
if options.get_info:
48+
kw_options['get_info'] = True
49+
50+
kw_options['use_adblock_filters'] = options.use_adblock_filters
51+
kw_options['debug'] = options.debug
52+
53+
try:
54+
url = args[0]
55+
except IndexError:
56+
url = None
57+
if options.url:
58+
url = options.url
59+
60+
if not url:
61+
print("URL required. Please use the url option or pass a url as the first argument")
62+
sys.exit(-1)
63+
64+
if options.benchmark:
65+
t1 = time.time()
66+
else:
67+
t1 = 0
68+
69+
i = ImageResolver(**kw_options)
70+
i.register(FileExtensionResolver(**kw_options))
71+
i.register(WebpageResolver(**kw_options))
72+
73+
print(i.resolve(url))
74+
75+
if options.benchmark:
76+
print('TOTAL TIME', time.time() - t1)
77+
78+
79+

imageresolver/abpy

Submodule abpy updated from ca906f0 to 3391402

0 commit comments

Comments
 (0)