Пример #1
0
    def update_timestamp(self, attrs):
        self.ts = attrs.get("timestamp", time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()))
        self.version = attrs.get("version", None)

        if not self._tstamp_updated:
            try:
                execute_sql(self._dbcurs, "INSERT INTO dynpoi_update (source, timestamp, remote_url, remote_ip, version) VALUES(%s, %s, %s, %s, %s);",
                                     (self._source_id, utils.pg_escape(self.ts),
                                      utils.pg_escape(self._source_url),
                                      utils.pg_escape(self._remote_ip),
                                      utils.pg_escape(self.version)))
            except psycopg2.IntegrityError:
                self._dbconn.rollback()
                execute_sql(self._dbcurs, "SELECT count(*) FROM dynpoi_update WHERE source = %s AND \"timestamp\" = %s",
                                     (self._source_id, utils.pg_escape(self.ts)))
                r = self._dbcurs.fetchone()
                if r["count"] == 1:
                    raise OsmoseUpdateAlreadyDone, "source=%s and timestamp=%s are already present" % (self._source_id, utils.pg_escape(self.ts))
                else:
                    raise

            execute_sql(self._dbcurs, "UPDATE dynpoi_update_last SET timestamp=%s WHERE source=%s;",
                                 (utils.pg_escape(self.ts), self._source_id))
            if self._dbcurs.rowcount == 0:
                execute_sql(self._dbcurs, "INSERT INTO dynpoi_update_last VALUES(%s, %s);",
                                 (self._source_id, utils.pg_escape(self.ts)))

            self._tstamp_updated = True
Пример #2
0
    def update_timestamp(self, attrs):
        self.ts = attrs.get("timestamp",
                            time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()))
        self.version = attrs.get("version", None)

        if not self._tstamp_updated:
            try:
                execute_sql(
                    self._dbcurs,
                    "INSERT INTO dynpoi_update (source, timestamp, remote_url, remote_ip, version) VALUES(%s, %s, %s, %s, %s);",
                    (self._source_id, utils.pg_escape(
                        self.ts), utils.pg_escape(self._source_url),
                     utils.pg_escape(
                         self._remote_ip), utils.pg_escape(self.version)))
            except psycopg2.IntegrityError:
                self._dbconn.rollback()
                execute_sql(
                    self._dbcurs,
                    "SELECT count(*) FROM dynpoi_update WHERE source = %s AND \"timestamp\" = %s",
                    (self._source_id, utils.pg_escape(self.ts)))
                r = self._dbcurs.fetchone()
                if r["count"] == 1:
                    raise OsmoseUpdateAlreadyDone, "source=%s and timestamp=%s are already present" % (
                        self._source_id, utils.pg_escape(self.ts))
                else:
                    raise

            execute_sql(
                self._dbcurs,
                "UPDATE dynpoi_update_last SET timestamp=%s WHERE source=%s;",
                (utils.pg_escape(self.ts), self._source_id))
            if self._dbcurs.rowcount == 0:
                execute_sql(self._dbcurs,
                            "INSERT INTO dynpoi_update_last VALUES(%s, %s);",
                            (self._source_id, utils.pg_escape(self.ts)))

            self._tstamp_updated = True
