예제 #1
0
def main():
    (opts, args) = op.parse_args()

    logging.basicConfig(level=logging.DEBUG if opts.verbose else logging.INFO)

    if not args:
        op.error("you must provide at least one css file")

    if opts.in_memory:
        css_cls = InMemoryCSSFile
    else:
        css_cls = CSSFile

    base = {}

    if opts.conf:
        from ConfigParser import ConfigParser
        cp = ConfigParser()
        with open(opts.conf) as fp:
            cp.readfp(fp)
        base.update(cp.items("spritemapper"))
    if opts.anneal:
        base["anneal_steps"] = opts.anneal
    if opts.padding:
        base["padding"] = (opts.padding, opts.padding)

    conf = CSSConfig(base=base)
    spritemap([css_cls.open_file(fn, conf=conf) for fn in args], conf=conf)
예제 #2
0
def test_confed_single_map():
    conf = CSSConfig(base={"output_image": "sm.png"}, root="test")
    sm_fn = "test/sm.png"
    sfns = ("test/foo/bar.png",
            "test/foo/blah.png",
            "test/foo/quux/abc.png",
            "test/foo.png")
    return (conf, dict((sfn, sm_fn) for sfn in sfns))
예제 #3
0
def test_confed_no_recurse():
    conf = CSSConfig(base={"recursive": False,
                           "sprite_dirs": "foo"}, root="test")
    rels = {"test/foo/bar.png": "test/foo.png",
            "test/foo/blah.png": "test/foo.png",
            "test/foo/quux/abc.png": "test/foo.png",
            "test/foo.png": None,
            "test.png": None}
    return (conf, rels)
예제 #4
0
    def map_file(self, fname, mapper=None):
        """Convenience function to map the sprites of a given CSS file."""
        from spritecss.css import CSSParser
        from spritecss.finder import find_sprite_refs

        with open(fname, "rb") as fp:
            parser = CSSParser.read_file(fp)
            evs = list(parser.iter_events())

        conf = CSSConfig(evs, base=self.conf, fname=fname)
        srefs = find_sprite_refs(evs, source=fname, conf=conf)

        if mapper is None:
            mapper = SpriteDirsMapper.from_conf(conf)

        return self.map_sprite_refs(srefs, mapper=mapper)
예제 #5
0
    def input(self, _in, out, **kw):

        source_path = kw['source_path']

        # Save the input data for later
        css = _in.read()

        # Build config object
        conf = CSSConfig(base=self.options, fname=source_path)

        # Instantiate a dummy file instance
        cssfile = FakeCSSFile(fname=source_path, conf=conf, data=css)

        # Find spritemaps
        smaps = SpriteMapCollector(conf=conf)
        smaps.collect(cssfile.map_sprites())

        # Weed out single-image spritemaps
        smaps = [sm for sm in smaps if len(sm) > 1]

        # Generate spritemapped image
        # This code is almost verbatim from spritecss.main.spritemap
        sm_plcs = []
        for smap in smaps:
            with open_sprites(smap, pad=conf.padding) as sprites:
                print(("Packing sprites in mapping %s" % (smap.fname,)))
                packed = PackedBoxes(sprites, anneal_steps=conf.anneal_steps)
                print_packed_size(packed)
                sm_plcs.append((smap, packed.placements))
                print(("Writing spritemap image at %s" % (smap.fname,)))
                im = stitch(packed)
                with open(smap.fname, "wb") as fp:
                    im.save(fp)

        # Instantiate a fake file instance again
        cssfile = FakeCSSFile(fname=source_path, conf=conf, data=css)

        # Output rewritten CSS with spritemapped URLs
        replacer = SpriteReplacer(sm_plcs)
        for data in iter_print_css(replacer(cssfile)):
            out.write(data)
예제 #6
0
 def __init__(self, conf=None):
     if conf is None:
         conf = CSSConfig()
     self.conf = conf
     self._maps = {}
예제 #7
0
 def open_file(cls, fname, conf=None):
     with cls(fname).open_parser() as p:
         return cls(fname, conf=CSSConfig(p, base=conf, fname=fname))
예제 #8
0
def test_confed_default():
    rels = {"test/foo/bar.png": "test/foo.png",
            "test/foo/blah.png": "test/foo.png",
            "test/foo/quux/abc.png": "test/foo/quux.png",
            "test/foo.png": "test.png"}
    return (CSSConfig(root="test"), rels)