13

I want to implement a script that reads a CSS file and makes meaningful changes to it (adding/removing/replacing lines/words etc.). The basic logic is implement an RTL (right-to-left) transformation.

I could think of quite a few approaches to it:

  • file reader - read a line, analyze it and make the needed changes to it.
  • two phase scan - create in memory model, scan and change it, save model to text.
  • regular expressions - It might be quite difficult because some of them might be very complex.

basically what I'm wondering is which of those, or other methods, would be the python way to do it? are there any relevant libraries you think I should be familiar with for this kind of operation?

Edit: it's should be noted that this is a "learn python through this usable project" kind of project so I'm not familiar with most libraries you would mention here.

5
  • nedbatchelder.com/text/python-parsers.html contains a good survey of state of language parsing tools for Python. My best (non-analytic) guess is that CSS is a context-free (not regular) language and thus could only be transmogrified correctly with a stateful parser. Commented Jul 21, 2012 at 13:07
  • 3
    have you tried an existing CSS parser in Python such as cssutils? Commented Jul 21, 2012 at 13:12
  • do you want to keep the formatting, comments, etc ? Commented Jul 21, 2012 at 13:14
  • first of all it's probably important to mention that this is a "learn python through this usable project" kind of project so I'm not familiar with most things you mentioned above (I will add this remark to the main post as well). By looking at the cssutils page and examples it seems like it belongs to the second approach I mentioned. and I will surely look into it, but still remains the question if that is that actually the best python practice in this case? Commented Jul 21, 2012 at 13:35
  • formatting is pretty flexible for me, it's not a 100% must at this time... Commented Jul 21, 2012 at 13:36

1 Answer 1

25

If you want something "quick and dirty" there are many interesting ways to do this. (As you said: line-by-line, regular expressions, …)

But if you want to do it "right" (correct on all kinds of inputs) you’ll need a real parser based on the official CSS tokenization and grammar. In Python there is cssutils and tinycss. (Disclaimer: I’m tinycss’s author.) If you want to learn, I like to think that tinycss’s source code is straightforward and easy to learn :)

Sign up to request clarification or add additional context in comments.

4 Comments

would love to hear from you about the difference between the two parser tools. maybe in the form of an asnwer to "why did you create tinycss when cssutils already exists?"
arikg: There it is: exyr.org/2012/tinycss-css-parser In short, it’s mostly that cssutils was not easy to extend for new syntax.
I just tried doing this with tinycss but writing back the css file is somehow not straightforward at all. I can write out rules with some hand written code but it's then missing all comments and all the things tinycss doesn't actually parse like @media...
Yes, tinycss doesn’t include a proper serializer. I suggest using github.com/SimonSapin/tinycss2 instead, which does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.