Skip to content

Commit

Permalink
start to use short() for printing objects
Browse files Browse the repository at this point in the history
We should have never defined our own __str__ function on classes
and never used str() when generating output. You live, your learn.

Started using short() as a replacement name and checked for the
presence of the function to not cause regression even if it will
slow things down.

Also renamed 'route refresh' to 'route-refresh' as I am bad at
keeping one change per patch.
  • Loading branch information
thomas-mangin committed Apr 22, 2023
1 parent f9b2e18 commit cb244de
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
2 changes: 2 additions & 0 deletions doc/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Version 5.0.0:
* Feature: Support for the 'ipv4' and 'ipv6' options in the Announce statement to exabgp-cli
patch: Takeru Hayasaka
* Compatibility: remove "alias" not-a-fragment which should be not expressed as !is-fragment
* Compatibility: the JSON string changed
* Compatibility: "route refresh" is now "route-refresh"

Version 4.2.7:
* Feature: logging parsing in debug mode will now print the JSON of updates
Expand Down
4 changes: 2 additions & 2 deletions src/exabgp/bgp/message/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class _MessageCode(int):
UPDATE: 'update',
NOTIFICATION: 'notification',
KEEPALIVE: 'keepalive',
ROUTE_REFRESH: 'route refresh',
ROUTE_REFRESH: 'route-refresh',
OPERATIONAL: 'operational',
}

Expand All @@ -65,7 +65,7 @@ def __repr__(self):
return str(self)

def short(self):
return self.short_names.get(self, 'unknown')
return self.short_names.get(self, '%s' % self)


# ================================================================== BGP Message
Expand Down
30 changes: 22 additions & 8 deletions src/exabgp/bgp/message/update/nlri/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ def pack(self):
# ID is defined in subclasses
return bytes([self.ID]) + raw # pylint: disable=E1101

def short(self):
return str(self.cidr)

def __str__(self):
return str(self.cidr)

Expand All @@ -164,6 +167,9 @@ def pack(self):
# ID is defined in subclasses
return bytes([self.ID, self.cidr.mask, self.offset]) + self.cidr.pack_ip() # pylint: disable=E1101

def short(self):
return "%s/%s" % (self.cidr, self.offset)

def __str__(self):
return "%s/%s" % (self.cidr, self.offset)

Expand Down Expand Up @@ -250,11 +256,16 @@ class NumericString(object):
NumericOperator.AND | NumericOperator.FALSE: '&false',
}

def __str__(self):
def short(self):
op = self.operations & (CommonOperator.EOL ^ 0xFF)
if op in [NumericOperator.TRUE, NumericOperator.FALSE]:
return self._string[op]
return "%s%s" % (self._string.get(op, "%02X" % op), self.value)
# ugly hack as dynamic languages are what they are and use used __str__ in the past
value = self.value.short() if hasattr(self.value, 'short') else str(self.value)
return "%s%s" % (self._string.get(op, "%02X" % op), value)

def __str__(self):
return self.short()


class BinaryString(object):
Expand All @@ -273,10 +284,12 @@ class BinaryString(object):
BinaryOperator.AND | BinaryOperator.NOT | BinaryOperator.MATCH: '&!=',
}

def __str__(self):
def short(self):
op = self.operations & (CommonOperator.EOL ^ 0xFF)
return "%s%s" % (self._string.get(op, "%02X" % op), self.value)

def __str__(self):
return self.short()

# Components ..............................

Expand Down Expand Up @@ -584,14 +597,15 @@ def _rules(self):
string = []
for index in sorted(self.rules):
rules = self.rules[index]
s = []
r_str = []
for idx, rule in enumerate(rules):
# only add ' ' after the first element
if idx and not rule.operations & NumericOperator.AND:
s.append(' ')
s.append(rule)
line = ''.join(str(_) for _ in s)
if len(s) > 1:
r_str.append(' ')
# ugly hack as dynamic languages are what they are and use used __str__ in the past
r_str.append(rule.short() if hasattr(rule,'short') else str(rule))
line = ''.join(r_str)
if len(r_str) > 1:
line = '[ %s ]' % line
string.append(' %s %s' % (rules[0].NAME, line))
return ''.join(string)
Expand Down
23 changes: 17 additions & 6 deletions src/exabgp/protocol/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ def __new__(cls, *args):
Resource.cache[cls][key] = instance
return instance

def short(self):
return self.names.get(self, '%ld' % self)

def __str__(self):
return self.names.get(self, 'unknown %s type %ld' % (self.NAME, int(self)))
return self.names.get(self, 'unknown %s type %ld' % (self.NAME, self))

@classmethod
def _value(cls, string):
Expand All @@ -52,7 +55,7 @@ def named(cls, string):


class BitResource(Resource):
def bits(self):
def named_bits(self):
value = int(self)
for bit in self.names.keys():
if value & bit or value == bit:
Expand All @@ -61,9 +64,17 @@ def bits(self):
if value:
yield self.names.get(self, 'unknown %s type %ld' % (self.NAME, int(self)))

def named_bits(self):
for value in self.bits():
yield value
def bits(self):
value = int(self)
for bit in self.names.keys():
if value & bit or value == bit:
yield self.names[bit]
value -= bit
if value:
yield self.names.get(self, '%s' % hex(self))

def __str__(self):
def short(self):
return '+'.join(self.bits())

def __str__(self):
return '+'.join(self.named_bits())

0 comments on commit cb244de

Please sign in to comment.