<commit_before># encoding: utf-8
"""
rt.py

Created by <NAME> on 2014-06-20.
Copyright (c) 2014-2015 Orange. All rights reserved.
Copyright (c) 2014-2015 Exa Networks. All rights reserved.
"""

from struct import pack
from struct import unpack

from exabgp.protocol.ip import IPv4
from exabgp.bgp.message.open.asn import ASN
from exabgp.bgp.message.update.attribute import Attribute
from exabgp.bgp.message.update.attribute.community.extended import ExtendedCommunity


# ================================================================== RouteTarget
# RFC 4360 / RFC 7153

class RouteTarget (ExtendedCommunity):
	COMMUNITY_SUBTYPE = 0x02
	LIMIT = 0

	@property
	def la (self):
		return self.community[2:self.LIMIT]

	@property
	def ga (self):
		return self.community[self.LIMIT:8]

	def __eq__ (self, other):
		return \
			self.COMMUNITY_SUBTYPE == other.COMMUNITY_SUBTYPE and \
			ExtendedCommunity.__eq__(self,other)

	def __ne__ (self, other):
		return not self.__eq__(other)

# ============================================================= RouteTargetASN2Number
# RFC 4360 / RFC 7153

@ExtendedCommunity.register
class RouteTargetASN2Number (RouteTarget):
	COMMUNITY_TYPE = 0x00
	LIMIT = 4

	__slots__ = ['asn','number']

	def __init__ (self, asn, number, transitive=True, community=None):
		self.asn = asn
		self.number = number
		# assert(number < pow(2,32))
		RouteTarget.__init__(
			self,
			community if community else pack(
				'!2sHL',
				self._subtype(transitive),
				asn,number
			)
		)

	def __hash__(self):
		return hash((self.asn,self.number))

	def __repr__ (self):
		return "target:%d:%d" % (self.asn,self.number)

	@staticmethod
	def unpack (data):
		asn,number = unpack('!HL',data[2:8])
		return RouteTargetASN2Number(ASN(asn),number,False,data[:8])


# ============================================================= RouteTargetIPNumber
# RFC 4360 / RFC 7153

@ExtendedCommunity.register
class RouteTargetIPNumber (RouteTarget):
	COMMUNITY_TYPE = 0x01
	LIMIT = 6

	__slots__ = ['ip','number']

	def __init__ (self, ip, number, transitive=True, community=None):
		self.ip = ip
		self.number = number
		# assert(number < pow(2,16))
		RouteTarget.__init__(
			self,
			community if community else pack(
				'!2s4sH',
				self._subtype(transitive),
				IPv4.pton(ip),number
			)
		)

	# why could we not simply use ExtendedCommunity.hash ?
	def __hash__(self):
		return hash((self.ip,self.number))

	def __repr__ (self):
		return "target:%s:%d" % (self.ip, self.number)

	@staticmethod
	def unpack (data):
		ip,number = unpack('!4sH',data[2:8])
		return RouteTargetIPNumber(IPv4.ntop(ip),number,False,data[:8])


# ======================================================== RouteTargetASN4Number
# RFC 4360 / RFC 7153

@ExtendedCommunity.register
class RouteTargetASN4Number (RouteTarget):
	COMMUNITY_TYPE = 0x02
	LIMIT = 6

	__slots__ = ['asn','number']

	def __init__ (self, asn, number, transitive=True, community=None):
		self.asn = asn
		self.number = number
		# assert(number < pow(2,16))
		RouteTarget.__init__(
			self,
			community if community else pack(
				'!2sLH',
				self._subtype(transitive),
				asn,number
			)
		)

	# why could we not simply use ExtendedCommunity.hash ?
	def __hash__(self):
		return hash((self.asn,self.number))

	def __repr__ (self):
		return "target:%dL:%d" % (self.asn, self.number)

	@staticmethod
	def unpack (data):
		asn,number = unpack('!LH',data[2:8])
		return RouteTargetASN4Number(ASN(asn),number,False,data[:8])<commit_msg>Make RouteTarget classes easily inherited from<commit_after># encoding: utf-8
