Skip to content

Commit 5c16e1a

Browse files
committed
Remove parse_iso_datetime stuff
1 parent d8fdf4d commit 5c16e1a

File tree

1 file changed

+24
-146
lines changed

1 file changed

+24
-146
lines changed

scripts/check-gmp.gmp.py

Lines changed: 24 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
from enum import Enum
2323
import logging
2424
import os
25-
import re
2625
import signal
2726
import sqlite3
2827
import sys
2928
import tempfile
3029
from argparse import ArgumentParser, Namespace, RawTextHelpFormatter
3130
from datetime import datetime, timedelta, tzinfo
32-
from decimal import Decimal
3331
from pathlib import Path
3432
from typing import Any, Tuple
3533
from xml.etree import ElementTree
@@ -192,21 +190,23 @@ def is_old_report(self, last_scan_end, params_used) -> bool:
192190
"SELECT scan_end, params_used FROM Report WHERE host=?",
193191
(self.host,),
194192
)
195-
db_entry = self.cursor.fetchone()
193+
(scan_end, old_params_used) = self.cursor.fetchone()
196194

197-
logger.debug("%s %s", db_entry, last_scan_end)
195+
logger.debug(
196+
"DB last report: %s | New report: %s", scan_end, last_scan_end
197+
)
198198

199-
if not db_entry:
199+
if not scan_end:
200200
return True
201201
else:
202-
old = parse_date(db_entry[0])
202+
old = parse_date(scan_end)
203203
new = parse_date(last_scan_end)
204204

205205
logger.debug(
206206
"Old time (from db): %s\nNew time (from rp): %s", old, new
207207
)
208208

209-
if new <= old and params_used == db_entry[1]:
209+
if new <= old and params_used == old_params_used:
210210
return False
211211
else:
212212
# Report is newer. Delete old entry.
@@ -225,10 +225,10 @@ def load_local_report(self) -> None:
225225
self.cursor.execute(
226226
"SELECT report FROM Report WHERE host=?", (self.host,)
227227
)
228-
db_entry = self.cursor.fetchone()
228+
last_report = self.cursor.fetchone()
229229

230-
if db_entry:
231-
return etree.fromstring(db_entry[0])
230+
if last_report:
231+
return etree.fromstring(last_report[0])
232232
else:
233233
logger.debug("Report from host %s is not in the db", self.host)
234234

@@ -993,96 +993,21 @@ def print_without_pipe(msg):
993993
return msg.replace("|", "¦")
994994

995995

996-
# ISO 8601 date time string parsing
997-
998-
# Copyright (c) 2007 - 2015 Michael Twomey
999-
1000-
# Permission is hereby granted, free of charge, to any person obtaining a
1001-
# copy of this software and associated documentation files (the
1002-
# "Software"), to deal in the Software without restriction, including
1003-
# without limitation the rights to use, copy, modify, merge, publish,
1004-
# distribute, sublicense, and/or sell copies of the Software, and to
1005-
# permit persons to whom the Software is furnished to do so, subject to
1006-
# the following conditions:
1007-
1008-
# The above copyright notice and this permission notice shall be included
1009-
# in all copies or substantial portions of the Software.
1010-
1011-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1012-
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1013-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
1014-
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
1015-
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
1016-
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
1017-
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1018-
1019-
__all__ = ["parse_date", "ParseError", "UTC"]
1020-
1021-
# Adapted from http://delete.me.uk/2005/03/iso8601.html
1022-
ISO8601_REGEX = re.compile(
1023-
r"""
1024-
(?P<year>[0-9]{4})
1025-
(
1026-
(
1027-
(-(?P<monthdash>[0-9]{1,2}))
1028-
|
1029-
(?P<month>[0-9]{2})
1030-
(?!$) # Don't allow YYYYMM
1031-
)
1032-
(
1033-
(
1034-
(-(?P<daydash>[0-9]{1,2}))
1035-
|
1036-
(?P<day>[0-9]{2})
1037-
)
1038-
(
1039-
(
1040-
(?P<separator>[ T])
1041-
(?P<hour>[0-9]{2})
1042-
(:{0,1}(?P<minute>[0-9]{2})){0,1}
1043-
(
1044-
:{0,1}(?P<second>[0-9]{1,2})
1045-
([.,](?P<second_fraction>[0-9]+)){0,1}
1046-
){0,1}
1047-
(?P<timezone>
1048-
Z
1049-
|
1050-
(
1051-
(?P<tz_sign>[-+])
1052-
(?P<tz_hour>[0-9]{2})
1053-
:{0,1}
1054-
(?P<tz_minute>[0-9]{2}){0,1}
1055-
)
1056-
){0,1}
1057-
){0,1}
1058-
)
1059-
){0,1} # YYYY-MM
1060-
){0,1} # YYYY only
1061-
$
1062-
""",
1063-
re.VERBOSE,
1064-
)
1065-
1066-
1067996
class ParseError(Exception):
1068997
"""Raised when there is a problem parsing a date string"""
1069998

