Conditions | 13 |
Total Lines | 74 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 AccountAPI.get_resources() 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 | # -*- coding: utf-8 -*- |
||
77 | def get_resources(self, service=None, name=None, resource=None, |
||
78 | include_locked=False): |
||
79 | """ Gets a list of resources for which the auth token is granted. |
||
80 | |||
81 | The permissions of a token is the sum of: |
||
82 | |||
83 | - token's permissions |
||
84 | - user's permissions |
||
85 | - user's roles permissions |
||
86 | - token's roles permissions |
||
87 | |||
88 | Roles are linked to organizations. |
||
89 | |||
90 | This function doesn't return the permissions retrieved from locked |
||
91 | organizations unless `include_locked` is True. Setting `include_lock` |
||
92 | to True is useful when you need to check the permissions of a token, |
||
93 | but don't care if the owner's organization is locked or not. |
||
94 | |||
95 | Note: If you - the reader - are not a staff member, this pydoc might be |
||
96 | a little confusing. Roles and permissions are not yet fully exposed by |
||
97 | our APIs, but I promise we will try to expose and document them very |
||
98 | soon. Anyway, if you have questions, we'll be glad to answer you guys! |
||
99 | """ |
||
100 | assert isinstance(include_locked, bool) |
||
101 | |||
102 | if not self.auth_token: |
||
103 | return [] |
||
104 | |||
105 | # GET /tokens/:id/permissions on account-api |
||
106 | try: |
||
107 | response = self.query() \ |
||
108 | .tokens(self.auth_token) \ |
||
109 | .permissions.get(include_locked=include_locked) |
||
110 | except slumber.exceptions.HttpClientError as exc: |
||
111 | if exc.response.status_code in (400, 404): |
||
112 | raise BadToken() |
||
113 | |||
114 | if exc.response.status_code == 410: |
||
115 | raise ExpiredToken() |
||
116 | |||
117 | raise |
||
118 | |||
119 | # Apply filters on effective permissions |
||
120 | # |
||
121 | # >>> print response.get('permissions') |
||
122 | # { |
||
123 | # 'service_name': { |
||
124 | # 'perm_name': ['resource1', 'resource2', ...], |
||
125 | # ... |
||
126 | # }, |
||
127 | # ... |
||
128 | # } |
||
129 | ret = [] |
||
130 | |||
131 | for (eff_service_name, |
||
132 | eff_service_perms) in response.get('permissions', {}).items(): |
||
133 | |||
134 | # Filter on service |
||
135 | if eff_service_name == service or service is None: |
||
136 | |||
137 | # Filter on perms |
||
138 | for (eff_perm_name, |
||
139 | eff_perm_resources) in eff_service_perms.items(): |
||
140 | |||
141 | if self.perm_matches(name, eff_perm_name): |
||
142 | |||
143 | # Filter on resources |
||
144 | ret.extend([ |
||
145 | eff_perm_resource |
||
146 | for eff_perm_resource in eff_perm_resources |
||
147 | if self.perm_matches(resource, eff_perm_resource) |
||
148 | ]) |
||
149 | |||
150 | return list(set(ret)) |
||
151 | |||
190 |