From 97e3b4c35049896f1692665e87d477f53ee9b9be Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 3 Jan 2023 21:37:02 +0100 Subject: add update script that embeds metadata --- fietsboek/updater/__init__.py | 5 +- .../scripts/upd_20230103_lu8w3rwlz4ddcpms.py | 77 ++++++++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 fietsboek/updater/scripts/upd_20230103_lu8w3rwlz4ddcpms.py diff --git a/fietsboek/updater/__init__.py b/fietsboek/updater/__init__.py index d1532b4..8eca10d 100644 --- a/fietsboek/updater/__init__.py +++ b/fietsboek/updater/__init__.py @@ -247,14 +247,15 @@ class Updater: loader = jinja2.DictLoader({"revision.py": TEMPLATE}) env = jinja2.Environment(loader=loader, autoescape=False) template = env.get_template("revision.py") + date = datetime.datetime.now() revision = template.render( update_id=revision_id, previous=current_versions, alembic_revision=current_alembic, - date=datetime.datetime.now(), + date=date, ) - filename = f"upd_{revision_id}.py" + filename = f"upd_{date:%Y%m%d}_{revision_id}.py" filepath = Path(__file__).parent / "scripts" / filename LOGGER.info("Writing new revision (%s) to %r", revision_id, filepath) with open(filepath, "x", encoding="utf-8") as fobj: diff --git a/fietsboek/updater/scripts/upd_20230103_lu8w3rwlz4ddcpms.py b/fietsboek/updater/scripts/upd_20230103_lu8w3rwlz4ddcpms.py new file mode 100644 index 0000000..1630fbb --- /dev/null +++ b/fietsboek/updater/scripts/upd_20230103_lu8w3rwlz4ddcpms.py @@ -0,0 +1,77 @@ +"""Revision upgrade script lu8w3rwlz4ddcpms + +Update to embed the GPX metadata (title, description, author) into the GPX +files. + +Note that the down-revision is a no-nop - while we could use the backed-up +files to restore the metadata, it seems a bit unnecessary to go through the +effort of parsing them and extracting the old metadata. + +Date created: 2023-01-03 21:01:04.770362 +""" +import datetime +from pathlib import Path + +import brotli +import gpxpy +from sqlalchemy import create_engine + +from fietsboek.updater.script import UpdateScript + +update_id = 'lu8w3rwlz4ddcpms' +previous = [ + '30ppwg8zi4ujb46f', +] +alembic_revision = 'c939800af428' + + +class Up(UpdateScript): + def pre_alembic(self, config): + engine = create_engine(config["sqlalchemy.url"]) + connection = engine.connect() + data_dir = Path(config["fietsboek.data_dir"]) + + sql = ( + "SELECT tracks.id, tracks.title, tracks.description, tracks.date_raw, " + "tracks.date_tz, users.name " + "FROM tracks, users " + "WHERE tracks.owner_id = users.id;" + ) + for row in connection.execute(sql): + track_id, title, description, date_raw, date_tz, author_name = row + if isinstance(date_raw, str): + date_raw = datetime.datetime.strptime(date_raw, "%Y-%m-%d %H:%M:%S.%f") + if date_tz is None: + timezone = datetime.timezone.utc + else: + timezone = datetime.timezone(datetime.timedelta(minutes=date_tz)) + date = date_raw.replace(tzinfo=timezone) + + self.tell(f"Embedding metadata for track {track_id}") + track_dir = data_dir / "tracks" / str(track_id) + gpx_path = track_dir / "track.gpx.br" + + raw_gpx = brotli.decompress(gpx_path.read_bytes()) + gpx = gpxpy.parse(raw_gpx) + + for track in gpx.tracks: + track.name = None + track.description = None + + gpx.author_name = author_name + gpx.name = title + gpx.description = description + gpx.time = date + + gpx_path.write_bytes(brotli.compress(gpx.to_xml().encode("utf-8"), quality=4)) + + def post_alembic(self, config): + pass + + +class Down(UpdateScript): + def pre_alembic(self, config): + pass + + def post_alembic(self, config): + pass -- cgit v1.2.3