Пример #3
0
    def endElement(self, name):
        if name == u"error":
            ## build all_elem
            all_elem = u""
            for e in self._error_elements:
                all_elem += e[u"type"] + e[u"id"] + "_"
            all_elem = all_elem.rstrip("_")

            ## sql template
            sql_marker = u"INSERT INTO marker (source, class, subclass, item, lat, lon, elems, subtitle) VALUES (" + "%s," * 7 + "%s) RETURNING id;"

            ## add data at all location
            if len(self._error_locations) == 0:
                print "No location on error found on line %d" % self.locator.getLineNumber(
                )
                return

            cpt = 0
            for location in self._error_locations:
                cpt += 1

                lat = float(location["lat"])
                lon = float(location["lon"])

                execute_sql(self._dbcurs, sql_marker, (
                    self._source_id,
                    self._class_id,
                    self._class_sub,
                    self._class_item[self._class_id],
                    lat,
                    lon,
                    utils.pg_escape(all_elem),
                    self._error_texts,
                ))
                marker_id = self._dbcurs.fetchone()[0]

            ## add all elements
            sql_elem = u"INSERT INTO marker_elem (marker_id, elem_index, data_type, id, tags, username) VALUES (" + "%s, " * 5 + "%s)"
            num = 0
            for elem in self._error_elements:
                if elem["type"] in ("node", "way", "relation"):
                    execute_sql(self._dbcurs, sql_elem,
                                (marker_id, num, elem["type"][0].upper(),
                                 int(elem["id"]), elem["tag"], elem["user"]))
                    num += 1
                if elem["type"] in ("infos"):
                    execute_sql(self._dbcurs, sql_elem,
                                (marker_id, num, elem["type"][0].upper(), 0,
                                 elem["tag"], elem["user"]))
                    num += 1

            ## add quickfixes
            sql_fix = u"INSERT INTO marker_fix (marker_id, diff_index, elem_data_type, elem_id, tags_create, tags_modify, tags_delete) VALUES (" + "%s, " * 6 + "%s)"
            num = 0
            for fix in self._fixes:
                for elem in fix:
                    if elem["type"] in ("node", "way", "relation"):
                        execute_sql(self._dbcurs, sql_fix,
                                    (marker_id, num, elem["type"][0].upper(),
                                     int(elem["id"]), elem["tags_create"],
                                     elem["tags_modify"], elem["tags_delete"]))
                num += 1

        elif name in [u"node", u"way", u"relation", u"infos"]:
            if self.elem_mode == "info":
                self._elem[u"tag"] = self._elem_tags
                self._error_elements.append(self._elem)
            else:
                self._elem[u"tags_create"] = self._fix_create
                self._elem[u"tags_modify"] = self._fix_modify
                self._elem[u"tags_delete"] = self._fix_delete
                self._fix.append(self._elem)

        elif name == u"class":
            keys = [
                "source", "class", "item", "title", "level", "tags",
                "timestamp"
            ]
            vals = [
                self._source_id,
                self._class_id,
                self._class_item[self._class_id],
                self._class_texts,
                self._class_level,
                self._class_tags,
                utils.pg_escape(self.ts),
            ]
            if self.mode == "analyser":
                execute_sql(
                    self._dbcurs,
                    "DELETE FROM marker WHERE source = %s AND class = %s;",
                    (self._source_id, self._class_id))

                execute_sql(
                    self._dbcurs,
                    "DELETE FROM dynpoi_class WHERE source = %s AND class = %s",
                    (self._source_id, self._class_id))
                sql = u"INSERT INTO dynpoi_class (" + u','.join(keys) + u") "
                sql += u"VALUES (" + (u','.join(["%s"] * len(keys))) + u");"
                execute_sql(self._dbcurs, sql, vals)

            else:
                sql = u"UPDATE dynpoi_class SET " + (
                    u' = %s, '.join(keys)) + u" = %s "
                sql += u"WHERE source = %s AND class = %s;"
                ch_vals = vals + [self._source_id, self._class_id]
                execute_sql(self._dbcurs, sql, ch_vals)

                if self._dbcurs.rowcount == 0:
                    sql = u"INSERT INTO dynpoi_class (" + u','.join(
                        keys) + u") "
                    sql += u"VALUES (" + (u','.join(
                        ["%s"] * len(keys))) + u");"
                    execute_sql(self._dbcurs, sql, vals)

        elif name == u"fixes":
            self.elem_mode = "info"
        elif name == u"fix":
            self._fixes.append(self._fix)
