Skip to content

Commit

Permalink
PEP8 and Sphinx documentation fixes for bridgedb.geo.getCountryCode().
Browse files Browse the repository at this point in the history
  • Loading branch information
isislovecruft committed Apr 10, 2015
1 parent 63bd41f commit 6a3ca67
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/bridgedb/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,33 @@
geoip = None
geoipv6 = None

def getCountryCode(IPAddr):

def getCountryCode(ip):
"""Return the two-letter country code of a given IP address.
:param IPAddr: (:class:`ipaddr.IPAddress`) An IPv4 OR IPv6 address.
"""
:type ip: :class:`ipaddr.IPAddress`
:param ip: An IPv4 OR IPv6 address.
:rtype: ``None`` or str
ip = None
:returns: If the GeoIP databases are loaded, and the **ip** lookup is
successful, then this returns a two-letter country code. Otherwise,
this returns ``None``.
"""
addr = None
version = None
try:
ip = IPAddr.compressed
version = IPAddr.version
addr = ip.compressed
version = ip.version
except AttributeError as err:
logging.warn("Wrong type passed to getCountryCode. Offending call:"
" %r" % err)
logging.warn("Wrong type passed to getCountryCode: %s" % str(err))
return None

# GeoIP has two databases: one for IPv4 addresses, and one for IPv6
# addresses. This will ensure we use the correct one.
db = None
# First, make sure we loaded GeoIP properly.
if None in (geoip, geoipv6):
logging.warn("GeoIP databases failed to load; could not look up"\
" country code.")
logging.warn("GeoIP databases aren't loaded; can't look up country code")
return None
else:
if version == 4:
Expand All @@ -69,10 +73,10 @@ def getCountryCode(IPAddr):
db = geoipv6

# Look up the country code of the address.
countryCode = db.country_code_by_addr(ip)
countryCode = db.country_code_by_addr(addr)
if countryCode:
logging.debug("Looked up country code: %s" % countryCode)
return countryCode
else:
logging.debug("Country code was not detected. IP: %s" % ip)
logging.debug("Country code was not detected. IP: %s" % addr)
return None

0 comments on commit 6a3ca67

Please sign in to comment.