34
34
import tempfile
35
35
import json
36
36
import re
37
+ import fnmatch
37
38
from typing import Optional , Dict , Union , Callable
38
39
from collections import defaultdict
39
40
from .Analyser_Osmosis import Analyser_Osmosis
@@ -322,7 +323,7 @@ def __init__(self, attribution = None, millesime = None, encoding = "utf-8", fil
322
323
@param file: file name in storage
323
324
@param urlFile: remote URL of source file
324
325
@param fileUrlCache: days for file in cache
325
- @param zip: extract file from zip
326
+ @param zip: extract a file from zip. Unix filename pattern matching.
326
327
@param extract: extract file from any archive format
327
328
@param gzip: uncompress from bz2
328
329
@param gzip: uncompress from gzip
@@ -350,13 +351,23 @@ def __init__(self, attribution = None, millesime = None, encoding = "utf-8", fil
350
351
if self .attribution and "{0}" in self .attribution :
351
352
self .attribution_re = re .compile (self .attribution .replace ("{0}" , ".*" ))
352
353
354
+ def zipFile (self ):
355
+ if self .file :
356
+ f = open (self .file , 'rb' )
357
+ elif self .fileUrl :
358
+ f = downloader .urlopen (self .fileUrl , self .fileUrlCache , mode = 'rb' )
359
+
360
+ z = zipfile .ZipFile (f , 'r' )
361
+ print (z .namelist ())
362
+ filename = next (filter (lambda zipinfo : fnmatch .fnmatch (zipinfo .filename , self .zip ), z .infolist ()))
363
+ return filename
364
+
353
365
def time (self ):
354
366
if self .file :
355
367
return int (os .path .getmtime (self .file )+ .5 )
356
368
elif self .fileUrl :
357
- if self .zip :
358
- f = downloader .urlopen (self .fileUrl , self .fileUrlCache , mode = 'rb' )
359
- date_time = zipfile .ZipFile (f , 'r' ).getinfo (self .zip ).date_time
369
+ if self .zipFile ():
370
+ date_time = self .zipFile ().date_time
360
371
return int (time .mktime (date_time + (0 , 0 , - 1 ))+ .5 )
361
372
else :
362
373
return int (downloader .urlmtime (self .fileUrl , self .fileUrlCache )+ .5 )
@@ -374,8 +385,8 @@ def open(self, binary = False):
374
385
elif self .fileUrl :
375
386
f = downloader .urlopen (self .fileUrl , self .fileUrlCache , mode = 'rb' )
376
387
377
- if self .zip :
378
- z = zipfile .ZipFile (f , 'r' ).open (self .zip )
388
+ if self .zipFile () :
389
+ z = zipfile .ZipFile (f , 'r' ).open (self .zipFile (). filename )
379
390
f = io .BytesIO (z .read ())
380
391
f .seek (0 )
381
392
elif self .extract :
@@ -689,7 +700,7 @@ def import_(self, table, srid, osmosis):
689
700
self .source .encoding ,
690
701
srid ,
691
702
tmp_file .name ,
692
- self .source .zip ,
703
+ self .source .zipFile (). filename ,
693
704
table ,
694
705
tmp_file .name
695
706
)
0 commit comments