import decimal import ipaddress from typing import TypeVar, cast from faker import Faker from location.connectors.ip2location import IP2LocationRecord, empty_record T = TypeVar("T") class FakerTyped: def __init__(self) -> None: self._faker = Faker(["es-ES"]) def ipv4(self) -> str: return cast(str, self._faker.ipv4()) def ipv4_private(self) -> str: return cast(str, self._faker.ipv4_private()) def ipv6(self) -> str: return cast(str, self._faker.ipv6()) def country(self) -> str: return cast(str, self._faker.country()) def country_code(self) -> str: return cast(str, self._faker.country_code()) def region(self) -> str: return cast(str, self._faker.region()) def city(self) -> str: return cast(str, self._faker.city()) def latitude(self) -> decimal.Decimal: return cast(decimal.Decimal, self._faker.latitude()) def longitude(self) -> decimal.Decimal: return cast(decimal.Decimal, self._faker.longitude()) def ip2location_record(self) -> IP2LocationRecord: record = IP2LocationRecord() record.ip = self.ipv4() record.resolved = True record.country_short = self.country_code() record.country_long = self.country() record.region = self.region() record.city = self.city() record.latitude = str(self.latitude()) record.longitude = str(self.longitude()) return record def ip2location_empty_record(self) -> IP2LocationRecord: return empty_record( self.ip2location_record(), ip=ipaddress.ip_address(self.ipv4()), )