class Cruncher(object): """XML Cruncher!!!""" current_node = None nodes = [] def start(self, tag, attrib): """parser enters tag""" if tag == "node": self.current_node = Node(attrib["id"], attrib["lat"], attrib["lon"]) elif tag == "tag" and self.current_node: self.current_node.add_tag(attrib["k"], attrib["v"]) def end(self, tag): """parses end tags""" if tag == "node" and self.current_node: if self.current_node.tags.has_key("name"): self.nodes.append(self.current_node) self.current_node = None def close(self): """parser closes""" nodes, self.nodes = self.nodes, [] return nodes
class MongoLoader(object): """Parses xml nodes and inserts appropriate ones to db""" def __init__(self, collection_name, named_only, tag_names): """C'tor sets tag wich we'll count""" self._named_only = named_only if isinstance(tag_names, list): self._tag_names = set(tag_names) else: self._tag_names = set([tag_names]) self._current_node = None self._count = 0 conn = Connection('localhost', 27017) db = conn['openstreet'] self._ca = db[collection_name] def start(self, tag, attrib): """parser enters tag""" if tag == "node": # Start node writing self._current_node = Node( float(attrib["lat"]), float(attrib["lon"]), id = int(attrib["id"]) ) elif tag == "tag" and self._current_node: # Add tag to node self._current_node.add_tag(attrib["k"], attrib["v"]) def end(self, tag): """parser reached end of tag""" if tag == "node" and self._current_node: # tags_set = set(self._current_node.tags) # if not self._named_only or "name" in tags_set: # if len(tags_set & self._tag_names): # self._save_node() if "name" in self._current_node.tags: self._save_node() self._current_node = None def close(self): """parser closes""" count, self._count = self._count, 0 return count def _save_node(self): """Save current_node to list""" self._ca.insert(self._current_node) self._count += 1
class MongoLoader(object): """Parses xml nodes and inserts appropriate ones to db""" def __init__(self, collection_name): """C'tor sets tag wich we'll count""" from pymongo import Connection conn = Connection('localhost', 27017) db = conn['openstreetmaps'] self._ca = db[collection_name] self._current_node = None self._count = 0 def start(self, tag, attrib): """parser enters tag""" from model import Node if tag == "node": # Start node writing self._current_node = Node( float(attrib["lat"]), float(attrib["lon"]), id = int(attrib["id"]) ) elif tag == "tag" and self._current_node: # Add tag to node self._current_node.add_tag(attrib["k"], attrib["v"]) def end(self, tag): """parser reached end of tag""" if tag == "node" and self._current_node: self.__save_node() self._current_node = None def close(self): """parser closes""" count, self._count = self._count, 0 return count def __save_node(self): """Save current_node to list""" self._ca.insert(self._current_node) self._count += 1
class MongoLoader(object): """Parses xml nodes and inserts appropriate ones to db""" def __init__(self, collection_name): """C'tor sets tag wich we'll count""" from pymongo import Connection conn = Connection('localhost', 27017) db = conn['openstreetmaps'] self._ca = db[collection_name] self._current_node = None self._count = 0 def start(self, tag, attrib): """parser enters tag""" from model import Node if tag == "node": # Start node writing self._current_node = Node(float(attrib["lat"]), float(attrib["lon"]), id=int(attrib["id"])) elif tag == "tag" and self._current_node: # Add tag to node self._current_node.add_tag(attrib["k"], attrib["v"]) def end(self, tag): """parser reached end of tag""" if tag == "node" and self._current_node: self.__save_node() self._current_node = None def close(self): """parser closes""" count, self._count = self._count, 0 return count def __save_node(self): """Save current_node to list""" self._ca.insert(self._current_node) self._count += 1
class JSONLoader(object): """Parses xml nodes and inserts appropriate ones to db""" def __init__(self): """C'tor sets tag wich we'll count""" from json import JSONEncoder self._encoder = JSONEncoder() self._current_node = None self._count = 0 def start(self, tag, attrib): """parser enters tag""" from model import Node if tag == "node": # Start node writing self._current_node = Node( float(attrib["lat"]), float(attrib["lon"]), id = int(attrib["id"]) ) elif tag == "tag" and self._current_node: # Add tag to node self._current_node.add_tag(attrib["k"], attrib["v"]) def end(self, tag): """parser reached end of tag""" if tag == "node" and self._current_node: self.__save_node() self._current_node = None def close(self): """parser closes""" count, self._count = self._count, 0 return count def __save_node(self): """Save current_node to list""" print(self._encoder.encode(self._current_node)) self._count += 1
class JSONLoader(object): """Parses xml nodes and inserts appropriate ones to db""" def __init__(self): """C'tor sets tag wich we'll count""" from json import JSONEncoder self._encoder = JSONEncoder() self._current_node = None self._count = 0 def start(self, tag, attrib): """parser enters tag""" from model import Node if tag == "node": # Start node writing self._current_node = Node(float(attrib["lat"]), float(attrib["lon"]), id=int(attrib["id"])) elif tag == "tag" and self._current_node: # Add tag to node self._current_node.add_tag(attrib["k"], attrib["v"]) def end(self, tag): """parser reached end of tag""" if tag == "node" and self._current_node: self.__save_node() self._current_node = None def close(self): """parser closes""" count, self._count = self._count, 0 return count def __save_node(self): """Save current_node to list""" print(self._encoder.encode(self._current_node)) self._count += 1