def __init__(self, fileName=''): self.doc = minidom.Document() self.rootNode = XmlUtils.GetRoot(self.doc) self.name = '' self.fileName = '' # 绝对路径 self.dirName = '' # 绝对路径 self.baseName = '' # 项目文件名 # 用于判断是否重新生成 makefile,此标志为真时,必重新生成 makefile # FIXME: 如果此标志为真,但是此时关闭工作区,下次启动时不会重建 makefile self.isModified = False self.tranActive = False self.modifyTime = 0 self.settings = None if fileName: try: self.doc = minidom.parse(fileName) except IOError: print 'Invalid fileName:', fileName raise IOError self.rootNode = XmlUtils.GetRoot(self.doc) self.name = XmlUtils.GetRoot(self.doc).getAttribute('Name')\ .encode('utf-8') self.fileName = os.path.abspath(fileName) #绝对路径 self.dirName, self.baseName = os.path.split(self.fileName) #绝对路径 self.modifyTime = Globals.GetFileModificationTime(fileName) self.settings = ProjectSettings( XmlUtils.FindFirstByTagName(self.rootNode, 'Settings'))
def GetDependencies(self, configuration): '''返回依赖的项目的名称列表(构建顺序)''' result = [] # 依赖的项目名称列表 # dependencies are located directly under the root level rootNode = XmlUtils.GetRoot(self.doc) if not rootNode: return result for i in rootNode.childNodes: if i.nodeName == 'Dependencies' \ and i.getAttribute('Name') == configuration: # have found it for j in i.childNodes: if j.nodeName == 'Project': result.append(XmlUtils.ReadString(j, 'Name')) return result # if we are here, it means no match for the given configuration # return the default dependencies node = XmlUtils.FindFirstByTagName(rootNode, 'Dependencies') if node: for i in node.childNodes: if i.nodeName == 'Project': result.append(XmlUtils.ReadString(i, 'Name')) return result
def GetDescription(self): rootNode = XmlUtils.GetRoot(self.doc) if rootNode: node = XmlUtils.FindFirstByTagName(rootNode, 'Description') if node: return XmlUtils.GetNodeContent(node) return ''
def __init__(self, node=None): self.configs = {} # 构建设置,即 xml 中的 Configuration 元素 self.globalSettings = None # 全局构建设置,一个 BuildConfigCommon self.projectType = '' # 项目类型,可运行、静态库、动态库三种 if node: # load configurations self.projectType = XmlUtils.ReadString(node, 'Type') for i in node.childNodes: if i.nodeName == 'Configuration': configName = XmlUtils.ReadString(i, 'Name') self.configs[configName] = BuildConfig.BuildConfig(i) # 若此设置的项目类型为空, 赋值为项目总的类型, 以免为空 if not self.configs[configName].projectType: self.configs[configName].projectType = self.projectType elif i.nodeName == 'GlobalSettings': self.globalSettings = BuildConfig.BuildConfigCommon( i, 'GlobalSettings') else: # create new settings with default values # 默认为可运行类型项目 #self.projectType = str(Project.Project.STATIC_LIBRARY) self.projectType = str(Project.Project.EXECUTABLE) self.configs['Debug'] = BuildConfig.BuildConfig() # Create global settings if it's not been loaded or by default if not self.globalSettings: self.globalSettings = BuildConfig.BuildConfigCommon( None, 'GlobalSettings')
def SetSettings(self, settings, autoSave=True): '''设置项目设置,并保存xml文件''' oldSettings = XmlUtils.FindFirstByTagName(XmlUtils.GetRoot(self.doc), 'Settings') if oldSettings: oldSettings.parentNode.removeChild(oldSettings) XmlUtils.GetRoot(self.doc).appendChild(settings.ToXmlNode()) if autoSave: self.DoSaveXmlFile() self.settings = settings
def SetGlobalSettings(self, globalSettings): settingsNode = XmlUtils.FindFirstByTagName(XmlUtils.GetRoot(self.doc), 'Settings') oldSettings = XmlUtils.FindFirstByTagName(settingsNode, 'GlobalSettings') if oldSettings: oldSettings.parentNode.removeChild(oldSettings) settingsNode.appendChild(globalSettings.ToXmlNode()) self.DoSaveXmlFile() self.settings = ProjectSettings(settingsNode)
def get_project_path(project_name, branch_name, code_path): manifest_real_path = os.path.realpath(code_path + MANIFEST_SOFT_LINK) tree = XmlUtils.read_xml(manifest_real_path) project_nodes = tree.findall("project") kv_map = {"name": project_name, "revision": branch_name} for node in project_nodes: if XmlUtils.check_node_by_attribute(node, kv_map): project_path = node.get("path") #print("project_path =", project_path) return project_path return None
def setTextAtPath(self, path, text): """ raises exception if path does not exist """ node = self.selectSingleNode(self.dom, path) if node: # self.setText (element, text) if node.nodeType == Node.ATTRIBUTE_NODE: node.value = text else: XmlUtils.setText(node, text) else: raise Exception, "element not found at %s" % path
def __init__(self, node = None, name = '', selected = False): #FIXME: selected 参数应该强制为 False,需要设置只能由 BuildMatrix 设置 self.name = name self.mappingList = [] #保存 ConfigMappingEntry 的列表 self.isSelected = selected if node: self.name = XmlUtils.ReadString(node, 'Name') self.isSelected = XmlUtils.ReadBool(node, 'Selected') for i in node.childNodes: if i.nodeName == 'Project': projName = XmlUtils.ReadString(i, 'Name') confName = XmlUtils.ReadString(i, 'ConfigName') self.mappingList.append(ConfigMappingEntry(projName, confName))
def __init__(self, node=None): self.configs = {} # 构建设置,即 xml 中的 Configuration 元素 self.globalSettings = None # 全局构建设置,一个 BuildConfigCommon self.projectType = '' # 项目类型,可运行、静态库、动态库三种 ### 文件自动导入的设置 ### # 实际目录路径(相对) -> 项目虚拟目录路径 self.direList = [] # 通配模式, ; 相隔, ;; 表示单个分号 self.inclGlob = '*.c;*.cpp;*.cxx;*.c++;*.cc;*.h;*.hpp;*.hxx;Makefile' # 同上 self.exclGlob = '*.mod.c' # 示例 self.direList.append({ 'Enable': 1, 'Recursive': 0, 'RealPath': '.', 'VirtPath': '.' }) if node: # load configurations self.projectType = XmlUtils.ReadString(node, 'Type') for i in node.childNodes: if i.nodeName == 'Configuration': configName = XmlUtils.ReadString(i, 'Name') self.configs[configName] = BuildConfig.BuildConfig(i) # 若此设置的项目类型为空, 赋值为项目总的类型, 以免为空 if not self.configs[configName].projectType: self.configs[configName].projectType = self.projectType elif i.nodeName == 'GlobalSettings': self.globalSettings = BuildConfig.BuildConfigCommon( i, 'GlobalSettings') elif i.nodeName == 'FileImportGlob': self.inclGlob = XmlUtils.ReadString(i, 'IncludeGlob') self.exclGlob = XmlUtils.ReadString(i, 'ExcludeGlob') del self.direList[:] for j in i.childNodes: if j.nodeName == 'Directory': d = {} d['Enable'] = int(XmlUtils.ReadBool(j, 'Enable')) d['Recursive'] = int( XmlUtils.ReadBool(j, 'Recursive')) d['RealPath'] = XmlUtils.ReadString(j, 'RealPath') d['VirtPath'] = XmlUtils.ReadString(j, 'VirtPath') self.direList.append(d) else: # create new settings with default values # 默认为可运行类型项目 #self.projectType = str(Project.Project.STATIC_LIBRARY) self.projectType = str(Project.Project.EXECUTABLE) self.configs['Debug'] = BuildConfig.BuildConfig() # Create global settings if it's not been loaded or by default if not self.globalSettings: self.globalSettings = BuildConfig.BuildConfigCommon( None, 'GlobalSettings')
def SetDependencies(self, deps, configuration): '''设置依赖 deps: 依赖的项目名称列表 configuration: 本依赖的名称''' # first try to locate the old node rootNode = XmlUtils.GetRoot(self.doc) for i in rootNode.childNodes: if i.nodeName == 'Dependencies' \ and i.getAttribute('Name') == configuration: rootNode.removeChild(i) break # create new dependencies node node = self.doc.createElement('Dependencies') node.setAttribute('Name', configuration.decode('utf-8')) rootNode.appendChild(node) for i in deps: child = self.doc.createElement('Project') child.setAttribute('Name', i.decode('utf-8')) node.appendChild(child) # save changes self.DoSaveXmlFile() self.SetModified(True)
def GenerateCode(comp, save_location): """\ Writes a component out to an XML file """ #make base tag and supply attributes as necessary root = ElementTree.Element( Component.GetName().lower(), { "name": comp.name, "description": comp.description, "version": XmlUtils.VERSION }) #add the tpcl_requirements tag and its text tpcl_req_elem = ElementTree.SubElement(root, "tpcl_requirements") tpcl_req_elem.text = comp.tpcl_requirements #add the categories root.append(ElementTree.Comment("categories")) for cat in comp.categories: cat_elem = ElementTree.SubElement(root, "category", {'name': cat}) #add the properties root.append(ElementTree.Comment("properties")) for name, tpcl_cost in comp.properties.iteritems(): propelem = ElementTree.SubElement(root, "property", {"name": name}) tpcl_cost_elem = ElementTree.SubElement(propelem, "tpcl_cost") tpcl_cost_elem.text = tpcl_cost et = ElementTree.ElementTree(root) XmlUtils.WriteElementTree(et, save_location, indent=True)
def Create(self, name, description, path, projectType): self.name = name self.fileName = os.path.join(path, name + os.extsep + PROJECT_FILE_SUFFIX) self.fileName = os.path.abspath(self.fileName) self.dirName, self.baseName = os.path.split(self.fileName) self.doc = minidom.Document() rootNode = self.doc.createElement('CodeLite_Project') self.doc.appendChild(rootNode) rootNode.setAttribute('Name', name.decode('utf-8')) descNode = self.doc.createElement('Description') XmlUtils.SetNodeContent(descNode, description) rootNode.appendChild(descNode) # Create the default virtual directories srcNode = self.doc.createElement('VirtualDirectory') srcNode.setAttribute('Name', 'src') rootNode.appendChild(srcNode) headNode = self.doc.createElement('VirtualDirectory') headNode.setAttribute('Name', 'include') rootNode.appendChild(headNode) # creae dependencies node depNode = self.doc.createElement('Dependencies') rootNode.appendChild(depNode) self.DoSaveXmlFile() # create a minimalist build settings settings = ProjectSettings() settings.SetProjectType(projectType) self.SetSettings(settings) self.SetModified(True)
def getValuesAtPath(self, xpath): """ return list of values for all nodes at specified path """ # nodes = self.selectNodes (self.dom, path) # if nodes: # return map (lambda e: self.getText(e), nodes) # else: # return [] return XmlUtils.getValuesAtPath(self.dom, xpath, self.xpath_delimiter)
def ParseCode(cat, save_location): """\ Returns the component serialized to the given XML file """ et = ElementTree.ElementTree(file=save_location) root = et.getroot() XmlUtils.VerifyVersion(root) try: cat.description = root.get("description") except KeyError: raise MalformedXmlError()
def GenerateCode(prop, save_location): """\ Writes a component out to an XML file """ #make base tag and supply attributes as necessary root = ElementTree.Element(Category.GetName().lower(), {"name": prop.name, "description": prop.description, "version": XmlUtils.VERSION}) et = ElementTree.ElementTree(root) XmlUtils.WriteElementTree(et, save_location, indent=True)
def __init__(self, node = None): self.name = '' self.toolPath = '' self.toolOptions = '' self.toolJobs = '' self.isActive = False # added on 2011-12-10: 替代 toolPath, toolOptions, toolJobs,为了泛用 self.command = '' if node: self.name = XmlUtils.ReadString(node, 'Name') self.toolPath = XmlUtils.ReadString(node, 'ToolPath') self.toolOptions = XmlUtils.ReadString(node, 'Options') self.toolJobs = XmlUtils.ReadString(node, 'Jobs', '1') self.isActive = XmlUtils.ReadBool(node, 'Active', self.isActive) self.command = node.getAttribute('Command') if not self.command: self.command = "%s %s" % (self.toolPath, self.toolOptions)
def __init__(self, fileName=''): self.doc = minidom.Document() self.rootNode = XmlUtils.GetRoot(self.doc) self.name = '' self.fileName = '' # 绝对路径 self.dirName = '' # 绝对路径 self.baseName = '' # 项目文件名 # 用于判断是否重新生成 makefile,此标志为真时,必重新生成 makefile # FIXME: 如果此标志为真,但是此时关闭工作区,下次启动时不会重建 makefile self.isModified = False self.tranActive = False self.modifyTime = 0 self.settings = None self.version = 0 if fileName: try: self.doc = minidom.parse(fileName) except IOError: print 'Invalid fileName:', fileName raise IOError self.rootNode = XmlUtils.GetRoot(self.doc) self.name = self.rootNode.getAttribute('Name').encode('utf-8') # 添加版本号 if self.rootNode.hasAttribute('Version'): self.version = int(self.rootNode.getAttribute('Version')) # 绝对路径 self.fileName = os.path.abspath(fileName) # NOTE: 还必须是真实路径 self.fileName = os.path.realpath(self.fileName) self.dirName, self.baseName = os.path.split(self.fileName) #绝对路径 self.modifyTime = GetMTime(fileName) self.settings = ProjectSettings( XmlUtils.FindFirstByTagName(self.rootNode, 'Settings')) self._ConvertSettings() self._CleanupSettings()
def GetAllFiles(self, absPath=False, projConfName=''): ''' 如果 projConfName 为空,返回结果会包含被忽略的文件,也就是全部文件 如果 projConfName 非空,排除 projConfName 配置指定忽略的文件 NOTE: 暂时只有在构建的时候才需要排除忽略的文件 ''' ignoredFiles = set() if projConfName: settings = self.GetSettings() bldConf = settings.GetBuildConfiguration(projConfName) if bldConf: ignoredFiles = bldConf.ignoredFiles.copy() if absPath: ds = DirSaver() os.chdir(self.dirName) files = self.GetFilesOfNode(XmlUtils.GetRoot(self.doc), True, '', ignoredFiles) #WSP_PATH_SEP + self.name) else: files = self.GetFilesOfNode(XmlUtils.GetRoot(self.doc), False, '', ignoredFiles) #WSP_PATH_SEP + self.name) return files
def DoSaveXmlFile(self, fileName=''): try: if not fileName: fileName = self.fileName dirName = os.path.dirname(fileName) if not os.path.exists(dirName): os.makedirs(dirName) f = open(fileName, 'wb') #self.doc.writexml(f, encoding = 'utf-8') f.write(XmlUtils.ToPrettyXmlString(self.doc)) self.SetProjectLastModifiedTime(self.GetProjFileLastModifiedTime()) f.close() except IOError: print 'IOError:', fileName raise IOError
def __init__(self, input_fw, xmlroot, tag, hashval): self.exists = False xml = xmlroot.find("%s[@%s='%s']"%(tag, Settings.hash_attrib, hashval)) if xml is None: # DNE. index = self.getNextIndex(xmlroot, tag) xml = XmlUtils.getOrAddChild(xmlroot, tag, {Settings.index_attrib:str(index), Settings.hash_attrib:hashval}) else: self.exists = True index = xml.get(Settings.index_attrib) self.output_fw = input_fw.getExtended("%s%s"%(tag, index)) self.xml = xml # If dir already exists, nothing happens. if not self.output_fw.createDir(): raise Exception()
def run(self): # For each matrix in the input source set: i = 0 for fw in self.input_fw.getSortedDirContents(): if len(self.xmlmerged) > 0: xmlmergedmatrix = self.xmlmerged.find( "%s[@%s='%d']" % (Settings.matrix_tag, Settings.index_attrib, i)) if xmlmergedmatrix is None: # FIXME: getting here indicates current input set matrix count != expected. # Raise a custom exception. raise Exception if Utils.isDecoded(xmlmergedmatrix): # DECODED ALREADY #print "Already processed (skipping): %s"%(Utils.prettyPrint1(xmlmergedmatrix)) i += 1 continue else: xmlmergedmatrix = None xmlmatrix = XmlUtils.getOrAddChild(self.xmlset, Settings.matrix_tag, {Settings.index_attrib: str(i)}) qrcode = QRCode.QRCode(self, PngUtils.PngWrapper(fw), self.output_fw, Dimension.matrix, xmlmergedmatrix, xmlmatrix) # Schedule a worker: self.scheduleWorker(qrcode, qrcode.work) i += 1 # Start the workers: self.startWorkers() # All workers complete without error? if not self.joinWorkers(): # FIXME: raise QRCode.FatalDecodeError( str("SetManager:run(): fatal error raised")) # First time run there will be no merged session branch so create it now by copying the first session run branch: if len(self.xmlmerged) == 0: for worker in self: self.xmlmerged.append(copy.deepcopy(worker.xml))
def ParseCode(prop, save_location): """\ Returns the component serialized to the given XML file """ et = ElementTree.ElementTree(file=save_location) root = et.getroot() XmlUtils.VerifyVersion(root) try: prop.description = root.get("description") prop.display_text = root.get("display_text") prop.rank = root.get("rank") prop.categories = [] for cat in root.findall("category"): prop.categories.append(cat.get("name")) prop.tpcl_display = root.findtext( "tpcl_display", Property.DEFAULT_TPCL_DISPLAY).strip() prop.tpcl_requires = root.findtext( "tpcl_requires", Property.DEFAULT_TPCL_REQUIRES).strip() except KeyError: raise MalformedXmlError()
def ParseCode(comp, save_location): """\ Returns the component serialized to the given XML file """ et = ElementTree.ElementTree(file=save_location) root = et.getroot() XmlUtils.VerifyVersion(root) try: comp.description = root.get("description") comp.category = root.get("category") comp.categories = [] for cat in root.findall("category"): comp.categories.append(cat.get("name")) comp.tpcl_requirements = root.findtext( "tpcl_requirements", Component.DEFAULT_TPCL_REQUIREMENTS).strip() comp.properties = {} for prop in root.findall("property"): comp.properties[prop.get("name")] = prop.findtext( "tpcl_cost", '(lambda (design) 0)').strip() except KeyError: raise MalformedXmlError()
def SetFiles(self, projectIns): '''copy files (and virtual directories) from src project to this project note that this call replaces the files that exists under this project''' # first remove all the virtual directories from this project # Remove virtual folders vdNode = XmlUtils.FindFirstByTagName(XmlUtils.GetRoot(self.doc), 'VirtualDirectory') while vdNode: XmlUtils.GetRoot(self.doc).removeChild(vdNode) vdNode = XmlUtils.FindFirstByTagName(XmlUtils.GetRoot(self.doc), 'VirtualDirectory') # copy the virtual directories from the src project for i in XmlUtils.GetRoot(self.doc).childNodes: if i.nodeName == 'VirtualDirectory': # FIXME: deep? newNode = i.cloneNode(1) XmlUtils.GetRoot(self.doc).appendChild(newNode) self.DoSaveXmlFile()
def GenerateCode(prop, save_location): """\ Writes a component out to an XML file """ #make base tag and supply attributes as necessary root = ElementTree.Element( Property.GetName().lower(), { "name": prop.name, "description": prop.description, "rank": prop.rank, "display_text": prop.display_text, "version": XmlUtils.VERSION }) #add the tpcl_requirements tag and its text tpcl_display_elem = ElementTree.SubElement(root, "tpcl_display") tpcl_display_elem.text = prop.tpcl_display tpcl_requires_elem = ElementTree.SubElement(root, "tpcl_requires") tpcl_requires_elem.text = prop.tpcl_requires #add the categories root.append(ElementTree.Comment("categories")) for cat in prop.categories: cat_elem = ElementTree.SubElement(root, "category", {'name': cat}) et = ElementTree.ElementTree(root) XmlUtils.WriteElementTree(et, save_location, indent=True)
"""\ The XML I/O module for Component objects. """ from game_objects import Component from rde.Exceptions import MalformedXmlError import XmlUtils ElementTree = XmlUtils.ImportElementTree() def GenerateCode(comp, save_location): """\ Writes a component out to an XML file """ #make base tag and supply attributes as necessary root = ElementTree.Element( Component.GetName().lower(), { "name": comp.name, "description": comp.description, "version": XmlUtils.VERSION }) #add the tpcl_requirements tag and its text tpcl_req_elem = ElementTree.SubElement(root, "tpcl_requirements") tpcl_req_elem.text = comp.tpcl_requirements #add the categories root.append(ElementTree.Comment("categories")) for cat in comp.categories: cat_elem = ElementTree.SubElement(root, "category", {'name': cat}) #add the properties root.append(ElementTree.Comment("properties")) for name, tpcl_cost in comp.properties.iteritems():
def run(self): ############################# for target in self.srcmanager.targetsets: i = target.get(Settings.index_attrib) srcset = "%s%s" % (Settings.matrix_set, i) input_fw = self.srcmanager.output_fw.getExtended("%s" % (srcset)) output_fw = self.output_fw.getExtended("%s/%s" % (Settings.session, srcset)) # FIXME: can't we get the workers to do this? if not output_fw.createDir(): raise OSError("Error: could not create directory: %s" % (output_fw.getPath())) extract_params = Extract.Parameters(Settings.x_tolerance, Settings.y_tolerance, Settings.region_tolerance, Settings.first_dimension) test = Extract.QRExtractor(input_fw, output_fw) extractcount = test.run(extract_params) print "%d QRs extracted" % (extractcount) decode_params = Decode.Parameters( Settings.maxpayloadsize, Settings.minpayloadsize, Settings.minimum_resize_dimension, Settings.maximum_resize_dimension, Settings.resize_increment) test2 = Decode.QRDecoder( output_fw, FilePathWrapper.FilePathWrapper("/tmp/jeremy")) test2.run(decode_params) exit(-1) ############################ # Get merged sessions (if available): xmlmerged = XmlUtils.getOrAddChild(self.xml, Settings.merged_tag) # Get hash over the decode args (the "session" hash): hasher = FileHasher.FileHasher(Settings.hashing_algorithm) Settings.getHash(hasher, Settings.hashlist1, 0) hashval = hasher.getHash() # Get a previous existing session or create a new one. # NOTE: getting a previous existing session is OK, because we might have new source sets with which to attempt decoding. xmlsession = XmlUtils.getOrAddChild(self.xml, Settings.session_tag, {Settings.hash_attrib: hashval}) index = xmlsession.get(Settings.index_attrib) if index is None: index = len(self.xml) - 2 xmlsession.set(Settings.index_attrib, str(index)) else: index = int(index) #print "Found previous session..." s = "" s += "\nPREVIOUS SESSION\n" s += "----------------\n" s += "QR count: %d\n" % (Utils.getQRCount(xmlmerged)) s += "Decoded total: %d\n" % (Utils.getDecodedCount(xmlmerged)) s += "Decoded remaining: %d" % (Utils.getQRCount(xmlmerged) - Utils.getDecodedCount(xmlmerged)) print s i2 = 0 # FIXME: also need a way to get ALL available sets, not just the targets (Settings option) # For each target source set: for target in self.srcmanager.targetsets: i = target.get(Settings.index_attrib) # Have we already attempted to decode this source set? xmlset = xmlsession.find( "%s[@%s='%s']" % (Settings.set_tag, Settings.index_attrib, i)) if xmlset is not None: # Yes. Skip it. i2 += 1 continue else: # No. Schedule it. xmlset = XmlUtils.getOrAddChild(xmlsession, Settings.set_tag, {Settings.index_attrib: i}) srcset = "%s%s" % (Settings.matrix_set, i) input_fw = self.srcmanager.output_fw.getExtended("%s" % (srcset)) output_fw = self.output_fw.getExtended( "%s%s/%s" % (Settings.session, index, srcset)) # FIXME: can't we get the workers to do this? if not output_fw.createDir(): raise OSError("Error: could not create directory: %s" % (output_fw.getPath())) prevDecodeCount = Utils.getDecodedCount(xmlmerged) # Create a setmanager instance: setManager = SetManager(input_fw, output_fw, xmlmerged, xmlset) setManager.run() currDecodeCount = Utils.getDecodedCount(xmlmerged) qrCount = Utils.getQRCount(xmlmerged) s = "" s += "\nPASS %d\n" % (i2 + 1) s += "---------\n" s += "QR count: %d\n" % (qrCount) s += "Decoded now: %d\n" % (currDecodeCount - prevDecodeCount) s += "Decoded total: %d\n" % (currDecodeCount) s += "Decoded remaining: %d" % (qrCount - currDecodeCount) print s i2 += 1
exit(-1) # Does xml config file exists? if xml_config_fw.isExistingFile(): # Yes. Open it. et = ElementTree.parse(xml_config_fw.getPath()) xmlroot = et.getroot() else: # No. Create it. xmlroot = ElementTree.Element(Settings.project_tag) et = ElementTree.ElementTree(xmlroot) ################################################################################################################# # ACQUAINT OURSELVES WITH PREVIOUS SESSION DATA xmlmerged = XmlUtils.getOrAddChild(xmlroot, Settings.merged_tag) target_qr_count = -1 prev_decoded_qr_count = 0 if len(xmlmerged) > 0: print "" print "Previous session detected..." # At this stage, all subsequent input have to contain this many QR codes (for consistency): xmlqrcount = xmlmerged.find("%s[@%s]"%(Settings.qr_tag, Settings.count_attrib)) if xmlqrcount is not None: target_qr_count = int(xmlqrcount.get(Settings.count_attrib)) print " QR count: %d"%(target_qr_count) else: print "Error: invalid config file: %s"%(xml_config_fw) exit(-1)
def run(self): if self.input_fw.isExistingDir(): # The input is assumed to be a directory of ORDERED source files (single set), so this structure will be created: # # <path to>/project/src/setx <--- input source will be copied to here # Where 'x' is the next available set index. # Generate input hash over input src: hasher = FileHasher.FileHasher(Settings.hashing_algorithm) srclist = [] for f in self.input_fw.getSortedDirContents(): if (f.isExistingFile()) and (f.getExtension(False).lower() == "png"): srclist.append(f.getPath()) hasher.addFile(srclist) hashval = hasher.getHash() xmlset = self.xml.find( "%s[@%s='%s']" % (Settings.set_tag, Settings.hash_attrib, hashval)) if xmlset is None: nextIndex = len(self.xml) xmlset = XmlUtils.getOrAddChild( self.xml, Settings.set_tag, { Settings.index_attrib: "%d" % (nextIndex), Settings.hash_attrib: hashval }) self.targetsets.append(xmlset) set_fw = self.output_fw.getExtended( "%s%d" % (Settings.matrix_set, nextIndex)) if not set_fw.createDir(): raise OSError("Error: could not create directory: %s" % (set_fw.getPath())) i = 0 for f in srclist: try: shutil.copyfile(f, (set_fw.getExtended( "%s-%d.png" % (Settings.converted_pdf, i)).getPath())) except: raise Exception( "Error: could not copy '%s' to: %s" % (f, set_fw.getExtended( "%s-%d.png" % (Settings.converted_pdf, i)).getPath())) # FIX ME: RAISE EXCEPTION INSTEAD i += 1 # Assert that input is a .pdf file # FIXME: is there a better check than just checking the extension??? elif (self.input_fw.isExistingFile()) and ( self.input_fw.getExtension() == ".pdf"): # The input is assumed to be a file that is required to be converted into source files (across multiple sets), so this structure will be created: # # <path to>/project/src/setx/ <--- converted files will be written here # <path to>/project/src/sety/ <--- converted files will be written here # <path to>/project/src/setz/ <--- converted files will be written here # etc. # Where 'x', 'y', 'z' are set indices incremented from the next available one. i = 0 while self.density.inRange(i): # Generate input hash over input src file + conversion params: hasher = FileHasher.FileHasher(Settings.hashing_algorithm) hasher.addFile([self.input_fw.getPath()]) Settings.getHash(hasher, Settings.hashlist2, i) hashval = hasher.getHash() xmlset = self.xml.find( "%s[@%s='%s']" % (Settings.set_tag, Settings.hash_attrib, hashval)) if xmlset is not None: # A converted set APPEARS (according to our xml config) to already exist. # We'll just silently skip over then... #print "Converted source set already exists: %s"%(hashval) pass else: # No converted set exists. We'll have to create one then. nextIndex = len(self.xml) xmlset = XmlUtils.getOrAddChild( self.xml, Settings.set_tag, { Settings.index_attrib: "%d" % (nextIndex), Settings.hash_attrib: hashval }) set_fw = self.output_fw.getExtended( "%s%d" % (Settings.matrix_set, nextIndex)) # FIXME: is this check too much? #if set_fw.isExistingDir(): # raise Exception("Error: directory already exists: %s"%(set_fw.getPath())) # Can't we get the workers to do this? # Short answer: no. There seem to be not properly understood issues at play. if not set_fw.createDir(): raise OSError("Error: could not create directory: %s" % (set_fw.getPath())) # Create a worker: worker = ConversionWorker(self.input_fw, set_fw) args = [self.density.getVal(i), self.depth, self.quality] # Schedule the worker: self.scheduleWorker(worker, worker.work, args) self.targetsets.append(xmlset) i += 1 # Start the workers: self.startWorkers() if not self.joinWorkers(): raise AssertionError( "Not all workers have completed succesfully!") else: # FIXME raise Exception("Invalid input: %s" % (self.input_fw.getPath()))