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

65 lines
1.4 KiB
Python
Raw Normal View History

2024-05-19 10:26:52 +02:00
from dataclasses import dataclass
from io import TextIOWrapper
2024-05-23 16:16:02 +02:00
from json import loads
2024-05-19 10:26:52 +02:00
from os import sep
2024-05-25 19:05:16 +02:00
from typing import Any, Dict
2024-05-19 10:26:52 +02:00
@dataclass
class Article:
id: str
title: str
summary: str
content: str
2024-05-25 19:05:16 +02:00
custom: Dict[str, str]
2024-05-19 10:26:52 +02:00
2024-05-23 16:16:02 +02:00
@staticmethod
2024-05-25 19:05:16 +02:00
def from_open_file(id: str, file: TextIOWrapper) -> "Article":
kwargs = {'id': id}
custom = {}
2024-05-19 10:26:52 +02:00
2024-05-25 19:05:16 +02:00
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)
2024-05-21 17:43:53 +02:00
@dataclass
2024-05-22 14:42:05 +02:00
class Site:
2024-05-21 17:43:53 +02:00
name: str
url: str
2024-05-25 19:05:16 +02:00
custom: Dict[str, str]
2024-05-21 17:43:53 +02:00
2024-05-23 16:16:02 +02:00
@staticmethod
def from_open_file(file: TextIOWrapper) -> "Site":
2024-05-25 19:05:16 +02:00
kwargs = {}
custom = {}
2024-05-23 16:16:02 +02:00
data = file.read()
data = loads(data)
2024-05-25 19:05:16 +02:00
for key, value in data.items():
if key in Site.__annotations__:
kwargs[key] = value
else:
custom[key] = value
return Site(custom=custom, **kwargs)
2024-05-26 14:53:09 +02:00
@dataclass
class Page:
index: int
last: int
url_previous: str
url_next: str