From 24b6324e544bc87b5ad9efb9d360922fba74dcd3 Mon Sep 17 00:00:00 2001 From: Famlam Date: Sun, 18 Aug 2024 21:41:45 +0200 Subject: [PATCH] Change string date to int date parsing Also update tests and add a warning just in case the pbf plugin changes the output format again --- plugins/Construction.py | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/plugins/Construction.py b/plugins/Construction.py index 1300d091b..15f656168 100644 --- a/plugins/Construction.py +++ b/plugins/Construction.py @@ -88,11 +88,16 @@ def node(self, data, tags): date = self.convert2date(tagDate) end_date = False - if date: - end_date = date - elif "timestamp" in data: - end_date = datetime.datetime.strptime(data["timestamp"][0:10], "%Y-%m-%d") + self.date_limit - else: + try: + if date: + end_date = date + elif data["timestamp"] > 0: + end_date = datetime.datetime.fromtimestamp(data["timestamp"]) + self.date_limit + else: + return + except: + # This should only trigger in case the pbf reader is updated (and the plugin not), causing the timestamp format to change (or be absent) + print("Error: Unexpected format of timestamp or no timestamp provided, expected numerical timestamp") return delta = int(self.total_seconds(self.today - end_date)) @@ -122,6 +127,9 @@ def setUp(self): self.p.init(None) def test(self): + # Use today, so it won't trigger due to the timestamp in this test + ts = datetime.datetime.timestamp(datetime.datetime.today()) + constr_tags = [{"construction": "yes"}, {"highway": "construction"}, {"landuse": "construction"}, @@ -155,31 +163,34 @@ def test(self): for val_d in correct_dates: t = tags.copy() t.update({tag_d: val_d}) - self.check_err(self.p.node({}, t), t) + self.check_err(self.p.node({"timestamp": ts}, t), t) for val_d in not_correct_dates: t = tags.copy() t.update({tag_d: val_d}) - assert not self.p.way({}, t, None), t + assert not self.p.way({"timestamp": ts}, t, None), t for tags in other_tags: for tag_d in self.p.tag_date: for val_d in correct_dates: t = tags.copy() t.update({tag_d: val_d}) - assert not self.p.relation({}, t, None), t + assert not self.p.relation({"timestamp": ts}, t, None), t def test_timestamp(self): tags = {"construction": "yes"} for ts in ["2003-01-04", "1989-03-10"]: - self.check_err(self.p.node({"timestamp": ts}, tags), ts) - for ts in ["2078-01-04"]: - assert not self.p.node({"timestamp": ts}, tags), ts + ts_int = datetime.datetime.timestamp(datetime.datetime.strptime(ts,"%Y-%m-%d")) + self.check_err(self.p.node({"timestamp": ts_int}, tags), ts) + for ts in ["2078-01-04", str(datetime.datetime.today())[0:10]]: + ts_int = datetime.datetime.timestamp(datetime.datetime.strptime(ts,"%Y-%m-%d")) + assert not self.p.node({"timestamp": ts_int}, tags), ts + assert not self.p.node({"timestamp": 0}, tags) def test_recall(self): tags = {"construction": "yes"} today = datetime.datetime.today() td = datetime.timedelta(days=6 * 30) for i in range(5, 10, 1): - e = self.p.node({"timestamp": (today - i*td).strftime("%Y-%m-%d")}, tags) + e = self.p.node({"timestamp": datetime.datetime.timestamp(today - i*td)}, tags) self.check_err(e, i) assert e["subclass"] == i - 5