Commit f551dfb5 by William Tisäter

Rename region_name to region_code

parent e957f76c
...@@ -57,21 +57,23 @@ Create your GeoIP instance with appropriate access flag. `STANDARD` reads data f ...@@ -57,21 +57,23 @@ Create your GeoIP instance with appropriate access flag. `STANDARD` reads data f
>>> gi = pygeoip.GeoIP('/path/to/GeoIPRegion.dat') >>> gi = pygeoip.GeoIP('/path/to/GeoIPRegion.dat')
>>> gi.region_by_name('apple.com') >>> gi.region_by_name('apple.com')
{'region_name': 'CA', 'country_code': 'US'} {'region_code': 'CA', 'country_code': 'US'}
### City Lookup ### ### City Lookup ###
>>> gi = pygeoip.GeoIP('/path/to/GeoIPCity.dat') >>> gi = pygeoip.GeoIP('/path/to/GeoIPCity.dat')
>>> gi.record_by_addr('64.233.161.99') >>> gi.record_by_addr('64.233.161.99')
{ {
'city': 'Mountain View', 'city': u'Mountain View',
'region_name': 'CA', 'region_code': u'CA',
'area_code': 650, 'area_code': 650,
'longitude': -122.0574, 'time_zone': 'America/Los_Angeles',
'country_code3': 'USA',
'latitude': 37.419199999999989,
'postal_code': '94043',
'dma_code': 807, 'dma_code': 807,
'metro_code': 'San Francisco, CA',
'country_code3': 'USA',
'latitude': 37.41919999999999,
'postal_code': u'94043',
'longitude': -122.0574,
'country_code': 'US', 'country_code': 'US',
'country_name': 'United States', 'country_name': 'United States',
'continent': 'NA' 'continent': 'NA'
......
...@@ -288,19 +288,18 @@ class GeoIP(object): ...@@ -288,19 +288,18 @@ class GeoIP(object):
def _get_region(self, ipnum): def _get_region(self, ipnum):
""" """
Seek and return the region info (dict containing country_code Seek and return the region information.
and region_name).
@param ipnum: Converted IP address @param ipnum: Converted IP address
@type ipnum: int @type ipnum: int
@return: dict containing country_code and region_name @return: dict containing country_code and region_code
@rtype: dict @rtype: dict
""" """
region = '' region_code = None
country_code = '' country_code = None
seek_country = self._seek_country(ipnum) seek_country = self._seek_country(ipnum)
def get_region_name(offset): def get_region_code(offset):
region1 = chr(offset // 26 + 65) region1 = chr(offset // 26 + 65)
region2 = chr(offset % 26 + 65) region2 = chr(offset % 26 + 65)
return ''.join([region1, region2]) return ''.join([region1, region2])
...@@ -309,7 +308,7 @@ class GeoIP(object): ...@@ -309,7 +308,7 @@ class GeoIP(object):
seek_region = seek_country - const.STATE_BEGIN_REV0 seek_region = seek_country - const.STATE_BEGIN_REV0
if seek_region >= 1000: if seek_region >= 1000:
country_code = 'US' country_code = 'US'
region = get_region_name(seek_region - 1000) region_code = get_region_code(seek_region - 1000)
else: else:
country_code = const.COUNTRY_CODES[seek_region] country_code = const.COUNTRY_CODES[seek_region]
elif self._databaseType == const.REGION_EDITION_REV1: elif self._databaseType == const.REGION_EDITION_REV1:
...@@ -318,20 +317,20 @@ class GeoIP(object): ...@@ -318,20 +317,20 @@ class GeoIP(object):
pass pass
elif seek_region < const.CANADA_OFFSET: elif seek_region < const.CANADA_OFFSET:
country_code = 'US' country_code = 'US'
region = get_region_name(seek_region - const.US_OFFSET) region_code = get_region_code(seek_region - const.US_OFFSET)
elif seek_region < const.WORLD_OFFSET: elif seek_region < const.WORLD_OFFSET:
country_code = 'CA' country_code = 'CA'
region = get_region_name(seek_region - const.CANADA_OFFSET) region_code = get_region_code(seek_region - const.CANADA_OFFSET)
else: else:
index = (seek_region - const.WORLD_OFFSET) // const.FIPS_RANGE index = (seek_region - const.WORLD_OFFSET) // const.FIPS_RANGE
if index in const.COUNTRY_CODES: if index in const.COUNTRY_CODES:
country_code = const.COUNTRY_CODES[index] country_code = const.COUNTRY_CODES[index]
elif self._databaseType in const.CITY_EDITIONS: elif self._databaseType in const.CITY_EDITIONS:
rec = self._get_record(ipnum) rec = self._get_record(ipnum)
region = rec.get('region_name', '') region_code = rec.get('region_code')
country_code = rec.get('country_code', '') country_code = rec.get('country_code')
return {'country_code': country_code, 'region_name': region} return {'country_code': country_code, 'region_code': region_code}
def _get_record(self, ipnum): def _get_record(self, ipnum):
""" """
...@@ -339,9 +338,9 @@ class GeoIP(object): ...@@ -339,9 +338,9 @@ class GeoIP(object):
@param ipnum: Converted IP address @param ipnum: Converted IP address
@type ipnum: int @type ipnum: int
@return: dict with country_code, country_code3, country_name, @return: dict with city, region_code, area_code, time_zone,
region, city, postal_code, latitude, longitude, dma_code, metro_code, country_code3, latitude, postal_code,
dma_code, metro_code, area_code, region_name, time_zone longitude, country_code, country_name, continent
@rtype: dict @rtype: dict
""" """
seek_country = self._seek_country(ipnum) seek_country = self._seek_country(ipnum)
...@@ -387,7 +386,7 @@ class GeoIP(object): ...@@ -387,7 +386,7 @@ class GeoIP(object):
return offset, buf[buf_pos:offset] return offset, buf[buf_pos:offset]
return offset, '' return offset, ''
offset, record['region_name'] = get_data(buf, buf_pos) offset, record['region_code'] = get_data(buf, buf_pos)
offset, record['city'] = get_data(buf, offset + 1) offset, record['city'] = get_data(buf, offset + 1)
offset, record['postal_code'] = get_data(buf, offset + 1) offset, record['postal_code'] = get_data(buf, offset + 1)
buf_pos = offset + 1 buf_pos = offset + 1
...@@ -417,7 +416,7 @@ class GeoIP(object): ...@@ -417,7 +416,7 @@ class GeoIP(object):
record['area_code'] = dmaarea_combo % 1000 record['area_code'] = dmaarea_combo % 1000
record['metro_code'] = const.DMA_MAP.get(record['dma_code']) record['metro_code'] = const.DMA_MAP.get(record['dma_code'])
params = (record['country_code'], record['region_name']) params = (record['country_code'], record['region_code'])
record['time_zone'] = time_zone_by_country_and_region(*params) record['time_zone'] = time_zone_by_country_and_region(*params)
return record return record
...@@ -562,7 +561,7 @@ class GeoIP(object): ...@@ -562,7 +561,7 @@ class GeoIP(object):
@type addr: str @type addr: str
@return: Dictionary with country_code, country_code3, country_name, @return: Dictionary with country_code, country_code3, country_name,
region, city, postal_code, latitude, longitude, dma_code, region, city, postal_code, latitude, longitude, dma_code,
metro_code, area_code, region_name, time_zone metro_code, area_code, region_code, time_zone
@rtype: dict @rtype: dict
""" """
if self._databaseType not in const.CITY_EDITIONS: if self._databaseType not in const.CITY_EDITIONS:
...@@ -585,7 +584,7 @@ class GeoIP(object): ...@@ -585,7 +584,7 @@ class GeoIP(object):
@type hostname: str @type hostname: str
@return: Dictionary with country_code, country_code3, country_name, @return: Dictionary with country_code, country_code3, country_name,
region, city, postal_code, latitude, longitude, dma_code, region, city, postal_code, latitude, longitude, dma_code,
metro_code, area_code, region_name, time_zone metro_code, area_code, region_code, time_zone
@rtype: dict @rtype: dict
""" """
addr = self._gethostbyname(hostname) addr = self._gethostbyname(hostname)
...@@ -598,7 +597,7 @@ class GeoIP(object): ...@@ -598,7 +597,7 @@ class GeoIP(object):
@param addr: IP address @param addr: IP address
@type addr: str @type addr: str
@return: Dictionary containing country_code, region and region_name @return: Dictionary containing country_code and region_code
@rtype: dict @rtype: dict
""" """
if self._databaseType not in const.REGION_CITY_EDITIONS: if self._databaseType not in const.REGION_CITY_EDITIONS:
...@@ -615,7 +614,7 @@ class GeoIP(object): ...@@ -615,7 +614,7 @@ class GeoIP(object):
@param hostname: Hostname @param hostname: Hostname
@type hostname: str @type hostname: str
@return: Dictionary containing country_code, region, and region_name @return: Dictionary containing country_code, region_code and region
@rtype: dict @rtype: dict
""" """
addr = self._gethostbyname(hostname) addr = self._gethostbyname(hostname)
......
...@@ -21,7 +21,7 @@ class TestGeoIPCityFunctions(unittest.TestCase): ...@@ -21,7 +21,7 @@ class TestGeoIPCityFunctions(unittest.TestCase):
self.us_record_data = { self.us_record_data = {
'city': 'Mountain View', 'city': 'Mountain View',
'region_name': 'CA', 'region_code': 'CA',
'area_code': 650, 'area_code': 650,
'longitude': -122.05740356445312, 'longitude': -122.05740356445312,
'country_code3': 'USA', 'country_code3': 'USA',
...@@ -36,7 +36,7 @@ class TestGeoIPCityFunctions(unittest.TestCase): ...@@ -36,7 +36,7 @@ class TestGeoIPCityFunctions(unittest.TestCase):
self.gb_record_data = { self.gb_record_data = {
'city': 'Tadworth', 'city': 'Tadworth',
'region_name': 'N7', 'region_code': 'N7',
'area_code': 0, 'area_code': 0,
'longitude': -0.23339999999998895, 'longitude': -0.23339999999998895,
'country_code3': 'GBR', 'country_code3': 'GBR',
...@@ -49,8 +49,8 @@ class TestGeoIPCityFunctions(unittest.TestCase): ...@@ -49,8 +49,8 @@ class TestGeoIPCityFunctions(unittest.TestCase):
'time_zone': 'Europe/London' 'time_zone': 'Europe/London'
} }
self.us_region_data = {'region_name': 'CA', 'country_code': 'US'} self.us_region_data = {'region_code': 'CA', 'country_code': 'US'}
self.gb_region_data = {'region_name': 'N7', 'country_code': 'GB'} self.gb_region_data = {'region_code': 'N7', 'country_code': 'GB'}
self.gic = pygeoip.GeoIP(CITY_DB_PATH) self.gic = pygeoip.GeoIP(CITY_DB_PATH)
self.gic_mem = pygeoip.GeoIP(CITY_DB_PATH, pygeoip.MEMORY_CACHE) self.gic_mem = pygeoip.GeoIP(CITY_DB_PATH, pygeoip.MEMORY_CACHE)
......
...@@ -11,7 +11,7 @@ class TestGeoIPRegionFunctions(unittest.TestCase): ...@@ -11,7 +11,7 @@ class TestGeoIPRegionFunctions(unittest.TestCase):
self.us_hostname = 'apple.com' self.us_hostname = 'apple.com'
self.us_ip = '17.172.224.47' self.us_ip = '17.172.224.47'
self.us_region_data = { self.us_region_data = {
'region_name': 'CA', 'region_code': 'CA',
'country_code': 'US' 'country_code': 'US'
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment