Projet

Général

Profil

Development #40704

connecteur csv : import xslx impossible

Ajouté par Thomas Noël il y a environ 4 ans. Mis à jour il y a presque 4 ans.

Statut:
Fermé
Priorité:
Normal
Assigné à:
Version cible:
-
Début:
12 mars 2020
Echéance:
% réalisé:

0%

Temps estimé:
Patch proposed:
Oui
Planning:
Non

Description

La création d'un connecteur tableur avec un fichier .xlsx débouche sur une erreur « Fichier CSV invalide : [Errno 2] No such file or directory: u'/var/lib/passerelle/tenants/passerelle-calvados.test.entrouvert.org/media/communes_apcr.xlsx' »


Fichiers

Révisions associées

Révision 14a4cce0 (diff)
Ajouté par Benjamin Dauvergne il y a presque 4 ans

csvdatasource: store data in temporary file when loading excel sheets (#40704)

Historique

#2

Mis à jour par Thomas Noël il y a environ 4 ans

diff --git a/passerelle/apps/csvdatasource/models.py b/passerelle/apps/csvdatasource/models.py
index a8d85467..7c47cdd9 100644
--- a/passerelle/apps/csvdatasource/models.py
+++ b/passerelle/apps/csvdatasource/models.py
@@ -234,7 +234,7 @@ class CsvDataSource(BaseResource):
                 content = get_data_ods(self.csv_file)

             elif file_type == 'xls' or file_type == 'xlsx':
-                content = get_data_xls(self.csv_file.path)
+                content = get_data_xls(self.csv_file)

             if len(content.keys()) == 1:
                 # if there's a single sheet, ignore specified sheet name and

Ça n'a jamais du être testé en vrai depuis un changement quelque part, et les tests sont pas bons. J'enquête...

#3

Mis à jour par Thomas Noël il y a environ 4 ans

(enquête abandonnée pour l'instant, à votre bon coeur)

#4

Mis à jour par Thomas Noël il y a environ 4 ans

Ca vient de #39971 qui ne peut pas fonctionner en xlsx car le fichier n'est pas encore sur le disque au moment du get_rows()

#5

Mis à jour par Thomas Noël il y a environ 4 ans

L'API de pyexcel_xls est tellement pas documentée que je doute d'y arriver (tenté plein de truc, fini avec

-                content = get_data_xls(self.csv_file.path)
+                self.csv_file.seek(0)
+                afile = six.StringIO(self.csv_file.read())
+                content = get_data_xls(afile)

qui se termine en :
passerelle/apps/csvdatasource/models.py:239: in get_rows
    content = get_data_xls(afile)
/tmp/tox-thomas/passerelle/py2-django111-pg-junit/local/lib/python2.7/site-packages/pyexcel_xls/__init__.py:32: in get_data
    return read_data(afile, file_type=file_type, **keywords)
/tmp/tox-thomas/passerelle/py2-django111-pg-junit/local/lib/python2.7/site-packages/pyexcel_io/io.py:73: in get_data
    afile, file_type=file_type, streaming=False, **keywords
/tmp/tox-thomas/passerelle/py2-django111-pg-junit/local/lib/python2.7/site-packages/pyexcel_io/io.py:91: in _get_data
    return load_data(**keywords)
/tmp/tox-thomas/passerelle/py2-django111-pg-junit/local/lib/python2.7/site-packages/pyexcel_io/io.py:216: in load_data
    result = reader.read_all()
/tmp/tox-thomas/passerelle/py2-django111-pg-junit/local/lib/python2.7/site-packages/pyexcel_xls/xlsr.py:175: in read_all
    self._native_book = self._get_book()
/tmp/tox-thomas/passerelle/py2-django111-pg-junit/local/lib/python2.7/site-packages/pyexcel_xls/xlsr.py:205: in _get_book
    xls_book = xlrd.open_workbook(**xlrd_params)
/tmp/tox-thomas/passerelle/py2-django111-pg-junit/local/lib/python2.7/site-packages/xlrd/__init__.py:138: in open_workbook

    def open_workbook_2007_xml(zf,
                               component_names,
                               logfile=sys.stdout,
                               verbosity=0,
                               use_mmap=0,
                               formatting_info=0,
                               on_demand=0,
                               ragged_rows=0):
        ensure_elementtree_imported(verbosity, logfile)
        bk = Book()
        bk.logfile = logfile
        bk.verbosity = verbosity
        bk.formatting_info = formatting_info
        if formatting_info:
>           raise NotImplementedError("formatting_info=True not yet implemented")
E           NotImplementedError: formatting_info=True not yet implemented

Je lâche l'affaire, considérant qu'on devrait abandonner xls et xlsx et ne prendre en charge que ods et csv.

#6

Mis à jour par Benjamin Dauvergne il y a environ 4 ans

Thomas Noël a écrit :

L'API de pyexcel_xls est tellement pas documentée que je doute d'y arriver (tenté plein de truc, fini avec
[...]
qui se termine en :
[...]

Je lâche l'affaire, considérant qu'on devrait abandonner xls et xlsx et ne prendre en charge que ods et csv.

L'objet file de Django est caca mais on peut faire plus simple :

with open(self.csv_file.path, 'rb') as fd:  # enlever 'b' si pas nécessaire, c'est bizarre que ça marche en StringIO, xls c'est pas du binaire ?
     content = get_data_xls(fd)
#7

Mis à jour par Thomas Noël il y a environ 4 ans

Le soucis c'est que self.csv_file.path pointe vers un truc qui n'existe pas (au moment du get_rows() fait par le clean()). C'est un faux chemin (genre media/filename) d'où l'erreur qui s'affiche (No such file or directory: u'/var/lib/passerelle/tenants/xxx/media/file.xlsx)

#8

Mis à jour par Benjamin Dauvergne il y a environ 4 ans

Thomas Noël a écrit :

Le soucis c'est que self.csv_file.path pointe vers un truc qui n'existe pas (au moment du get_rows() fait par le clean()). C'est un faux chemin (genre media/filename) d'où l'erreur qui s'affiche (No such file or directory: u'/var/lib/passerelle/tenants/xxx/media/file.xlsx)

Ok, de toute façon c'est moche.

#10

Mis à jour par Thomas Noël il y a presque 4 ans

Je propose ce contournement (ne pas chercher à valider les fichiers xls/xlsx reçus) ; je n'ai vraiment rien trouvé d'autre.

#11

Mis à jour par Benjamin Dauvergne il y a presque 4 ans

  • Assigné à mis à Benjamin Dauvergne
#12

Mis à jour par Benjamin Dauvergne il y a presque 4 ans

Ça m'a fait de la peine, mais les tests de csvdatasource c'est juste Verdun en '17.

#13

Mis à jour par Thomas Noël il y a presque 4 ans

  • Statut changé de Solution proposée à Solution validée
#14

Mis à jour par Benjamin Dauvergne il y a presque 4 ans

  • Statut changé de Solution validée à Résolu (à déployer)
commit 14a4cce0ba826269754a50c9f9836d4bf21b6771
Author: Benjamin Dauvergne <bdauvergne@entrouvert.com>
Date:   Thu Apr 23 21:11:02 2020 +0200

    csvdatasource: store data in temporary file when loading excel sheets (#40704)
#15

Mis à jour par Frédéric Péters il y a presque 4 ans

  • Statut changé de Résolu (à déployer) à Solution déployée

Formats disponibles : Atom PDF