diff options
Diffstat (limited to 'chimere/utils.py')
| -rw-r--r-- | chimere/utils.py | 112 | 
1 files changed, 57 insertions, 55 deletions
| diff --git a/chimere/utils.py b/chimere/utils.py index 2459075..268ec3b 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -21,6 +21,7 @@  Utilitaries  """ +import tempfile  import urllib2, re  import unicodedata  import zipfile @@ -38,27 +39,6 @@ def unicode_normalize(string):          (c for c in unicodedata.normalize('NFD', string)          if unicodedata.category(c) != 'Mn')) -def get_files_inside_zip(zippedfile, suffixes): -    try: -        flz = zipfile.ZipFile(zippedfile) -    except zipfile.BadZipfile: -        return [], _(u"Bad zip file") -    namelist = flz.namelist() -    filenames = [] -    for suffix in suffixes: -        current_file_name = None -        for name in namelist: -            if name.endswith(suffix): -                current_file_name = name -        filenames.append(current_file_name) -    files = [] -    for filename in filenames: -        if filename: -            files.append(flz.open(filename)) -        else: -            files.append(None) -    return files -  class ImportManager:      u"""      Generic class for specific importers @@ -72,6 +52,55 @@ class ImportManager:      def put(self):          pass +    @classmethod +    def get_files_inside_zip(cls, zippedfile, suffixes): +        try: +            flz = zipfile.ZipFile(zippedfile) +        except zipfile.BadZipfile: +            return [], _(u"Bad zip file") +        namelist = flz.namelist() +        filenames = [] +        for suffix in suffixes: +            current_file_name = None +            for name in namelist: +                if name.endswith(suffix) \ +                  or name.endswith(suffix.lower()) \ +                  or name.endswith(suffix.upper()): +                    current_file_name = name +            filenames.append(current_file_name) +        files = [] +        for filename in filenames: +            if filename: +                files.append(flz.open(filename)) +            else: +                files.append(None) +        return files + +    def get_source_file(self, source, suffixes): +        if not source: +            try: +                remotehandle = urllib2.urlopen(self.importer_instance.source) +                source = StringIO.StringIO(remotehandle.read()) +                remotehandle.close() +            except ValueError: +                # assume it is a local file +                try: +                    source = open(self.importer_instance.source) +                except IOError, msg: +                    return (None, msg) +            except urllib2.URLError as error: +                return (None, error.message) +        if self.importer_instance.zipped: +            try: +                files = self.get_files_inside_zip(source, suffixes) +            except zipfile.BadZipfile: +                return (None, _(u"Bad zip file")) +            if not files or None in files: +                return (None, +                        _(u"Missing file(s) inside the zip file")) +            source = files[0] if len(suffixes) == 1 else files +        return (source, None) +  class KMLManager(ImportManager):      u"""      KML importer @@ -98,28 +127,9 @@ class KMLManager(ImportManager):          """          from models import Marker          new_item, updated_item, msg = 0, 0, '' -        if not source: -            try: -                remotehandle = urllib2.urlopen(self.importer_instance.source) -                source = StringIO.StringIO(remotehandle.read()) -                remotehandle.close() -            except ValueError: -                # assume it is a local file -                try: -                    source = open(self.importer_instance.source) -                except IOError, msg: -                    return (new_item, updated_item, msg) -            except urllib2.URLError as error: -                return (new_item, updated_item, error.message) -        if self.importer_instance.zipped: -            try: -                files = get_files_inside_zip(source, ['.kml']) -            except zipfile.BadZipfile: -                return (new_item, updated_item, _(u"Bad zip file")) -            if not files or not files[0]: -                return (new_item, updated_item, -                        _(u"No KML file inside the zip file")) -            source = files[0] +        source, msg = self.get_source_file(source, ['.kml']) +        if msg: +            return (0, 0, msg)          tree = etree.parse(source)          # try to get default namespace          if not self.ns: @@ -172,6 +182,7 @@ class KMLManager(ImportManager):                      m.categories.add(cat)          return (new_item, updated_item, msg) +  RE_NODE = re.compile('node\[([^\]]*)\]')  # manage deleted item from OSM @@ -198,18 +209,9 @@ class OSMManager(ImportManager):          from models import Marker          new_item, updated_item = 0 , 0          items = [] -        msg = '' -        try: -            source = urllib2.urlopen(settings.CHIMERE_XAPI_URL+ -                                     self.importer_instance.filtr) -        except ValueError: -            # assume it is a local file -            try: -                source = open(self.importer_instance.source) -            except IOError, msg: -                return (new_item, updated_item, msg) -        except urllib2.URLError as error: -            return (new_item, updated_item, error.message) +        source, msg = self.get_source_file(source, ['.osm']) +        if msg: +            return (0, 0, msg)          tree = etree.parse(source)          for node in tree.xpath('//node'):              name, point, linestring = None, None, None | 
