add a function to return a formatted version

Replaces the various repeated format strings throughout the code.
This commit is contained in:
Sarah Hoffmann
2022-05-10 23:00:18 +02:00
parent 5ff35d9984
commit 4e1e166c6a
7 changed files with 24 additions and 19 deletions

View File

@@ -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

View File

@@ -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):

View File

@@ -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())

View File

@@ -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:

View File

@@ -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()

View File

@@ -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"""\
<?php
@define('CONST_Debug', $_GET['debug'] ?? false);
@define('CONST_LibDir', '{0}');
@define('CONST_TokenizerDir', '{2}');
@define('CONST_NominatimVersion', '{1[0]}.{1[1]}.{1[2]}-{1[3]}');
@define('CONST_LibDir', '{config.lib_dir.php}');
@define('CONST_TokenizerDir', '{config.project_dir / 'tokenizer'}');
@define('CONST_NominatimVersion', '{version_str()}');
""".format(config.lib_dir.php, NOMINATIM_VERSION,
config.project_dir / 'tokenizer'))
""")
for php_name, conf_name, var_type in PHP_CONST_DEFS:
varout = _quote_php_variable(var_type, config, conf_name)

View File

@@ -34,3 +34,11 @@ POSTGIS_REQUIRED_VERSION = (2, 2)
# cmake/tool-installed.tmpl is used to build the binary 'nominatim'. Inside
# there is a call to set the variable value below.
GIT_COMMIT_HASH = None
# pylint: disable=consider-using-f-string
def version_str():
"""
Return a human-readable string of the version.
"""
return '{}.{}.{}-{}'.format(*NOMINATIM_VERSION)