[sync] schedules updated (new format), toml based schedules
This commit is contained in:
1 parent
26ef317190
commit
35a488978c
5 files changed
+112
-58
No files matched your search
@@ -95,6 +95,7 @@ date_formats_reverse = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
collection_documents = 'documents'
|
collection_documents = 'documents'
|
||||||
|
collection_schedules = 'schedules'
|
||||||
|
|
||||||
sync_provider_type = env('SYNC_PROVIDER', 'fs')
|
sync_provider_type = env('SYNC_PROVIDER', 'fs')
|
||||||
|
|
||||||
@@ -105,6 +106,7 @@ _providers_roots = {
|
|||||||
f = _providers_roots[sync_provider_type]
|
f = _providers_roots[sync_provider_type]
|
||||||
|
|
||||||
dir_documents = f('Hudozka/Site/Documents')
|
dir_documents = f('Hudozka/Site/Documents')
|
||||||
|
dir_schedules = f('Hudozka/Site/Schedules')
|
||||||
dir_events = f('Hudozka/Site/Events')
|
dir_events = f('Hudozka/Site/Events')
|
||||||
dir_news = f('Hudozka/Site/News')
|
dir_news = f('Hudozka/Site/News')
|
||||||
|
|
||||||
|
|||||||
+5
-2
@@ -3,6 +3,7 @@ from db import db
|
|||||||
from sync.data.fs import FSProvider
|
from sync.data.fs import FSProvider
|
||||||
from sync.data.yandexdisk import YDProvider
|
from sync.data.yandexdisk import YDProvider
|
||||||
from sync.document import sync_documents
|
from sync.document import sync_documents
|
||||||
|
from sync.schedule import sync_schedules
|
||||||
from utils.fn import lprint_json
|
from utils.fn import lprint_json
|
||||||
|
|
||||||
|
|
||||||
@@ -27,6 +28,8 @@ def main(provider, fn, collection_name):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
client = get_provider(settings.sync_provider_type, settings.dir_documents)
|
# client = get_provider(settings.sync_provider_type, settings.dir_documents)
|
||||||
|
# main(client, sync_documents, settings.collection_documents)
|
||||||
|
|
||||||
main(client, sync_documents, settings.collection_documents)
|
client = get_provider(settings.sync_provider_type, settings.dir_schedules)
|
||||||
|
main(client, sync_schedules, settings.collection_schedules)
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import os
|
||||||
|
from tempfile import mkstemp
|
||||||
|
|
||||||
|
from sync.core import Sync
|
||||||
|
from utils.fn import last_good, lmap
|
||||||
|
from utils.hash import hash_str, hash_file
|
||||||
|
from utils.image import create_image
|
||||||
|
from utils.image.resize import image_magick_pdf_to_img
|
||||||
|
from utils.text.transform import url_encode_text, url_encode_file
|
||||||
|
|
||||||
|
|
||||||
|
class SyncSchedule(Sync):
|
||||||
|
def __init__(self, collection, provider):
|
||||||
|
super().__init__()
|
||||||
|
self.collection = collection
|
||||||
|
self.provider = provider
|
||||||
|
|
||||||
|
def create_id(self, document):
|
||||||
|
period = get_period(document['period'])
|
||||||
|
doc_id = document['id'] if 'id' in document and document['id'] else None
|
||||||
|
if not doc_id:
|
||||||
|
doc_id = 'schedule-{period}-{semester}'.format(period=period, semester=document['semester'])
|
||||||
|
document['id'] = doc_id
|
||||||
|
return document
|
||||||
|
|
||||||
|
def create_hash(self, document):
|
||||||
|
return {
|
||||||
|
**document,
|
||||||
|
'hash': self.provider.hash(document['file'])
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_period(value):
|
||||||
|
if isinstance(value, str):
|
||||||
|
return value
|
||||||
|
|
||||||
|
if isinstance(value, list):
|
||||||
|
return '-'.join(lmap(str, value))
|
||||||
@@ -64,3 +64,8 @@ def scan_subdirs(provider, ext, path='.'):
|
|||||||
|
|
||||||
items = combine(items)
|
items = combine(items)
|
||||||
return items
|
return items
|
||||||
|
|
||||||
|
|
||||||
|
def get_data(provider, path, transform):
|
||||||
|
data = provider.read(path)
|
||||||
|
return transform(data)
|
||||||
+62
-56
@@ -1,95 +1,101 @@
|
|||||||
import os
|
from yaml import load as yaml
|
||||||
from glob import glob
|
from toml import loads as toml_str
|
||||||
|
|
||||||
from db import db
|
from sync import untouched
|
||||||
from sync import Sync, untouched
|
from sync.core.schedule import SyncSchedule
|
||||||
from utils.fn import lprint, lprint_json, lmap
|
from sync.data import scan_subdirs, get_data
|
||||||
from utils.hash import hash_file
|
from utils.fn import lmap
|
||||||
from utils.io import read_yaml
|
from utils.io import read_yaml
|
||||||
|
|
||||||
dir_documents = '/Users/tmshv/Dropbox/Dev/Hud school/Schedules'
|
dir_documents = '/Users/tmshv/Dropbox/Dev/Hud school/Schedules'
|
||||||
|
|
||||||
|
|
||||||
class SyncSchedule(Sync):
|
def sync_schedules(provider, collection, update=True, delete=True):
|
||||||
def __init__(self):
|
"""
|
||||||
super().__init__()
|
:param provider:
|
||||||
self.collection = db().schedules
|
:param update:
|
||||||
|
:param delete:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
|
||||||
def create_id(self, document):
|
return main(
|
||||||
id = document['id'] if 'id' in document and document['id'] else None
|
SyncSchedule(
|
||||||
if not id:
|
collection,
|
||||||
id = 'schedule-{period}-{semester}'.format(**document)
|
provider
|
||||||
document['id'] = id
|
),
|
||||||
return document
|
update_documents=update,
|
||||||
|
delete_documents=delete
|
||||||
def create_hash(self, document):
|
)
|
||||||
return {
|
|
||||||
**document,
|
|
||||||
'hash': hash_file(document['file'])
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def combine(fns):
|
||||||
# ALL DOCUMENTS IDS FOUNDED
|
def call(value):
|
||||||
scope_documents_ids = []
|
data = value.read()
|
||||||
|
for f in fns:
|
||||||
|
try:
|
||||||
|
return f(data)
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
return None
|
||||||
|
return call
|
||||||
|
|
||||||
# INIT
|
|
||||||
os.chdir(dir_documents)
|
|
||||||
sync = SyncSchedule()
|
|
||||||
|
|
||||||
|
def constant(value):
|
||||||
|
return lambda _: value
|
||||||
|
|
||||||
|
|
||||||
|
def toml(bytes_value):
|
||||||
|
s = bytes_value.decode('utf-8')
|
||||||
|
return toml_str(s)
|
||||||
|
|
||||||
|
|
||||||
|
def main(sync, update_documents=False, delete_documents=False):
|
||||||
# COLLECT YAML/MD/JPG/PDF MANIFEST FILES
|
# COLLECT YAML/MD/JPG/PDF MANIFEST FILES
|
||||||
documents = glob('*.yaml')
|
documents = sync.provider.type_filter('.', '.yaml')
|
||||||
|
documents += sync.provider.type_filter('.', '.toml')
|
||||||
|
|
||||||
# READ MANIFEST
|
# READ MANIFEST
|
||||||
documents = lmap(
|
documents = lmap(
|
||||||
lambda i: {
|
lambda i: {
|
||||||
'file': i,
|
'file': i,
|
||||||
**read_yaml(i)
|
**get_data(sync.provider, i, combine([yaml, toml, constant({})]))
|
||||||
},
|
},
|
||||||
documents
|
documents
|
||||||
)
|
)
|
||||||
|
|
||||||
# # CREATE DOCUMENT IDENTITY
|
# CREATE DOCUMENT IDENTITY
|
||||||
documents = lmap(sync.create_id, documents)
|
documents = lmap(sync.create_id, documents)
|
||||||
|
|
||||||
# CREATE DOCUMENT HASH
|
# CREATE DOCUMENT HASH
|
||||||
documents = lmap(sync.create_hash, documents)
|
documents = lmap(sync.create_hash, documents)
|
||||||
|
|
||||||
# # CREATE SCOPE OF CURRENT SESSION
|
# CREATE SCOPE OF CURRENT SESSION
|
||||||
|
# ALL DOCUMENTS IDS FOUNDED
|
||||||
scope_documents_ids = lmap(
|
scope_documents_ids = lmap(
|
||||||
lambda i: i['id'],
|
lambda i: i['id'],
|
||||||
documents
|
documents
|
||||||
)
|
)
|
||||||
|
|
||||||
# # SKIP UNTOUCHED DOCUMENTS
|
# SKIP UNTOUCHED DOCUMENTS
|
||||||
documents = untouched(documents, sync)
|
documents = untouched(documents, sync)
|
||||||
|
|
||||||
# DO HEAVY PROCESS WITH DOCUMENTS
|
# DO HEAVY PROCESS WITH DOCUMENTS
|
||||||
|
|
||||||
# SYNC TEACHER_PROFILE
|
# SYNC SCHEDULE
|
||||||
documents = lmap(
|
if update_documents:
|
||||||
sync.update,
|
documents = lmap(
|
||||||
documents
|
sync.update,
|
||||||
)
|
documents
|
||||||
|
)
|
||||||
|
|
||||||
documents_to_remove = sync.query({'id': {'$nin': scope_documents_ids}})
|
documents_to_remove = sync.query({'id': {'$nin': scope_documents_ids}})
|
||||||
documents_to_remove = lmap(
|
if delete_documents:
|
||||||
sync.delete,
|
documents_to_remove = lmap(
|
||||||
map(
|
sync.delete,
|
||||||
lambda i: {'_id': i['_id']},
|
map(
|
||||||
documents_to_remove
|
lambda i: {'_id': i['_id']},
|
||||||
|
documents_to_remove
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
# SCOPE
|
return documents, documents_to_remove
|
||||||
print('SCOPE:')
|
|
||||||
lprint(scope_documents_ids)
|
|
||||||
|
|
||||||
# # DELETE
|
|
||||||
print('DELETE DOCUMENTS:')
|
|
||||||
lprint_json(documents_to_remove)
|
|
||||||
|
|
||||||
# DELETE
|
|
||||||
print('UPDATE DOCUMENTS:')
|
|
||||||
lprint_json(documents)
|
|
||||||
print('[SYNC DOCUMENTS DONE]')
|
|
||||||
Reference in new issue
Block a user