export files

This commit is contained in:
Guilhem Fauré 2023-05-23 10:26:03 +02:00
parent bad5fef4c6
commit 79f1a9e958
2 changed files with 22 additions and 12 deletions

View File

@ -1,4 +1,5 @@
# pyright: strict # pyright: strict
from os.path import basename, splitext
from typing import Any, Optional from typing import Any, Optional
from slugify import slugify from slugify import slugify
@ -158,9 +159,9 @@ class Document:
def __init__(self, document: SpipDocuments) -> None: def __init__(self, document: SpipDocuments) -> None:
self.id: int = document.id_document self.id: int = document.id_document
self.thumbnail_id: int = document.id_vignette self.thumbnail_id: int = document.id_vignette
self.title: str = document.titre self.title: str = convert_meta(document.titre)
self.date: str = document.date self.date: str = document.date
self.description: str = document.descriptif self.description: str = convert_meta(document.descriptif)
self.file: str = document.fichier self.file: str = document.fichier
self.draft: bool = document.statut == "publie" self.draft: bool = document.statut == "publie"
self.creation: str = document.date self.creation: str = document.date
@ -169,7 +170,11 @@ class Document:
self.media: str = document.media self.media: str = document.media
def get_slug(self, date: bool = False) -> str: def get_slug(self, date: bool = False) -> str:
return slugify((self.publication + "-" if date else "") + self.title) name_type = splitext(basename(self.file))
return (
slugify((self.publication + "-" if date else "") + name_type[0])
+ name_type[1]
)
class LimitCounter: class LimitCounter:

View File

@ -1,6 +1,7 @@
#!python #!python
# pyright: strict # pyright: strict
from os import makedirs, mkdir from os import makedirs, mkdir
from os.path import expanduser
from shutil import copyfile, rmtree from shutil import copyfile, rmtree
from sys import argv from sys import argv
@ -123,21 +124,25 @@ if __name__ == "__main__": # Only if script is directly executed
s: str = "s" if counter.remaining() > 1 else "" s: str = "s" if counter.remaining() > 1 else ""
print(" Exporting", end="") print(" Exporting", end="")
style(f" {counter.remaining()}", BO, B) style(f" {counter.remaining()}", BO, B)
style(f" file{s}") style(f" document{s}")
print(" in this article") print(" in this article")
# Print the name of the file with a counter # Print the name of the file with a counter
style(f" {counter.count + 1}. {document.media} ") style(f" {counter.count + 1}. {document.media} ")
print(f" {document.title}") if len(document.title) > 0:
# highlight(document.title, *unknown_chars(document.title)) highlight(document.title + " ", *unknown_chars(document.title))
style("at ")
print(document.file)
# Define document path
documentpath: str = expanduser(config.data_dir + "/" + document.file)
# Copy the document from its SPIP location to the new location # Copy the document from its SPIP location to the new location
try: try:
copyfile(config.data_dir + "/" + document.file, document.get_slug()) copyfile(documentpath, articledir + "/" + document.get_slug())
except FileNotFoundError: except FileNotFoundError:
style(" NOT FOUND", BO, R) style(" NOT FOUND: ", BO, R)
print(f" {document.file}") print(documentpath)
else: else:
# Print the outputted files path when copied the file # Print the outputted files path when copied the file
style("\n --> ACTUALLY FOUND", BO, B) style(" -->", BO, B)
print(f" {articledir}/{document.get_slug()}") print(f" {articledir}/{document.get_slug()}")
# Print the outputted files path when finished exporting the article # Print the outputted files path when finished exporting the article
style(" --> ", BO, Y) style(" --> ", BO, Y)