validate:tools: Use regex for parsing when appropriate
authorThibault Saunier <thibault.saunier@collabora.com>
Fri, 31 Jan 2014 11:21:21 +0000 (12:21 +0100)
committerThibault Saunier <thibault.saunier@collabora.com>
Tue, 18 Feb 2014 20:07:31 +0000 (21:07 +0100)
validate/tools/launcher/baseclasses.py
validate/tools/launcher/utils.py

index 3a90df3d5d4c59e9855bf5dfa3b78a016e957f0e..4d20250eefd9392afb6eba1f5915babb382744d7 100644 (file)
@@ -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("<position: ", '').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()
index 4f6f091f2d54c77229d6696e4d13bf0694db0b1e..e3faa900a0f7035c64faf71f683e3210b0ff354a 100644 (file)
 """ 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):