Commit 71190eea by William Tisäter

Remove unnecessary __future__ imports

parent 05322189
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
*env *env
*.pyc *.pyc
*.egg-info *.egg-info
*.dat tests/data/*
.tox .tox
build build
dist dist
\ No newline at end of file
...@@ -32,7 +32,6 @@ You should have received a copy of the GNU Lesser General Public License ...@@ -32,7 +32,6 @@ You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>. along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
""" """
from __future__ import with_statement, division
import os import os
import math import math
import socket import socket
...@@ -70,7 +69,6 @@ class GeoIPMetaclass(type): ...@@ -70,7 +69,6 @@ class GeoIPMetaclass(type):
flag (default) and then try later to initialize with MEMORY_CACHE, it flag (default) and then try later to initialize with MEMORY_CACHE, it
will still return the STANDARD one. will still return the STANDARD one.
""" """
if not hasattr(cls, '_instances'): if not hasattr(cls, '_instances'):
cls._instances = {} cls._instances = {}
...@@ -106,21 +104,19 @@ class GeoIP(GeoIPBase): ...@@ -106,21 +104,19 @@ class GeoIP(GeoIPBase):
self._flags = flags self._flags = flags
if self._flags & const.MMAP_CACHE: if self._flags & const.MMAP_CACHE:
with open(filename, 'rb') as f: f = open(filename, 'rb')
access = mmap.ACCESS_READ access = mmap.ACCESS_READ
self._filehandle = mmap.mmap(f.fileno(), 0, access=access) self._filehandle = mmap.mmap(f.fileno(), 0, access=access)
f.close()
elif self._flags & const.MEMORY_CACHE: elif self._flags & const.MEMORY_CACHE:
if filename.endswith('.gz'): f = open(filename, 'rb')
opener = gzip.open self._memoryBuffer = f.read()
else: self._filehandle = StringIO(self._memoryBuffer)
opener = open f.close()
with opener(filename, 'rb') as f:
self._memoryBuffer = f.read()
self._filehandle = StringIO(self._memoryBuffer)
else: else:
self._filehandle = codecs.open(filename, 'rb', 'latin_1') self._filehandle = codecs.open(filename, 'rb', 'iso-8859-1')
self._lock = Lock() self._lock = Lock()
self._setup_segments() self._setup_segments()
...@@ -158,15 +154,16 @@ class GeoIP(GeoIPBase): ...@@ -158,15 +154,16 @@ class GeoIP(GeoIPBase):
if (delim == chars) if PY3 else (delim == unicode(chars, flag)): if (delim == chars) if PY3 else (delim == unicode(chars, flag)):
self._databaseType = ord(self._filehandle.read(1)) self._databaseType = ord(self._filehandle.read(1))
# Backwards compatibility with databases from # Compatibility with databases from April 2003 and earlier
# April 2003 and earlier
if (self._databaseType >= 106): if (self._databaseType >= 106):
self._databaseType -= 105 self._databaseType -= 105
if self._databaseType == const.REGION_EDITION_REV0: if self._databaseType == const.REGION_EDITION_REV0:
self._databaseSegments = const.STATE_BEGIN_REV0 self._databaseSegments = const.STATE_BEGIN_REV0
elif self._databaseType == const.REGION_EDITION_REV1: elif self._databaseType == const.REGION_EDITION_REV1:
self._databaseSegments = const.STATE_BEGIN_REV1 self._databaseSegments = const.STATE_BEGIN_REV1
elif self._databaseType in (const.CITY_EDITION_REV0, elif self._databaseType in (const.CITY_EDITION_REV0,
const.CITY_EDITION_REV1, const.CITY_EDITION_REV1,
const.ORG_EDITION, const.ORG_EDITION,
...@@ -174,6 +171,7 @@ class GeoIP(GeoIPBase): ...@@ -174,6 +171,7 @@ class GeoIP(GeoIPBase):
const.ASNUM_EDITION): const.ASNUM_EDITION):
self._databaseSegments = 0 self._databaseSegments = 0
buf = self._filehandle.read(const.SEGMENT_RECORD_LENGTH) buf = self._filehandle.read(const.SEGMENT_RECORD_LENGTH)
for j in range(const.SEGMENT_RECORD_LENGTH): for j in range(const.SEGMENT_RECORD_LENGTH):
self._databaseSegments += (ord(buf[j]) << (j * 8)) self._databaseSegments += (ord(buf[j]) << (j * 8))
...@@ -183,6 +181,7 @@ class GeoIP(GeoIPBase): ...@@ -183,6 +181,7 @@ class GeoIP(GeoIPBase):
break break
else: else:
self._filehandle.seek(-4, os.SEEK_CUR) self._filehandle.seek(-4, os.SEEK_CUR)
self._filehandle.seek(filepos, os.SEEK_SET) self._filehandle.seek(filepos, os.SEEK_SET)
self._lock.release() self._lock.release()
...@@ -225,7 +224,7 @@ class GeoIP(GeoIPBase): ...@@ -225,7 +224,7 @@ class GeoIP(GeoIPBase):
return x[0] return x[0]
offset = x[0] offset = x[0]
raise Exception('Error traversing database - perhaps it is corrupt?') raise GeoIPError('Corrupt database')
def _get_org(self, ipnum): def _get_org(self, ipnum):
""" """
......
...@@ -373,6 +373,7 @@ DATABASE_INFO_MAX_SIZE = 100 ...@@ -373,6 +373,7 @@ DATABASE_INFO_MAX_SIZE = 100
# Database editions # Database editions
COUNTRY_EDITION = 1 COUNTRY_EDITION = 1
COUNTRY_EDITION_V6 = 12
REGION_EDITION_REV0 = 7 REGION_EDITION_REV0 = 7
REGION_EDITION_REV1 = 3 REGION_EDITION_REV1 = 3
CITY_EDITION_REV0 = 6 CITY_EDITION_REV0 = 6
...@@ -383,7 +384,6 @@ ASNUM_EDITION = 9 ...@@ -383,7 +384,6 @@ ASNUM_EDITION = 9
# Not yet supported databases # Not yet supported databases
PROXY_EDITION = 8 PROXY_EDITION = 8
NETSPEED_EDITION = 11 NETSPEED_EDITION = 11
COUNTRY_EDITION_V6 = 12
# Collection of databases # Collection of databases
IPV6_EDITIONS = (COUNTRY_EDITION_V6,) IPV6_EDITIONS = (COUNTRY_EDITION_V6,)
......
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