| Conditions | 8 |
| Total Lines | 77 |
| Lines | 0 |
| Ratio | 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:
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 37 | |||
| 38 | class PolicyTypeListCommand(resource.ResourceListCommand): |
||
| 39 | display_attributes = ['id', 'resource_type', 'name', 'description'] |
||
| 40 | |||
| 41 | def __init__(self, resource, *args, **kwargs): |
||
|
|
|||
| 42 | super(PolicyTypeListCommand, self).__init__(resource, *args, **kwargs) |
||
| 43 | |||
| 44 | self.parser.add_argument('-r', '--resource-type', type=str, dest='resource_type', |
||
| 45 | help='Return policy types for the resource type.') |
||
| 46 | |||
| 47 | @resource.add_auth_token_to_kwargs_from_cli |
||
| 48 | def run(self, args, **kwargs): |
||
| 49 | if args.resource_type: |
||
| 50 | filters = {'resource_type': args.resource_type} |
||
| 51 | filters.update(**kwargs) |
||
| 52 | return self.manager.query(**filters) |
||
| 53 | else: |
||
| 54 | return self.manager.get_all(**kwargs) |
||
| 55 | |||
| 56 | |||
| 57 | class PolicyTypeGetCommand(resource.ResourceGetCommand): |
||
| 58 | pk_argument_name = 'ref_or_id' |
||
| 59 | |||
| 60 | def get_resource(self, ref_or_id, **kwargs): |
||
| 61 | return self.get_resource_by_ref_or_id(ref_or_id=ref_or_id, **kwargs) |
||
| 62 | |||
| 63 | |||
| 64 | class PolicyBranch(resource.ResourceBranch): |
||
| 65 | |||
| 66 | def __init__(self, description, app, subparsers, parent_parser=None): |
||
| 67 | super(PolicyBranch, self).__init__( |
||
| 68 | models.Policy, description, app, subparsers, |
||
| 69 | parent_parser=parent_parser, |
||
| 70 | commands={ |
||
| 71 | 'list': PolicyListCommand, |
||
| 72 | 'get': PolicyGetCommand, |
||
| 73 | 'update': PolicyUpdateCommand, |
||
| 74 | 'delete': PolicyDeleteCommand |
||
| 75 | }) |
||
| 76 | |||
| 77 | |||
| 78 | class PolicyListCommand(resource.ContentPackResourceListCommand): |
||
| 79 | display_attributes = ['ref', 'resource_ref', 'policy_type'] |
||
| 80 | |||
| 81 | def __init__(self, resource, *args, **kwargs): |
||
| 82 | super(PolicyListCommand, self).__init__(resource, *args, **kwargs) |
||
| 83 | |||
| 84 | self.parser.add_argument('-r', '--resource-ref', type=str, dest='resource_ref', |
||
| 85 | help='Return policies for the resource ref.') |
||
| 86 | self.parser.add_argument('-pt', '--policy-type', type=str, dest='policy_type', |
||
| 87 | help='Return policies of the policy type.') |
||
| 88 | |||
| 89 | @resource.add_auth_token_to_kwargs_from_cli |
||
| 90 | def run(self, args, **kwargs): |
||
| 91 | if args.resource_ref or args.policy_type: |
||
| 92 | filters = {} |
||
| 93 | |||
| 94 | if args.resource_ref: |
||
| 95 | filters['resource_ref'] = args.resource_ref |
||
| 96 | |||
| 97 | if args.policy_type: |
||
| 98 | filters['policy_type'] = args.policy_type |
||
| 99 | |||
| 100 | filters.update(**kwargs) |
||
| 101 | |||
| 102 | return self.manager.query(**filters) |
||
| 103 | else: |
||
| 104 | return self.manager.get_all(**kwargs) |
||
| 105 | |||
| 106 | |||
| 107 | class PolicyGetCommand(resource.ContentPackResourceGetCommand): |
||
| 108 | display_attributes = ['all'] |
||
| 109 | attribute_display_order = ['id', 'ref', 'pack', 'name', 'description', |
||
| 110 | 'enabled', 'resource_ref', 'policy_type', |
||
| 111 | 'parameters'] |
||
| 112 | |||
| 113 | |||
| 114 | class PolicyUpdateCommand(resource.ContentPackResourceUpdateCommand): |
||
| 120 |
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: