def checkPaperLogic(self, xml): """ This method checks, if the given types of the paper elements are used in a convenient way. """ paper = xml.findall("{http://www.bitplant.de/template}paper") #InternalCounter self.defPaper = {} #Ignore duplicate attributions in element paper for pap in paper: if pap.get("type"): if not self.defPaper.has_key(pap.get("type")): self.defPaper[pap.get("type")] = pap else: break if len(self.defPaper) != len(paper): self.mb.setMessage( "warning", _(u"Duplicate value attribution of attribute %s within the children of element %s" ) % ("type", xml.tag)) paperSizes = PaperSizes() for pap in paper: self.checkDependendEnum(pap, "type", "layout", "value", ["oneside", "twoside"]) self.checkDependendEnum(pap, "type", "format", "value", paperSizes.getAllowedSizes()) self.checkDependendEnum(pap, "type", "orientation", "value", ["portrait", "landscape"])
def getPaper(self, xml, sxml, position): #Parse one paper element papers = xml.findall("{http://www.bitplant.de/template}paper") if len(papers) == 0: self.createNewPaper(sxml, position) else: defPaper = {} for paper in papers: if paper.get("type"): if not defPaper.has_key(paper.get("type")): defPaper[paper.get("type")] = paper for pap, val in defPaper.iteritems(): spap = ET.SubElement(sxml, "{http://www.bitplant.de/template}paper") self.saniSetPaper(val, spap, "layout", ["oneside", "twoside"], position) paperSizes = PaperSizes() self.saniSetPaper(val, spap, "format", paperSizes.getAllowedSizes(), position) self.saniSetPaper(val, spap, "orientation", ["portrait", "landscape"], position)
def getPaper(self, xml, sxml, position): #Parse one paper element papers = xml.findall("{http://www.bitplant.de/template}paper") if len(papers) == 0: self.createNewPaper(sxml, position) else: defPaper = {} for paper in papers: if paper.get("type"): if not defPaper.has_key(paper.get("type")): defPaper[paper.get("type")] = paper for pap, val in defPaper.iteritems(): spap = ET.SubElement(sxml, "{http://www.bitplant.de/template}paper") self.saniSetPaper(val, spap, "layout", ["oneside", "twoside"], position) paperSizes = PaperSizes() self.saniSetPaper(val, spap, "format", paperSizes.getAllowedSizes(), position) self.saniSetPaper(val, spap, "orientation", ["portrait", "landscape"], position)
def checkPaperLogic(self, xml): """ This method checks, if the given types of the paper elements are used in a convenient way. """ paper = xml.findall("{http://www.bitplant.de/template}paper") #InternalCounter self.defPaper = {} #Ignore duplicate attributions in element paper for pap in paper: if pap.get("type"): if not self.defPaper.has_key(pap.get("type")): self.defPaper[pap.get("type")] = pap else: break if len(self.defPaper) != len(paper): self.mb.setMessage("warning", _(u"Duplicate value attribution of attribute %s within the children of element %s") % ("type", xml.tag)) paperSizes = PaperSizes() for pap in paper: self.checkDependendEnum(pap, "type", "layout", "value", ["oneside", "twoside"]) self.checkDependendEnum(pap, "type", "format", "value", paperSizes.getAllowedSizes()) self.checkDependendEnum(pap, "type", "orientation", "value", ["portrait", "landscape"])
class parseDefaults(): def __init__(self, dir="."): self.defaults = {} ET._namespace_map["http://www.bitplant.de/template"] = "bit" self.defaultXml = ET.parse(dir + "/" + "default.xml") self.defaultRoot = self.defaultXml.getroot() self.setDefaultDesigner() self.setDefaultTemplate() self.setDefaultPage() self.setDefaultFrame() self.setDefaultContent() self.paperSizes = PaperSizes() def setDefaultDesigner(self): designer = self.defaultRoot self.defaults["designerName"] = designer.get("name", "Designer") self.defaults["designerLang"] = designer.get("lang", "en-EN") def setDefaultTemplate(self): template = self.defaultRoot.find(".//{http://www.bitplant.de/template}template") self.defaults["templateName"] = template.get("name", "Template") self.defaults["templateLang"] = template.get("lang", "en-EN") templateDescription = template.find(".//{http://www.bitplant.de/template}description") if templateDescription.text: self.defaults["templateDescription"] = templateDescription.text else: self.defaults["templateDescription"] = "This is a Description" templatePaper = template.findall(".//{http://www.bitplant.de/template}paper") #Get paper defaults from default file for paper in templatePaper: if paper.get("type") == "layout": if paper.get("value") in ["oneside", "twoside"]: self.defaults["templateLayoutValue"] = paper.get("value", "oneside") if paper.get("type") == "orientation": if paper.get("value") in ["portrait", "landscape"]: self.defaults["templateOrientationValue"] = paper.get("value", "portrait") if paper.get("type") == "format": if paper.get("value") in self.paperSizes.getAllowedSizes(): self.defaults["templateFormatValue"] = paper.get("value", "a4") #If default file is not fully populated with paper values self.addFallbackDefault("templateLayoutValue", "oneside") self.addFallbackDefault("templateOrientationValue", "portrait") self.addFallbackDefault("templateFormatValue", "a4") templateDimension = template.findall(".//{http://www.bitplant.de/template}dimension") for dim in templateDimension: self.iterAddDimPos(dim, "template", "height", ["mm", "cm", "pt", "inch"]) self.iterAddDimPos(dim, "template", "width", ["mm", "cm", "pt", "inch"]) self.addFallbackDefault("templateHeightUnit", "mm") self.addFallbackDefault("templateHeightValue", "240") self.addFallbackDefault("templateWidthUnit", "mm") self.addFallbackDefault("templateWidthValue", "175") templatePosition = template.findall(".//{http://www.bitplant.de/template}position") for pos in templatePosition: self.iterAddDimPos(pos, "template", "top", ["mm", "cm", "pt", "inch"]) self.iterAddDimPos(pos, "template", "left", ["mm", "cm", "pt", "inch"]) self.iterAddDimPos(pos, "template", "right", ["mm", "cm", "pt", "inch"]) self.iterAddDimPos(pos, "template", "bottom", ["mm", "cm", "pt", "inch"]) self.addFallbackDefault("templateTopUnit", "mm") self.addFallbackDefault("templateTopValue", "35") self.addFallbackDefault("templateLeftUnit", "mm") self.addFallbackDefault("templateLeftValue", "25") self.addFallbackDefault("templateRightUnit", "mm") self.addFallbackDefault("templateRightValue", "10") self.addFallbackDefault("templateBottomUnit", "mm") self.addFallbackDefault("templateBottomValue", "22") def iterAddDimPos(self, elem, kind, type, unit_enum): if elem.get("type") == type: if elem.get("unit") in unit_enum: self.defaults[kind + type.capitalize() + "Unit"] = elem.get("unit", "mm") if re.match(ur"\A[0-9]{1,}(\.[0-9]{1,})?\Z", elem.get("value"), re.UNICODE): self.defaults[kind + type.capitalize() + "Value"] = elem.get("value") else: if re.match(ur"\A[0-9]{1,}(\.[0-9]{1,})?\Z", elem.get("value"), re.UNICODE): self.defaults[kind + type.capitalize() + "Value"] = elem.get("value")
class parseDefaults(): def __init__(self, dir="."): self.defaults = {} ET._namespace_map["http://www.bitplant.de/template"] = "bit" self.defaultXml = ET.parse(dir + "/" + "default.xml") self.defaultRoot = self.defaultXml.getroot() self.setDefaultDesigner() self.setDefaultTemplate() self.setDefaultPage() self.setDefaultFrame() self.setDefaultContent() self.paperSizes = PaperSizes() def setDefaultDesigner(self): designer = self.defaultRoot self.defaults["designerName"] = designer.get("name", "Designer") self.defaults["designerLang"] = designer.get("lang", "en-EN") def setDefaultTemplate(self): template = self.defaultRoot.find( ".//{http://www.bitplant.de/template}template") self.defaults["templateName"] = template.get("name", "Template") self.defaults["templateLang"] = template.get("lang", "en-EN") templateDescription = template.find( ".//{http://www.bitplant.de/template}description") if templateDescription.text: self.defaults["templateDescription"] = templateDescription.text else: self.defaults["templateDescription"] = "This is a Description" templatePaper = template.findall( ".//{http://www.bitplant.de/template}paper") #Get paper defaults from default file for paper in templatePaper: if paper.get("type") == "layout": if paper.get("value") in ["oneside", "twoside"]: self.defaults["templateLayoutValue"] = paper.get( "value", "oneside") if paper.get("type") == "orientation": if paper.get("value") in ["portrait", "landscape"]: self.defaults["templateOrientationValue"] = paper.get( "value", "portrait") if paper.get("type") == "format": if paper.get("value") in self.paperSizes.getAllowedSizes(): self.defaults["templateFormatValue"] = paper.get( "value", "a4") #If default file is not fully populated with paper values self.addFallbackDefault("templateLayoutValue", "oneside") self.addFallbackDefault("templateOrientationValue", "portrait") self.addFallbackDefault("templateFormatValue", "a4") templateDimension = template.findall( ".//{http://www.bitplant.de/template}dimension") for dim in templateDimension: self.iterAddDimPos(dim, "template", "height", ["mm", "cm", "pt", "inch"]) self.iterAddDimPos(dim, "template", "width", ["mm", "cm", "pt", "inch"]) self.addFallbackDefault("templateHeightUnit", "mm") self.addFallbackDefault("templateHeightValue", "240") self.addFallbackDefault("templateWidthUnit", "mm") self.addFallbackDefault("templateWidthValue", "175") templatePosition = template.findall( ".//{http://www.bitplant.de/template}position") for pos in templatePosition: self.iterAddDimPos(pos, "template", "top", ["mm", "cm", "pt", "inch"]) self.iterAddDimPos(pos, "template", "left", ["mm", "cm", "pt", "inch"]) self.iterAddDimPos(pos, "template", "right", ["mm", "cm", "pt", "inch"]) self.iterAddDimPos(pos, "template", "bottom", ["mm", "cm", "pt", "inch"]) self.addFallbackDefault("templateTopUnit", "mm") self.addFallbackDefault("templateTopValue", "35") self.addFallbackDefault("templateLeftUnit", "mm") self.addFallbackDefault("templateLeftValue", "25") self.addFallbackDefault("templateRightUnit", "mm") self.addFallbackDefault("templateRightValue", "10") self.addFallbackDefault("templateBottomUnit", "mm") self.addFallbackDefault("templateBottomValue", "22") def iterAddDimPos(self, elem, kind, type, unit_enum): if elem.get("type") == type: if elem.get("unit") in unit_enum: self.defaults[kind + type.capitalize() + "Unit"] = elem.get( "unit", "mm") if re.match(ur"\A[0-9]{1,}(\.[0-9]{1,})?\Z", elem.get("value"), re.UNICODE): self.defaults[kind + type.capitalize() + "Value"] = elem.get("value") else: if re.match(ur"\A[0-9]{1,}(\.[0-9]{1,})?\Z", elem.get("value"), re.UNICODE): self.defaults[kind + type.capitalize() + "Value"] = elem.get("value")
def getPaper(self, xml): #Parse one paper element paper = xml.findall("{http://www.bitplant.de/template}paper") if len(paper) == 0: self.mb.setMessage("notice", _(u"The element %s includes no element %s") % (xml.tag, "paper")) return elif len(paper) > 3: self.mb.setMessage("error", _(u"The element %s must contain at least zero and at most three elements %s") % (xml.tag, "paper")) for pap in paper: self.checkPermission(pap, ["type", "value"]) self.checkRequiredEnum(pap, "type", ["layout", "orientation", "format"]) paperSizes = PaperSizes() self.checkRequiredEnum(pap, "value", ["portrait", "landscape", "oneside", "twoside"] + paperSizes.getAllowedSizes())