CSS-Parser
Verfasst: Dienstag 1. August 2006, 13:26
Hallo,
da ich mit dem Adressbuch nich zufriden war und auch keine Lust mehr hatte weiterzumachen
, hab ich mich entschlossen etwas zu schreiben, was ich selber auch gebrauchen kann.
Also hab ich mich entschlossen nen CSS-Parser zu schreiben(den Vorschlag hat irgendjemand hier im Forum gebracht, also es nich 100% auf meinem Mist gewaschen
)
Nunja, bis jetz ist er noch nich wirklich fertig. Soweit bin ich:
Man bekommt ein Dictionary mit dem Tag, das definiert wurde, als key und als Value eine Liste von den Definitionen.
TODO:
Kritik und Verbessrungsvorschläge sind erwünscht.
Greetz
----------------------------------------------------------------------------
CSS-Parser (0.0.1a):
Noch ne Frage: Macht es Sinn, bei jeder Methode, die Parameter hat, zu überprüfen, ob auch der richtige Datentyp übergeben worden ist und falls nicht einen TypeError zu werfen oder soll ich das lieber lassen?
da ich mit dem Adressbuch nich zufriden war und auch keine Lust mehr hatte weiterzumachen

Also hab ich mich entschlossen nen CSS-Parser zu schreiben(den Vorschlag hat irgendjemand hier im Forum gebracht, also es nich 100% auf meinem Mist gewaschen

Nunja, bis jetz ist er noch nich wirklich fertig. Soweit bin ich:
Man bekommt ein Dictionary mit dem Tag, das definiert wurde, als key und als Value eine Liste von den Definitionen.
TODO:
- Fehler ausgeben, wenn ein Semikolon vergessen wurde
- Warnungen ausgeben, wenn etwas vergessen wurde(z.B. keine Vordergrundfarbe trotz definierter HG-Farbe)
- GUI
Kritik und Verbessrungsvorschläge sind erwünscht.
Greetz
----------------------------------------------------------------------------
CSS-Parser (0.0.1a):
Code: Alles auswählen
#!/usr/bin/python
#-*- encoding: utf-8 -*-
class CSSParser:
#-------------------------------------------
def __init__(self, css):
#Open CSS file
self.f = open(css, 'r')
#Read all lines of the file
self.lines = self.f.read()
#-------------------------------------------
def getAttributes(self):
'''
Fetch all attributes and there related tags and return them as a dict
:return tagAttributes: dictionary with the tag as key and a list of the
tags definitions as value
'''
#get all indexes of the char `}`
indexes = self._getIndexes(self.lines, '}')
#and the strings between the indexes
defs = self._getTags(self.lines,indexes)
tagAttributes = dict()
for defin in defs:
#delete all newlines, tabs and leading and following whitespaces
defin = defin.replace('\n', '').replace('\t', '').strip()
attributes = list()
if defin:
#get a list of the tagdefinitions
splitedTag = defin.split('{')
attrs = splitedTag[1].replace('}', '').strip().split(';')
#add all definitions in a list
attributes = [attr for attr in attrs if attr]
#add the values to the dict
tagAttributes[splitedTag[0].strip()] = attributes
#return the dict
return tagAttributes
#-------------------------------------------
def _getIndexes(self, string, char):
'''
Get the indexes of a string by `char`
:param string: string may containing char
:param char: character to search for it to get the indexes
:return indexes: the indexes if found
'''
indexes = [0,]
while True:
try:
#try to get the index number
index = string.index(char, indexes[-1]) + 1
#if string.index() was succesfull add the index to the list
indexes.append(index)
#break if a ValueError is raised
except ValueError:
break
#return the list with the indexes
return indexes
#-------------------------------------------
def _getTags(self, string, indexes):
'''
Get the tags with there definitions and return them in a list
:param string: string witch contains the previously gotten indexcharacters
:param indexes: a list containing the indexes
:return tags: a list with the tags and there definitions
'''
#last index
lindex = 0
tags = list()
for index in indexes:
#add the tag to the list
tags.append(string[lindex:index])
#update the last index
lindex = index
#return the Tags
return tags
##################### Programmtests #######################
def main():
css = CSSParser('css.css')
for key, val in css.getAttributes().items():
print key + ':'
for val in val:
print '\t' + val
print '-'*40
if __name__ == '__main__':
main()