How to use the bypy.cached function in bypy

To help you get started, we’ve selected a few bypy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github houtianze / bypy / bypy.py View on Github external
def __call__(self, *args):
		assert len(args) > 0
		result = None
		path = args[0]
		dir, file = os.path.split(path) # the 'filename' parameter
		absdir = os.path.abspath(dir)
		if absdir in cached.cache:
			entry = cached.cache[absdir]
			if file in entry:
				info = entry[file]
				if self.f.__name__ in info \
					and info['size'] == getfilesize(path) \
					and info['mtime'] == getfilemtime_int(path) \
					and self.f.__name__ in info \
					and cached.usecache:
					result = info[self.f.__name__]
					if cached.debug:
						pdbg("Cache hit for file '{}',\n{}: {}\nsize: {}\nmtime: {}".format(
							path, self.f.__name__,
							result if isinstance(result, (int, long, float, complex)) else binascii.hexlify(result),
							info['size'], info['mtime']))
				else:
					result = self.f(*args)
					self.__store(info, path, result)
github houtianze / bypy / bypy.py View on Github external
def __store(self, info, path, value):
		cached.dirty = True
		info['size'] = getfilesize(path)
		info['mtime'] = getfilemtime_int(path)
		info[self.f.__name__] = value
		if cached.debug:
			situation = "Storing cache"
			if cached.usecache:
				situation = "Cache miss"
			pdbg((situation + " for file '{}',\n{}: {}\nsize: {}\nmtime: {}").format(
				path, self.f.__name__,
				value if isinstance(value, (int, long, float, complex)) else binascii.hexlify(value),
				info['size'], info['mtime']))

		# periodically save to prevent loss in case of system crash
		global last_cache_save
		now = time.time()
		if now - last_cache_save >= CacheSavePeriodInSec:
			cached.savecache()
			last_cache_save = now
		if cached.debug:
			pdbg("Periodically saving Hash Cash")
github houtianze / bypy / bypy.py View on Github external
info['mtime'] = getfilemtime_int(path)
		info[self.f.__name__] = value
		if cached.debug:
			situation = "Storing cache"
			if cached.usecache:
				situation = "Cache miss"
			pdbg((situation + " for file '{}',\n{}: {}\nsize: {}\nmtime: {}").format(
				path, self.f.__name__,
				value if isinstance(value, (int, long, float, complex)) else binascii.hexlify(value),
				info['size'], info['mtime']))

		# periodically save to prevent loss in case of system crash
		global last_cache_save
		now = time.time()
		if now - last_cache_save >= CacheSavePeriodInSec:
			cached.savecache()
			last_cache_save = now
		if cached.debug:
			pdbg("Periodically saving Hash Cash")
github houtianze / bypy / bypy / bypy.py View on Github external
global pprgr
	opr = pr
	oprcolor = prcolor
	oask = ask
	opprgr = pprgr
	def restoremp():
		global pr
		global prcolor
		global ask
		global pprgr
		pr = cachedm.pr = util.pr = opr
		prcolor = util.prcolor = printer.prcolor = oprcolor
		ask = util.ask = oask
		pprgr = util.pprgr = opprgr

	pr = cachedm.pr = util.pr = makemppr(opr)
	prcolor = util.prcolor = printer.prcolor = makemppr(oprcolor)
	ask = util.ask = makemppr(oask)
	pprgr = util.pprgr = makemppprgr(opprgr)

	return restoremp
github houtianze / bypy / bypy.py View on Github external
def __init__(self, f):
		super(cached, self).__init__()
		self.f = f
github houtianze / bypy / bypy.py View on Github external
def savecache(force_saving = False):
		saved = False
		# even if we were unable to load the cache, we still save it.
		if cached.dirty or force_saving:
			if cached.verbose:
				pr("Saving Hash Cache...")

			try:
				with open(cached.hashcachepath, 'wb') as f:
					pickle.dump(cached.cache, f)
					f.close()
				if cached.verbose:
					pr("Hash Cache saved.")
				saved = True
				cached.dirty = False
			except Exception:
				perr("Failed to save Hash Cache. Exception:\n{}".format(format_exc_str()))
		else:
			if cached.verbose:
				pr("Skip saving Hash Cache since it has not been updated.")
github houtianze / bypy / bypy / bypy.py View on Github external
def restoremp():
		global pr
		global prcolor
		global ask
		global pprgr
		pr = cachedm.pr = util.pr = opr
		prcolor = util.prcolor = printer.prcolor = oprcolor
		ask = util.ask = oask
		pprgr = util.pprgr = opprgr
github houtianze / bypy / bypy.py View on Github external
def cleancache(self):
		''' Usage: cleancache - remove invalid entries from hash cache file'''
		if os.path.exists(self.__hashcachepath):
			try:
				# backup first
				backup = self.__hashcachepath + '.lastclean'
				shutil.copy(self.__hashcachepath, backup)
				self.pd("Hash Cache file '{}' backed up as '{}".format(
					self.__hashcachepath, backup))
				cached.cleancache()
				return ENoError
			except:
				perr("Exception:\n{}".format(format_exc_str()))
				return EException
		else:
			return EFileNotFound
github houtianze / bypy / bypy.py View on Github external
def __store(self, info, path, value):
		cached.dirty = True
		info['size'] = getfilesize(path)
		info['mtime'] = getfilemtime_int(path)
		info[self.f.__name__] = value
		if cached.debug:
			situation = "Storing cache"
			if cached.usecache:
				situation = "Cache miss"
			pdbg((situation + " for file '{}',\n{}: {}\nsize: {}\nmtime: {}").format(
				path, self.f.__name__,
				value if isinstance(value, (int, long, float, complex)) else binascii.hexlify(value),
				info['size'], info['mtime']))

		# periodically save to prevent loss in case of system crash
		global last_cache_save
		now = time.time()
		if now - last_cache_save >= CacheSavePeriodInSec:
github houtianze / bypy / bypy / bypy.py View on Github external
def __remove_local_on_success(self, localpath):
		if self.__deletesource:
			self.pd("Removing local path '{}' after successful upload.".format(localpath))
			result = cachedm.remove_path_and_cache(localpath)
			if result == const.ENoError:
				self.pd("Local path '{}' removed.".format(localpath))
			else:
				perr("Failed to remove local path '{}'.".format(localpath))
			return result