"""
rt.py

Created by <NAME> on 2014-06-20.
Copyright (c) 2014-2015 Orange. All rights reserved.
Copyright (c) 2014-2015 Exa Networks. All rights reserved.
"""

from struct import pack
from struct import unpack

from exabgp.protocol.ip import IPv4
from exabgp.bgp.message.open.asn import ASN
from exabgp.bgp.message.update.attribute import Attribute
from exabgp.bgp.message.update.attribute.community.extended import ExtendedCommunity


# ================================================================== RouteTarget
# RFC 4360 / RFC 7153

class RouteTarget (ExtendedCommunity):
	COMMUNITY_SUBTYPE = 0x02
	LIMIT = 0

	DESC = 'target'

	@property
	def la (self):
		return self.community[2:self.LIMIT]

	@property
	def ga (self):
		return self.community[self.LIMIT:8]

	def __eq__ (self, other):
		return \
			self.COMMUNITY_SUBTYPE == other.COMMUNITY_SUBTYPE and \
			ExtendedCommunity.__eq__(self,other)

	def __ne__ (self, other):
		return not self.__eq__(other)

# ============================================================= RouteTargetASN2Number
# RFC 4360 / RFC 7153

@ExtendedCommunity.register
class RouteTargetASN2Number (RouteTarget):
	COMMUNITY_TYPE = 0x00
	LIMIT = 4

	__slots__ = ['asn','number']

	def __init__ (self, asn, number, transitive=True, community=None):
		self.asn = asn
		self.number = number
		# assert(number < pow(2,32))
		RouteTarget.__init__(
			self,
			community if community else pack(
				'!2sHL',
				self._subtype(transitive),
				asn,number
			)
		)

	def __hash__(self):
		return hash((self.asn,self.number))

	def __repr__ (self):
		return "%s:%d:%d" % (self.DESC,self.asn,self.number)

	@classmethod
	def unpack (cls, data):
		asn,number = unpack('!HL',data[2:8])
		return cls(ASN(asn),number,False,data[:8])


# ============================================================= RouteTargetIPNumber
# RFC 4360 / RFC 7153

@ExtendedCommunity.register
class RouteTargetIPNumber (RouteTarget):
	COMMUNITY_TYPE = 0x01
	LIMIT = 6

	__slots__ = ['ip','number']

	def __init__ (self, ip, number, transitive=True, community=None):
		self.ip = ip
		self.number = number
		# assert(number < pow(2,16))
		RouteTarget.__init__(
			self,
			community if community else pack(
				'!2s4sH',
				self._subtype(transitive),
				IPv4.pton(ip),number
			)
		)

	# why could we not simply use ExtendedCommunity.hash ?
	def __hash__(self):
		return hash((self.ip,self.number))

	def __repr__ (self):
		return "%s:%d:%d" % (self.DESC,self.ip,self.number)

	@classmethod
	def unpack (cls, data):
		ip,number = unpack('!4sH',data[2:8])
		return cls(IPv4.ntop(ip),number,False,data[:8])


# ======================================================== RouteTargetASN4Number
# RFC 4360 / RFC 7153

@ExtendedCommunity.register
class RouteTargetASN4Number (RouteTarget):
	COMMUNITY_TYPE = 0x02
	LIMIT = 6

	__slots__ = ['asn','number']

	def __init__ (self, asn, number, transitive=True, community=None):
		self.asn = asn
		self.number = number
		# assert(number < pow(2,16))
		RouteTarget.__init__(
			self,
			community if community else pack(
				'!2sLH',
				self._subtype(transitive),
				asn,number
			)
		)

	# why could we not simply use ExtendedCommunity.hash ?
	def __hash__(self):
		return hash((self.asn,self.number))

	def __repr__ (self):
		return "%s:%dL:%d" % (self.DESC,self.asn, self.number)

	@classmethod
	def unpack (cls, data):
		asn,number = unpack('!LH',data[2:8])
		return cls(ASN(asn),number,False,data[:8])
{'fraction_non_alphanumeric': 0.1888381387382244, 'fraction_numerical': 0.03025977733371396, 'mean_word_length': 4.752873563218391, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3955704', 'n_tokens_mistral': 2772, 'n_tokens_neox': 2355, 'n_words': 552}