Пример #4
0
    def endElement(self, name):
        if name == u"error":
            ## build all_elem
            all_elem = u""
            for e in self._error_elements:
                all_elem += e[u"type"] + e[u"id"] + "_"
            all_elem = all_elem.rstrip("_")

            ## sql template
            sql_marker = (
                u"INSERT INTO marker (source, class, subclass, item, lat, lon, elems, subtitle) VALUES ("
                + "%s," * 7
                + "%s) RETURNING id;"
            )

            ## add data at all location
            if len(self._error_locations) == 0:
                print "No location on error found on line %d" % self.locator.getLineNumber()
                return

            cpt = 0
            for location in self._error_locations:
                cpt += 1

                lat = float(location["lat"])
                lon = float(location["lon"])

                execute_sql(
                    self._dbcurs,
                    sql_marker,
                    (
                        self._source_id,
                        self._class_id,
                        self._class_sub,
                        self._class_item[self._class_id],
                        lat,
                        lon,
                        utils.pg_escape(all_elem),
                        self._error_texts,
                    ),
                )
                marker_id = self._dbcurs.fetchone()[0]

            ## add all elements
            sql_elem = (
                u"INSERT INTO marker_elem (marker_id, elem_index, data_type, id, tags, username) VALUES ("
                + "%s, " * 5
                + "%s)"
            )
            num = 0
            for elem in self._error_elements:
                if elem["type"] in ("node", "way", "relation"):
                    execute_sql(
                        self._dbcurs,
                        sql_elem,
                        (marker_id, num, elem["type"][0].upper(), int(elem["id"]), elem["tag"], elem["user"]),
                    )
                    num += 1
                if elem["type"] in ("infos"):
                    execute_sql(
                        self._dbcurs, sql_elem, (marker_id, num, elem["type"][0].upper(), 0, elem["tag"], elem["user"])
                    )
                    num += 1

            ## add quickfixes
            sql_fix = (
                u"INSERT INTO marker_fix (marker_id, diff_index, elem_data_type, elem_id, tags_create, tags_modify, tags_delete) VALUES ("
                + "%s, " * 6
                + "%s)"
            )
            num = 0
            for fix in self._fixes:
                for elem in fix:
                    if elem["type"] in ("node", "way", "relation"):
                        execute_sql(
                            self._dbcurs,
                            sql_fix,
                            (
                                marker_id,
                                num,
                                elem["type"][0].upper(),
                                int(elem["id"]),
                                elem["tags_create"],
                                elem["tags_modify"],
                                elem["tags_delete"],
                            ),
                        )
                num += 1

        elif name in [u"node", u"way", u"relation", u"infos"]:
            if self.elem_mode == "info":
                self._elem[u"tag"] = self._elem_tags
                self._error_elements.append(self._elem)
            else:
                self._elem[u"tags_create"] = self._fix_create
                self._elem[u"tags_modify"] = self._fix_modify
                self._elem[u"tags_delete"] = self._fix_delete
                self._fix.append(self._elem)

        elif name == u"class":
            keys = ["source", "class", "item", "title", "level", "tags", "timestamp"]
            vals = [
                self._source_id,
                self._class_id,
                self._class_item[self._class_id],
                self._class_texts,
                self._class_level,
                self._class_tags,
                utils.pg_escape(self.ts),
            ]
            if self.mode == "analyser":
                execute_sql(
                    self._dbcurs,
                    "DELETE FROM marker WHERE source = %s AND class = %s;",
                    (self._source_id, self._class_id),
                )

                execute_sql(
                    self._dbcurs,
                    "DELETE FROM dynpoi_class WHERE source = %s AND class = %s",
                    (self._source_id, self._class_id),
                )
                sql = u"INSERT INTO dynpoi_class (" + u",".join(keys) + u") "
                sql += u"VALUES (" + (u",".join(["%s"] * len(keys))) + u");"
                execute_sql(self._dbcurs, sql, vals)

            else:
                sql = u"UPDATE dynpoi_class SET " + (u" = %s, ".join(keys)) + u" = %s "
                sql += u"WHERE source = %s AND class = %s;"
                ch_vals = vals + [self._source_id, self._class_id]
                execute_sql(self._dbcurs, sql, ch_vals)

                if self._dbcurs.rowcount == 0:
                    sql = u"INSERT INTO dynpoi_class (" + u",".join(keys) + u") "
                    sql += u"VALUES (" + (u",".join(["%s"] * len(keys))) + u");"
                    execute_sql(self._dbcurs, sql, vals)

        elif name == u"fixes":
            self.elem_mode = "info"
        elif name == u"fix":
            self._fixes.append(self._fix)