more typing

This commit is contained in:
Guilhem Fauré 2023-05-16 14:01:35 +02:00
parent f23073ef12
commit aa1b822688
5 changed files with 41 additions and 34 deletions

View File

@ -1,3 +1,4 @@
# pyright: basic
from re import finditer from re import finditer
from converter import convertBody, convertMeta, unknownIso from converter import convertBody, convertMeta, unknownIso
@ -9,36 +10,36 @@ from yaml import dump
class Article: class Article:
def __init__(self, article): def __init__(self, article):
self.id = article.id_article self.id: int = article.id_article
# self.surtitle = article.surtitre # Probably unused # self.surtitle = article.surtitre # Probably unused
self.title = convertMeta(article.titre) self.title: str = convertMeta(article.titre)
self.subtitle = article.soustitre # Probably unused self.subtitle: str = article.soustitre # Probably unused
self.section_id = article.id_rubrique self.section_id: int = article.id_rubrique
self.description = convertMeta(article.descriptif) self.description: str = convertMeta(article.descriptif)
self.caption = article.chapo # Probably unused self.caption: str = article.chapo # Probably unused
self.text = convertBody(article.texte) # Markdown self.text: str = convertBody(article.texte) # Markdown
self.ps = article.ps # Probably unused self.ps: str = article.ps # Probably unused
self.publicationDate = article.date self.publicationDate: str = article.date
self.draft = False if article.statut == "publie" else True self.draft: bool = False if article.statut == "publie" else True
# self.sector = article.id_secteur # TODO join # self.sector = article.id_secteur # TODO join
self.update = article.maj self.update: str = article.maj
# self.export = article.export # USELESS # self.export = article.export # USELESS
self.creationDate = article.date_redac self.creationDate: str = article.date_redac
# self.views = article.visites # USELESS in static # self.views = article.visites # USELESS in static
self.referers = article.referers # TODO Why? # self.referers = article.referers # TODO Why?
# self.popularity = article.popularite # USELESS in static # self.popularity = article.popularite # USELESS in static
self.acceptForum = article.accepter_forum # TODO Why? # self.acceptForum = article.accepter_forum # TODO Why?
self.contentUpdate = article.date_modif # Probably unused self.contentUpdate: str = article.date_modif # Probably unused
self.lang = article.lang self.lang: str = article.lang
self.choosenLang = article.langue_choisie # TODO Why? self.choosenLang: str = article.langue_choisie # TODO Why?
# self.translation = article.id_trad # TODO join # self.translation = article.id_trad # TODO join
self.extra = article.extra # Probably unused self.extra: str = article.extra # Probably unused
# self.version = article.id_version # USELESS # self.version = article.id_version # USELESS
self.sitename = article.nom_site # Probably useless self.sitename: str = article.nom_site # Probably useless
self.virtual = article.virtuel # TODO Why? self.virtual: str = article.virtuel # TODO Why?
self.microblog = article.microblog # Probably unused self.microblog: str = article.microblog # Probably unused
def getSection(self): def getSection(self) -> str:
return convertMeta( return convertMeta(
SpipRubriques.select() SpipRubriques.select()
.where(SpipRubriques.id_rubrique == self.section_id)[0] .where(SpipRubriques.id_rubrique == self.section_id)[0]
@ -53,7 +54,7 @@ class Article:
def getFilename(self) -> str: def getFilename(self) -> str:
return "index.fr.md" return "index.fr.md"
def getAuthors(self): def getAuthors(self) -> tuple:
return ( return (
SpipAuteurs.select() SpipAuteurs.select()
.join( .join(
@ -63,7 +64,7 @@ class Article:
.where(SpipAuteursLiens.id_objet == self.id) .where(SpipAuteursLiens.id_objet == self.id)
) )
def getFrontmatter(self): def getFrontmatter(self) -> str:
return dump( return dump(
{ {
"lang": self.lang, "lang": self.lang,
@ -79,7 +80,7 @@ class Article:
allow_unicode=True, allow_unicode=True,
) )
def getArticle(self): def getArticle(self) -> str:
# Build the final article text # Build the final article text
article: str = "---\n" + self.getFrontmatter() + "---" article: str = "---\n" + self.getFrontmatter() + "---"
# If there is a caption, add the caption followed by a hr # If there is a caption, add the caption followed by a hr
@ -99,8 +100,8 @@ class Article:
article += "\n\n# MICROBLOGGING\n\n" + self.microblog article += "\n\n# MICROBLOGGING\n\n" + self.microblog
return article return article
def getUnknownChars(self) -> list: def getUnknownChars(self) -> list[str]:
errors: list = [] errors: list[str] = []
for text in (self.title, self.text): for text in (self.title, self.text):
for char in unknownIso: for char in unknownIso:
for match in finditer(char + r".*(?=\r?\n|$)", text): for match in finditer(char + r".*(?=\r?\n|$)", text):
@ -111,7 +112,7 @@ class Article:
class Articles: class Articles:
exported: int = 0 exported: int = 0
def __init__(self, maxToExport) -> None: def __init__(self, maxToExport: int) -> None:
# Query the DB to retrieve all articles sorted by publication date # Query the DB to retrieve all articles sorted by publication date
self.articles = ( self.articles = (
SpipArticles.select().order_by(SpipArticles.date.desc()).limit(maxToExport) SpipArticles.select().order_by(SpipArticles.date.desc()).limit(maxToExport)

View File

@ -1,9 +1,10 @@
# pyright: strict
from os.path import isfile from os.path import isfile
from yaml import CLoader as Loader from yaml import CLoader as Loader
from yaml import load from yaml import load
configPaths: tuple = ("spip2md.yml", "spip2md.yaml") configPaths = ("spip2md.yml", "spip2md.yaml")
class Configuration: class Configuration:
@ -14,7 +15,7 @@ class Configuration:
outputDir = "output" outputDir = "output"
defaultNbToExport = 1000 defaultNbToExport = 1000
def __init__(self, configFile=None) -> None: def __init__(self, configFile: str | None = None) -> None:
if configFile != None: if configFile != None:
with open(configFile) as f: with open(configFile) as f:
config = load(f.read(), Loader=Loader) config = load(f.read(), Loader=Loader)

View File

@ -1,7 +1,8 @@
# pyright: strict
from re import I, S, compile, finditer from re import I, S, compile, finditer
# SPIP syntax to Markdown # SPIP syntax to Markdown
spipToMarkdown: tuple = ( spipToMarkdown = (
( # horizontal rule ( # horizontal rule
compile(r"- ?- ?- ?- ?[\- ]*|<hr ?.*?>", S | I), compile(r"- ?- ?- ?- ?[\- ]*|<hr ?.*?>", S | I),
# r"---", # r"---",
@ -113,7 +114,7 @@ spipToMarkdown: tuple = (
), ),
) )
spipToText: tuple = ( spipToText = (
( # strong ( # strong
compile(r"\{\{ *(.*?) *\}\}", S | I), compile(r"\{\{ *(.*?) *\}\}", S | I),
r"\1", r"\1",
@ -158,7 +159,7 @@ spipToText: tuple = (
), ),
) )
isoToUtf: tuple = ( isoToUtf = (
# Broken encoding # Broken encoding
( # Fix UTF-8 appostrophe that was interpreted as ISO 8859-1 ( # Fix UTF-8 appostrophe that was interpreted as ISO 8859-1
"’", "’",
@ -252,7 +253,7 @@ isoToUtf: tuple = (
) )
## WARNING unknown broken encoding ## WARNING unknown broken encoding
unknownIso: tuple = ( unknownIso = (
r"
", # unknown 
 r"
", # unknown 

r"∆", # unknown â^† r"∆", # unknown â^†
) )
@ -273,11 +274,13 @@ def convertMeta(text: str) -> str:
text.replace(iso, utf) text.replace(iso, utf)
return text return text
def removeUnknownChars(text: str) -> str: def removeUnknownChars(text: str) -> str:
for char in unknownIso: for char in unknownIso:
text.replace(char, "") text.replace(char, "")
return text return text
def highlightUnknownChars(text: str) -> str: def highlightUnknownChars(text: str) -> str:
# Define terminal escape sequences to stylize output, regex escaped # Define terminal escape sequences to stylize output, regex escaped
COLOR: str = "\033[91m" + "\033[1m" # Red + Bold COLOR: str = "\033[91m" + "\033[1m" # Red + Bold

View File

@ -1,3 +1,4 @@
# pyright: basic
from peewee import (SQL, BigAutoField, BigIntegerField, CharField, from peewee import (SQL, BigAutoField, BigIntegerField, CharField,
CompositeKey, DateField, DateTimeField, FloatField, CompositeKey, DateField, DateTimeField, FloatField,
IntegerField, Model, MySQLDatabase, TextField) IntegerField, Model, MySQLDatabase, TextField)

View File

@ -1,4 +1,5 @@
#!python #!python
# pyright: basic
from articles import Article, Articles from articles import Article, Articles
from config import config from config import config
from converter import highlightUnknownChars from converter import highlightUnknownChars