Module span_data
[hide private]
[frames] | no frames]

Source Code for Module span_data

  1  """ 
  2  Parser for the span element 
  3  """ 
  4   
  5  # Modified for KAF/NAF 
  6   
  7  from lxml import etree 
  8  from lxml.objectify import dump 
  9   
10 -class Ctarget:
11 """ 12 This class encapsulates the target element within a span object 13 """
14 - def __init__(self,node=None):
15 """ 16 Constructor of the object 17 @type node: xml Element or None (to create and empty one) 18 @param node: this is the node of the element. If it is None it will create a new object 19 """ 20 self.type = 'NAF/KAF' 21 if node is None: 22 self.node = etree.Element('target') 23 else: 24 self.node = node
25
26 - def get_id(self):
27 """ 28 Returns the id of the element 29 @rtype: string 30 @return: the id of the element 31 """ 32 return self.node.get('id')
33
34 - def set_id(self,this_id):
35 """ 36 Set the id of the element 37 @type this_id: string 38 @param this_id: the id for the element 39 """ 40 self.node.set('id',this_id)
41
42 - def set_head(self,head):
43 """ 44 Sets value of head 45 """ 46 self.node.set('head',head)
47
48 - def is_head(self):
49 """ 50 Returns whether this target is set as head or not 51 @rtype: boolean 52 @return: whether this target is set as head or not 53 """ 54 head = self.node.get('head') 55 return (head is not None)
56
57 - def get_node(self):
58 """ 59 Returns the node of the element 60 @rtype: xml Element 61 @return: the node of the element 62 """ 63 return self.node
64 65
66 -class Cspan:
67 """ 68 This class encapsulates a span object in KAF/NAF 69 """
70 - def __init__(self,node=None):
71 """ 72 Constructor of the object 73 @type node: xml Element or None (to create and empty one) 74 @param node: this is the node of the element. If it is None it will create a new object 75 """ 76 self.type = 'NAF/KAF' 77 if node is None: 78 self.node = etree.Element('span') 79 else: 80 self.node = node
81
82 - def get_id_head(self):
83 ''' 84 Returns the id of the target that is set as "head" 85 @rtype: string 86 @return: the target id (or None) of the head target 87 ''' 88 id_head = None 89 for target_node in self: 90 if target_node.is_head(): 91 id_head = target_node.get_id() 92 break 93 return id_head
94
95 - def add_target_id(self,this_id):
96 """ 97 Adds a new target to the span with the specified id 98 @type this_id: string 99 @param this_id: the id of the new target 100 """ 101 new_target = Ctarget() 102 new_target.set_id(this_id) 103 self.node.append(new_target.get_node())
104
105 - def create_from_ids(self,list_ids):
106 """ 107 Adds new targets to the span with the specified ids 108 @type list_ids: list 109 @param list_ids: list of identifiers 110 """ 111 for this_id in list_ids: 112 new_target = Ctarget() 113 new_target.set_id(this_id) 114 self.node.append(new_target.get_node())
115
116 - def create_from_targets(self,list_targs):
117 """ 118 Adds new targets to the span that are defined in a list 119 @type list_targs: list 120 @param list_targs: list of Ctargets 121 """ 122 for this_target in list_targs: 123 self.node.append(this_target.get_node())
124 125
126 - def add_target(self,target):
127 """ 128 Adds a target object to the span 129 @type target: L{Ctarget} 130 @param target: target object 131 """ 132 self.node.append(target.get_node())
133 134 135
136 - def __get_target_nodes(self):
137 for target_node in self.node.findall('target'): 138 yield target_node
139
140 - def __iter__(self):
141 """ 142 Iterator taht returns the target objects 143 @rtype: L{Ctarget} 144 @return: list of target objects (iterator) 145 """ 146 for target_node in self.__get_target_nodes(): 147 yield Ctarget(target_node)
148
149 - def get_span_ids(self):
150 """ 151 Returns the list of target ids for the span 152 @rtype: list 153 @return: list of target ids 154 """ 155 return [t_obj.get_id() for t_obj in self]
156
157 - def __str__(self):
158 return dump(self.node)
159
160 - def get_node(self):
161 """ 162 Returns the node of the element 163 @rtype: xml Element 164 @return: the node of the element 165 """ 166 return self.node
167