Conditions | 14 |
Total Lines | 81 |
Code Lines | 50 |
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.template.get_template() 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 | # SPDX-License-Identifier: LGPL-3.0-only |
||
34 | def get_template(obj, path, ext, template): |
||
35 | """Return the correct template. |
||
36 | |||
37 | Return correct template according to the published type. |
||
38 | If a template has been specified, use that. Otherwise, use doorstop's |
||
39 | built-in templates. |
||
40 | |||
41 | Create the output folder and template folder. |
||
42 | """ |
||
43 | |||
44 | # Set assets, ouput and template folders. |
||
45 | if is_tree(obj): |
||
46 | assets_dir = os.path.join(path, Document.ASSETS) # path is a directory name |
||
47 | document_template = obj.documents[0].template |
||
48 | template_dir = os.path.join(path, "template") |
||
49 | output_dir = path |
||
50 | else: |
||
51 | assets_dir = os.path.join( |
||
52 | os.path.dirname(path), Document.ASSETS |
||
53 | ) # path is a filename |
||
54 | document_template = obj.template |
||
55 | template_dir = os.path.join(os.path.dirname(path), "template") |
||
56 | output_dir = os.path.dirname(path) |
||
57 | |||
58 | # Check for custom template and verify that it is available. |
||
59 | if template and not document_template: |
||
60 | raise common.DoorstopError( |
||
61 | "Template flag set, but no 'template' folder was found." |
||
62 | ) |
||
63 | |||
64 | # Get the builtin templates. |
||
65 | template_assets = os.path.join(os.path.dirname(__file__), "files", "templates") |
||
66 | builtin_template = None |
||
67 | # Check extension and set template folder accordingly. |
||
68 | if ext == ".md": |
||
69 | template_assets = template_assets + "/markdown" |
||
70 | elif ext == ".tex": |
||
71 | template_assets = template_assets + "/latex" |
||
72 | builtin_template = "doorstop" |
||
73 | elif ext == ".txt": |
||
74 | template_assets = template_assets + "/text" |
||
75 | else: |
||
76 | template_assets = template_assets + "/html" |
||
77 | builtin_template = HTMLTEMPLATE |
||
78 | |||
79 | # Remove existing templates and assets first. |
||
80 | if os.path.isdir(assets_dir): |
||
81 | log.info("Deleting contents of assets directory %s", assets_dir) |
||
82 | common.delete_contents(assets_dir) |
||
83 | if os.path.isdir(template_dir): |
||
84 | log.info("Deleting contents of template directory %s", template_dir) |
||
85 | common.delete(template_dir) |
||
86 | |||
87 | # Create the output path only. |
||
88 | if not os.path.isdir(output_dir): |
||
89 | os.makedirs(output_dir) |
||
90 | |||
91 | # Copy template from document if it exists and template is given. |
||
92 | if document_template and template: |
||
93 | os.makedirs(template_dir) |
||
94 | log.info( |
||
95 | "Copying %s to %s", |
||
96 | document_template, |
||
97 | os.path.join(os.path.dirname(path), "template"), |
||
98 | ) |
||
99 | common.copy_dir_contents(document_template, template_dir) |
||
100 | # Only create template_dir if template actually exists. |
||
101 | elif os.path.isdir(template_assets): |
||
102 | os.makedirs(template_dir) |
||
103 | log.info( |
||
104 | "Copying %s to %s", |
||
105 | template_assets, |
||
106 | os.path.join(os.path.dirname(path), "template"), |
||
107 | ) |
||
108 | common.copy_dir_contents(template_assets, template_dir) |
||
109 | |||
110 | # Return correct template and assets folder. |
||
111 | if not template: |
||
112 | return assets_dir, builtin_template |
||
113 | |||
114 | return assets_dir, template |
||
115 | |||
172 |