From a8ebb2cd246daf04d7b3ec199de10cb094e2b11c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilhem=20Faur=C3=A9?= Date: Wed, 7 Jun 2023 16:05:09 +0200 Subject: [PATCH] add option to ignore certain objects --- spip2md/config.py | 1 + spip2md/extended_models.py | 14 +++++++++++++- spip2md/lib.py | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spip2md/config.py b/spip2md/config.py index 38f1513..c544eeb 100644 --- a/spip2md/config.py +++ b/spip2md/config.py @@ -31,6 +31,7 @@ class Configuration: unknown_char_replacement: str = "??" # Replaces unknown characters clear_log: bool = True # Clear log before every run instead of appending to clear_output: bool = True # Remove eventual output dir before running + ignore_pattern: list[str] = [] # Ignore objects of which title match logfile: str = "log-spip2md.log" # File where logs will be written, relative to wd loglevel: str = "WARNING" # Minimum criticity of logs written in logfile logname: str = "spip2md" # Labelling of logs diff --git a/spip2md/extended_models.py b/spip2md/extended_models.py index 7a34f18..58a5dc6 100644 --- a/spip2md/extended_models.py +++ b/spip2md/extended_models.py @@ -2,7 +2,7 @@ import logging from os import listdir, makedirs from os.path import basename, isfile, splitext -from re import Match, Pattern, finditer, match, search +from re import I, Match, Pattern, finditer, match, search from shutil import copyfile from typing import Any, Optional @@ -301,6 +301,10 @@ class Document(WritableObject, NormalizedDocument): return super().write_all(parentdepth, parentdir, index, total) +class IgnoredPatternError(Exception): + pass + + class LangNotFoundError(Exception): pass @@ -524,6 +528,8 @@ class RedactionalObject(WritableObject): LOG.debug(err) except DontExportDraftError as err: LOG.debug(err) + except IgnoredPatternError as err: + LOG.debug(err) return output # Write object to output destination @@ -559,6 +565,12 @@ class RedactionalObject(WritableObject): # Apply post-init conversions and cancel the export if self not of the right lang def convert(self, forced_lang: str) -> None: self.convert_title(forced_lang) + for p in CFG.ignore_pattern: + m = match(p, self._title, I) + if m is not None: + raise IgnoredPatternError( + f"{self._title} is matching with ignore pattern {p}, ignoring" + ) self.convert_text(forced_lang) self.convert_extra() if self.lang != forced_lang: diff --git a/spip2md/lib.py b/spip2md/lib.py index 423505b..99f7ebc 100644 --- a/spip2md/lib.py +++ b/spip2md/lib.py @@ -9,6 +9,7 @@ from spip2md.config import CFG from spip2md.extended_models import ( DeepDict, DontExportDraftError, + IgnoredPatternError, LangNotFoundError, Section, ) @@ -54,6 +55,8 @@ as database user {esc(BOLD)}{CFG.db_user}{esc()} ROOTLOG.debug(err) # Log the message except DontExportDraftError as err: # Will happen in not CFG.export_drafts ROOTLOG.debug(err) # Log the message + except IgnoredPatternError as err: + ROOTLOG.debug(err) # Log the message print() # Break line between level 0 sections in output ROOTLOG.debug(f"Finished exporting {lang} root section {i}/{nb} {s._title}") return {"sections": buffer}