|
1 #!/usr/bin/env python |
|
2 |
|
3 """ |
|
4 Python clone of the pngtile binary. |
|
5 """ |
|
6 |
|
7 import optparse |
|
8 |
|
9 import pypngtile as pt |
|
10 |
|
11 # CLI options |
|
12 options = None |
|
13 |
|
14 def log_debug (fmt, *args) : |
|
15 if options.debug or options.verbose : |
|
16 print fmt % args |
|
17 |
|
18 def log_info (fmt, *args) : |
|
19 if not options.quiet : |
|
20 print fmt % args |
|
21 |
|
22 def log_warn (fmt, *args) : |
|
23 print fmt % args |
|
24 |
|
25 |
|
26 def parse_hex (hex) : |
|
27 """ |
|
28 Parse a 0xHH.. style string as a raw bytestring |
|
29 """ |
|
30 |
|
31 if not hex.startswith("0x") : |
|
32 raise ValueError(hex) |
|
33 |
|
34 return hex[2:].decode("hex") |
|
35 |
|
36 def parse_args (args) : |
|
37 """ |
|
38 Parse given argv[1:] |
|
39 """ |
|
40 |
|
41 global options |
|
42 |
|
43 parser = optparse.OptionParser( |
|
44 usage = "Usage: %prog [options] <image file> [...]", |
|
45 ) |
|
46 |
|
47 # build opts list |
|
48 parser.add_option('-q', "--quiet", help="Supress informational output", action='store_true') |
|
49 parser.add_option('-v', "--verbose", help="Display more output", action='store_true') |
|
50 parser.add_option('-D', "--debug", help="Equivalent to -v", action='store_true') |
|
51 parser.add_option('-U', "--force-update", help="Unconditionally update the image caches", action='store_true') |
|
52 parser.add_option('-N', "--no-update", help="Do not update the image caches, even if unusable", action='store_true') |
|
53 parser.add_option('-B', "--background", help="Background pattern for sparse cache file", metavar="0xHH..") |
|
54 |
|
55 # parse |
|
56 options, args = parser.parse_args(args=args) |
|
57 |
|
58 # decode |
|
59 if options.background : |
|
60 try : |
|
61 options.background = parse_hex(options.background) |
|
62 |
|
63 except ValueError : |
|
64 raise ValueError("Invalid value for --background: %s" % options.background) |
|
65 |
|
66 return args |
|
67 |
|
68 |
|
69 def process_image (image) : |
|
70 """ |
|
71 Process given image |
|
72 """ |
|
73 |
|
74 # check cache status |
|
75 status = image.status() |
|
76 |
|
77 # update if stale? |
|
78 if status != pt.CACHE_FRESH or options.force_update : |
|
79 # decode status |
|
80 if status == pt.CACHE_NONE : |
|
81 log_info("\tImage cache is missing") |
|
82 |
|
83 elif status == pt.CACHE_STALE : |
|
84 log_info("\tImage cache is stale") |
|
85 |
|
86 elif status == pt.CACHE_INCOMPAT : |
|
87 log_info("\tImage cache is incompatible") |
|
88 |
|
89 elif status == pt.CACHE_FRESH : |
|
90 log_info("\tImage cache is fresh") |
|
91 |
|
92 else : |
|
93 log_warn("\tImage cache status unknown: %d", status) |
|
94 |
|
95 |
|
96 # update unless supressed |
|
97 if not options.no_update : |
|
98 log_info("\tUpdating image cache...") |
|
99 |
|
100 # update with optional background color |
|
101 image.update(background_color=options.background) |
|
102 |
|
103 log_debug("\tUpdated image cache") |
|
104 |
|
105 else : |
|
106 # warn |
|
107 log_warn("\tSupressing cache update even though it is needed") |
|
108 |
|
109 else: |
|
110 log_debug("\tImage cache is fresh") |
|
111 |
|
112 # show info |
|
113 info = image.info() |
|
114 |
|
115 log_info("\tImage dimensions: %d x %d (%d bpp)", info['img_width'], info['img_height'], info['img_bpp']) |
|
116 log_info("\tImage mtime=%d, bytes=%d", info['image_mtime'], info['image_bytes']) |
|
117 log_info("\tCache mtime=%d, bytes=%d, blocks=%d (%d bytes), version=%d", |
|
118 info['cache_mtime'], info['cache_bytes'], info['cache_blocks'], info['cache_blocks'] * 512, info['cache_version'] |
|
119 ) |
|
120 |
|
121 |
|
122 def process_images (image_paths) : |
|
123 """ |
|
124 Open up each image_path and process using process_image |
|
125 """ |
|
126 |
|
127 for image_path in image_paths : |
|
128 log_debug("Loading image: %s", image_path) |
|
129 |
|
130 # open up |
|
131 image = pt.Image(image_path, pt.OPEN_UPDATE) |
|
132 |
|
133 log_info("Opened image: %s", image_path); |
|
134 |
|
135 # process |
|
136 process_image(image) |
|
137 |
|
138 |
|
139 def main (args) : |
|
140 """ |
|
141 Operate on given argv[1:] |
|
142 """ |
|
143 |
|
144 # parse opts/args |
|
145 args = parse_args(args) |
|
146 |
|
147 # handle each image |
|
148 process_images(args) |
|
149 |
|
150 |
|
151 if __name__ == '__main__' : |
|
152 from sys import argv |
|
153 |
|
154 main(argv[1:]) |
|
155 |