Skip to content

Commit

Permalink
more pep8 work
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-mangin committed Jan 18, 2015
1 parent f0e0471 commit aaded19
Show file tree
Hide file tree
Showing 47 changed files with 424 additions and 221 deletions.
8 changes: 4 additions & 4 deletions lib/exabgp/application/bmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,17 @@ def drop ():

from exabgp.configuration.environment import environment

dict_space = {
_SPACE = {
'space': ' '*33
}

help_stdout = """\
HELP_STDOUT = """\
where logging should log
%(space)s syslog (or no setting) sends the data to the local syslog syslog
%(space)s host:<location> sends the data to a remote syslog server
%(space)s stdout sends the data to stdout
%(space)s stderr sends the data to stderr
%(space)s <filename> send the data to a file""" % dict_space
%(space)s <filename> send the data to a file""" % _SPACE


environment.application = 'exabmp'
Expand Down Expand Up @@ -223,7 +223,7 @@ def drop ():
'read': environment.unquote,
'write': environment.quote,
'value': 'stdout',
'help': help_stdout,
'help': HELP_STDOUT,
},
'all': {
'read': environment.boolean,
Expand Down
8 changes: 4 additions & 4 deletions lib/exabgp/bgp/message/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ class ID (int):
}

def __str__ (self):
return self.names.get(self,'UNKNOWN MESSAGE %s' % hex(self))
return self.names.get(self,'unknown message %s' % hex(self))

def __repr__ (self):
return str(self)

@classmethod
def name (cls,message_id):
return cls.names.get(message_id,'UNKNOWN MESSAGE %s' % hex(message_id))
@staticmethod
def name (message_id):
return Message.ID.names.get(message_id,'unknown message %s' % hex(message_id))

class Name:
NOP = 'NOP'
Expand Down
4 changes: 1 addition & 3 deletions lib/exabgp/bgp/message/update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def __init__ (self,nlris,attributes):
def __str__ (self):
return '\n'.join(['%s%s' % (str(self.nlris[n]),str(self.attributes)) for n in range(len(self.nlris))])


@staticmethod
def prefix (data):
return '%s%s' % (pack('!H',len(data)),data)
Expand All @@ -92,7 +91,7 @@ def split (data):
if len(attributes) != len_attributes:
raise Notify(3,1,'invalid total path attribute length, not enough data available')

if 2 + len_withdrawn + 2+ len_attributes + len(announced) != length:
if 2 + len_withdrawn + 2 + len_attributes + len(announced) != length:
raise Notify(3,1,'error in BGP message length, not enough data for the size announced')

return withdrawn,attributes,announced
Expand Down Expand Up @@ -241,7 +240,6 @@ def messages (self,negotiated):

yield self._message(Update.prefix(packed_del) + Update.prefix(attr + packed_mp_del + packed_mp_add) + packed_add)


# XXX: FIXME: this can raise ValueError. IndexError,TypeError, struct.error (unpack) = check it is well intercepted
@classmethod
def unpack_message (cls,data,negotiated):
Expand Down
89 changes: 44 additions & 45 deletions lib/exabgp/bgp/message/update/attribute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@
from exabgp.configuration.environment import environment

# Must be imported for the register API to work
from exabgp.bgp.message.update.attribute.attribute import Attribute
from exabgp.bgp.message.update.attribute.generic import GenericAttribute
from exabgp.bgp.message.update.attribute.origin import Origin
from exabgp.bgp.message.update.attribute.aspath import ASPath
from exabgp.bgp.message.update.attribute.nexthop import NextHop
from exabgp.bgp.message.update.attribute.med import MED
from exabgp.bgp.message.update.attribute.localpref import LocalPreference
from exabgp.bgp.message.update.attribute.atomicaggregate import AtomicAggregate
from exabgp.bgp.message.update.attribute.aggregator import Aggregator
from exabgp.bgp.message.update.attribute.community import Community
from exabgp.bgp.message.update.attribute.originatorid import OriginatorID
from exabgp.bgp.message.update.attribute.clusterlist import ClusterList
from exabgp.bgp.message.update.attribute.mprnlri import MPRNLRI
from exabgp.bgp.message.update.attribute.mpurnlri import MPURNLRI
#from exabgp.bgp.message.update.attribute.community import Community
from exabgp.bgp.message.update.attribute.community.extended import ExtendedCommunity
from exabgp.bgp.message.update.attribute.pmsi import PMSI
from exabgp.bgp.message.update.attribute.aigp import AIGP
from exabgp.bgp.message.update.attribute.attribute import Attribute # noqa
from exabgp.bgp.message.update.attribute.generic import GenericAttribute # noqa
from exabgp.bgp.message.update.attribute.origin import Origin # noqa
from exabgp.bgp.message.update.attribute.aspath import ASPath # noqa
from exabgp.bgp.message.update.attribute.nexthop import NextHop # noqa
from exabgp.bgp.message.update.attribute.med import MED # noqa
from exabgp.bgp.message.update.attribute.localpref import LocalPreference # noqa
from exabgp.bgp.message.update.attribute.atomicaggregate import AtomicAggregate # noqa
from exabgp.bgp.message.update.attribute.aggregator import Aggregator # noqa
from exabgp.bgp.message.update.attribute.community import Community # noqa
from exabgp.bgp.message.update.attribute.originatorid import OriginatorID # noqa
from exabgp.bgp.message.update.attribute.clusterlist import ClusterList # noqa
from exabgp.bgp.message.update.attribute.mprnlri import MPRNLRI # noqa
from exabgp.bgp.message.update.attribute.mpurnlri import MPURNLRI # noqa
# from exabgp.bgp.message.update.attribute.community import Community # noqa
from exabgp.bgp.message.update.attribute.community.extended import ExtendedCommunity # noqa
from exabgp.bgp.message.update.attribute.pmsi import PMSI # noqa
from exabgp.bgp.message.update.attribute.aigp import AIGP # noqa
# /forced import

from exabgp.bgp.message.notification import Notify

from exabgp.logger import Logger
from exabgp.logger import LazyFormat


class _NOTHING (object):
def pack (self,negotiated=None):
return ''
Expand Down Expand Up @@ -74,7 +75,6 @@ def __str__ (self):
return '%s' % ' '.join(str(_) for _ in self)



# =================================================================== Attributes
#

Expand All @@ -88,22 +88,22 @@ class Attributes (dict):
previous = ''

representation = {
# key: (how, default, name, text_presentation, json_presentation),
Attribute.ID.ORIGIN : ('string', '', 'origin', '%s', '%s'),
Attribute.ID.AS_PATH : ('multiple','', ('as-path','as-set','confederation-path','confederation-set'), '%s', '%s'),
Attribute.ID.NEXT_HOP : ('string', '', 'next-hop', '%s', '%s'),
Attribute.ID.MED : ('integer', '', 'med', '%s', '%s'),
Attribute.ID.LOCAL_PREF : ('integer', '', 'local-preference', '%s', '%s'),
Attribute.ID.ATOMIC_AGGREGATE : ('boolean', '', 'atomic-aggregate', '%s', '%s'),
Attribute.ID.AGGREGATOR : ('string', '', 'aggregator', '( %s )', '%s'),
Attribute.ID.AS4_AGGREGATOR : ('string', '', 'aggregator', '( %s )', '%s'),
Attribute.ID.COMMUNITY : ('list', '', 'community', '%s', '%s'),
Attribute.ID.ORIGINATOR_ID : ('inet', '', 'originator-id', '%s', '%s'),
Attribute.ID.CLUSTER_LIST : ('list', '', 'cluster-list', '%s', '%s'),
Attribute.ID.EXTENDED_COMMUNITY : ('list', '', 'extended-community', '%s', '%s'),
Attribute.ID.PMSI_TUNNEL : ('string', '', 'pmsi', '%s', '%s'),
Attribute.ID.AIGP : ('integer', '', 'aigp', '%s', '%s'),
Attribute.ID.INTERNAL_NAME : ('string', '', 'name', '%s', '%s'),
# key: (how, default, name, text_presentation, json_presentation),
Attribute.ID.ORIGIN: ('string', '', 'origin', '%s', '%s'),
Attribute.ID.AS_PATH: ('multiple','', ('as-path','as-set','confederation-path','confederation-set'), '%s', '%s'),
Attribute.ID.NEXT_HOP: ('string', '', 'next-hop', '%s', '%s'),
Attribute.ID.MED: ('integer', '', 'med', '%s', '%s'),
Attribute.ID.LOCAL_PREF: ('integer', '', 'local-preference', '%s', '%s'),
Attribute.ID.ATOMIC_AGGREGATE: ('boolean', '', 'atomic-aggregate', '%s', '%s'),
Attribute.ID.AGGREGATOR: ('string', '', 'aggregator', '( %s )', '%s'),
Attribute.ID.AS4_AGGREGATOR: ('string', '', 'aggregator', '( %s )', '%s'),
Attribute.ID.COMMUNITY: ('list', '', 'community', '%s', '%s'),
Attribute.ID.ORIGINATOR_ID: ('inet', '', 'originator-id', '%s', '%s'),
Attribute.ID.CLUSTER_LIST: ('list', '', 'cluster-list', '%s', '%s'),
Attribute.ID.EXTENDED_COMMUNITY: ('list', '', 'extended-community', '%s', '%s'),
Attribute.ID.PMSI_TUNNEL: ('string', '', 'pmsi', '%s', '%s'),
Attribute.ID.AIGP: ('integer', '', 'aigp', '%s', '%s'),
Attribute.ID.INTERNAL_NAME: ('string', '', 'name', '%s', '%s'),
}

def _generate_text (self,extra=None):
Expand Down Expand Up @@ -207,13 +207,13 @@ def pack (self,negotiated,with_default=True):
message = ''

default = {
Attribute.ID.ORIGIN: lambda l,r: Origin(Origin.IGP),
Attribute.ID.AS_PATH: lambda l,r: ASPath([],[]) if l == r else ASPath([local_asn,],[]),
Attribute.ID.LOCAL_PREF: lambda l,r: LocalPreference(100) if l == r else NOTHING,
Attribute.ID.ORIGIN: lambda l,r: Origin(Origin.IGP), # noqa
Attribute.ID.AS_PATH: lambda l,r: ASPath([],[]) if l == r else ASPath([local_asn,],[]), # noqa
Attribute.ID.LOCAL_PREF: lambda l,r: LocalPreference(100) if l == r else NOTHING, # noqa
}

check = {
Attribute.ID.NEXT_HOP: lambda l,r,nh: nh.ipv4() == True,
Attribute.ID.NEXT_HOP: lambda l,r,nh: nh.ipv4() == True, # noqa
Attribute.ID.LOCAL_PREF: lambda l,r,nh: l == r,
}

Expand Down Expand Up @@ -298,7 +298,7 @@ def flag_attribute_content (data):
return flag, attr, data[4:length+4]
else:
length = ord(data[2])
return flag, attr , data[3:length+3]
return flag, attr, data[3:length+3]

def parse (self,data,negotiated):
if not data:
Expand Down Expand Up @@ -341,7 +341,7 @@ def parse (self,data,negotiated):
# it is an unknown transitive attribute we need to pass on
if flag & Attribute.Flag.TRANSITIVE:
logger.parser('unknown transitive attribute (flag 0x%02X, aid 0x%02X)' % (flag,aid))
self.add(GenericAttribute(aid,flag|Attribute.Flag.PARTIAL,attribute),attribute)
self.add(GenericAttribute(aid,flag | Attribute.Flag.PARTIAL,attribute),attribute)
return self.parse(next,negotiated)

# it is an unknown non-transitive attribute we can ignore.
Expand All @@ -355,7 +355,7 @@ def merge_attributes (self):
self.remove(Attribute.ID.AS4_PATH)

# this key is unique as index length is a two header, plus a number of ASN of size 2 or 4
# so adding the : make the length odd and unique
# so adding the: make the length odd and unique
key = "%s:%s" % (as2path.index, as4path.index)

# found a cache copy
Expand Down Expand Up @@ -389,12 +389,10 @@ def merge_attributes (self):
aspath = ASPath(as_seq,as_set)
self.add(aspath,key)


def __hash__(self):
# XXX: FIXME: not excellent... :-(
return hash(repr(self))


# Orange BAGPIPE code ..

# test that sets of attributes exactly match
Expand All @@ -403,7 +401,8 @@ def __hash__(self):
def sameValuesAs(self,other):
# we sort based on string representation since the items do not
# necessarily implement __cmp__
sorter = lambda x,y: cmp(repr(x), repr(y))
def sorter (x,y):
return cmp(repr(x), repr(y))

try:
for key in set(self.iterkeys()).union(set(other.iterkeys())):
Expand Down
6 changes: 5 additions & 1 deletion lib/exabgp/bgp/message/update/attribute/aggregator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

from exabgp.bgp.message.update.attribute.attribute import Attribute


# =============================================================== AGGREGATOR (7)
#

