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
from os.path import basename, splitext
from typing import Any, Optional
from slugify import slugify
@ -158,9 +159,9 @@ class Document:
def __init__(self, document: SpipDocuments) -> None:
self.id: int = document.id_document
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.description: str = document.descriptif
self.description: str = convert_meta(document.descriptif)
self.file: str = document.fichier
self.draft: bool = document.statut == "publie"
self.creation: str = document.date
@ -169,7 +170,11 @@ class Document:
self.media: str = document.media
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:

View File

@ -1,6 +1,7 @@
#!python
# pyright: strict
from os import makedirs, mkdir
from os.path import expanduser
from shutil import copyfile, rmtree
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 ""
print(" Exporting", end="")
style(f" {counter.remaining()}", BO, B)
style(f" file{s}")
style(f" document{s}")
print(" in this article")
# Print the name of the file with a counter
style(f" {counter.count + 1}. {document.media} ")
print(f" {document.title}")
# highlight(document.title, *unknown_chars(document.title))
if len(document.title) > 0:
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
try:
copyfile(config.data_dir + "/" + document.file, document.get_slug())
copyfile(documentpath, articledir + "/" + document.get_slug())
except FileNotFoundError:
style(" NOT FOUND", BO, R)
print(f" {document.file}")
style(" NOT FOUND: ", BO, R)
print(documentpath)
else:
# 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 the outputted files path when finished exporting the article
style(" --> ", BO, Y)