22
22
from enum import Enum
23
23
import logging
24
24
import os
25
- import re
26
25
import signal
27
26
import sqlite3
28
27
import sys
29
28
import tempfile
30
29
from argparse import ArgumentParser , Namespace , RawTextHelpFormatter
31
30
from datetime import datetime , timedelta , tzinfo
32
- from decimal import Decimal
33
31
from pathlib import Path
34
32
from typing import Any , Tuple
35
33
from xml .etree import ElementTree
@@ -192,21 +190,23 @@ def is_old_report(self, last_scan_end, params_used) -> bool:
192
190
"SELECT scan_end, params_used FROM Report WHERE host=?" ,
193
191
(self .host ,),
194
192
)
195
- db_entry = self .cursor .fetchone ()
193
+ ( scan_end , old_params_used ) = self .cursor .fetchone ()
196
194
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
+ )
198
198
199
- if not db_entry :
199
+ if not scan_end :
200
200
return True
201
201
else :
202
- old = parse_date (db_entry [ 0 ] )
202
+ old = parse_date (scan_end )
203
203
new = parse_date (last_scan_end )
204
204
205
205
logger .debug (
206
206
"Old time (from db): %s\n New time (from rp): %s" , old , new
207
207
)
208
208
209
- if new <= old and params_used == db_entry [ 1 ] :
209
+ if new <= old and params_used == old_params_used :
210
210
return False
211
211
else :
212
212
# Report is newer. Delete old entry.
@@ -225,10 +225,10 @@ def load_local_report(self) -> None:
225
225
self .cursor .execute (
226
226
"SELECT report FROM Report WHERE host=?" , (self .host ,)
227
227
)
228
- db_entry = self .cursor .fetchone ()
228
+ last_report = self .cursor .fetchone ()
229
229
230
- if db_entry :
231
- return etree .fromstring (db_entry [0 ])
230
+ if last_report :
231
+ return etree .fromstring (last_report [0 ])
232
232
else :
233
233
logger .debug ("Report from host %s is not in the db" , self .host )
234
234
@@ -993,96 +993,21 @@ def print_without_pipe(msg):
993
993
return msg .replace ("|" , "¦" )
994
994
995
995
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
-
1067
996
class ParseError (Exception ):
1068
997
"""Raised when there is a problem parsing a date string"""
1069
998
1070
999
1071
- # Yoinked from python docs
1072
- ZERO = timedelta (0 )
1073
-
1074
-
1075
1000
class Utc (tzinfo ):
1076
1001
"""UTC Timezone"""
1077
1002
1078
1003
def utcoffset (self , dt ):
1079
- return ZERO
1004
+ return timedelta ( 0 )
1080
1005
1081
1006
def tzname (self , dt ):
1082
1007
return "UTC"
1083
1008
1084
1009
def dst (self , dt ):
1085
- return ZERO
1010
+ return timedelta ( 0 )
1086
1011
1087
1012
def __repr__ (self ):
1088
1013
return "<iso8601.Utc>"
@@ -1098,7 +1023,7 @@ def __init__(self, offset_hours, offset_minutes, name):
1098
1023
self .__offset_hours = offset_hours # Keep for later __getinitargs__
1099
1024
# Keep for later __getinitargs__
1100
1025
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 )
1102
1027
self .__name = name
1103
1028
1104
1029
def __eq__ (self , other ):
@@ -1114,14 +1039,15 @@ def __eq__(self, other):
1114
1039
def __getinitargs__ (self ):
1115
1040
return (self .__offset_hours , self .__offset_minutes , self .__name )
1116
1041
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
1119
1045
1120
- def tzname (self , dt ):
1121
- return self .__name
1046
+ # def tzname(self, dt):
1047
+ # return self.__name
1122
1048
1123
1049
def dst (self , dt ):
1124
- return ZERO
1050
+ return timedelta ( 0 )
1125
1051
1126
1052
def __repr__ (self ):
1127
1053
return f"<FixedOffset { self .__name } { self .__offset } >"
@@ -1170,62 +1096,14 @@ def parse_timezone(matches, default_timezone=UTC):
1170
1096
return FixedOffset (hours , minutes , description )
1171
1097
1172
1098
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."""
1193
1101
if not isinstance (datestring , str ):
1194
1102
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
-
1208
1103
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" )
1229
1107
1230
1108
1231
1109
def _parse_args (args : Namespace ) -> Namespace :
0 commit comments