From 40893d5f4c106c20d8f3704a97aa26eb043f1870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Wed, 11 Oct 2023 06:49:59 +0300 Subject: [PATCH] Replaced Flake8 with Ruff (#155) * Replaced Flake8 with Ruff * Ignored the UP015 rule * Replaced one `%` with an f-string --- .pre-commit-config.yaml | 9 +-- README.rst | 10 +-- pyproject.toml | 15 ++++- setup.cfg | 4 -- tests/test_data/vardbzoneinfo/etc/timezone | 1 - tzlocal/__init__.py | 3 +- tzlocal/utils.py | 12 ++-- tzlocal/win32.py | 2 +- update_windows_mappings.py | 77 ++++++++++++---------- 9 files changed, 73 insertions(+), 60 deletions(-) delete mode 100644 setup.cfg diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bdd92ec..2736102 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,14 +5,15 @@ repos: - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.0.292 + hooks: + - id: ruff + args: [--fix, --show-fixes] - repo: https://github.com/psf/black-pre-commit-mirror rev: 23.9.1 hooks: - id: black -- repo: https://github.com/pycqa/flake8 - rev: 6.1.0 - hooks: - - id: flake8 - repo: https://github.com/regebro/pyroma rev: '4.2' hooks: diff --git a/README.rst b/README.rst index 2028da8..11a1f58 100644 --- a/README.rst +++ b/README.rst @@ -26,8 +26,8 @@ updates, but there will be no further functional changes on the 4.x branch. Info ---- -This Python module returns a the `IANA time zone name -`_ for your local time zone or a ``tzinfo`` +This Python module returns a the `IANA time zone name +`_ for your local time zone or a ``tzinfo`` object with the local timezone information, under Unix and Windows. It requires Python 3.8 or later, and will use the ``backports.tzinfo`` @@ -48,11 +48,11 @@ What it's not for ----------------- It's not for converting the curfrent time betwee UTC and your local time. There are -other, simpler ways of doing this. This is of you need to know things like the name -of the time zone, or if you need to be able to convert between your time zone and +other, simpler ways of doing this. This is of you need to know things like the name +of the time zone, or if you need to be able to convert between your time zone and another time zone for times that are in the future or in the past. -For current time conversions to and from UTC, look in the python ``time`` module. +For current time conversions to and from UTC, look in the python ``time`` module. Supported systems diff --git a/pyproject.toml b/pyproject.toml index 2ea40ce..e932e71 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,13 +39,22 @@ devenv = [ "pytest >= 4.3", "pytest-mock >= 3.3", "pytest-cov", - "black", - "flake8", "check_manifest", - "pyroma", "zest.releaser", ] +[tool.ruff] +line-length = 120 +select = [ + "E", "F", "W", # default flake-8 + "I", # isort + "ISC", # flake8-implicit-str-concat + "PGH", # pygrep-hooks + "RUF100", # unused noqa (yesqa) + "UP", # pyupgrade +] +ignore = ["UP015"] + [tool.zest.releaser] create-wheel = true diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 22ea4dd..0000000 --- a/setup.cfg +++ /dev/null @@ -1,4 +0,0 @@ -[flake8] -max-line-length=120 -# black and flake8 differs in opinion here, and I can't change black: -ignore=E203 diff --git a/tests/test_data/vardbzoneinfo/etc/timezone b/tests/test_data/vardbzoneinfo/etc/timezone index 8b13789..e69de29 100644 --- a/tests/test_data/vardbzoneinfo/etc/timezone +++ b/tests/test_data/vardbzoneinfo/etc/timezone @@ -1 +0,0 @@ - diff --git a/tzlocal/__init__.py b/tzlocal/__init__.py index 50abb09..8296a15 100644 --- a/tzlocal/__init__.py +++ b/tzlocal/__init__.py @@ -5,13 +5,12 @@ get_localzone, get_localzone_name, reload_localzone, - ) # pragma: no cover + ) else: from tzlocal.unix import get_localzone, get_localzone_name, reload_localzone from tzlocal.utils import assert_tz_offset - __all__ = [ "get_localzone", "get_localzone_name", diff --git a/tzlocal/utils.py b/tzlocal/utils.py index 4ba82d3..3990535 100644 --- a/tzlocal/utils.py +++ b/tzlocal/utils.py @@ -1,8 +1,8 @@ +import calendar +import datetime import logging import os import time -import datetime -import calendar import warnings try: @@ -35,9 +35,9 @@ def assert_tz_offset(tz, error=True): # No one has timezone offsets less than a minute, so this should be close enough: if abs(tz_offset - system_offset) > 60: msg = ( - "Timezone offset does not match system offset: {} != {}. " + f"Timezone offset does not match system offset: {tz_offset} != {system_offset}. " "Please, check your config files." - ).format(tz_offset, system_offset) + ) if error: raise ValueError(msg) warnings.warn(msg) @@ -107,6 +107,6 @@ def _tz_from_env(tzenv=None): except zoneinfo.ZoneInfoNotFoundError: # Nope, it's something like "PST4DST" etc, we can't handle that. raise zoneinfo.ZoneInfoNotFoundError( - "tzlocal() does not support non-zoneinfo timezones like %s. \n" - "Please use a timezone in the form of Continent/City" % tzenv + f"tzlocal() does not support non-zoneinfo timezones like {tzenv}. \n" + "Please use a timezone in the form of Continent/City" ) from None diff --git a/tzlocal/win32.py b/tzlocal/win32.py index 6998d7c..2fa59fe 100644 --- a/tzlocal/win32.py +++ b/tzlocal/win32.py @@ -11,8 +11,8 @@ except ImportError: from backports import zoneinfo # pragma: no cover -from tzlocal.windows_tz import win_tz from tzlocal import utils +from tzlocal.windows_tz import win_tz _cache_tz = None _cache_tz_name = None diff --git a/update_windows_mappings.py b/update_windows_mappings.py index 0657656..2b89e43 100755 --- a/update_windows_mappings.py +++ b/update_windows_mappings.py @@ -9,15 +9,15 @@ import ftplib import logging +import tarfile from io import BytesIO from pprint import pprint -import tarfile from urllib.parse import urlparse from urllib.request import urlopen from xml.dom import minidom -WIN_ZONES_URL = 'https://raw.githubusercontent.com/unicode-org/cldr/master/common/supplemental/windowsZones.xml' -ZONEINFO_URL = 'ftp://ftp.iana.org/tz/tzdata-latest.tar.gz' +WIN_ZONES_URL = "https://raw.githubusercontent.com/unicode-org/cldr/master/common/supplemental/windowsZones.xml" +ZONEINFO_URL = "ftp://ftp.iana.org/tz/tzdata-latest.tar.gz" logging.basicConfig(level=logging.INFO) log = logging.getLogger("tzlocal") @@ -27,28 +27,28 @@ def update_old_names(): """Fetches the list of old tz names and returns a mapping""" url = urlparse(ZONEINFO_URL) - log.info('Connecting to %s' % url.netloc) + log.info("Connecting to %s" % url.netloc) ftp = ftplib.FTP(url.netloc) ftp.login() gzfile = BytesIO() - log.info('Fetching zoneinfo database') - ftp.retrbinary('RETR ' + url.path, gzfile.write) + log.info("Fetching zoneinfo database") + ftp.retrbinary("RETR " + url.path, gzfile.write) gzfile.seek(0) - log.info('Extracting backwards data') + log.info("Extracting backwards data") archive = tarfile.open(mode="r:gz", fileobj=gzfile) backward = {} - for line in archive.extractfile('backward').readlines(): - if line[0] == '#': + for line in archive.extractfile("backward").readlines(): + if line[0] == "#": continue if len(line.strip()) == 0: continue parts = line.split() - if parts[0] != b'Link': + if parts[0] != b"Link": continue - backward[parts[2].decode('ascii')] = parts[1].decode('ascii') + backward[parts[2].decode("ascii")] = parts[1].decode("ascii") return backward @@ -56,27 +56,32 @@ def update_old_names(): def update_windows_zones(): backward = update_old_names() - log.info('Fetching Windows mapping info from unicode.org') + log.info("Fetching Windows mapping info from unicode.org") source = urlopen(WIN_ZONES_URL).read() dom = minidom.parseString(source) - for element in dom.getElementsByTagName('mapTimezones'): - if element.getAttribute('type') == 'windows': + for element in dom.getElementsByTagName("mapTimezones"): + if element.getAttribute("type") == "windows": break - log.info('Making windows mapping') + log.info("Making windows mapping") win_tz = {} tz_win = {} - for mapping in element.getElementsByTagName('mapZone'): - if mapping.getAttribute('territory') == '001': - win_tz[mapping.getAttribute('other')] = mapping.getAttribute('type').split(' ')[0] - if win_tz[mapping.getAttribute('other')].startswith('Etc'): - print (win_tz[mapping.getAttribute('other')], mapping.getAttribute('type').split(' ')[0]) - - for tz_name in mapping.getAttribute('type').split(' '): - tz_win[tz_name] = mapping.getAttribute('other') - - log.info('Adding backwards and forwards data') + for mapping in element.getElementsByTagName("mapZone"): + if mapping.getAttribute("territory") == "001": + win_tz[mapping.getAttribute("other")] = mapping.getAttribute("type").split( + " " + )[0] + if win_tz[mapping.getAttribute("other")].startswith("Etc"): + print( + win_tz[mapping.getAttribute("other")], + mapping.getAttribute("type").split(" ")[0], + ) + + for tz_name in mapping.getAttribute("type").split(" "): + tz_win[tz_name] = mapping.getAttribute("other") + + log.info("Adding backwards and forwards data") # Map in the backwards (or forwards) compatible zone names for backward_compat_name, standard_name in backward.items(): if backward_compat_name not in tz_win: @@ -89,18 +94,22 @@ def update_windows_zones(): tz_win[standard_name] = win_zone # Etc/UTC is a common but non-standard alias for Etc/GMT: - tz_win['Etc/UTC'] = 'UTC' - - log.info('Writing mapping') - with open('tzlocal/windows_tz.py', "wt") as out: - out.write("# This file is autogenerated by the update_windows_mapping.py script\n" - "# Do not edit.\nwin_tz = ") + tz_win["Etc/UTC"] = "UTC" + + log.info("Writing mapping") + with open("tzlocal/windows_tz.py", "w") as out: + out.write( + "# This file is autogenerated by the update_windows_mapping.py script\n" + "# Do not edit.\nwin_tz = " + ) pprint(win_tz, out) - out.write("\n# Old name for the win_tz variable:\ntz_names = win_tz\n\ntz_win = ") + out.write( + "\n# Old name for the win_tz variable:\ntz_names = win_tz\n\ntz_win = " + ) pprint(tz_win, out) - log.info('Done') + log.info("Done") -if __name__ == '__main__': +if __name__ == "__main__": update_windows_zones()