# Path: txtorcon/util.py
# class NetLocation(object):
#     """
#     Represents the location of an IP address, either city or country
#     level resolution depending on what GeoIP database was loaded. If
#     the ASN database is available you get that also.
#     """
# 
#     def __init__(self, ipaddr):
#         "ipaddr should be a dotted-quad"
#         self.ip = ipaddr
#         self.latlng = (None, None)
#         self.countrycode = None
#         self.city = None
#         self.asn = None
# 
#         if self.ip is None or self.ip == 'unknown':
#             return
# 
#         if city:
#             try:
#                 r = city.record_by_addr(self.ip)
#             except Exception:
#                 r = None
#             if r is not None:
#                 self.countrycode = r['country_code']
#                 self.latlng = (r['latitude'], r['longitude'])
#                 try:
#                     self.city = (r['city'], r['region_code'])
#                 except KeyError:
#                     self.city = (r['city'], r['region_name'])
# 
#         elif country:
#             self.countrycode = country.country_code_by_addr(ipaddr)
# 
#         else:
#             self.countrycode = ''
# 
#         if asn:
#             try:
#                 self.asn = asn.org_by_addr(self.ip)
#             except Exception:
#                 self.asn = None
# 
# Path: txtorcon/util.py
# class _Version(object):
#     """
#     Replacement for incremental.Version until
#     https://github.com/meejah/txtorcon/issues/233 and/or
#     https://github.com/hawkowl/incremental/issues/31 is fixed.
#     """
#     # as of latest incremental, it should only access .package and
#     # .short() via the getVersionString() method that Twisted's
#     # deprecated() uses...
# 
#     def __init__(self, package, major, minor, patch):
#         self.package = package
#         self.major = major
#         self.minor = minor
#         self.patch = patch
# 
#     def short(self):
#         return '{}.{}.{}'.format(self.major, self.minor, self.patch)


import json
import six
from datetime import datetime
from .util import NetLocation
from .util import _Version
from base64 import b64encode, b64decode
from binascii import b2a_hex, a2b_hex
from twisted.internet.defer import inlineCallbacks, returnValue, succeed
from twisted.python.deprecate import deprecated
from twisted.web.client import readBody
        Returns a Deferred that fires with a NetLocation object for this
        router.
        """
        if self._location:
            return succeed(self._location)
        if self.ip != 'unknown':
            self._location = NetLocation(self.ip)
        else:
            self._location = NetLocation(None)
        if not self._location.countrycode and self.ip != 'unknown':
            # see if Tor is magic and knows more...
            d = self.controller.get_info_raw('ip-to-country/' + self.ip)
            d.addCallback(self._set_country)
            d.addCallback(lambda _: self._location)
            return d
        return succeed(self._location)

    @property
    @deprecated(_Version("txtorcon", 18, 0, 0))
    def location(self):
        """
        A NetLocation instance with some GeoIP or pygeoip information
        about location, asn, city (if available).
        """
        if self._location:
            return self._location

        if self.ip != 'unknown':
            self._location = NetLocation(self.ip)
        else:

# Path: txtorcon/util.py
# class NetLocation(object):
#     """
#     Represents the location of an IP address, either city or country
#     level resolution depending on what GeoIP database was loaded. If
#     the ASN database is available you get that also.
#     """
# 
#     def __init__(self, ipaddr):
#         "ipaddr should be a dotted-quad"
#         self.ip = ipaddr
#         self.latlng = (None, None)
#         self.countrycode = None
#         self.city = None
#         self.asn = None
# 
#         if self.ip is None or self.ip == 'unknown':
#             return
# 
#         if city:
#             try:
#                 r = city.record_by_addr(self.ip)
#             except Exception:
#                 r = None
#             if r is not None:
#                 self.countrycode = r['country_code']
#                 self.latlng = (r['latitude'], r['longitude'])
#                 try:
#                     self.city = (r['city'], r['region_code'])
#                 except KeyError:
#                     self.city = (r['city'], r['region_name'])
# 
#         elif country:
#             self.countrycode = country.country_code_by_addr(ipaddr)
# 
#         else:
#             self.countrycode = ''
# 
#         if asn:
#             try:
#                 self.asn = asn.org_by_addr(self.ip)
#             except Exception:
#                 self.asn = None
# 
# Path: txtorcon/util.py
# class _Version(object):
#     """
#     Replacement for incremental.Version until
#     https://github.com/meejah/txtorcon/issues/233 and/or
#     https://github.com/hawkowl/incremental/issues/31 is fixed.
#     """
#     # as of latest incremental, it should only access .package and
#     # .short() via the getVersionString() method that Twisted's
#     # deprecated() uses...
# 
#     def __init__(self, package, major, minor, patch):
#         self.package = package
#         self.major = major
#         self.minor = minor
#         self.patch = patch
# 
#     def short(self):
#         return '{}.{}.{}'.format(self.major, self.minor, self.patch)


# Path: txtorcon/router.py
import json
import six
from datetime import datetime
from .util import NetLocation
from .util import _Version
from base64 import b64encode, b64decode
from binascii import b2a_hex, a2b_hex
from twisted.internet.defer import inlineCallbacks, returnValue, succeed
from twisted.python.deprecate import deprecated
from twisted.web.client import readBody

        Returns a Deferred that fires with a NetLocation object for this
        router.
        """
        if self._location:
            return succeed(self._location)
        if self.ip != 'unknown':
            self._location = NetLocation(self.ip)
        else:
            self._location = NetLocation(None)
        if not self._location.countrycode and self.ip != 'unknown':
            # see if Tor is magic and knows more...
            d = self.controller.get_info_raw('ip-to-country/' + self.ip)
            d.addCallback(self._set_country)
            d.addCallback(lambda _: self._location)
            return d
        return succeed(self._location)

    @property
    @deprecated(_Version("txtorcon", 18, 0, 0))
    def location(self):
        """
        A NetLocation instance with some GeoIP or pygeoip information
        about location, asn, city (if available).
        """
        if self._location:
            return self._location

        if self.ip != 'unknown':
            self._location = NetLocation(self.ip)
        else:

            self._location = NetLocation(None)