def _loadCallsign(self, element, context): if self.ForeignCallsign is None: self.ForeignCallsign = ForeignHelper(self, "Callsign") lang = element.get(u"lang") if lang is not None: self.ForeignCallsign.Value = unicode(element.text) self.ForeignCallsign.LangCode = unicode(lang) else: self.Callsign = unicode(element.text)
def __storm_loaded__(self): self.ForeignCallsign = ForeignHelper(self, "Callsign")
def __storm_invalidated__(self): self.ForeignCallsign = ForeignHelper(self, "Callsign")
class Transmission(PriyomBase, XMLIntf.XMLStorm): __storm_table__ = "transmissions" ID = Int(primary=True) BroadcastID = Int() Broadcast = Reference(BroadcastID, Broadcast.ID) Callsign = Unicode() Timestamp = Int() ClassID = Int() RecordingURL = Unicode() Remarks = Unicode() xmlMapping = {u"Recording": "RecordingURL", u"Remarks": "Remarks"} def updateBlocks(self): blocks = [] store = Store.of(self) for table in self.Class.tables: for block in store.find( table.PythonClass, table.PythonClass.TransmissionID == self.ID): blocks.append(block) blocks.sort(cmp=lambda x, y: cmp(x.Order, y.Order)) self._blocks = blocks def __init__(self): super(Transmission, self).__init__() self.ForeignCallsign = None self._blocks = None self._blockUpdate = None def __storm_invalidated__(self): self.ForeignCallsign = ForeignHelper(self, "Callsign") def __storm_loaded__(self): self.ForeignCallsign = ForeignHelper(self, "Callsign") def _loadCallsign(self, element, context): if self.ForeignCallsign is None: self.ForeignCallsign = ForeignHelper(self, "Callsign") lang = element.get(u"lang") if lang is not None: self.ForeignCallsign.Value = unicode(element.text) self.ForeignCallsign.LangCode = unicode(lang) else: self.Callsign = unicode(element.text) def _loadBroadcastID(self, element, context): self.Broadcast = context.resolveId(Broadcast, int(element.text)) def _loadClassID(self, element, context): self.Class = context.resolveId(TransmissionClass, int(element.text)) """ Note that loading the contents is, in contrast to most other import operations, replacing instead of merging. """ def _loadContents(self, node, context): store = Store.of(self) for block in self.blocks: store.remove(block) if self.Class is None: print("Invalid class id: %d" % (self.ClassID)) return False for group in node.iterfind("{{{0}}}group".format( XMLIntf.importNamespace)): table = store.find( TransmissionTable, TransmissionTable.TableName == group.getAttribute(u"name")).any() if table is None: print("Invalid transmission class table: %s" % (group.get(u"name"))) return False block = table.PythonClass(store) block.fromDom(group, context) def _metadataToDom(self, parentNode): XMLIntf.appendTextElements(parentNode, ((u"BroadcastID", self.Broadcast.ID), (u"ClassID", self.Class.ID), (u"Callsign", self.Callsign))) self.ForeignCallsign.toDom(parentNode, u"Callsign") XMLIntf.appendDateElement(parentNode, u"Timestamp", self.Timestamp) XMLIntf.appendTextElements(parentNode, ((u"Recording", self.RecordingURL), (u"Remarks", self.Remarks))) def toDom(self, parentNode, flags=None): transmission = XMLIntf.SubElement(parentNode, u"transmission") XMLIntf.SubElement(transmission, u"ID").text = unicode(self.ID) self._metadataToDom(transmission) if flags is not None and self.Broadcast is not None and "with-freqs" in flags: for freq in self.Broadcast.Frequencies: freq.toDom(transmission) contents = XMLIntf.SubElement(transmission, u"Contents") for block in self.blocks: block.toDom(contents) def loadElement(self, tag, element, context): try: { u"BroadcastID": self._loadBroadcastID, u"ClassID": self._loadClassID, u"Callsign": self._loadCallsign, u"Contents": self._loadContents }[tag](element, context) except KeyError: pass def clear(self): store = Store.of(self) for table in list(self.Class.tables): for block in list( store.find(table.PythonClass, table.PythonClass.TransmissionID == self.ID)): block.deleteForeignSupplements() store.remove(block) def setParsedContents(self, contents): self.clear() store = Store.of(self) for order, rowData in enumerate(contents): tableClass = rowData[0].PythonClass contentDict = rowData[1] row = tableClass(store) try: row.Transmission = self row.Order = order for key, (value, foreign) in contentDict.iteritems(): setattr(row, key, value) if foreign is not None: supplement = row.supplements[key] try: supplement.LangCode = foreign[0] supplement.Value = foreign[1] except: supplement.removeSupplement() raise except: store.remove(row) raise @property def Contents(self): return u" ".join((unicode(block) for block in self.blocks)) @Contents.setter def Contents(self, value): try: parsed = self.Class.parsePlainText(value) except NodeError as e: raise ValueError(unicode(e)) self.setParsedContents(parsed) self.touch() @Contents.deleter def Contents(self): self.clear() def __unicode__(self): return u"Transmission with callsign {0} and {1} segments".format( self.Callsign, len(self.blocks)) @property def blocks(self): if not hasattr(self, "_blocks") or not hasattr(self, "_blockUpdate"): self._blocks = None self._blockUpdate = None self.Modified = AutoReload if self._blockUpdate is None or self._blockUpdate != self.Modified: self.updateBlocks() return self._blocks
def initSupplements(self): self.supplements = {} for field in self.fields: self.supplements[field.FieldName] = ForeignHelper( self, field.FieldName)
class Transmission(PriyomBase, XMLIntf.XMLStorm): __storm_table__ = "transmissions" ID = Int(primary = True) BroadcastID = Int() Broadcast = Reference(BroadcastID, Broadcast.ID) Callsign = Unicode() Timestamp = Int() ClassID = Int() RecordingURL = Unicode() Remarks = Unicode() xmlMapping = { u"Recording": "RecordingURL", u"Remarks": "Remarks" } def updateBlocks(self): blocks = [] store = Store.of(self) for table in self.Class.tables: for block in store.find(table.PythonClass, table.PythonClass.TransmissionID == self.ID): blocks.append(block) blocks.sort(cmp=lambda x,y: cmp(x.Order, y.Order)) self._blocks = blocks def __init__(self): super(Transmission, self).__init__() self.ForeignCallsign = None self._blocks = None self._blockUpdate = None def __storm_invalidated__(self): self.ForeignCallsign = ForeignHelper(self, "Callsign") def __storm_loaded__(self): self.ForeignCallsign = ForeignHelper(self, "Callsign") def _loadCallsign(self, element, context): if self.ForeignCallsign is None: self.ForeignCallsign = ForeignHelper(self, "Callsign") lang = element.get(u"lang") if lang is not None: self.ForeignCallsign.Value = unicode(element.text) self.ForeignCallsign.LangCode = unicode(lang) else: self.Callsign = unicode(element.text) def _loadBroadcastID(self, element, context): self.Broadcast = context.resolveId(Broadcast, int(element.text)) def _loadClassID(self, element, context): self.Class = context.resolveId(TransmissionClass, int(element.text)) """ Note that loading the contents is, in contrast to most other import operations, replacing instead of merging. """ def _loadContents(self, node, context): store = Store.of(self) for block in self.blocks: store.remove(block) if self.Class is None: print("Invalid class id: %d" % (self.ClassID)) return False for group in node.iterfind("{{{0}}}group".format(XMLIntf.importNamespace)): table = store.find(TransmissionTable, TransmissionTable.TableName == group.getAttribute(u"name")).any() if table is None: print("Invalid transmission class table: %s" % (group.get(u"name"))) return False block = table.PythonClass(store) block.fromDom(group, context) def _metadataToDom(self, parentNode): XMLIntf.appendTextElements(parentNode, ( (u"BroadcastID", self.Broadcast.ID), (u"ClassID", self.Class.ID), (u"Callsign", self.Callsign) ) ) self.ForeignCallsign.toDom(parentNode, u"Callsign") XMLIntf.appendDateElement(parentNode, u"Timestamp", self.Timestamp) XMLIntf.appendTextElements(parentNode, ( (u"Recording", self.RecordingURL), (u"Remarks", self.Remarks) ) ) def toDom(self, parentNode, flags=None): transmission = XMLIntf.SubElement(parentNode, u"transmission") XMLIntf.SubElement(transmission, u"ID").text = unicode(self.ID) self._metadataToDom(transmission) if flags is not None and self.Broadcast is not None and "with-freqs" in flags: for freq in self.Broadcast.Frequencies: freq.toDom(transmission) contents = XMLIntf.SubElement(transmission, u"Contents") for block in self.blocks: block.toDom(contents) def loadElement(self, tag, element, context): try: { u"BroadcastID": self._loadBroadcastID, u"ClassID": self._loadClassID, u"Callsign": self._loadCallsign, u"Contents": self._loadContents }[tag](element, context) except KeyError: pass def clear(self): store = Store.of(self) for table in list(self.Class.tables): for block in list(store.find(table.PythonClass, table.PythonClass.TransmissionID == self.ID)): block.deleteForeignSupplements() store.remove(block) def setParsedContents(self, contents): self.clear() store = Store.of(self) for order, rowData in enumerate(contents): tableClass = rowData[0].PythonClass contentDict = rowData[1] row = tableClass(store) try: row.Transmission = self row.Order = order for key, (value, foreign) in contentDict.iteritems(): setattr(row, key, value) if foreign is not None: supplement = row.supplements[key] try: supplement.LangCode = foreign[0] supplement.Value = foreign[1] except: supplement.removeSupplement() raise except: store.remove(row) raise @property def Contents(self): return u" ".join((unicode(block) for block in self.blocks)) @Contents.setter def Contents(self, value): try: parsed = self.Class.parsePlainText(value) except NodeError as e: raise ValueError(unicode(e)) self.setParsedContents(parsed) self.touch() @Contents.deleter def Contents(self): self.clear() def __unicode__(self): return u"Transmission with callsign {0} and {1} segments".format(self.Callsign, len(self.blocks)) @property def blocks(self): if not hasattr(self, "_blocks") or not hasattr(self, "_blockUpdate"): self._blocks = None self._blockUpdate = None self.Modified = AutoReload if self._blockUpdate is None or self._blockUpdate != self.Modified: self.updateBlocks() return self._blocks