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