sync Page refactoring
This commit is contained in:
1 parent
7bf49680cf
commit
6a5dffe567
1 file changed
+33
-42
@@ -1,16 +1,12 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
import lxml.html
|
import lxml.html
|
||||||
|
|
||||||
import kazimir
|
import kazimir
|
||||||
import settings
|
import settings
|
||||||
from db import collection
|
from db import collection
|
||||||
from sync.data import request
|
from sync.data import request
|
||||||
from sync.models import Model
|
from sync.models import Model
|
||||||
|
|
||||||
from sync import create_date, title_from_html, create_post
|
from sync import create_date, title_from_html, create_post
|
||||||
from sync.models.Image import Image
|
from sync.models.Image import Image
|
||||||
from utils.hash import hash_str
|
from utils.hash import hash_str
|
||||||
@@ -42,57 +38,68 @@ class PageController:
|
|||||||
|
|
||||||
async def get_items(self):
|
async def get_items(self):
|
||||||
scan_path = settings.dir_pages
|
scan_path = settings.dir_pages
|
||||||
items = [
|
items = []
|
||||||
Page.read(self.provider, path)
|
for path in self.provider.scan(scan_path):
|
||||||
for path in self.provider.scan(scan_path)
|
if not self.provider.is_dir(path):
|
||||||
if self.provider.is_dir(path)
|
continue
|
||||||
]
|
item = await self.create_item(path)
|
||||||
items = [i for i in items if i]
|
if not item:
|
||||||
|
continue
|
||||||
|
items.append(item)
|
||||||
return items
|
return items
|
||||||
|
|
||||||
# async def read(self, path: str):
|
async def create_item(self, path: str):
|
||||||
# return Page.read(self.provider, path)
|
|
||||||
|
|
||||||
|
|
||||||
class Page(Model):
|
|
||||||
@staticmethod
|
|
||||||
def read(provider, path):
|
|
||||||
"""
|
"""
|
||||||
:param provider:
|
|
||||||
:param path: path to page manifest file. For example: /Pages/<Some_Page>/<Some_Page>.md
|
:param path: path to page manifest file. For example: /Pages/<Some_Page>/<Some_Page>.md
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
dir_name = os.path.basename(path)
|
dir_name = os.path.basename(path)
|
||||||
|
manifest_path = os.path.join(path, f'{dir_name}.md')
|
||||||
|
|
||||||
manifest_file = lambda ext: os.path.join(path, dir_name + ext)
|
if not self.provider.exists(manifest_path):
|
||||||
manifest_path = manifest_file('.md')
|
|
||||||
|
|
||||||
if not provider.exists(manifest_path):
|
|
||||||
logger.warning(f'Manifest not found on path {manifest_path}')
|
logger.warning(f'Manifest not found on path {manifest_path}')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
'file': os.path.basename(manifest_path),
|
'file': os.path.basename(manifest_path),
|
||||||
'folder': os.path.dirname(manifest_path),
|
'folder': os.path.dirname(manifest_path),
|
||||||
'title': os.path.basename(manifest_path),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
manifest = get_manifest(provider, manifest_path)
|
manifest = await self.get_manifest(manifest_path)
|
||||||
params = {
|
params = {
|
||||||
**params,
|
**params,
|
||||||
**manifest,
|
**manifest,
|
||||||
}
|
}
|
||||||
|
|
||||||
if 'url' not in params:
|
if 'url' not in params:
|
||||||
logger.warning('URL not specified for Page {}'.format(manifest_path))
|
logger.warning(f'URL not specified for Page {manifest_path}')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return Page(
|
return Page(
|
||||||
provider,
|
self.provider,
|
||||||
manifest_path,
|
manifest_path,
|
||||||
params=params,
|
params=params,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def get_manifest(self, path: str):
|
||||||
|
data = self.provider.read(path).read().decode('utf-8')
|
||||||
|
manifest = parse_yaml_front_matter(data)
|
||||||
|
|
||||||
|
if 'date' in manifest:
|
||||||
|
manifest['date'] = create_date(
|
||||||
|
manifest['date'], settings.date_formats)
|
||||||
|
|
||||||
|
if 'until' in manifest:
|
||||||
|
manifest['until'] = create_date(
|
||||||
|
manifest['until'], settings.date_formats)
|
||||||
|
|
||||||
|
if 'id' in manifest:
|
||||||
|
manifest['id'] = str(manifest['id'])
|
||||||
|
|
||||||
|
return manifest
|
||||||
|
|
||||||
|
|
||||||
|
class Page(Model):
|
||||||
def __init__(self, provider, file, params=None):
|
def __init__(self, provider, file, params=None):
|
||||||
self.__hash_salt = settings.hash_salt_pages
|
self.__hash_salt = settings.hash_salt_pages
|
||||||
self.__hash_keys = ['id', 'url']
|
self.__hash_keys = ['id', 'url']
|
||||||
@@ -187,19 +194,3 @@ class Page(Model):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '<Page file={} id={}>'.format(self.file, self.id)
|
return '<Page file={} id={}>'.format(self.file, self.id)
|
||||||
|
|
||||||
|
|
||||||
def get_manifest(provider, path):
|
|
||||||
data = provider.read(path).read().decode('utf-8')
|
|
||||||
manifest = parse_yaml_front_matter(data)
|
|
||||||
|
|
||||||
if 'date' in manifest:
|
|
||||||
manifest['date'] = create_date(manifest['date'], settings.date_formats)
|
|
||||||
|
|
||||||
if 'until' in manifest:
|
|
||||||
manifest['until'] = create_date(manifest['until'], settings.date_formats)
|
|
||||||
|
|
||||||
if 'id' in manifest:
|
|
||||||
manifest['id'] = str(manifest['id'])
|
|
||||||
|
|
||||||
return manifest
|
|
||||||
Reference in new issue
Block a user