validate:tools: Use regex for parsing when appropriate

This commit is contained in:
Thibault Saunier 2014-01-31 12:21:21 +01:00
parent df712e9404
commit ca12b78be3
2 changed files with 24 additions and 17 deletions

View File

@ -200,6 +200,8 @@ class Test(Loggable):
class GstValidateTest(Test): class GstValidateTest(Test):
""" A class representing a particular test. """ """ A class representing a particular test. """
findpos_regex = re.compile('.*position.*(\d+):(\d+):(\d+).(\d+).*duration.*(\d+):(\d+):(\d+).(\d+)')
findlastseek_regex = re.compile('seeking to.*(\d+):(\d+):(\d+).(\d+).*stop.*(\d+):(\d+):(\d+).(\d+).*rate.*(\d+)\.(\d+)')
def __init__(self, application_name, classname, def __init__(self, application_name, classname,
options, reporter, timeout=DEFAULT_TIMEOUT, options, reporter, timeout=DEFAULT_TIMEOUT,
@ -263,17 +265,14 @@ class GstValidateTest(Test):
)) ))
def _parse_position(self, p): def _parse_position(self, p):
self.log("Parsing %s" % p) self.log("Parsing %s" % p)
times = self.findpos_regex.findall(p)
start_stop = p.replace("<position: ", '').replace("/>", "").split(" duration: ") if len(times) != 1:
if len(start_stop) < 2:
self.warning("Got a unparsable value: %s" % p) self.warning("Got a unparsable value: %s" % p)
return 0, 0 return 0, 0
if "speed:"in start_stop[1]: return (utils.gsttime_from_tuple(times[0][:4]),
start_stop[1] = start_stop[1].split("speed:")[0].rstrip().lstrip() utils.gsttime_from_tuple(times[0][4:]))
return utils.parse_gsttimeargs(start_stop[0]), utils.parse_gsttimeargs(start_stop[1])
def _parse_buffering(self, b): def _parse_buffering(self, b):
@ -303,7 +302,7 @@ class GstValidateTest(Test):
elif j.startswith("buffering") and j.endswith("%"): elif j.startswith("buffering") and j.endswith("%"):
position, duration = self._parse_buffering(j) position, duration = self._parse_buffering(j)
else: else:
self.debug("No info in %s" % j) self.log("No info in %s" % j)
return position, duration return position, duration
@ -321,12 +320,16 @@ class GstValidateTest(Test):
self.debug("Could not fine any seeking info") self.debug("Could not fine any seeking info")
return start, stop, rate return start, stop, rate
tmp = m.split("seeking to: ")[1].split(" stop: ")
start = tmp[0]
stop_rate = tmp[1].split(" Rate")
return utils.parse_gsttimeargs(start), \ values = self.findlastseek_regex.findall(m)
utils.parse_gsttimeargs(stop_rate[0]), float(stop_rate[1].replace(":","")) if len(values) != 1:
self.warning("Got a unparsable value: %s" % p)
return start, stop, rate
v = values[0]
return (utils.gsttime_from_tuple(v[:4]),
utils.gsttime_from_tuple(v[4:8]),
float(str(v[8]) + "." + str(v[9])))
def get_current_position(self): def get_current_position(self):
position, duration = self._get_position() position, duration = self._get_position()

View File

@ -19,11 +19,14 @@
""" Some utilies. """ """ Some utilies. """
import os import os
import re
import urllib import urllib
import loggable import loggable
import urlparse import urlparse
import subprocess import subprocess
from operator import itemgetter
GST_SECOND = 1000000000 GST_SECOND = 1000000000
DEFAULT_TIMEOUT = 10 DEFAULT_TIMEOUT = 10
@ -199,11 +202,12 @@ def get_profile(combination):
################################################## ##################################################
# Some utilities to parse gst-validate output # # Some utilities to parse gst-validate output #
################################################## ##################################################
def gsttime_from_tuple(stime):
return long((int(stime[0]) * 3600 + int(stime[1]) * 60 + int(stime[2]) * 60) * GST_SECOND + int(stime[3]))
timeregex = re.compile(r'(?P<_0>.+):(?P<_1>.+):(?P<_2>.+)\.(?P<_3>.+)')
def parse_gsttimeargs(time): def parse_gsttimeargs(time):
stime = time.split(":") stime = map(itemgetter(1), sorted(timeregex.match(time).groupdict().items()))
sns = stime[2].split(".")
stime[2] = sns[0]
stime.append(sns[1])
return long((int(stime[0]) * 3600 + int(stime[1]) * 60 + int(stime[2]) * 60) * GST_SECOND + int(stime[3])) return long((int(stime[0]) * 3600 + int(stime[1]) * 60 + int(stime[2]) * 60) * GST_SECOND + int(stime[3]))
def get_duration(media_file): def get_duration(media_file):