This repository has been archived on 2025-01-08. You can view files and clone it, but cannot push or open issues or pull requests.
blog-software/article.py
2024-05-26 14:53:09 +02:00

65 lines
No EOL
1.4 KiB
Python

from dataclasses import dataclass
from io import TextIOWrapper
from json import loads
from os import sep
from typing import Any, Dict
@dataclass
class Article:
id: str
title: str
summary: str
content: str
custom: Dict[str, str]
@staticmethod
def from_open_file(id: str, file: TextIOWrapper) -> "Article":
kwargs = {'id': id}
custom = {}
for line in file:
if line.strip() == 'content':
break
kv = line.strip().split(' ', 1)
key = kv[0]
value = kv[1] if len(kv) > 1 else ''
if key in Article.__annotations__:
kwargs[key] = value
else:
custom[key] = value
content = file.read()
return Article(content=content, custom=custom, **kwargs)
@dataclass
class Site:
name: str
url: str
custom: Dict[str, str]
@staticmethod
def from_open_file(file: TextIOWrapper) -> "Site":
kwargs = {}
custom = {}
data = file.read()
data = loads(data)
for key, value in data.items():
if key in Site.__annotations__:
kwargs[key] = value
else:
custom[key] = value
return Site(custom=custom, **kwargs)
@dataclass
class Page:
index: int
last: int
url_previous: str
url_next: str