-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
93 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,61 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import hashlib | ||
from . import kodi | ||
from .utils import open_file_wrapper, os, re, json, time, provider_temp_dir | ||
from .utils import open_file_wrapper, os, time, provider_temp_dir | ||
|
||
def __cache_key_path(key): | ||
path = ''.join([x if x.isalnum() else '_' for x in key]) + '.json' | ||
path = ''.join([x if x.isalnum() else '_' for x in key]) | ||
return os.path.join(provider_temp_dir, path) | ||
|
||
def __cache_save(key, data): | ||
path = __cache_key_path(key) | ||
with open_file_wrapper(path, mode='w')() as f: | ||
f.write(json.dumps(data, indent=4)) | ||
with open_file_wrapper(path, mode='wb')() as f: | ||
f.write(data) | ||
|
||
def __cache_get(key): | ||
path = __cache_key_path(key) | ||
if not os.path.exists(path): | ||
return {} | ||
try: | ||
with open_file_wrapper(path)() as f: | ||
return json.load(f) | ||
with open_file_wrapper(path, mode='rb')() as f: | ||
return f.read() | ||
except: | ||
return {} | ||
|
||
def __generate_md5(*args): | ||
md5_hash = hashlib.md5() | ||
def __cache_check(key): | ||
path = __cache_key_path(key) | ||
return os.path.exists(path) | ||
|
||
def __cache_cleanup(): | ||
try: | ||
[md5_hash.update(str(arg)) for arg in args] | ||
# while temp dir bigger than 500MiB, remove files sorted by age (oldest first) | ||
max_size = 500 * 1024 * 1024 | ||
files = [] | ||
size = 0 | ||
|
||
for file in os.listdir(provider_temp_dir): | ||
path = os.path.join(provider_temp_dir, file) | ||
if os.path.isfile(path): | ||
files.append((path, os.path.getmtime(path))) | ||
size += os.path.getsize(path) | ||
|
||
if size < max_size: | ||
return | ||
|
||
original_size = size | ||
|
||
files.sort(key=lambda x: x[1]) | ||
for file, _ in files: | ||
if size < max_size: | ||
break | ||
size -= os.path.getsize(file) | ||
os.remove(file) | ||
|
||
return original_size - size | ||
except: | ||
[md5_hash.update(str(arg).encode('utf-8')) for arg in args] | ||
return str(md5_hash.hexdigest()) | ||
|
||
def __get_function_name(function_instance): | ||
return re.sub(r'.+?\s*method\s*|.+function\s*|\sat\s*?.+|\s*?of\s*?.+', '', repr(function_instance)) | ||
|
||
def __hash_function(function_instance, *args): | ||
return __get_function_name(function_instance) + __generate_md5(args) | ||
|
||
def __get_or_add(key, value, fn, duration, *args, **kwargs): | ||
key = __hash_function(fn, *args) if not key else key | ||
if not value: | ||
data = __cache_get(key) | ||
if data: | ||
if not duration or time.time() - data['t'] < (duration * 60): | ||
return data['v'] | ||
|
||
if not value and not fn: | ||
return None | ||
|
||
value = fn(*args, **kwargs) if not value else value | ||
data = { 't': time.time(), 'v': value } | ||
__cache_save(key, data) | ||
return value | ||
|
||
database = lambda: None | ||
def db_get(fn, duration, *args, **kwargs): | ||
return __get_or_add(None, None, fn, duration, *args, **kwargs) | ||
database.get = db_get | ||
database.cache_get = lambda key: __get_or_add(key, None, None, None) | ||
database.cache_insert = lambda key, value: __get_or_add(key, value, None, None) | ||
pass | ||
|
||
db = lambda: None | ||
db.set = lambda key, value: __cache_save(key, value) | ||
db.get = lambda key: __cache_get(key) | ||
db.check = lambda key: __cache_check(key) | ||
db.cleanup = lambda: __cache_cleanup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
04b4bc63dac4924a998aefa868552532a8d67443 | ||
94ed66018b0d476f20cd1368dd61e6c3180dea6b |