| Conditions | 39 |
| Total Lines | 149 |
| Code Lines | 106 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like exabgp.bgp.neighbor.Neighbor.string() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # encoding: utf-8 |
||
| 249 | def string (self, with_changes=True): |
||
| 250 | changes = '' |
||
| 251 | if with_changes: |
||
| 252 | changes += '\nstatic { ' |
||
| 253 | for changes in self.rib.incoming.queued_changes(): |
||
| 254 | changes += '\n\t\t%s' % changes.extensive() |
||
| 255 | changes += '\n}' |
||
| 256 | |||
| 257 | families = '' |
||
| 258 | for afi,safi in self.families(): |
||
| 259 | families += '\n\t\t%s %s;' % (afi.name(),safi.name()) |
||
| 260 | |||
| 261 | nexthops = '' |
||
| 262 | for afi, safi, nexthop in self.nexthops(): |
||
| 263 | nexthops += '\n\t\t%s %s %s;' % (afi.name(), safi.name(), nexthop.name()) |
||
| 264 | |||
| 265 | addpaths = '' |
||
| 266 | for afi,safi in self.addpaths(): |
||
| 267 | addpaths += '\n\t\t%s %s;' % (afi.name(),safi.name()) |
||
| 268 | |||
| 269 | codes = Message.CODE |
||
| 270 | |||
| 271 | _extension_global = { |
||
| 272 | 'neighbor-changes': 'neighbor-changes', |
||
| 273 | 'negotiated': 'negotiated', |
||
| 274 | 'fsm': 'fsm', |
||
| 275 | 'signal': 'signal', |
||
| 276 | } |
||
| 277 | |||
| 278 | _extension_receive = { |
||
| 279 | 'receive-packets': 'packets', |
||
| 280 | 'receive-parsed': 'parsed', |
||
| 281 | 'receive-consolidate': 'consolidate', |
||
| 282 | 'receive-%s' % codes.NOTIFICATION.SHORT: 'notification', |
||
| 283 | 'receive-%s' % codes.OPEN.SHORT: 'open', |
||
| 284 | 'receive-%s' % codes.KEEPALIVE.SHORT: 'keepalive', |
||
| 285 | 'receive-%s' % codes.UPDATE.SHORT: 'update', |
||
| 286 | 'receive-%s' % codes.ROUTE_REFRESH.SHORT: 'refresh', |
||
| 287 | 'receive-%s' % codes.OPERATIONAL.SHORT: 'operational', |
||
| 288 | } |
||
| 289 | |||
| 290 | _extension_send = { |
||
| 291 | 'send-packets': 'packets', |
||
| 292 | 'send-parsed': 'parsed', |
||
| 293 | 'send-consolidate': 'consolidate', |
||
| 294 | 'send-%s' % codes.NOTIFICATION.SHORT: 'notification', |
||
| 295 | 'send-%s' % codes.OPEN.SHORT: 'open', |
||
| 296 | 'send-%s' % codes.KEEPALIVE.SHORT: 'keepalive', |
||
| 297 | 'send-%s' % codes.UPDATE.SHORT: 'update', |
||
| 298 | 'send-%s' % codes.ROUTE_REFRESH.SHORT: 'refresh', |
||
| 299 | 'send-%s' % codes.OPERATIONAL.SHORT: 'operational', |
||
| 300 | } |
||
| 301 | |||
| 302 | apis = '' |
||
| 303 | |||
| 304 | for process in self.api.get('processes',[]): |
||
| 305 | _global = [] |
||
| 306 | _receive = [] |
||
| 307 | _send = [] |
||
| 308 | |||
| 309 | for api,name in _extension_global.items(): |
||
| 310 | _global.extend(['\t\t%s;\n' % name,] if process in self.api[api] else []) |
||
| 311 | |||
| 312 | for api,name in _extension_receive.items(): |
||
| 313 | _receive.extend(['\t\t\t%s;\n' % name,] if process in self.api[api] else []) |
||
| 314 | |||
| 315 | for api,name in _extension_send.items(): |
||
| 316 | _send.extend(['\t\t\t%s;\n' % name,] if process in self.api[api] else []) |
||
| 317 | |||
| 318 | _api = '\tapi {\n' |
||
| 319 | _api += '\t\tprocesses [ %s ];\n' % process |
||
| 320 | _api += ''.join(_global) |
||
| 321 | if _receive: |
||
| 322 | _api += '\t\treceive {\n' |
||
| 323 | _api += ''.join(_receive) |
||
| 324 | _api += '\t\t}\n' |
||
| 325 | if _send: |
||
| 326 | _api += '\t\tsend {\n' |
||
| 327 | _api += ''.join(_send) |
||
| 328 | _api += '\t\t}\n' |
||
| 329 | _api += '\t}\n' |
||
| 330 | |||
| 331 | apis += _api |
||
| 332 | |||
| 333 | returned = \ |
||
| 334 | 'neighbor %s {\n' \ |
||
| 335 | '\tdescription "%s";\n' \ |
||
| 336 | '\trouter-id %s;\n' \ |
||
| 337 | '\thost-name %s;\n' \ |
||
| 338 | '\tdomain-name %s;\n' \ |
||
| 339 | '\tlocal-address %s;\n' \ |
||
| 340 | '\tlocal-as %s;\n' \ |
||
| 341 | '\tpeer-as %s;\n' \ |
||
| 342 | '\thold-time %s;\n' \ |
||
| 343 | '\trate-limit %s;\n' \ |
||
| 344 | '\tmanual-eor %s;\n' \ |
||
| 345 | '%s%s%s%s%s%s%s%s%s%s%s\n' \ |
||
| 346 | '\tcapability {\n' \ |
||
| 347 | '%s%s%s%s%s%s%s%s%s\t}\n' \ |
||
| 348 | '\tfamily {%s\n' \ |
||
| 349 | '\t}\n' \ |
||
| 350 | '\tnexthop {%s\n' \ |
||
| 351 | '\t}\n' \ |
||
| 352 | '\tadd-path {%s\n' \ |
||
| 353 | '\t}\n' \ |
||
| 354 | '%s' \ |
||
| 355 | '%s' \ |
||
| 356 | '}' % ( |
||
| 357 | self.peer_address, |
||
| 358 | self.description, |
||
| 359 | self.router_id, |
||
| 360 | self.host_name, |
||
| 361 | self.domain_name, |
||
| 362 | self.local_address if not self.auto_discovery else 'auto', |
||
| 363 | self.local_as, |
||
| 364 | self.peer_as, |
||
| 365 | self.hold_time, |
||
| 366 | 'disable' if self.rate_limit == 0 else self.rate_limit, |
||
| 367 | 'true' if self.manual_eor else 'false', |
||
| 368 | '\n\tpassive %s;\n' % ('true' if self.passive else 'false'), |
||
| 369 | '\n\tlisten %d;\n' % self.listen if self.listen else '', |
||
| 370 | '\n\tconnect %d;\n' % self.connect if self.connect else '', |
||
| 371 | '\tgroup-updates %s;\n' % ('true' if self.group_updates else 'false'), |
||
| 372 | '\tauto-flush %s;\n' % ('true' if self.flush else 'false'), |
||
| 373 | '\tadj-rib-in %s;\n' % ('true' if self.adj_rib_in else 'false'), |
||
| 374 | '\tadj-rib-out %s;\n' % ('true' if self.adj_rib_out else 'false'), |
||
| 375 | '\tmd5-password "%s";\n' % self.md5_password if self.md5_password else '', |
||
| 376 | '\tmd5-base64 %s;\n' % ('true' if self.md5_base64 is True else 'false' if self.md5_base64 is False else 'auto'), |
||
| 377 | '\tmd5-ip "%s";\n' % self.md5_ip if not self.auto_discovery else '', |
||
| 378 | '\toutgoing-ttl %s;\n' % self.ttl_out if self.ttl_out else '', |
||
| 379 | '\tincoming-ttl %s;\n' % self.ttl_in if self.ttl_in else '', |
||
| 380 | '\t\tasn4 %s;\n' % ('enable' if self.asn4 else 'disable'), |
||
| 381 | '\t\troute-refresh %s;\n' % ('enable' if self.route_refresh else 'disable'), |
||
| 382 | '\t\tgraceful-restart %s;\n' % (self.graceful_restart if self.graceful_restart else 'disable'), |
||
| 383 | '\t\tnexthop %s;\n' % ('enable' if self.nexthop else 'disable'), |
||
| 384 | '\t\tadd-path %s;\n' % (AddPath.string[self.add_path] if self.add_path else 'disable'), |
||
| 385 | '\t\tmulti-session %s;\n' % ('enable' if self.multisession else 'disable'), |
||
| 386 | '\t\toperational %s;\n' % ('enable' if self.operational else 'disable'), |
||
| 387 | '\t\taigp %s;\n' % ('enable' if self.aigp else 'disable'), |
||
| 388 | families, |
||
| 389 | nexthops, |
||
| 390 | addpaths, |
||
| 391 | apis, |
||
| 392 | changes |
||
| 393 | ) |
||
| 394 | |||
| 395 | # '\t\treceive {\n%s\t\t}\n' % receive if receive else '', |
||
| 396 | # '\t\tsend {\n%s\t\t}\n' % send if send else '', |
||
| 397 | return returned.replace('\t',' ') |
||
| 398 | |||
| 401 |