Robert Guice
Robert Guice

Reputation: 659

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

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!

Upvotes: 4

Views: 1038

Answers (1)

Pradip Bhattacharya
Pradip Bhattacharya

Reputation: 118

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 :)

Upvotes: 4

Related Questions