Skip to content

Commit fc55363

Browse files
author
dom
committed
factorize the extraction of object internal type from it's representation
1 parent 27d1008 commit fc55363

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

swig/openscap_api.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ def _import_helper():
5656

5757
import os
5858

59+
def extract_type_from_obj(obj):
60+
# Extract the name of structure from the representation of the object
61+
# "<Swig Object of type 'struct xccdf_result_iterator *' at 0x7f8f65fc1390>"
62+
# or "<Swig Object of type 'oval_agent_session_t *' at 0x7f9aa2cdf360>"
63+
return re.findall(r"type '(struct )?(\b\S*\b)", obj.__repr__())[0][1]
64+
5965
class OSCAP_List(list):
6066
"""OSCAP List class is designed to store lists generated from openscap iterators. All functions that return iterators
6167
are preprocessed by creation of OSCAP List instance and move all objects given by oscap list iteration loop to list.
@@ -136,11 +142,7 @@ def __init__(self, object, instance=None):
136142
@staticmethod
137143
def new(retobj):
138144
if type(retobj).__name__ in ('SwigPyObject', 'PySwigObject'):
139-
# Extract the name of structure from the representation of the object
140-
# "<Swig Object of type 'struct xccdf_result_iterator *' at 0x7f8f65fc1390>"
141-
# or "<Swig Object of type 'oval_agent_session_t *' at 0x7f9aa2cdf360>"
142-
structure = re.findall(r"type '(struct )?(\b\S*\b)", retobj.__repr__())[0][1]
143-
return OSCAP_Object(structure, retobj)
145+
return OSCAP_Object(extract_type_from_obj(retobj), retobj)
144146
else:
145147
return retobj
146148

@@ -322,9 +324,8 @@ def __start_callback(self, rule, obj):
322324
return obj[0](OSCAP_Object("xccdf_rule", rule), obj[1])
323325

324326
def __output_callback(self, result, obj):
325-
# the returned object can be a rule_result or an oval_definition_result, so I extract the right name from the object repr.
326-
structure = re.findall(r"type '(struct )?(\b\S*\b)", result.__repr__())[0][1]
327-
return obj[0](OSCAP_Object(structure, rule_result), obj[1])
327+
# the returned object can be a rule_result or an oval_definition_result, so I extract the right name from the object repr.
328+
return obj[0](OSCAP_Object(extract_type_from_obj(result), result), obj[1])
328329

329330
def register_start_callback(self, cb, usr):
330331
if self.object != "xccdf_policy_model":

0 commit comments

Comments
 (0)