| Conditions | 12 |
| Total Lines | 107 |
| 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:
Complex classes like src.docmanager.xmlrenderer() 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 | # |
||
| 177 | def xmlrenderer(data, **kwargs): # pylint: disable=unused-argument |
||
| 178 | """Output as XML |
||
| 179 | |||
| 180 | :param list data: Filename with properties |
||
| 181 | syntax: [(FILENAME, {PROPERTIES}), ...] |
||
| 182 | :param dict kwargs: for further customizations |
||
| 183 | :return: rendered output |
||
| 184 | :rtype: str |
||
| 185 | """ |
||
| 186 | |||
| 187 | root = etree.Element("docmanager") |
||
| 188 | tree = root.getroottree() |
||
| 189 | |||
| 190 | args = kwargs["args"] |
||
| 191 | index = 0 |
||
| 192 | |||
| 193 | if args.action == "alias": |
||
| 194 | aliaseselem = etree.Element("aliases") |
||
| 195 | root.append(aliaseselem) |
||
| 196 | |||
| 197 | for name in data['aliases'].keys(): |
||
| 198 | value = data['aliases'][name] |
||
| 199 | |||
| 200 | root[0].append(etree.Element("alias")) |
||
| 201 | |||
| 202 | child = root[0][index] |
||
| 203 | child.set("name", name) |
||
| 204 | |||
| 205 | child.text = value |
||
| 206 | |||
| 207 | index += 1 |
||
| 208 | |||
| 209 | elif args.action == "get": |
||
| 210 | fileselem = etree.Element("files") |
||
| 211 | root.append(fileselem) |
||
| 212 | |||
| 213 | for i in data['data']: |
||
| 214 | if len(i[1]): |
||
| 215 | filename = i[0] |
||
| 216 | |||
| 217 | root[0].append(etree.Element("file")) |
||
| 218 | |||
| 219 | child = root[0][index] |
||
| 220 | child.set("name", filename) |
||
| 221 | |||
| 222 | for x in i[1]: |
||
| 223 | prop = x |
||
| 224 | value = i[1][x] |
||
| 225 | |||
| 226 | elem = etree.Element("property") |
||
| 227 | elem.set("name", prop) |
||
| 228 | elem.text = value |
||
| 229 | |||
| 230 | child.append(elem) |
||
| 231 | |||
| 232 | index += 1 |
||
| 233 | |||
| 234 | elif args.action == "get_attr": |
||
| 235 | """ |
||
| 236 | Output structure: |
||
| 237 | <!DOCTYPE docmanager> |
||
| 238 | <docmanager> |
||
| 239 | <files> |
||
| 240 | <file name="example.xml"> |
||
| 241 | <property name="priority/test/hello"> |
||
| 242 | <attribute name="someattribute">Hello</attribute> |
||
| 243 | <attribute name="attr2">Hallo</attribute> |
||
| 244 | </property> |
||
| 245 | <property name="status"> |
||
| 246 | <attribute name="attr3">Hi</attribute> |
||
| 247 | </property> |
||
| 248 | </file> |
||
| 249 | </files> |
||
| 250 | </docmanager> |
||
| 251 | """ |
||
| 252 | |||
| 253 | fileselem = etree.Element("files") |
||
| 254 | root.append(fileselem) |
||
| 255 | |||
| 256 | if data['data']: |
||
| 257 | index = 0 |
||
| 258 | |||
| 259 | for i in data['data']: |
||
| 260 | filename = i |
||
| 261 | root[0].append(etree.Element("file")) |
||
| 262 | |||
| 263 | child = root[0][index] |
||
| 264 | child.set("name", filename) |
||
| 265 | |||
| 266 | for prop in data['data'][i]: |
||
| 267 | propelem = etree.Element("property") |
||
| 268 | child.append(propelem) |
||
| 269 | propelem.set("name", prop) |
||
| 270 | |||
| 271 | for key, value in data['data'][i][prop].items(): |
||
| 272 | elem = etree.Element("attribute") |
||
| 273 | elem.set("name", key) |
||
| 274 | elem.text = value |
||
| 275 | |||
| 276 | propelem.append(elem) |
||
| 277 | |||
| 278 | index += 1 |
||
| 279 | |||
| 280 | print(etree.tostring(tree, |
||
| 281 | encoding="unicode", |
||
| 282 | pretty_print=True, |
||
| 283 | doctype="<!DOCTYPE docmanager>")) |
||
| 284 | |||
| 305 |