class Aggregator (Attribute):
ID = Attribute.ID.AGGREGATOR
FLAG = Attribute.Flag.TRANSITIVE|Attribute.Flag.OPTIONAL
FLAG = Attribute.Flag.TRANSITIVE | Attribute.Flag.OPTIONAL
MULTIPLE = False
CACHING = True

Expand Down Expand Up @@ -51,7 +53,9 @@ def unpack (cls,data,negotiated):

Aggregator.register_attribute()


# ============================================================== AGGREGATOR (18)
#

class Aggregator4 (Aggregator):
ID = Attribute.ID.AS4_AGGREGATOR
Expand Down
3 changes: 3 additions & 0 deletions lib/exabgp/bgp/message/update/attribute/aigp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

from exabgp.bgp.message.update.attribute.attribute import Attribute


# ========================================================================== TLV
#

# 0 1 2 3
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
Expand All @@ -32,6 +34,7 @@ def __init__(self,what,value):
self.type = what
self.value = value


class TLVS (list):
__slots__ = []

Expand Down
35 changes: 18 additions & 17 deletions lib/exabgp/bgp/message/update/attribute/aspath.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from exabgp.bgp.message.update.attribute.attribute import Attribute
from exabgp.bgp.message.notification import Notify


# =================================================================== ASPath (2)
# only 2-4% of duplicated data therefore it is not worth to cache

Expand Down Expand Up @@ -53,9 +54,9 @@ def __cmp__(self,other):
return 0

def _segment (self,seg_type,values,negotiated):
l = len(values)
if l:
if l>255:
length = len(values)
if length:
if length > 255:
return self._segment(seg_type,values[:255],negotiated) + self._segment(seg_type,values[255:],negotiated)
return "%s%s%s" % (chr(seg_type),chr(len(values)),''.join([v.pack(negotiated) for v in values]))
return ""
Expand Down Expand Up @@ -114,7 +115,7 @@ def string (self,aseq,aset):
string = '[ %d ]' % aseq[0]
else:
string = '[ %s %s]' % (aseq[0],'( %s ) ' % (' '.join([str(_) for _ in aset])))
elif lseq > 1 :
elif lseq > 1:
if lset:
string = '[ %s %s]' % ((' '.join([str(_) for _ in aseq])),'( %s ) ' % (' '.join([str(_) for _ in aset])))
else:
Expand All @@ -125,11 +126,11 @@ def string (self,aseq,aset):

def json (self,name):
match = {
# data , default representation
'as-path' : (self.as_seq , '[]'),
'as-set' : (self.as_set , ''),
'confederation-path' : (self.as_cseq , '[]'),
'confederation-set' : (self.as_cset , ''),
# data , default representation
'as-path': (self.as_seq, '[]'),
'as-set': (self.as_set, ''),
'confederation-path': (self.as_cseq, '[]'),
'confederation-set': (self.as_cset, ''),
}

data,default = match[name]
Expand All @@ -146,18 +147,18 @@ def __new_aspaths (cls,data,asn4,klass=None):
backup = data

unpacker = {
False : '!H',
True : '!L',
False: '!H',
True: '!L',
}
size = {
False: 2,
True : 4,
True: 4,
}
as_choice = {
ASPath.AS_SEQUENCE : as_seq,
ASPath.AS_SET : as_set,
ASPath.AS_CONFED_SEQUENCE : as_cseq,
ASPath.AS_CONFED_SET : as_cset,
ASPath.AS_SEQUENCE: as_seq,
ASPath.AS_SET: as_set,
ASPath.AS_CONFED_SEQUENCE: as_cseq,
ASPath.AS_CONFED_SET: as_cset,
}

upr = unpacker[asn4]
Expand Down Expand Up @@ -208,7 +209,7 @@ def unpack (cls,data,negotiated):

class AS4Path (ASPath):
ID = Attribute.ID.AS4_PATH
FLAG = Attribute.Flag.TRANSITIVE|Attribute.Flag.OPTIONAL
FLAG = Attribute.Flag.TRANSITIVE | Attribute.Flag.OPTIONAL
ASN4 = True

def pack (self,negotiated=None):
Expand Down
2 changes: 2 additions & 0 deletions lib/exabgp/bgp/message/update/attribute/atomicaggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from exabgp.bgp.message.update.attribute.attribute import Attribute
from exabgp.bgp.message.notification import Notify


# ========================================================== AtomicAggregate (6)
#

class AtomicAggregate (Attribute):
ID = Attribute.ID.ATOMIC_AGGREGATE
Expand Down
Loading

0 comments on commit aaded19

Please sign in to comment.