From ca12b78be38f394d800d3a9033971d87203e250b Mon Sep 17 00:00:00 2001 From: Thibault Saunier Date: Fri, 31 Jan 2014 12:21:21 +0100 Subject: [PATCH] validate:tools: Use regex for parsing when appropriate --- validate/tools/launcher/baseclasses.py | 29 ++++++++++++++------------ validate/tools/launcher/utils.py | 12 +++++++---- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/validate/tools/launcher/baseclasses.py b/validate/tools/launcher/baseclasses.py index 3a90df3d5d..4d20250eef 100644 --- a/validate/tools/launcher/baseclasses.py +++ b/validate/tools/launcher/baseclasses.py @@ -200,6 +200,8 @@ class Test(Loggable): class GstValidateTest(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, options, reporter, timeout=DEFAULT_TIMEOUT, @@ -263,17 +265,14 @@ class GstValidateTest(Test): )) def _parse_position(self, p): self.log("Parsing %s" % p) + times = self.findpos_regex.findall(p) - start_stop = p.replace("", "").split(" duration: ") - - if len(start_stop) < 2: + if len(times) != 1: self.warning("Got a unparsable value: %s" % p) return 0, 0 - if "speed:"in start_stop[1]: - start_stop[1] = start_stop[1].split("speed:")[0].rstrip().lstrip() - - return utils.parse_gsttimeargs(start_stop[0]), utils.parse_gsttimeargs(start_stop[1]) + return (utils.gsttime_from_tuple(times[0][:4]), + utils.gsttime_from_tuple(times[0][4:])) def _parse_buffering(self, b): @@ -303,7 +302,7 @@ class GstValidateTest(Test): elif j.startswith("buffering") and j.endswith("%"): position, duration = self._parse_buffering(j) else: - self.debug("No info in %s" % j) + self.log("No info in %s" % j) return position, duration @@ -321,12 +320,16 @@ class GstValidateTest(Test): self.debug("Could not fine any seeking info") 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), \ - utils.parse_gsttimeargs(stop_rate[0]), float(stop_rate[1].replace(":","")) + values = self.findlastseek_regex.findall(m) + 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): position, duration = self._get_position() diff --git a/validate/tools/launcher/utils.py b/validate/tools/launcher/utils.py index 4f6f091f2d..e3faa900a0 100644 --- a/validate/tools/launcher/utils.py +++ b/validate/tools/launcher/utils.py @@ -19,11 +19,14 @@ """ Some utilies. """ import os +import re import urllib import loggable import urlparse import subprocess +from operator import itemgetter + GST_SECOND = 1000000000 DEFAULT_TIMEOUT = 10 @@ -199,11 +202,12 @@ def get_profile(combination): ################################################## # 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): - stime = time.split(":") - sns = stime[2].split(".") - stime[2] = sns[0] - stime.append(sns[1]) + stime = map(itemgetter(1), sorted(timeregex.match(time).groupdict().items())) return long((int(stime[0]) * 3600 + int(stime[1]) * 60 + int(stime[2]) * 60) * GST_SECOND + int(stime[3])) def get_duration(media_file):