Created
January 28, 2011 00:46
-
-
Save docblades/799612 to your computer and use it in GitHub Desktop.
Parses the output from MKVinfo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Created by Christian Blades <christian dot blades at docblades dot com> | |
from xml.etree.cElementTree import Element, ElementTree | |
from urllib import quote_plus as quote | |
def _getData(line): | |
name, text = None, None | |
div = line.find(':') | |
if div == -1: | |
name = quote(line.strip()) | |
else: | |
name = quote(line[:div].strip()) | |
text = line[div + 1:].strip() | |
return name, text | |
def parse(theFile): | |
elStack = [] | |
root = [] | |
for line in theFile: | |
start = line.find("+ ") | |
if (start == -1): | |
continue | |
else: | |
name, text = _getData(line[start + 2:]) | |
myEl = Element(name) | |
myEl.text = text | |
if start == 0: | |
elStack = [[0, myEl]] | |
root.append(myEl) | |
else: | |
if start > elStack[-1][0]: | |
elStack[-1][1].append(myEl) | |
elStack.append([start, myEl]) | |
else: | |
while(elStack[-1][0] > start): | |
elStack.pop() | |
elStack.pop() | |
elStack[-1][1].append(myEl) | |
elStack.append([start, myEl]) | |
rootNode = Element("root") | |
for el in root: | |
rootNode.append(el) | |
return ElementTree(rootNode) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment