Skip to content

Commit bee4bc2

Browse files
committed
xpath - Implement the BodyElementValue_List type, to represent a list of other BEVs
1 parent 117c5c2 commit bee4bc2

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

AdvancedHTMLParser/xpath/_body.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,84 @@ def setValue(self, newValue):
631631
)
632632

633633

634+
class BodyElementValue_List(BodyElementValue):
635+
'''
636+
BodyElementValue_List - A BodyElementValue which is a list of other values.
637+
638+
All elements within this list will be other BodyElementValues, rather than raw values.
639+
'''
640+
641+
VALUE_TYPE = BODY_VALUE_TYPE_LIST
642+
643+
644+
def __init__(self, initialValues=None):
645+
'''
646+
__init__ - Create this object
647+
648+
649+
@param initialValues <None/list> Initial values to load into the internal list.
650+
'''
651+
if not initialValues:
652+
initialValues = []
653+
654+
BodyElementValue.__init__(self, initialValues)
655+
656+
657+
def setValue(self, newValues):
658+
'''
659+
setValue - Replace the previous lists with new list
660+
661+
662+
@param newValues list<...> - A new list from which to create the internal list.
663+
664+
All items must have a related BodyElementValue type, or already be one.
665+
'''
666+
667+
updatedList = [ ( issubclass(thisVal.__class__, BodyElementValue) and thisval ) or _pythonValueToBodyElementValue(thisVal) for thisVal in newValues ]
668+
669+
self.value = updatedList
670+
671+
672+
# PYTHON_TYPE_NAME_TO_BODY_VALUE_CLASS - The __name__ of the type(val), to the associated BEV container
673+
PYTHON_TYPE_NAME_TO_BODY_VALUE_CLASS = {
674+
'int' : BodyElementValue_Number,
675+
'float' : BodyElementValue_Number,
676+
'str' : BodyElementValue_String,
677+
'unicode' : BodyElementValue_String,
678+
'bool' : BodyElementValue_Boolean,
679+
'NoneType' : BodyElementValue_Null,
680+
'list' : BodyElementValue_List,
681+
'tuple' : BodyElementValue_List,
682+
'set' : BodyElementValue_List,
683+
}
684+
685+
686+
def _pythonValueToBodyElementValue(pythonValue):
687+
'''
688+
_pythonValueToBodyElementValue - Convert a native/raw python value to
689+
690+
its respective BodyElementValue subclassed container.
691+
692+
693+
@param pythonValue <???> - The python "raw" value (such as an int or a string)
694+
695+
696+
@return <BodyElementValue subclass> - A created container body element value wrapping provided value
697+
'''
698+
pythonValueTypeName = type(pythonValue).__name__
699+
700+
try:
701+
bodyElementValueClass = PYTHON_TYPE_NAME_TO_BODY_VALUE_CLASS[ pythonValueTypeName ]
702+
703+
except KeyError:
704+
# XXX: Exception or just use an "unknown" base BodyElementValue?
705+
# Maybe better to just shut it down early rather than introduce questionable things on down the line
706+
raise XPathRuntimeError('Failed to find a matching BodyElementValue type from python type "%s" ! Repr: %s' %( pythonValueTypeName, repr(pythonValue) ) )
707+
708+
return bodyElementValueClass( pythonValue )
709+
710+
711+
634712
#############################
635713
## Static Values ##
636714
#############################

0 commit comments

Comments
 (0)