From e33fc7779aa7ebaa9aab9e9688883949379b5bc4 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Mon, 25 Jan 2021 20:34:43 -0600 Subject: [PATCH] Added time parse for backup files --- requirements.txt | 2 ++ test_data/property_files/example-new.json | 1 + test_data/property_files/example-old.json | 1 + tibi_hardlinks/backup_parser.py | 10 ++++++++++ 4 files changed, 14 insertions(+) diff --git a/requirements.txt b/requirements.txt index e69de29..38dfcb8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,2 @@ +pytz +python-dateutil diff --git a/test_data/property_files/example-new.json b/test_data/property_files/example-new.json index f0c4956..b24e6e6 100644 --- a/test_data/property_files/example-new.json +++ b/test_data/property_files/example-new.json @@ -9,6 +9,7 @@ "app_label": "Luis.Babyphone", "app_version_code": "2000072", "app_version_name": "2.0.72", + "backup_time": 1604049161.0, "generation": "1", "has_dbdata": "0", "has_prefsdata": "0", diff --git a/test_data/property_files/example-old.json b/test_data/property_files/example-old.json index 9e43e78..23fe049 100644 --- a/test_data/property_files/example-old.json +++ b/test_data/property_files/example-old.json @@ -9,6 +9,7 @@ "app_label": "QPython3", "app_version_code": "103", "app_version_name": "1.0.3", + "backup_time": 1509521922.0, "generation": "1", "has_dbdata": "0", "has_prefsdata": "0", diff --git a/tibi_hardlinks/backup_parser.py b/tibi_hardlinks/backup_parser.py index 99b89a2..966fd64 100644 --- a/tibi_hardlinks/backup_parser.py +++ b/tibi_hardlinks/backup_parser.py @@ -1,4 +1,8 @@ import re +from dateutil.parser import parse as datetime_parse, ParserError +import pytz + +tzinfos = {name: pytz.timezone(name) for name in pytz.all_timezones} def parse_property_text(text): @@ -6,6 +10,12 @@ def parse_property_text(text): key_value_regex = re.compile(r"(?P^[^=]+)=(?P.*)") lines = text.split("\n") for line in lines: + if line.startswith("#"): + try: + time = datetime_parse(line.lstrip("#"), tzinfos=tzinfos) + properties["backup_time"] = time.astimezone(pytz.UTC).timestamp() + except ParserError: + pass match = key_value_regex.match(line) if match: key = match.group("key")