Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced Flake8 with Ruff #155

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 11 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ 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
]

[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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK; I found a syntax for this both black and ruff like, I'll add that back later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really want to ignore coverage here rather than just collect and combine from runs on multiple operating systems?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can try that, IIRC coverage didn't handle that correctly. Then again I don't have 100% coverage anyway so I guess it doesn't really matter.

)
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
2 changes: 1 addition & 1 deletion tzlocal/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _get_localzone_name(_root="/"):
for filename in ("etc/sysconfig/clock", "etc/conf.d/clock"):
tzpath = os.path.join(_root, filename)
try:
with open(tzpath, "rt") as tzfile:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we skip that UP015 rule? I like having the parameters there even if they are default.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

with open(tzpath) as tzfile:
data = tzfile.readlines()
log.debug(f"{tzpath} found, contents:\n {data}")

Expand Down
8 changes: 4 additions & 4 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
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()