Skip to content

Commit

Permalink
Le max de caractères d’un nom de fichier est juste sur le nom de fich…
Browse files Browse the repository at this point in the history
…ier et non sur tout le chemin

Issue: #61
  • Loading branch information
Seb35 committed Mar 9, 2019
1 parent b762322 commit fe08b3a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
10 changes: 9 additions & 1 deletion marcheolex/exports/FichierUnique.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
# Imports
from . import Organisations

try:
# @WojtekCh https://stackoverflow.com/a/32812228/174027
LIMIT_NAME_MAX = int(subprocess.check_output("getconf NAME_MAX /", shell=True))
except:
LIMIT_NAME_MAX = 0


class FichierUnique( Organisations ):

Expand All @@ -30,6 +36,8 @@ def __init__( self, extension ):
"""

self.extension = '.' + extension if extension else ''
self.limite_nom_texte = max(LIMIT_NAME_MAX - len(self.extension), 0) if LIMIT_NAME_MAX is not None else None
self.limite_nom_texte = None if self.limite_nom_texte is 0 else self.limite_nom_texte

def obtenir_nom_fichier( self, id, parents, num, titre ):

Expand All @@ -51,7 +59,7 @@ def obtenir_nom_fichier( self, id, parents, num, titre ):
if len(parents) == 0:
if not titre:
raise Exception()
return titre + self.extension
return titre[:self.limite_nom_texte] + self.extension

return None

Expand Down
1 change: 1 addition & 0 deletions marcheolex/exports/Organisations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# To Public License, Version 2, as published by Sam Hocevar. See
# the LICENSE file for more details.


class Organisations:

def obtenir_nom_fichier( self, id, parents, num, titre ):
Expand Down
7 changes: 1 addition & 6 deletions marcheolex/exports/StockageGitFichiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
import subprocess
from . import Stockage

try:
# @WojtekCh https://stackoverflow.com/a/32812228/174027
LIMIT_NAME_MAX = int(subprocess.check_output("getconf NAME_MAX /", shell=True))
except:
LIMIT_NAME_MAX = 0

class StockageGitFichiers( Stockage ):

Expand All @@ -44,7 +39,7 @@ def ecrire_ressource( self, id, parents, num, titre, texte ):

# Enregistrer les fichiers
for fichier in fichiers:
nom_fichier = os.path.join( self.dossier, fichier[0] )[-LIMIT_NAME_MAX:]
nom_fichier = os.path.join( self.dossier, fichier[0] )
os.makedirs( os.path.dirname( nom_fichier ), exist_ok=True )
with open( nom_fichier, 'w' ) as f:
contenu = fichier[1].strip()
Expand Down
15 changes: 12 additions & 3 deletions marcheolex/exports/UnArticleParFichierAvecHierarchie.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
import os
from . import Organisations

try:
# @WojtekCh https://stackoverflow.com/a/32812228/174027
LIMIT_NAME_MAX = int(subprocess.check_output("getconf NAME_MAX /", shell=True))
except:
LIMIT_NAME_MAX = 0


class UnArticleParFichierAvecHierarchie( Organisations ):

Expand All @@ -31,6 +37,9 @@ def __init__( self, extension ):
"""

self.extension = '.' + extension if extension else ''
self.limite_nom_section = None if LIMIT_NAME_MAX is 0 else LIMIT_NAME_MAX
self.limite_nom_article = max(LIMIT_NAME_MAX - len(self.extension) - 8, 0)
self.limite_nom_article = None if self.limite_nom_article is 0 else self.limite_nom_article

def obtenir_nom_fichier( self, id, parents, num, titre ):

Expand All @@ -50,11 +59,11 @@ def obtenir_nom_fichier( self, id, parents, num, titre ):
"""

if id[4:8] == 'ARTI':
chemin = os.path.dirname( os.path.join( *[parent[2] for parent in parents] ) )
chemin = os.path.dirname( os.path.join( *[parent[2][:self.limite_nom_section] for parent in parents] ) )
if num:
return os.path.join( chemin, 'Article_' + num.replace(' ', '_') + self.extension )
return os.path.join( chemin, 'Article_' + num.replace(' ', '_')[:self.limite_nom_article] + self.extension )
else:
return os.path.join( chemin, 'Article_' + id + self.extension )
return os.path.join( chemin, 'Article_' + id[:self.limite_nom_article] + self.extension )

return None

Expand Down
13 changes: 11 additions & 2 deletions marcheolex/exports/UnArticleParFichierSansHierarchie.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
# the LICENSE file for more details.

# Imports
import subprocess
from . import Organisations

try:
# @WojtekCh https://stackoverflow.com/a/32812228/174027
LIMIT_NAME_MAX = int(subprocess.check_output("getconf NAME_MAX /", shell=True))
except:
LIMIT_NAME_MAX = 0


class UnArticleParFichierSansHierarchie( Organisations ):

Expand All @@ -30,6 +37,8 @@ def __init__( self, extension ):
"""

self.extension = '.' + extension if extension else ''
self.limite_nom_article = max(LIMIT_NAME_MAX - len(self.extension) - 8, 0)
self.limite_nom_article = None if self.limite_nom_article is 0 else self.limite_nom_article

def obtenir_nom_fichier( self, id, parents, num, titre ):

Expand All @@ -50,9 +59,9 @@ def obtenir_nom_fichier( self, id, parents, num, titre ):

if id[4:8] == 'ARTI':
if num:
return 'Article_' + num.replace(' ', '_') + self.extension
return 'Article_' + num.replace(' ', '_')[:self.limite_nom_article] + self.extension
else:
return 'Article_' + id + self.extension
return 'Article_' + id[:self.limite_nom_article] + self.extension

return None

Expand Down

0 comments on commit fe08b3a

Please sign in to comment.