diff options
Diffstat (limited to 'chimere/utils.py')
| -rw-r--r-- | chimere/utils.py | 36 | 
1 files changed, 24 insertions, 12 deletions
| diff --git a/chimere/utils.py b/chimere/utils.py index 8f8128e..1a63bb5 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -52,6 +52,7 @@ class ImportManager:      u"""      Generic class for specific importers      """ +    default_source = None      def __init__(self, importer_instance):          self.importer_instance = importer_instance @@ -89,16 +90,23 @@ class ImportManager:                  files.append(None)          return files -    def get_source_file(self, source, suffixes, dest_dir=None): -        if not source: +    def get_source_file(self, source, suffixes, dest_dir=None, +                        extra_url=None): +        if not hasattr(source, 'read'): +            if not source: +                source = self.importer_instance.source \ +                       if self.importer_instance.source else self.default_source              try: -                remotehandle = urllib2.urlopen(self.importer_instance.source) +                url = source +                if extra_url: +                    url += extra_url +                remotehandle = urllib2.urlopen(url)                  source = StringIO.StringIO(remotehandle.read())                  remotehandle.close()              except ValueError:                  # assume it is a local file                  try: -                    source = open(self.importer_instance.source) +                    source = open(source)                  except IOError, msg:                      return (None, msg)              except urllib2.URLError as error: @@ -143,13 +151,15 @@ class KMLManager(ImportManager):          source, msg = self.get_source_file(source, ['.kml'])          if msg:              return (0, 0, msg) +        doc = source          # remove empty lines before declaration (bad XML file) -        splitted = source.getvalue().split('\n') -        for idx, line in enumerate(splitted): -            if line.strip(): -                break -        doc = "\n".join(splitted[idx:]) -        tree = etree.parse(StringIO.StringIO(doc)) +        if hasattr(source, 'getvalue'): +            splitted = source.getvalue().split('\n') +            for idx, line in enumerate(splitted): +                if line.strip(): +                    break +            doc = StringIO.StringIO("\n".join(splitted[idx:])) +        tree = etree.parse(doc)          # try to get default namespace          if not self.ns:              self.ns = tree.getroot().nsmap[None] @@ -400,6 +410,7 @@ class OSMManager(ImportManager):      The source url is a path to an OSM file or a XAPI url      The filtr argument is XAPI args or empty if it is an OSM file.      """ +    default_source = settings.CHIMERE_XAPI_URL      def get(self, source=None):          u""" @@ -416,8 +427,9 @@ class OSMManager(ImportManager):          from models import Marker          new_item, updated_item = 0 , 0          items = [] -        source, msg = self.get_source_file(source, ['.osm']) -        if msg: +        source, msg = self.get_source_file(source, ['.osm'], +                                         extra_url=self.importer_instance.filtr) +        if not source:              return (0, 0, msg)          tree = etree.parse(source)          for node in tree.xpath('//node'): | 
