16
16
dirname = os .path .dirname (__file__ )
17
17
18
18
19
+ class classproperty : # pylint: disable=invalid-name,too-few-public-methods
20
+ """Simple class-level equivalent of an @property."""
21
+
22
+ def __init__ (self , method ):
23
+ """Wrap a method."""
24
+ self .getter = method
25
+
26
+ def __get__ (self , _ , cls ):
27
+ """Call the wrapped method."""
28
+ return self .getter (cls )
29
+
30
+
19
31
class Geolocator :
20
32
"""Class to obtain Geo Location coordinates."""
21
33
22
34
# Keeping caching of local DB and timezone in the class
23
- db_location : Dict [Union [Tuple [str , str ], str ], Tuple [float , float ]] = {}
24
- timezone = None
35
+ _db_location : Dict [Union [Tuple [str , str ], str ], Tuple [float , float ]] = {}
36
+ _timezone = None
25
37
26
- def __init__ (self ):
27
- """Initialize instance."""
28
- self .load_db_location ()
29
- self .load_timezone ()
30
-
31
- @classmethod
32
- def load_timezone (cls ):
38
+ @classproperty
39
+ def timezone (cls ): # pylint: disable=no-self-argument
33
40
"""Load the timezone resolver."""
34
- if cls .timezone is None :
35
- cls .timezone = tzwhere .tzwhere ()
41
+ if cls ._timezone is None :
42
+ cls ._timezone = tzwhere .tzwhere ()
36
43
logger .info ("Loaded local timezone resolver." )
37
-
38
- @classmethod
39
- def load_db_location (cls ):
40
- """Load the localtions DB from CSV into a Dict."""
41
- with open (os .path .join (dirname , "data" , "worldcities.csv" )) as csvfile :
42
- reader = csv .DictReader (csvfile )
43
- for row in reader :
44
- # Index by city and country
45
- cls .db_location [(row ["city_ascii" ], row ["country" ])] = (float (row ["lat" ]), float (row ["lng" ]))
46
- # Index by city (first entry wins if duplicated names)
47
- if row ["city_ascii" ] not in cls .db_location :
48
- cls .db_location [row ["city_ascii" ]] = (float (row ["lat" ]), float (row ["lng" ]))
44
+ return cls ._timezone
45
+
46
+ @classproperty
47
+ def db_location (cls ): # pylint: disable=no-self-argument
48
+ """Load the locations DB from CSV into a Dict."""
49
+ if not cls ._db_location :
50
+ with open (os .path .join (dirname , "data" , "worldcities.csv" )) as csvfile :
51
+ reader = csv .DictReader (csvfile )
52
+ for row in reader :
53
+ # Index by city and country
54
+ cls ._db_location [(row ["city_ascii" ], row ["country" ])] = (float (row ["lat" ]), float (row ["lng" ]))
55
+ # Index by city (first entry wins if duplicated names)
56
+ if row ["city_ascii" ] not in cls ._db_location :
57
+ cls ._db_location [row ["city_ascii" ]] = (float (row ["lat" ]), float (row ["lng" ]))
58
+ return cls ._db_location
49
59
50
60
def get_location (self , city : str ) -> Tuple [float , float ]:
51
61
"""Get location."""
@@ -64,7 +74,9 @@ def get_location_from_local_file(self, city: str) -> Tuple[float, float]:
64
74
city_name = city .split (", " )[0 ]
65
75
country = city .split (", " )[- 1 ]
66
76
67
- lat , lng = self .db_location .get ((city_name , country ), self .db_location .get (city_name , (None , None )))
77
+ lat , lng = self .db_location .get ( # pylint: disable=no-member
78
+ (city_name , country ), self .db_location .get (city_name , (None , None )) # pylint: disable=no-member
79
+ )
68
80
if lat and lng :
69
81
logger .debug ("Resolved %s to lat %s, lon %sfrom local locations DB." , city , lat , lng )
70
82
return (lat , lng )
@@ -92,12 +104,12 @@ def city_timezone(self, city: str) -> str:
92
104
if self .timezone is not None :
93
105
try :
94
106
latitude , longitude = self .get_location (city )
95
- timezone = self .timezone .tzNameAt (latitude , longitude )
107
+ timezone = self .timezone .tzNameAt (latitude , longitude ) # pylint: disable=no-member
96
108
if not timezone :
97
109
# In some cases, given a latitued and longitued, the tzwhere library returns
98
110
# an empty timezone, so we try with the coordinates from the API as an alternative
99
111
latitude , longitude = self .get_location_from_api (city )
100
- timezone = self .timezone .tzNameAt (latitude , longitude )
112
+ timezone = self .timezone .tzNameAt (latitude , longitude ) # pylint: disable=no-member
101
113
102
114
if timezone :
103
115
logger .debug ("Matched city %s to timezone %s" , city , timezone )
0 commit comments