FUCKING BIG SYNC UPDATE: caching, events, albums, documents, awards, news, schedules, teachers, other

This commit is contained in:
tmshv committed 2016-03-03 01:36:50 +03:00
1 parent 359cc430f5
commit f07c39c317
20 files changed
+1190 -166

No files matched your search

+10 -1
View File
@@ -30,6 +30,10 @@ lprint_json = lambda s: lprint(lmap(
first = lambda i: i[0] if len(i) > 0 else None
def ext(ext):
return lambda i: os.path.splitext(i)[0] + ext
def map_item_key(item, key, map_fn):
"""
@@ -42,7 +46,12 @@ def map_item_key(item, key, map_fn):
return item
key_mapper = lambda key, fn: lambda i: map_item_key(i, key, fn)
def kmap(key):
return lambda i: i[key] if key in i else None
def key_mapper(key, fn):
return lambda i: map_item_key(i, key, fn)
def map_cases(param, cases, default_fn=None):
+25 -4
View File
@@ -1,9 +1,30 @@
import hashlib
import json
from utils.fn import map_cases
def file_hash(file):
def hash_file(file):
with open(file, 'rb') as f:
data = f.read()
sha = hashlib.sha256(data)
f.close()
return sha.hexdigest()
return hash_data(data)
def hash_data(data):
sha = hashlib.sha256(data)
return sha.hexdigest()
def hash_str(data):
to_json = lambda i: json.dumps(i, sort_keys=True)
data = map_cases(data, [
(lambda i: isinstance(i, dict), to_json),
(lambda i: isinstance(i, tuple), to_json),
(lambda i: isinstance(i, list), to_json),
(lambda i: isinstance(i, int), int),
(lambda i: isinstance(i, float), float),
])
if isinstance(data, dict):
data = json.dumps(data, sort_keys=True)
return hash_data(str(data).encode())
+22 -12
View File
@@ -11,7 +11,6 @@ def read_image(src):
try:
i = Image.open(src)
return i
# return orient(i)
except Exception as e:
print(src, e)
return None
@@ -27,8 +26,10 @@ def create_image(file, sizes, url_fn, output_dir, skip_processing=False):
:param skip_processing: True/False to skip thumbs generation
:return:
"""
image = read_image(file)
if not image:
# image = read_image(file)
# if not image:
# return None
if not os.path.exists(file):
return None
result = {}
@@ -36,20 +37,13 @@ def create_image(file, sizes, url_fn, output_dir, skip_processing=False):
for size in sizes:
size_name, width, height = size
image_width, image_height = image.size
if image_height > image_width:
width, height = height, width
image_url = url_fn(size_name, ext)
image_filename = os.path.basename(image_url)
local_image_path = os.path.join(output_dir, image_filename)
if not skip_processing:
if size_name == 'original':
thumb = optimize(file, local_image_path, quality=90)
else:
thumb = thumbnail(file, local_image_path, (width, height))
width, height = thumb.size
image = process_image(file, local_image_path, size)
width, height = image.size
result[size_name] = {
'url': image_url,
@@ -63,3 +57,19 @@ def create_image(file, sizes, url_fn, output_dir, skip_processing=False):
'hash': file_hash(file),
'data': result
}
def process_image(input_file, output_file, size):
size_name, width, height = size
image = read_image(input_file)
image_width, image_height = image.size
if image_height > image_width:
width, height = height, width
if size_name == 'original':
thumb = optimize(input_file, output_file, quality=90)
else:
thumb = thumbnail(input_file, output_file, (width, height))
return thumb
+19 -1
View File
@@ -26,11 +26,29 @@ def read_yaml_md(file):
data = read_file(file)
yaml_data, md_data = split_with(['\n\n', '\r\n\r\n'])(data)
if not yaml_data and not md_data:
return (read_yaml(file), None)
try:
y = yaml.load(yaml_data)
except:
y = None
m = markdown(md_data) if md_data else None
return y, m
def read_md_yaml(file):
data = read_file(file)
yaml_data, md_data = split_with(['\n\n', '\r\n\r\n'])(data)
if not yaml_data and not md_data:
return (None, read_md(file))
try:
y = yaml.load(yaml_data)
except:
y = None
m = markdown(md_data) if md_data else None
return y, m
+1 -1
View File
@@ -1,4 +1,4 @@
from utils.fn import lmap, until
from utils.fn import until
def split(data, separator=' '):
+48 -9
View File
@@ -4,6 +4,9 @@ from functools import reduce
import settings
from utils.fn import iterate_over_fns
remove_pattr = re.compile('[«»,!&]')
reduce_dash_pattr = re.compile('[-]+')
def translit(text):
"""
@@ -35,16 +38,52 @@ def interpolate(text, storage, key_fn=None):
return storage[key] if key in storage else text
interpolate_swift = lambda i: re.search("([\d\w-]+)", i).group(1)
def interpolate_swift(i):
return re.search("([\d\w-]+)", i).group(1)
remove_pattr = re.compile('[«»,!&]')
remove_punctuation = lambda i: remove_pattr.sub('', i)
space_to_dash = lambda i: i.replace(' ', '-')
dot_to_dash = lambda i: i.replace('.', '-')
remove_underscores = lambda i: i.replace('_', '')
text_lower = lambda i: i.lower()
def remove_punctuation(i):
return remove_pattr.sub('', i)
def reduce_dashes(i):
return reduce_dash_pattr.sub('-', i)
def space_to_dash(i):
return i.replace(' ', '-')
def short_dash(i):
"""
minus -> dash
tire -> dash
:param i:
:return:
"""
return i.replace('–', '-').replace('—', '-')
def dot_to_dash(i):
return i.replace('.', '-')
def remove_underscores(i):
return i.replace('_', '')
def text_lower(i):
return i.lower()
url_encode_file = iterate_over_fns([translit, space_to_dash, remove_underscores, remove_punctuation, text_lower])
url_encode_text = iterate_over_fns(
[translit, space_to_dash, dot_to_dash, remove_underscores, remove_punctuation, text_lower])
url_encode_text = iterate_over_fns([
translit,
space_to_dash,
dot_to_dash,
short_dash,
remove_underscores,
remove_punctuation,
reduce_dashes,
text_lower
])