aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2023-01-03 21:37:02 +0100
committerDaniel Schadt <kingdread@gmx.de>2023-01-03 21:37:02 +0100
commit97e3b4c35049896f1692665e87d477f53ee9b9be (patch)
tree1c9d4b75b4be8521d2d0514b17f9185e5dd2bcc2
parentf4fe38385be425494e6b6dfffd77607f2d61e5b0 (diff)
downloadfietsboek-97e3b4c35049896f1692665e87d477f53ee9b9be.tar.gz
fietsboek-97e3b4c35049896f1692665e87d477f53ee9b9be.tar.bz2
fietsboek-97e3b4c35049896f1692665e87d477f53ee9b9be.zip
add update script that embeds metadata
-rw-r--r--fietsboek/updater/__init__.py5
-rw-r--r--fietsboek/updater/scripts/upd_20230103_lu8w3rwlz4ddcpms.py77
2 files changed, 80 insertions, 2 deletions
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