Skip to content

Commit

Permalink
Add method to output mpd as a string, and associated test (#22)
Browse files Browse the repository at this point in the history
* Add method to output mpd as a string, and associated test

* Add debugging option in test to see whats happening on Python2.6

* provide own xml prettyprint work round bug in minidom 2.6

* import xml_utils correctly

* Rename xml_utils to something more sensible
  • Loading branch information
davemevans authored and sangwonl committed Apr 30, 2019
1 parent 563b864 commit cf24ec8
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mpegdash/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from mpegdash.nodes import MPEGDASH
from mpegdash.utils import parse_child_nodes, write_child_node
from mpegdash.prettyprinter import pretty_print


class MPEGDASHParser(object):
Expand All @@ -30,8 +31,16 @@ def parse(cls, string_or_url):
return parse_child_nodes(xml_root_node, 'MPD', MPEGDASH)[0]

@classmethod
def write(cls, mpd, filepath):
def get_as_doc(cls, mpd):
xml_doc = minidom.Document()
write_child_node(xml_doc, 'MPD', mpd)
return xml_doc

@classmethod
def write(cls, mpd, filepath):
with open(filepath, 'w') as f:
xml_doc.writexml(f, indent=' ', addindent=' ', newl='\n')
cls.get_as_doc(mpd).writexml(f, indent=' ', addindent=' ', newl='\n')

@classmethod
def toprettyxml(cls, mpd):
return pretty_print(cls.get_as_doc(mpd).toxml())
59 changes: 59 additions & 0 deletions mpegdash/prettyprinter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

# inspired by https://gist.github.com/sente/1083506
# The MIT License (MIT)

# Copyright (c) 2016 Stuart Powers

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import re

def pretty_print(xmlstr, indent=' ', newl='\n'):
# python2 doesn't have nonlocal
current = [0]

def indentline(line):
addition = 0

if re.match('.+<\/\w[^>]*>$', line):
# single line text element, don't change indentation
addition = 0
elif re.match('^<\/\w', line) and current[0] > 0:
# end of element and have padding, decrement identation by one
current[0] -= 1
elif re.match('^<\w[^>]*[^\/]>.*$', line):
# start of element, increment indentation by one
addition = 1
else:
# single line element, don't change indentation
addition = 0

# update and store current indentation in outer function
current[0] += addition

# pad the line and return
return (indent * (current[0] - addition)) + line

# split the document into line, indent each line, then rejoin lines
return (newl).join(
map(
indentline,
re.sub('(>)(<)(\/*)', r'\1\n\2\3', xmlstr).split('\n')
)
)
15 changes: 15 additions & 0 deletions tests/test_mpdtoxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,18 @@ def test_mpd2xml(self):

self.assertTrue(len(all_reprs) == 5)
self.assertTrue(len(all_reprs) == len(all_reprs2))

def test_mpd2xmlstr(self):
# set maxDiff to None for Python2.6
self.maxDiff = None
with open('./tests/mpd-samples/sample-001.mpd') as f:
# read the test MPD
mpd = MPEGDASHParser.parse(f.read())
# get the MPD as an XML string
xmlstrout = MPEGDASHParser.toprettyxml(mpd)
# then parse that string
mpd2 = MPEGDASHParser.parse(xmlstrout)
# get the reparsed MPD as a string
xmlstrout2 = MPEGDASHParser.toprettyxml(mpd2)
# and check the are equal
self.assertEqual(xmlstrout, xmlstrout2)

0 comments on commit cf24ec8

Please sign in to comment.