Conditions | 39 |
Total Lines | 152 |
Code Lines | 107 |
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 |
||
245 | def string(self, with_changes=True): |
||
246 | changes = '' |
||
247 | if with_changes: |
||
248 | changes += '\nstatic { ' |
||
249 | for change in self.rib.outgoing.queued_changes(): |
||
250 | changes += '\n\t\t%s' % change.extensive() |
||
251 | changes += '\n}' |
||
252 | |||
253 | families = '' |
||
254 | for afi, safi in self.families(): |
||
255 | families += '\n\t\t%s %s;' % (afi.name(), safi.name()) |
||
256 | |||
257 | nexthops = '' |
||
258 | for afi, safi, nexthop in self.nexthops(): |
||
259 | nexthops += '\n\t\t%s %s %s;' % (afi.name(), safi.name(), nexthop.name()) |
||
260 | |||
261 | addpaths = '' |
||
262 | for afi, safi in self.addpaths(): |
||
263 | addpaths += '\n\t\t%s %s;' % (afi.name(), safi.name()) |
||
264 | |||
265 | codes = Message.CODE |
||
266 | |||
267 | _extension_global = { |
||
268 | 'neighbor-changes': 'neighbor-changes', |
||
269 | 'negotiated': 'negotiated', |
||
270 | 'fsm': 'fsm', |
||
271 | 'signal': 'signal', |
||
272 | } |
||
273 | |||
274 | _extension_receive = { |
||
275 | 'receive-packets': 'packets', |
||
276 | 'receive-parsed': 'parsed', |
||
277 | 'receive-consolidate': 'consolidate', |
||
278 | 'receive-%s' % codes.NOTIFICATION.SHORT: 'notification', |
||
279 | 'receive-%s' % codes.OPEN.SHORT: 'open', |
||
280 | 'receive-%s' % codes.KEEPALIVE.SHORT: 'keepalive', |
||
281 | 'receive-%s' % codes.UPDATE.SHORT: 'update', |
||
282 | 'receive-%s' % codes.ROUTE_REFRESH.SHORT: 'refresh', |
||
283 | 'receive-%s' % codes.OPERATIONAL.SHORT: 'operational', |
||
284 | } |
||
285 | |||
286 | _extension_send = { |
||
287 | 'send-packets': 'packets', |
||
288 | 'send-parsed': 'parsed', |
||
289 | 'send-consolidate': 'consolidate', |
||
290 | 'send-%s' % codes.NOTIFICATION.SHORT: 'notification', |
||
291 | 'send-%s' % codes.OPEN.SHORT: 'open', |
||
292 | 'send-%s' % codes.KEEPALIVE.SHORT: 'keepalive', |
||
293 | 'send-%s' % codes.UPDATE.SHORT: 'update', |
||
294 | 'send-%s' % codes.ROUTE_REFRESH.SHORT: 'refresh', |
||
295 | 'send-%s' % codes.OPERATIONAL.SHORT: 'operational', |
||
296 | } |
||
297 | |||
298 | apis = '' |
||
299 | |||
300 | for process in self.api.get('processes', []): |
||
301 | _global = [] |
||
302 | _receive = [] |
||
303 | _send = [] |
||
304 | |||
305 | for api, name in _extension_global.items(): |
||
306 | _global.extend(['\t\t%s;\n' % name,] if process in self.api[api] else []) |
||
307 | |||
308 | for api, name in _extension_receive.items(): |
||
309 | _receive.extend(['\t\t\t%s;\n' % name,] if process in self.api[api] else []) |
||
310 | |||
311 | for api, name in _extension_send.items(): |
||
312 | _send.extend(['\t\t\t%s;\n' % name,] if process in self.api[api] else []) |
||
313 | |||
314 | _api = '\tapi {\n' |
||
315 | _api += '\t\tprocesses [ %s ];\n' % process |
||
316 | _api += ''.join(_global) |
||
317 | if _receive: |
||
318 | _api += '\t\treceive {\n' |
||
319 | _api += ''.join(_receive) |
||
320 | _api += '\t\t}\n' |
||
321 | if _send: |
||
322 | _api += '\t\tsend {\n' |
||
323 | _api += ''.join(_send) |
||
324 | _api += '\t\t}\n' |
||
325 | _api += '\t}\n' |
||
326 | |||
327 | apis += _api |
||
328 | |||
329 | returned = ( |
||
330 | 'neighbor %s {\n' |
||
331 | '\tdescription "%s";\n' |
||
332 | '\trouter-id %s;\n' |
||
333 | '\thost-name %s;\n' |
||
334 | '\tdomain-name %s;\n' |
||
335 | '\tlocal-address %s;\n' |
||
336 | '\tlocal-as %s;\n' |
||
337 | '\tpeer-as %s;\n' |
||
338 | '\thold-time %s;\n' |
||
339 | '\trate-limit %s;\n' |
||
340 | '\tmanual-eor %s;\n' |
||
341 | '%s%s%s%s%s%s%s%s%s%s%s\n' |
||
342 | '\tcapability {\n' |
||
343 | '%s%s%s%s%s%s%s%s%s\t}\n' |
||
344 | '\tfamily {%s\n' |
||
345 | '\t}\n' |
||
346 | '\tnexthop {%s\n' |
||
347 | '\t}\n' |
||
348 | '\tadd-path {%s\n' |
||
349 | '\t}\n' |
||
350 | '%s' |
||
351 | '%s' |
||
352 | '}' |
||
353 | % ( |
||
354 | self.peer_address, |
||
355 | self.description, |
||
356 | self.router_id, |
||
357 | self.host_name, |
||
358 | self.domain_name, |
||
359 | self.local_address if not self.auto_discovery else 'auto', |
||
360 | self.local_as, |
||
361 | self.peer_as, |
||
362 | self.hold_time, |
||
363 | 'disable' if self.rate_limit == 0 else self.rate_limit, |
||
364 | 'true' if self.manual_eor else 'false', |
||
365 | '\n\tpassive %s;\n' % ('true' if self.passive else 'false'), |
||
366 | '\n\tlisten %d;\n' % self.listen if self.listen else '', |
||
367 | '\n\tconnect %d;\n' % self.connect if self.connect else '', |
||
368 | '\tgroup-updates %s;\n' % ('true' if self.group_updates else 'false'), |
||
369 | '\tauto-flush %s;\n' % ('true' if self.flush else 'false'), |
||
370 | '\tadj-rib-in %s;\n' % ('true' if self.adj_rib_in else 'false'), |
||
371 | '\tadj-rib-out %s;\n' % ('true' if self.adj_rib_out else 'false'), |
||
372 | '\tmd5-password "%s";\n' % self.md5_password if self.md5_password else '', |
||
373 | '\tmd5-base64 %s;\n' |
||
374 | % ('true' if self.md5_base64 is True else 'false' if self.md5_base64 is False else 'auto'), |
||
375 | '\tmd5-ip "%s";\n' % self.md5_ip if not self.auto_discovery else '', |
||
376 | '\toutgoing-ttl %s;\n' % self.ttl_out if self.ttl_out else '', |
||
377 | '\tincoming-ttl %s;\n' % self.ttl_in if self.ttl_in else '', |
||
378 | '\t\tasn4 %s;\n' % ('enable' if self.asn4 else 'disable'), |
||
379 | '\t\troute-refresh %s;\n' % ('enable' if self.route_refresh else 'disable'), |
||
380 | '\t\tgraceful-restart %s;\n' % (self.graceful_restart if self.graceful_restart else 'disable'), |
||
381 | '\t\tnexthop %s;\n' % ('enable' if self.nexthop else 'disable'), |
||
382 | '\t\tadd-path %s;\n' % (AddPath.string[self.add_path] if self.add_path else 'disable'), |
||
383 | '\t\tmulti-session %s;\n' % ('enable' if self.multisession else 'disable'), |
||
384 | '\t\toperational %s;\n' % ('enable' if self.operational else 'disable'), |
||
385 | '\t\taigp %s;\n' % ('enable' if self.aigp else 'disable'), |
||
386 | families, |
||
387 | nexthops, |
||
388 | addpaths, |
||
389 | apis, |
||
390 | changes, |
||
391 | ) |
||
392 | ) |
||
393 | |||
394 | # '\t\treceive {\n%s\t\t}\n' % receive if receive else '', |
||
395 | # '\t\tsend {\n%s\t\t}\n' % send if send else '', |
||
396 | return returned.replace('\t', ' ') |
||
397 | |||
400 |