Skip to content

Commit c4becbb

Browse files
authored
Remove support for GeoIP (#54)
* Remove support for GeoIP This is a breaking, non-compatible change. * Fix CI * Update dependencies
1 parent 35e0131 commit c4becbb

File tree

9 files changed

+125
-794
lines changed

9 files changed

+125
-794
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ jobs:
2323
- name: Install dependencies
2424
run: |
2525
python -m pip install poetry
26-
poetry install -E geoip
27-
wget -q https://github.com/maxmind/MaxMind-DB/raw/main/test-data/GeoIP2-City-Test.mmdb
26+
poetry install
2827
2928
- name: Test with tox
3029
run: poetry run tox

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Removed
9+
- Drop support for GeoIP and `guess_timezone_by_ip()`.
810

911
## [2.2.0] - 2023-03-06
1012
### Added

Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ upload: tag build
2424
poetry publish
2525

2626

27-
GeoIP2-City-Test.mmdb:
28-
rm -f GeoIP2-City-Test.mmdb
29-
wget https://github.com/maxmind/MaxMind-DB/raw/main/test-data/GeoIP2-City-Test.mmdb
30-
31-
32-
test: GeoIP2-City-Test.mmdb
27+
test:
3328
poetry run pytest
3429

3530

poetry.lock

Lines changed: 118 additions & 649 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,14 @@ readme = "README.md"
2121

2222
[tool.poetry.dependencies]
2323
python = "^3.9"
24-
geoip2 = {version = "^4.5.0", optional = true}
2524

2625
[tool.poetry.dev-dependencies]
2726
pytest = "^7.1.2"
2827
tox = "^3.25.0"
2928
tox-gh-actions = "^2.9.1"
3029
mypy = "^0.960"
31-
types-geoip2 = "^3.0.0"
3230
ruff = "^0.2.1"
3331

34-
[tool.poetry.extras]
35-
geoip = ["geoip2"]
36-
3732
[build-system]
3833
requires = ["poetry-core>=1.0.0"]
3934
build-backend = "poetry.core.masonry.api"

timezones/test_timezones.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
from . import _defs, tz_rendering, tz_utils, zones
66

7-
GEOIP_DATA_LOCATION = os.path.abspath(
8-
os.path.join(__file__, "..", "..", "GeoIP2-City-Test.mmdb")
9-
)
10-
117

128
def assert_is_lower(offset_a, offset_b):
139
# Force a real sort to happen: to avoid optimizations from kicking in, make
@@ -44,44 +40,6 @@ def test_get_timezone():
4440
assert tz_utils.is_valid_timezone("Europe/Moscow1") is False
4541

4642

47-
@pytest.fixture
48-
def geoip_db():
49-
try:
50-
import geoip2 # noqa: F401
51-
except ImportError:
52-
pytest.skip("geoip2 not installed")
53-
if not os.path.exists(GEOIP_DATA_LOCATION):
54-
pytest.skip("No GeoIP2 database")
55-
56-
tz_utils.GEOIP_DATA_LOCATION = GEOIP_DATA_LOCATION
57-
yield
58-
tz_utils.GEOIP_DATA_LOCATION = None
59-
60-
61-
def test_guess_timezone_geoip(geoip_db):
62-
assert tz_utils.guess_timezone_by_ip("000.000.000.000", only_name=True) is None
63-
assert tz_utils.guess_timezone_by_ip("127.0.0.1", only_name=True) is None
64-
65-
# This uses the MaxMind test DBs; source:
66-
# https://github.com/maxmind/MaxMind-DB/blob/main/source-data/GeoIP2-City-Test.json
67-
assert (
68-
tz_utils.guess_timezone_by_ip("149.101.100.0", only_name=True)
69-
== "America/Chicago"
70-
)
71-
assert tz_utils.guess_timezone_by_ip("2001:230::1", only_name=True) == "Asia/Seoul"
72-
73-
# Addresses not in the test DB
74-
assert tz_utils.guess_timezone_by_ip("1.2.3.4", only_name=True) is None
75-
assert (
76-
tz_utils.guess_timezone_by_ip("2001:2002:2003:2004::0", only_name=True) is None
77-
)
78-
79-
80-
def test_guess_timezone_no_geoip():
81-
assert tz_utils.guess_timezone_by_ip("149.101.100.0", only_name=True) is None
82-
assert tz_utils.guess_timezone_by_ip("2001:230::1", only_name=True) is None
83-
84-
8543
def test_get_timezones():
8644
assert len(list(zones.get_timezones(only_us=True))) == 8
8745

timezones/tz_rendering.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44
55
HTML helper to render timezones. The output will be a SELECT element.
66
7-
Will auto guess the user's timezone based on IP. Will auto-select the current
8-
selected timezone.
7+
Will auto-select the current selected timezone.
98
109
Example usage (returns HTML based on current properties)::
1110
1211
html_timezones = tz_rendering.html_render_timezones(
1312
'timezone',
1413
current_selected,
15-
get_current_ip(),
1614
first_entry=_('Select your timezone'),
1715
)
1816
1917
:copyright: 2012 by Amir Salihefendic ( http://amix.dk/ )
2018
:license: MIT
2119
"""
20+
2221
from __future__ import annotations
2322

2423
import json
@@ -30,7 +29,6 @@
3029
def html_render_timezones(
3130
select_name: str,
3231
current_selected: str | None = None,
33-
user_ip: str | None = None,
3432
first_entry: str = "Select your timezone",
3533
force_current_selected: bool = False,
3634
select_id: Any = None,
@@ -45,9 +43,6 @@ def html_render_timezones(
4543
Is the name of the current timezone, e.g. "Europe/Copenhagen"
4644
is used to auto select the current timezone option in the select element
4745
48-
`user_ip` (optional):
49-
User's ip, used to auto guess the timezone
50-
5146
`first_entry` (optional):
5247
What should the first option be? If `None` it won't be shown at all
5348
@@ -89,18 +84,6 @@ def render_option_disabled():
8984
result.append(render_option(timezone[1], timezone[2], True))
9085
result.append(render_option_disabled())
9186

92-
# Guess user's timezone by user_ip
93-
if user_ip:
94-
gussed_timezone = tz_utils.guess_timezone_by_ip(user_ip)
95-
96-
if not gussed_timezone and default_timezone:
97-
gussed_timezone = format_tz(default_timezone)
98-
99-
if gussed_timezone:
100-
is_set = current_selected == gussed_timezone[1] or current_selected is None
101-
result.append(render_option(gussed_timezone[1], gussed_timezone[2], is_set))
102-
result.append(render_option_disabled())
103-
10487
for tz in zones.get_timezones(only_us=True):
10588
result.append(render_option(tz[1], tz[2], current_selected == tz[1]))
10689

timezones/tz_utils.py

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
print tz_utils.get_timezone('GMT +10:00')
1111
1212
13-
Example usage (guess timezone by IP, required geoip2!)::
14-
15-
tz_utils.GEOIP_DATA_LOCATION = '/usr/local/geo_ip/GeoIP2-City.mmdb'
16-
assert tz_utils.guess_timezone_by_ip('201.246.115.62') == 'Chile/Continental'
17-
18-
1913
Example usage (format timezone by name)::
2014
2115
print tz_utils.format_tz_by_name('Europe/Copenhagen')
@@ -33,6 +27,7 @@
3327
:copyright: 2012 by Amir Salihefendic ( http://amix.dk/ )
3428
:license: MIT
3529
"""
30+
3631
from __future__ import annotations
3732

3833
from datetime import datetime, timedelta, tzinfo
@@ -41,58 +36,13 @@
4136

4237
from . import _defs
4338

44-
try:
45-
import geoip2.database as geoip2_db
46-
47-
HAS_GEOIP2 = True
48-
except ImportError:
49-
HAS_GEOIP2 = False
50-
5139
# --- Exports ----------------------------------------------
5240
__all__ = [
5341
"get_timezone",
5442
"is_valid_timezone",
55-
"GEOIP_DATA_LOCATION",
56-
"guess_timezone_by_ip",
5743
"format_tz_by_name",
5844
]
5945

60-
# --- Specifies the location of GeoIP GeoLiteCity.dat database ---
61-
GEOIP_DATA_LOCATION = None
62-
63-
64-
# --- Functions ----------------------------------------------
65-
def guess_timezone_by_ip(ip: str, only_name: bool = False):
66-
"""Given an `ip` with guess timezone using geoip2.
67-
Returns a tuple of (tz_offets, tz_name, tz_formatted).
68-
`None` is returned if it can't guess a timezone.
69-
70-
For this to work you need to set tz_utils.GEOIP_DATA_LOCATION
71-
You can get this database from http://www.maxmind.com/app/geolitecity
72-
73-
Example usage::
74-
75-
from timezones import tz_utils
76-
77-
tz_utils.GEOIP_DATA_LOCATION = "/usr/local/geo_ip/GeoLiteCity.dat"
78-
assert tz_utils.guess_timezone_by_ip("201.246.115.62") == "Chile/Continental"
79-
80-
"""
81-
geo_lib = _get_geoip_lib()
82-
if geo_lib:
83-
try:
84-
record = geo_lib.city(ip)
85-
if record:
86-
location = record.location
87-
if location and location.time_zone:
88-
if only_name:
89-
return location.time_zone
90-
else:
91-
return format_tz_by_name(location.time_zone)
92-
except Exception:
93-
record = None
94-
return None
95-
9646

9747
def get_timezone(tzname: str) -> tzinfo | None:
9848
"""
@@ -154,23 +104,6 @@ def get_last_datetime_without_dst(tz: tzinfo):
154104

155105

156106
# --- Private ----------------------------------------------
157-
GEO_IP = None
158-
159-
160-
def _get_geoip_lib():
161-
global GEO_IP
162-
163-
if not HAS_GEOIP2 or not GEOIP_DATA_LOCATION:
164-
return None
165-
166-
try:
167-
GEO_IP = geoip2_db.Reader(GEOIP_DATA_LOCATION)
168-
except Exception:
169-
return None
170-
171-
return GEO_IP
172-
173-
174107
_zero = timedelta(0)
175108

176109

tox.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ python =
1010
[testenv]
1111
deps =
1212
pytest
13-
extras =
14-
geoip
1513
commands =
1614
pytest {posargs}
1715

1816
[testenv:mypy]
1917
deps =
2018
mypy
21-
types-geoip2
2219
commands =
2320
mypy timezones {posargs:--ignore-missing-imports}

0 commit comments

Comments
 (0)