Skip to content

Commit

Permalink
Replaced Flake8 with Ruff (#155)
Browse files Browse the repository at this point in the history
* Replaced Flake8 with Ruff

* Ignored the UP015 rule

* Replaced one `%` with an f-string
  • Loading branch information
agronholm authored Oct 11, 2023
1 parent 65af5a3 commit 40893d5
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 60 deletions.
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://www.iana.org/time-zones>`_ for your local time zone or a ``tzinfo``
This Python module returns a the `IANA time zone name
<https://www.iana.org/time-zones>`_ 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``
Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions setup.cfg

This file was deleted.

1 change: 0 additions & 1 deletion tests/test_data/vardbzoneinfo/etc/timezone
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

3 changes: 1 addition & 2 deletions tzlocal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions tzlocal/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import calendar
import datetime
import logging
import os
import time
import datetime
import calendar
import warnings

try:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion tzlocal/win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 43 additions & 34 deletions update_windows_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -27,56 +27,61 @@ 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


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:
Expand All @@ -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()

0 comments on commit 40893d5

Please sign in to comment.