def importFitFromBuffer(bufferStr, activeFit=None): sFit = svcFit.getInstance() _, fits = Port.importAuto(bufferStr, activeFit=activeFit) for fit in fits: fit.character = sFit.character fit.damagePattern = sFit.pattern fit.targetResists = sFit.targetResists db.save(fit) return fits
def importFitFromBuffer(self, bufferStr, activeFit=None): sFit = svcFit.getInstance() _, fits = Port.importAuto(bufferStr, activeFit=activeFit) for fit in fits: fit.character = sFit.character fit.damagePattern = sFit.pattern fit.targetResists = sFit.targetResists db.save(fit) return fits
def saveImportedFit(fit): modified = fit.modified sFit = svcFit.getInstance() fit.character = sFit.character fit.damagePattern = sFit.pattern fit.targetResists = sFit.targetResists if len(fit.implants) > 0: fit.implantLocation = ImplantLocation.FIT else: useCharImplants = sFit.serviceFittingOptions[ "useCharacterImplantsByDefault"] fit.implantLocation = ImplantLocation.CHARACTER if useCharImplants else ImplantLocation.FIT fit.modified = modified # Restore modified field, which we changed by modifying the fit db.save(fit) return fit
def importFitFromBuffer(bufferStr, activeFit=None): # type: (str, object) -> object # TODO: catch the exception? # activeFit is reserved?, bufferStr is unicode? (assume only clipboard string? sFit = svcFit.getInstance() _, fits = Port.importAuto(bufferStr, activeFit=activeFit) for fit in fits: fit.character = sFit.character fit.damagePattern = sFit.pattern fit.targetResists = sFit.targetResists if len(fit.implants) > 0: fit.implantLocation = ImplantLocation.FIT else: useCharImplants = sFit.serviceFittingOptions["useCharacterImplantsByDefault"] fit.implantLocation = ImplantLocation.CHARACTER if useCharImplants else ImplantLocation.FIT db.save(fit) return fits
def importFitFromBuffer(bufferStr, activeFit=None): # type: (str, object) -> object # TODO: catch the exception? # activeFit is reserved?, bufferStr is unicode? (assume only clipboard string? sFit = svcFit.getInstance() _, fits = Port.importAuto(bufferStr, activeFit=activeFit) for fit in fits: fit.character = sFit.character fit.damagePattern = sFit.pattern fit.targetResists = sFit.targetResists if len(fit.implants) > 0: fit.implantLocation = ImplantLocation.FIT else: useCharImplants = sFit.serviceFittingOptions[ "useCharacterImplantsByDefault"] fit.implantLocation = ImplantLocation.CHARACTER if useCharImplants else ImplantLocation.FIT db.save(fit) return fits
def importPatterns(self, text): lookup = {} current = self.getTargetResistsList() for pattern in current: lookup[pattern.name] = pattern imports, num = es_TargetResists.importPatterns(text) for pattern in imports: if pattern.name in lookup: match = lookup[pattern.name] match.__dict__.update(pattern.__dict__) else: db.save(pattern) db.commit() lenImports = len(imports) if lenImports == 0: raise ImportError("No patterns found for import") if lenImports != num: raise ImportError("%d patterns imported from clipboard; %d had errors" % (num, num - lenImports))
def importPatterns(self, text): lookup = {} current = self.getTargetResistsList() for pattern in current: lookup[pattern.name] = pattern imports, num = es_TargetResists.importPatterns(text) for pattern in imports: if pattern.name in lookup: match = lookup[pattern.name] match.__dict__.update(pattern.__dict__) else: db.save(pattern) db.commit() lenImports = len(imports) if lenImports == 0: raise ImportError("No patterns found for import") if lenImports != num: raise ImportError( "%d patterns imported from clipboard; %d had errors" % (num, num - lenImports))
def renamePattern(p, newName): p.name = newName db.save(p)
def newPattern(name): p = es_TargetProfile() p.name = name db.save(p) return p
def importFitFromFiles(paths, iportuser=None): """ Imports fits from file(s). First processes all provided paths and stores assembled fits into a list. This allows us to call back to the GUI as fits are processed as well as when fits are being saved. returns """ sFit = svcFit.getInstance() fit_list = [] try: for path in paths: if iportuser: # Pulse msg = "Processing file:\n%s" % path pyfalog.debug(msg) processing_notify( iportuser, IPortUser.PROCESS_IMPORT | IPortUser.ID_UPDATE, msg) # wx.CallAfter(callback, 1, msg) with open(path, "rb") as file_: srcString = file_.read() dammit = UnicodeDammit(srcString) srcString = dammit.unicode_markup if len(srcString) == 0: # ignore blank files pyfalog.debug("File is blank.") continue try: _, fitsImport = Port.importAuto(srcString, path, iportuser=iportuser) fit_list += fitsImport except xml.parsers.expat.ExpatError: pyfalog.warning("Malformed XML in:\n{0}", path) return False, "Malformed XML in %s" % path # IDs = [] # NOTE: what use for IDs? numFits = len(fit_list) for idx, fit in enumerate(fit_list): # Set some more fit attributes and save fit.character = sFit.character fit.damagePattern = sFit.pattern fit.targetResists = sFit.targetResists if len(fit.implants) > 0: fit.implantLocation = ImplantLocation.FIT else: useCharImplants = sFit.serviceFittingOptions[ "useCharacterImplantsByDefault"] fit.implantLocation = ImplantLocation.CHARACTER if useCharImplants else ImplantLocation.FIT db.save(fit) # IDs.append(fit.ID) if iportuser: # Pulse pyfalog.debug( "Processing complete, saving fits to database: {0}/{1}", idx + 1, numFits) processing_notify( iportuser, IPortUser.PROCESS_IMPORT | IPortUser.ID_UPDATE, "Processing complete, saving fits to database\n(%d/%d) %s" % (idx + 1, numFits, fit.ship.name)) except UserCancelException: return False, "Processing has been canceled.\n" except Exception as e: pyfalog.critical("Unknown exception processing: {0}", path) pyfalog.critical(e) # TypeError: not all arguments converted during string formatting # return False, "Unknown Error while processing {0}" % path return False, "Unknown error while processing %s\n\n Error: %s" % ( path, e.message) return True, fit_list
def importFitFromFiles(paths, iportuser=None): """ Imports fits from file(s). First processes all provided paths and stores assembled fits into a list. This allows us to call back to the GUI as fits are processed as well as when fits are being saved. returns """ sFit = svcFit.getInstance() fit_list = [] try: for path in paths: if iportuser: # Pulse msg = "Processing file:\n%s" % path pyfalog.debug(msg) processing_notify(iportuser, IPortUser.PROCESS_IMPORT | IPortUser.ID_UPDATE, msg) # wx.CallAfter(callback, 1, msg) with open(path, "rb") as file_: srcString = file_.read() dammit = UnicodeDammit(srcString) srcString = dammit.unicode_markup if len(srcString) == 0: # ignore blank files pyfalog.debug("File is blank.") continue try: _, fitsImport = Port.importAuto(srcString, path, iportuser=iportuser) fit_list += fitsImport except xml.parsers.expat.ExpatError: pyfalog.warning("Malformed XML in:\n{0}", path) return False, "Malformed XML in %s" % path # IDs = [] # NOTE: what use for IDs? numFits = len(fit_list) for idx, fit in enumerate(fit_list): # Set some more fit attributes and save fit.character = sFit.character fit.damagePattern = sFit.pattern fit.targetResists = sFit.targetResists if len(fit.implants) > 0: fit.implantLocation = ImplantLocation.FIT else: useCharImplants = sFit.serviceFittingOptions["useCharacterImplantsByDefault"] fit.implantLocation = ImplantLocation.CHARACTER if useCharImplants else ImplantLocation.FIT db.save(fit) # IDs.append(fit.ID) if iportuser: # Pulse pyfalog.debug("Processing complete, saving fits to database: {0}/{1}", idx + 1, numFits) processing_notify( iportuser, IPortUser.PROCESS_IMPORT | IPortUser.ID_UPDATE, "Processing complete, saving fits to database\n(%d/%d) %s" % (idx + 1, numFits, fit.ship.name) ) except UserCancelException: return False, "Processing has been canceled.\n" except Exception as e: pyfalog.critical("Unknown exception processing: {0}", path) pyfalog.critical(e) # TypeError: not all arguments converted during string formatting # return False, "Unknown Error while processing {0}" % path return False, "Unknown error while processing %s\n\n Error: %s" % (path, e.message) return True, fit_list
def saveChanges(self, p): db.save(p)
def importFitFromFiles(self, paths, callback=None): """ Imports fits from file(s). First processes all provided paths and stores assembled fits into a list. This allows us to call back to the GUI as fits are processed as well as when fits are being saved. returns """ defcodepage = locale.getpreferredencoding() sFit = svcFit.getInstance() fits = [] for path in paths: if callback: # Pulse wx.CallAfter(callback, 1, "Processing file:\n%s" % path) file_ = open(path, "r") srcString = file_.read() if len(srcString) == 0: # ignore blank files continue codec_found = None # If file had ANSI encoding, decode it to unicode using detection # of BOM header or if there is no header try default # codepage then fallback to utf-16, cp1252 if isinstance(srcString, str): encoding_map = ( ('\xef\xbb\xbf', 'utf-8'), ('\xff\xfe\0\0', 'utf-32'), ('\0\0\xfe\xff', 'UTF-32BE'), ('\xff\xfe', 'utf-16'), ('\xfe\xff', 'UTF-16BE')) for bom, encoding in encoding_map: if srcString.startswith(bom): codec_found = encoding savebom = bom if codec_found is None: logger.info("Unicode BOM not found in file %s.", path) attempt_codecs = (defcodepage, "utf-8", "utf-16", "cp1252") for page in attempt_codecs: try: logger.info("Attempting to decode file %s using %s page.", path, page) srcString = unicode(srcString, page) codec_found = page logger.info("File %s decoded using %s page.", path, page) except UnicodeDecodeError: logger.info("Error unicode decoding %s from page %s, trying next codec", path, page) else: break else: logger.info("Unicode BOM detected in %s, using %s page.", path, codec_found) srcString = unicode(srcString[len(savebom):], codec_found) else: # nasty hack to detect other transparent utf-16 loading if srcString[0] == '<' and 'utf-16' in srcString[:128].lower(): codec_found = "utf-16" else: codec_found = "utf-8" if codec_found is None: return False, "Proper codec could not be established for %s" % path try: _, fitsImport = Port.importAuto(srcString, path, callback=callback, encoding=codec_found) fits += fitsImport except xml.parsers.expat.ExpatError: return False, "Malformed XML in %s" % path except Exception: logger.exception("Unknown exception processing: %s", path) return False, "Unknown Error while processing %s" % path IDs = [] numFits = len(fits) for i, fit in enumerate(fits): # Set some more fit attributes and save fit.character = sFit.character fit.damagePattern = sFit.pattern fit.targetResists = sFit.targetResists db.save(fit) IDs.append(fit.ID) if callback: # Pulse wx.CallAfter( callback, 1, "Processing complete, saving fits to database\n(%d/%d)" % (i + 1, numFits) ) return True, fits
def saveChanges(p): db.save(p)
def copyPattern(p): newP = copy.deepcopy(p) db.save(newP) return newP
def renamePattern(p, newName): p.name = newName db.save(p)
def newPattern(name): p = es_TargetResists(0.0, 0.0, 0.0, 0.0) p.name = name db.save(p) return p
def copyPattern(p): newP = copy.deepcopy(p) db.save(newP) return newP
def newPattern(name): p = es_TargetResists(0.0, 0.0, 0.0, 0.0) p.name = name db.save(p) return p
def saveChanges(p): db.save(p)
def importFitFromFiles(paths, callback=None): """ Imports fits from file(s). First processes all provided paths and stores assembled fits into a list. This allows us to call back to the GUI as fits are processed as well as when fits are being saved. returns """ defcodepage = locale.getpreferredencoding() sFit = svcFit.getInstance() fits = [] for path in paths: if callback: # Pulse pyfalog.debug("Processing file:\n{0}", path) wx.CallAfter(callback, 1, "Processing file:\n%s" % path) file_ = open(path, "r") srcString = file_.read() if len(srcString) == 0: # ignore blank files pyfalog.debug("File is blank.") continue codec_found = None # If file had ANSI encoding, decode it to unicode using detection # of BOM header or if there is no header try default # codepage then fallback to utf-16, cp1252 if isinstance(srcString, str): savebom = None encoding_map = (('\xef\xbb\xbf', 'utf-8'), ('\xff\xfe\0\0', 'utf-32'), ('\0\0\xfe\xff', 'UTF-32BE'), ('\xff\xfe', 'utf-16'), ('\xfe\xff', 'UTF-16BE')) for bom, encoding in encoding_map: if srcString.startswith(bom): codec_found = encoding savebom = bom if codec_found is None: pyfalog.info("Unicode BOM not found in file {0}.", path) attempt_codecs = (defcodepage, "utf-8", "utf-16", "cp1252") for page in attempt_codecs: try: pyfalog.info( "Attempting to decode file {0} using {1} page.", path, page) srcString = unicode(srcString, page) codec_found = page pyfalog.info("File {0} decoded using {1} page.", path, page) except UnicodeDecodeError: pyfalog.info( "Error unicode decoding {0} from page {1}, trying next codec", path, page) else: break else: pyfalog.info( "Unicode BOM detected in {0}, using {1} page.", path, codec_found) srcString = unicode(srcString[len(savebom):], codec_found) else: # nasty hack to detect other transparent utf-16 loading if srcString[0] == '<' and 'utf-16' in srcString[:128].lower(): codec_found = "utf-16" else: codec_found = "utf-8" if codec_found is None: return False, "Proper codec could not be established for %s" % path try: _, fitsImport = Port.importAuto(srcString, path, callback=callback, encoding=codec_found) fits += fitsImport except xml.parsers.expat.ExpatError: pyfalog.warning("Malformed XML in:\n{0}", path) return False, "Malformed XML in %s" % path except Exception as e: pyfalog.critical("Unknown exception processing: {0}", path) pyfalog.critical(e) return False, "Unknown Error while processing {0}" % path IDs = [] numFits = len(fits) for i, fit in enumerate(fits): # Set some more fit attributes and save fit.character = sFit.character fit.damagePattern = sFit.pattern fit.targetResists = sFit.targetResists db.save(fit) IDs.append(fit.ID) if callback: # Pulse pyfalog.debug( "Processing complete, saving fits to database: {0}/{1}", i + 1, numFits) wx.CallAfter( callback, 1, "Processing complete, saving fits to database\n(%d/%d)" % (i + 1, numFits)) return True, fits