Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tiny CSS - Return Stylesheet (file you can save)?

Tags:

python

css

Using TinyCss, how do you Print_Parsed_CSS to a normal, usable CSS document?

Example Usage:

See all the styles, update all sets that include the selector ".some-class-name" where found to have certain attributes, and then print the stylesheet back to a file.

I am stuck on the simple task of Printing Styelsheet From TinyCSS, to a .css file.

It has to be something super simple but I've googled this like crazy and can't find a way to do it. I'm new to python so I'm probably overlooking something simple.

Thanks Gurus!

like image 682
Robert Guice Avatar asked Sep 17 '25 22:09

Robert Guice


1 Answers

Since I am replying to an old post, so there is a possibility that Robert has already solved it. Since I was also looking for the same thing I wrote the below

import tinycss;

css_text="""
.span-class1{
    background: url(images/test.png) no-repeat top left;
    width: 10px;
    height: 10px;
}
"""
css_parser=tinycss.make_parser('page3');

stylesheet=css_parser.parse_stylesheet_bytes(css_text);

for rule in stylesheet.rules:
    #let us reconstruct the css rule
    css_str=rule.selector.as_css()+"{\n";
    for d in rule.declarations:
        prop_css_val='';
        for v in d.value:
            prop_css_val=prop_css_val+' '+v.as_css();
        css_str=css_str+str(d.name)+":"+prop_css_val+";\n"
    css_str=css_str+"}\n"
    print css_str;

I am not very good with CSS & tinycss & python. If there is anything wrong with the above code, then dont kill me :)

like image 84
5 revs, 4 users 93% Avatar answered Sep 19 '25 12:09

5 revs, 4 users 93%