[Libosinfo] [PATCH osinfo-db 15/15] tests: Move isodata parsing to isodata.py

Cole Robinson crobinso at redhat.com
Mon Mar 25 14:32:19 UTC 2019


Upcoming changes will use this from other files, so move it
to a shared location

Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
 tests/isodata.py      | 91 ++++++++++++++++++++++++++++++++++++++++++
 tests/test_isoinfo.py | 92 +------------------------------------------
 2 files changed, 93 insertions(+), 90 deletions(-)
 create mode 100644 tests/isodata.py

diff --git a/tests/isodata.py b/tests/isodata.py
new file mode 100644
index 0000000..a5face9
--- /dev/null
+++ b/tests/isodata.py
@@ -0,0 +1,91 @@
+# This work is licensed under the GNU GPLv2 or later.
+# See the COPYING file in the top-level directory.
+
+
+class _ISODataMedia():
+    def __init__(self, filename, volumeid, publisherid, systemid,
+                 applicationid, volumesize):
+
+        self.filename = filename
+        self.volumeid = volumeid if volumeid is not None else ''
+        self.publisherid = publisherid if publisherid is not None else ''
+        self.systemid = systemid if systemid is not None else ''
+        self.applicationid = applicationid \
+                if applicationid is not None else ''
+        self.volumesize = volumesize if volumesize is not None else 0
+
+    def match(self, media):
+        volumesize = media.volumesize
+        if volumesize == 0:
+            volumesize = self.volumesize
+
+        if bool(media.volumeid.match(self.volumeid)) and \
+           bool(media.publisherid.match(self.publisherid)) and \
+           bool(media.applicationid.match(self.applicationid)) and \
+           bool(media.systemid.match(self.systemid)) and \
+           volumesize == self.volumesize:
+            return True
+
+        return False
+
+
+def _get_value(string, prefix, return_type=str):
+    if string.startswith(prefix):
+        return return_type(string.split(': ')[-1].strip())
+    return None
+
+
+def _get_volumeid(string):
+    return _get_value(string, 'Volume id: ')
+
+
+def _get_publisherid(string):
+    return _get_value(string, 'Publisher id: ')
+
+
+def _get_systemid(string):
+    return _get_value(string, 'System id: ')
+
+
+def _get_applicationid(string):
+    return _get_value(string, 'Application id: ')
+
+
+def _get_logicalblock(string):
+    return _get_value(string, 'Logical block size is: ', int)
+
+
+def _get_volumesize(string):
+    return _get_value(string, 'Volume size is: ', int)
+
+
+def get_isodatamedia(filepath):
+    volumeid = None
+    publisherid = None
+    systemid = None
+    applicationid = None
+    logicalblock = None
+    volumesize = None
+
+    with open(filepath, 'r') as out:
+        for line in out.readlines():
+            if volumeid is None:
+                volumeid = _get_volumeid(line)
+            if publisherid is None:
+                publisherid = _get_publisherid(line)
+            if systemid is None:
+                systemid = _get_systemid(line)
+            if applicationid is None:
+                applicationid = _get_applicationid(line)
+            if logicalblock is None:
+                logicalblock = _get_logicalblock(line)
+            if volumesize is None:
+                volumesize = _get_volumesize(line)
+
+    if logicalblock is not None and volumesize is not None:
+        volumesize *= logicalblock
+    else:
+        volumesize = None
+
+    return _ISODataMedia(filepath, volumeid, publisherid, systemid,
+                         applicationid, volumesize)
diff --git a/tests/test_isoinfo.py b/tests/test_isoinfo.py
index f5a15ba..e65e649 100644
--- a/tests/test_isoinfo.py
+++ b/tests/test_isoinfo.py
@@ -7,6 +7,7 @@ import os
 import pytest
 
 from . import util
+from . import isodata
 
 
 def _get_isodatapaths():
@@ -36,7 +37,7 @@ def test_iso_detection(testdata):
             continue
 
         detected = []
-        isodatamedia = _get_isodatamedia(isodatapath)
+        isodatamedia = isodata.get_isodatamedia(isodatapath)
         for osxml2 in util.DataFiles.oses():
             for media in osxml2.medias:
                 if isodatamedia.match(media.iso):
@@ -60,92 +61,3 @@ def test_iso_detection(testdata):
 
         raise AssertionError("isodata: %s\nMatched=%s but expected=%s" %
                 (isodatapath, detected, [osname]))
-
-
-class _ISODataMedia():
-    def __init__(self, filename, volumeid, publisherid, systemid,
-                 applicationid, volumesize):
-
-        self.filename = filename
-        self.volumeid = volumeid if volumeid is not None else ''
-        self.publisherid = publisherid if publisherid is not None else ''
-        self.systemid = systemid if systemid is not None else ''
-        self.applicationid = applicationid \
-                if applicationid is not None else ''
-        self.volumesize = volumesize if volumesize is not None else 0
-
-    def match(self, media):
-        volumesize = media.volumesize
-        if volumesize == 0:
-            volumesize = self.volumesize
-
-        if bool(media.volumeid.match(self.volumeid)) and \
-           bool(media.publisherid.match(self.publisherid)) and \
-           bool(media.applicationid.match(self.applicationid)) and \
-           bool(media.systemid.match(self.systemid)) and \
-           volumesize == self.volumesize:
-            return True
-
-        return False
-
-
-def _get_value(string, prefix, return_type=str):
-    if string.startswith(prefix):
-        return return_type(string.split(': ')[-1].strip())
-    return None
-
-
-def _get_volumeid(string):
-    return _get_value(string, 'Volume id: ')
-
-
-def _get_publisherid(string):
-    return _get_value(string, 'Publisher id: ')
-
-
-def _get_systemid(string):
-    return _get_value(string, 'System id: ')
-
-
-def _get_applicationid(string):
-    return _get_value(string, 'Application id: ')
-
-
-def _get_logicalblock(string):
-    return _get_value(string, 'Logical block size is: ', int)
-
-
-def _get_volumesize(string):
-    return _get_value(string, 'Volume size is: ', int)
-
-
-def _get_isodatamedia(filepath):
-    volumeid = None
-    publisherid = None
-    systemid = None
-    applicationid = None
-    logicalblock = None
-    volumesize = None
-
-    with open(filepath, 'r') as out:
-        for line in out.readlines():
-            if volumeid is None:
-                volumeid = _get_volumeid(line)
-            if publisherid is None:
-                publisherid = _get_publisherid(line)
-            if systemid is None:
-                systemid = _get_systemid(line)
-            if applicationid is None:
-                applicationid = _get_applicationid(line)
-            if logicalblock is None:
-                logicalblock = _get_logicalblock(line)
-            if volumesize is None:
-                volumesize = _get_volumesize(line)
-
-    if logicalblock is not None and volumesize is not None:
-        volumesize *= logicalblock
-    else:
-        volumesize = None
-
-    return _ISODataMedia(filepath, volumeid, publisherid, systemid,
-                         applicationid, volumesize)
-- 
2.21.0




More information about the Libosinfo mailing list