def dump(self, filename='category.dump.bz2'): '''Saves the contents of the dictionaries superclassDB and catContentDB to disk. ''' if not os.path.isabs(filename): filename = config.datafilepath(filename) if self.catContentDB or self.superclassDB: pywikibot.output(u'Dumping to %s, please wait...' % config.shortpath(filename)) f = bz2.BZ2File(filename, 'w') databases = { 'catContentDB': self.catContentDB, 'superclassDB': self.superclassDB } # store dump to disk in binary format try: pickle.dump(databases, f, protocol=pickle.HIGHEST_PROTOCOL) except pickle.PicklingError: pass f.close() else: try: os.remove(filename) except EnvironmentError: pass else: pywikibot.output(u'Database is empty. %s removed' % config.shortpath(filename))
def dump(self, filename=None): """Save the dictionaries to disk if not empty. Pickle the contents of the dictionaries superclassDB and catContentDB if at least one is not empty. If both are empty, removes the file from the disk. If the filename is None, it'll use the filename determined in __init__. """ if filename is None: filename = self.filename elif not os.path.isabs(filename): filename = config.datafilepath(filename) if self.is_loaded and (self.catContentDB or self.superclassDB): pywikibot.output("Dumping to %s, please wait..." % config.shortpath(filename)) f = bz2.BZ2File(filename, "w") databases = {"catContentDB": self.catContentDB, "superclassDB": self.superclassDB} # store dump to disk in binary format try: pickle.dump(databases, f, protocol=config.pickle_protocol) except pickle.PicklingError: pass f.close() else: try: os.remove(filename) except EnvironmentError: pass else: pywikibot.output("Database is empty. %s removed" % config.shortpath(filename))
def dump(self, filename='category.dump.bz2'): """Save the contents of the dictionaries superclassDB and catContentDB to disk. """ if not os.path.isabs(filename): filename = config.datafilepath(filename) if self.catContentDB or self.superclassDB: pywikibot.output(u'Dumping to %s, please wait...' % config.shortpath(filename)) f = bz2.BZ2File(filename, 'w') databases = { 'catContentDB': self.catContentDB, 'superclassDB': self.superclassDB } # store dump to disk in binary format try: pickle.dump(databases, f, protocol=pickle.HIGHEST_PROTOCOL) except pickle.PicklingError: pass f.close() else: try: os.remove(filename) except EnvironmentError: pass else: pywikibot.output(u'Database is empty. %s removed' % config.shortpath(filename))
def dump(self, filename=None): """Save the dictionaries to disk if not empty. Pickle the contents of the dictionaries superclassDB and catContentDB if at least one is not empty. If both are empty, removes the file from the disk. If the filename is None, it'll use the filename determined in __init__. """ if filename is None: filename = self.filename elif not os.path.isabs(filename): filename = config.datafilepath(filename) if self.is_loaded and (self.catContentDB or self.superclassDB): pywikibot.output(u'Dumping to %s, please wait...' % config.shortpath(filename)) databases = { 'catContentDB': self.catContentDB, 'superclassDB': self.superclassDB } # store dump to disk in binary format with open(filename, 'wb') as f: try: pickle.dump(databases, f, protocol=config.pickle_protocol) except pickle.PicklingError as e: pywikibot.output(e) else: try: os.remove(filename) except EnvironmentError as e: pywikibot.output(e) else: pywikibot.output(u'Database is empty. %s removed' % config.shortpath(filename))
def _load(self): if not self.is_loaded: try: f = bz2.BZ2File(self.filename, "r") pywikibot.output("Reading dump from %s" % config.shortpath(self.filename)) databases = pickle.load(f) f.close() # keys are categories, values are 2-tuples with lists as # entries. self.catContentDB = databases["catContentDB"] # like the above, but for supercategories self.superclassDB = databases["superclassDB"] del databases except: # If something goes wrong, just rebuild the database self.rebuild()
def _load(self): if not self.is_loaded: try: if config.verbose_output: pywikibot.output('Reading dump from %s' % config.shortpath(self.filename)) with open(self.filename, 'rb') as f: databases = pickle.load(f) # keys are categories, values are 2-tuples with lists as # entries. self.catContentDB = databases['catContentDB'] # like the above, but for supercategories self.superclassDB = databases['superclassDB'] del databases except Exception: pywikibot.output('Failed to load file...') # If something goes wrong, just rebuild the database self.rebuild()
def __init__(self, rebuild=False, filename='category.dump.bz2'): if rebuild: self.rebuild() else: try: if not os.path.isabs(filename): filename = config.datafilepath(filename) f = bz2.BZ2File(filename, 'r') pywikibot.output(u'Reading dump from %s' % config.shortpath(filename)) databases = pickle.load(f) f.close() # keys are categories, values are 2-tuples with lists as entries. self.catContentDB = databases['catContentDB'] # like the above, but for supercategories self.superclassDB = databases['superclassDB'] del databases except: # If something goes wrong, just rebuild the database self.rebuild()