1070999

1071-
# Yoinked from python docs
1072-
ZERO = timedelta(0)
1073-
1074-
10751000
class Utc(tzinfo):
10761001
"""UTC Timezone"""
10771002

10781003
def utcoffset(self, dt):
1079-
return ZERO
1004+
return timedelta(0)
10801005

10811006
def tzname(self, dt):
10821007
return "UTC"
10831008

10841009
def dst(self, dt):
1085-
return ZERO
1010+
return timedelta(0)
10861011

10871012
def __repr__(self):
10881013
return "<iso8601.Utc>"
@@ -1098,7 +1023,7 @@ def __init__(self, offset_hours, offset_minutes, name):
10981023
self.__offset_hours = offset_hours # Keep for later __getinitargs__
10991024
# Keep for later __getinitargs__
11001025
self.__offset_minutes = offset_minutes
1101-
self.__offset = timedelta(hours=offset_hours, minutes=offset_minutes)
1026+
self.offset = timedelta(hours=offset_hours, minutes=offset_minutes)
11021027
self.__name = name
11031028

11041029
def __eq__(self, other):
@@ -1114,14 +1039,15 @@ def __eq__(self, other):
11141039
def __getinitargs__(self):
11151040
return (self.__offset_hours, self.__offset_minutes, self.__name)
11161041

1117-
def utcoffset(self, dt):
1118-
return self.__offset
1042+
# this shit is not even used ...
1043+
# def utcoffset(self, dt):
1044+
# return self.offset
11191045

1120-
def tzname(self, dt):
1121-
return self.__name
1046+
# def tzname(self, dt):
1047+
# return self.__name
11221048

11231049
def dst(self, dt):
1124-
return ZERO
1050+
return timedelta(0)
11251051

11261052
def __repr__(self):
11271053
return f"<FixedOffset {self.__name} {self.__offset}>"
@@ -1170,62 +1096,14 @@ def parse_timezone(matches, default_timezone=UTC):
11701096
return FixedOffset(hours, minutes, description)
11711097

11721098

1173-
def parse_date(datestring: str, default_timezone=UTC):
1174-
"""Parses ISO 8601 dates into datetime objects
1175-
1176-
The timezone is parsed from the date string. However it is quite common to
1177-
have dates without a timezone (not strictly correct). In this case the
1178-
default timezone specified in default_timezone is used. This is UTC by
1179-
default.
1180-
1181-
Arguments
1182-
datestring: The date to parse as a string
1183-
default_timezone: A datetime tzinfo instance to use when no timezone
1184-
is specified in the datestring. If this is set to
1185-
None then a naive datetime object is returned.
1186-
Returns:
1187-
A datetime.datetime instance
1188-
Raises:
1189-
ParseError when there is a problem parsing the date or
1190-
constructing the datetime instance.
1191-
1192-
"""
1099+
def parse_date(datestring: str):
1100+
"""will be removed."""
11931101
if not isinstance(datestring, str):
11941102
raise ParseError(f"Expecting a string {datestring}")
1195-
1196-
match = ISO8601_REGEX.match(datestring)
1197-
if not match:
1198-
raise ParseError(f"Unable to parse date string {datestring}")
1199-
1200-
groups = match.groupdict()
1201-
1202-
tz = parse_timezone(groups, default_timezone=default_timezone)
1203-
1204-
groups["second_fraction"] = int(
1205-
Decimal(f"0.{groups['second_fraction'] or 0}") * Decimal("1000000.0")
1206-
)
1207-
12081103
try:
1209-
return datetime(
1210-
year=to_int(groups, "year"),
1211-
month=to_int(
1212-
groups,
1213-
"month",
1214-
default=to_int(groups, "monthdash", required=False, default=1),
1215-
),
1216-
day=to_int(
1217-
groups,
1218-
"day",
1219-
default=to_int(groups, "daydash", required=False, default=1),
1220-
),
1221-
hour=to_int(groups, "hour", default_to_zero=True),
1222-
minute=to_int(groups, "minute", default_to_zero=True),
1223-
second=to_int(groups, "second", default_to_zero=True),
1224-
microsecond=groups["second_fraction"],
1225-
tzinfo=tz,
1226-
)
1227-
except Exception as e:
1228-
raise ParseError(e) from None
1104+
return datetime.fromisoformat(datestring)
1105+
except ValueError:
1106+
raise ParseError(f"Couldn't parse {datestring} to ISO format")
12291107

12301108

12311109
def _parse_args(args: Namespace) -> Namespace:

0 commit comments

Comments
 (0)