From 4e1e166c6a3f4b83c114eb22babca0a4eee3f96e Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Tue, 10 May 2022 23:00:18 +0200 Subject: [PATCH] add a function to return a formatted version Replaces the various repeated format strings throughout the code. --- .pylintrc | 2 +- nominatim/cli.py | 4 ++-- nominatim/clicmd/setup.py | 5 ++--- nominatim/tools/exec_utils.py | 4 ++-- nominatim/tools/migration.py | 7 +++---- nominatim/tools/refresh.py | 13 ++++++------- nominatim/version.py | 8 ++++++++ 7 files changed, 24 insertions(+), 19 deletions(-) diff --git a/.pylintrc b/.pylintrc index d5c4514f..89135138 100644 --- a/.pylintrc +++ b/.pylintrc @@ -11,6 +11,6 @@ ignored-modules=icu,datrie # 'with' statements. ignored-classes=NominatimArgs,closing # 'too-many-ancestors' is triggered already by deriving from UserDict -disable=too-few-public-methods,duplicate-code,too-many-ancestors +disable=too-few-public-methods,duplicate-code,too-many-ancestors,bad-option-value good-names=i,x,y,fd,db diff --git a/nominatim/cli.py b/nominatim/cli.py index 01eb6119..f911023b 100644 --- a/nominatim/cli.py +++ b/nominatim/cli.py @@ -60,9 +60,9 @@ class CommandlineParser: def nominatim_version_text(): """ Program name and version number as string """ - text = 'Nominatim version %s.%s.%s.%s' % version.NOMINATIM_VERSION + text = f'Nominatim version {version.version_str()}' if version.GIT_COMMIT_HASH is not None: - text += ' (%s)' % version.GIT_COMMIT_HASH + text += f' ({version.GIT_COMMIT_HASH})' return text def add_subcommand(self, name, cmd): diff --git a/nominatim/clicmd/setup.py b/nominatim/clicmd/setup.py index e822cbe5..7968fbce 100644 --- a/nominatim/clicmd/setup.py +++ b/nominatim/clicmd/setup.py @@ -14,7 +14,7 @@ import psutil from nominatim.db.connection import connect from nominatim.db import status, properties -from nominatim.version import NOMINATIM_VERSION +from nominatim.version import version_str # Do not repeat documentation of subcommand classes. # pylint: disable=C0111 @@ -213,5 +213,4 @@ class SetupAll: except Exception as exc: # pylint: disable=broad-except LOG.error('Cannot determine date of database: %s', exc) - properties.set_property(conn, 'database_version', - '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(NOMINATIM_VERSION)) + properties.set_property(conn, 'database_version', version_str()) diff --git a/nominatim/tools/exec_utils.py b/nominatim/tools/exec_utils.py index b06b8533..db2a4c2e 100644 --- a/nominatim/tools/exec_utils.py +++ b/nominatim/tools/exec_utils.py @@ -12,7 +12,7 @@ import subprocess import urllib.request as urlrequest from urllib.parse import urlencode -from nominatim.version import NOMINATIM_VERSION +from nominatim.version import version_str from nominatim.db.connection import get_pg_env LOG = logging.getLogger() @@ -150,7 +150,7 @@ def run_osm2pgsql(options): def get_url(url): """ Get the contents from the given URL and return it as a UTF-8 string. """ - headers = {"User-Agent": "Nominatim/{0[0]}.{0[1]}.{0[2]}-{0[3]}".format(NOMINATIM_VERSION)} + headers = {"User-Agent": f"Nominatim/{version_str()}"} try: with urlrequest.urlopen(urlrequest.Request(url, headers=headers)) as response: diff --git a/nominatim/tools/migration.py b/nominatim/tools/migration.py index 76726e8c..290016ad 100644 --- a/nominatim/tools/migration.py +++ b/nominatim/tools/migration.py @@ -11,7 +11,7 @@ import logging from nominatim.db import properties from nominatim.db.connection import connect -from nominatim.version import NOMINATIM_VERSION +from nominatim.version import NOMINATIM_VERSION, version_str from nominatim.tools import refresh from nominatim.tokenizer import factory as tokenizer_factory from nominatim.errors import UsageError @@ -47,7 +47,7 @@ def migrate(config, paths): for version, func in _MIGRATION_FUNCTIONS: if db_version <= version: LOG.warning("Runnning: %s (%s)", func.__doc__.split('\n', 1)[0], - '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(version)) + '{}.{}.{}-{}'.format(*version)) kwargs = dict(conn=conn, config=config, paths=paths) func(**kwargs) conn.commit() @@ -59,8 +59,7 @@ def migrate(config, paths): tokenizer = tokenizer_factory.get_tokenizer_for_db(config) tokenizer.update_sql_functions(config) - properties.set_property(conn, 'database_version', - '{0[0]}.{0[1]}.{0[2]}-{0[3]}'.format(NOMINATIM_VERSION)) + properties.set_property(conn, 'database_version', version_str()) conn.commit() diff --git a/nominatim/tools/refresh.py b/nominatim/tools/refresh.py index aacc622b..b9a926f2 100644 --- a/nominatim/tools/refresh.py +++ b/nominatim/tools/refresh.py @@ -15,7 +15,7 @@ from psycopg2 import sql as pysql from nominatim.db.utils import execute_file from nominatim.db.sql_preprocessor import SQLPreprocessor -from nominatim.version import NOMINATIM_VERSION +from nominatim.version import version_str LOG = logging.getLogger() @@ -186,16 +186,15 @@ def setup_website(basedir, config, conn): LOG.info('Creating website directory.') basedir.mkdir() - template = dedent("""\ + template = dedent(f"""\