Conditions | 11 |
Total Lines | 90 |
Code Lines | 46 |
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 doorstop.core.publisher.publish() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # SPDX-License-Identifier: LGPL-3.0-only |
||
18 | def publish( |
||
19 | obj, |
||
20 | path, |
||
21 | ext=None, |
||
22 | linkify=None, |
||
23 | index=None, |
||
24 | matrix=None, |
||
25 | template=None, |
||
26 | toc=True, |
||
27 | **kwargs, |
||
28 | ): |
||
29 | """Publish an object to a given format. |
||
30 | |||
31 | The function can be called in two ways: |
||
32 | |||
33 | 1. document or item-like object + output file path |
||
34 | 2. tree-like object + output directory path |
||
35 | |||
36 | :param obj: (1) Item, list of Items, Document or (2) Tree |
||
37 | :param path: (1) output file path or (2) output directory path |
||
38 | :param ext: file extension to override output extension |
||
39 | :param linkify: turn links into hyperlinks (for Markdown, HTML or LaTeX) |
||
40 | :param index: create an index.html (for HTML) |
||
41 | :param matrix: create a traceability matrix, traceability.csv |
||
42 | |||
43 | :raises: :class:`doorstop.common.DoorstopError` for unknown file formats |
||
44 | |||
45 | :return: output location if files created, else None |
||
46 | |||
47 | """ |
||
48 | # Check that we have something to publish first. |
||
49 | if is_tree(obj): |
||
50 | if len(obj) == 0: |
||
51 | raise DoorstopError("nothing to publish") |
||
52 | |||
53 | # Determine the output format |
||
54 | ext = ext or os.path.splitext(path)[-1] or ".html" |
||
55 | publisher = check(ext, obj=obj) |
||
56 | |||
57 | # Setup publisher. |
||
58 | publisher.setPath(path) |
||
59 | publisher.setup(linkify, index, matrix) |
||
60 | |||
61 | # Process templates. |
||
62 | publisher.processTemplates(template) |
||
63 | log.info("Template = {}".format(publisher.getTemplate())) |
||
64 | # Run all preparations. |
||
65 | publisher.preparePublish() |
||
66 | |||
67 | # Publish documents |
||
68 | count = 0 |
||
69 | for obj2, path2 in iter_documents(obj, path, ext): |
||
70 | count += 1 |
||
71 | # Run all special actions. |
||
72 | publisher.publishAction(obj2, path2) |
||
73 | |||
74 | # Publish content to the specified path |
||
75 | log.info("publishing to {}...".format(publisher.getDocumentPath())) |
||
76 | lines = publish_lines( |
||
77 | obj2, |
||
78 | ext, |
||
79 | publisher=publisher, |
||
80 | linkify=publisher.getLinkify(), |
||
81 | template=publisher.getTemplate(), |
||
82 | toc=toc, |
||
83 | **kwargs, |
||
84 | ) |
||
85 | common.write_lines( |
||
86 | lines, publisher.getDocumentPath(), end=settings.WRITE_LINESEPERATOR |
||
87 | ) |
||
88 | if obj2.copy_assets(publisher.getAssetsPath()): |
||
89 | log.info( |
||
90 | "Copied assets from %s to %s", obj.assets, publisher.getAssetsPath() |
||
91 | ) |
||
92 | |||
93 | # Create index |
||
94 | if publisher.getIndex(): |
||
95 | publisher.create_index(path, tree=obj if is_tree(obj) else None) |
||
96 | |||
97 | # Create traceability matrix |
||
98 | if (publisher.getIndex() or ext == ".tex") and (publisher.getMatrix()): |
||
99 | publisher.create_matrix(path) |
||
100 | |||
101 | # Run all concluding operations. |
||
102 | publisher.concludePublish() |
||
103 | |||
104 | # Return the published path |
||
105 | msg = "published to {} file{}".format(count, "s" if count > 1 else "") |
||
106 | log.info(msg) |
||
107 | return path |
||
108 | |||
151 |