| Conditions | 19 |
| Total Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 clipboard_paste() 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 | #!/usr/bin/env python |
||
| 112 | @actions.route('/clipboard/paste', defaults={'path': ''}) |
||
| 113 | @actions.route('/clipboard/paste/<path:path>') |
||
| 114 | def clipboard_paste(path): |
||
| 115 | |||
| 116 | def copy(target, node, join_fnc=os.path.join): |
||
| 117 | dest = join_fnc( |
||
| 118 | target.path, |
||
| 119 | target.choose_filename(node.name) |
||
| 120 | ) |
||
| 121 | if node.is_directory: |
||
| 122 | shutil.copytree(node.path, dest) |
||
| 123 | else: |
||
| 124 | shutil.copy2(node.path, dest) |
||
| 125 | |||
| 126 | def cut(target, node, join_fnc=os.path.join): |
||
| 127 | if node.parent.path != target.path: |
||
| 128 | dest = join_fnc( |
||
| 129 | target.path, |
||
| 130 | target.choose_filename(node.name) |
||
| 131 | ) |
||
| 132 | shutil.move(node.path, dest) |
||
| 133 | |||
| 134 | try: |
||
| 135 | directory = Node.from_urlpath(path) |
||
| 136 | except OutsideDirectoryBase: |
||
| 137 | return NotFound() |
||
| 138 | |||
| 139 | if ( |
||
| 140 | not directory.is_directory or |
||
| 141 | not directory.can_upload or |
||
| 142 | directory.is_excluded |
||
| 143 | ): |
||
| 144 | return NotFound() |
||
| 145 | |||
| 146 | response = redirect(url_for('browse', path=directory.urlpath)) |
||
| 147 | clipboard = Clipboard.from_request() |
||
| 148 | mode = clipboard.mode |
||
| 149 | clipboard.mode = 'paste' # disables exclusion |
||
| 150 | |||
| 151 | nodes = [node for node in map(Node.from_urlpath, clipboard)] |
||
| 152 | |||
| 153 | if mode == 'cut': |
||
| 154 | paste_fnc = cut |
||
| 155 | for node in nodes: |
||
| 156 | if node.is_excluded or not node.can_remove: |
||
| 157 | raise InvalidClipboardItemError( |
||
| 158 | path=directory.path, |
||
| 159 | item=node.urlpath |
||
| 160 | ) |
||
| 161 | elif mode == 'copy': |
||
| 162 | paste_fnc = copy |
||
| 163 | for node in nodes: |
||
| 164 | if node.is_excluded: |
||
| 165 | raise InvalidClipboardItemError( |
||
| 166 | path=directory.path, |
||
| 167 | item=node.urlpath |
||
| 168 | ) |
||
| 169 | else: |
||
| 170 | raise InvalidClipboardModeError(path=directory.path, mode=mode) |
||
| 171 | |||
| 172 | for node in nodes: |
||
| 173 | try: |
||
| 174 | paste_fnc(directory, node) |
||
| 175 | except FileNotFoundError: |
||
| 176 | raise MissingClipboardItemError( |
||
| 177 | path=directory.path, |
||
| 178 | item=node.urlpath |
||
| 179 | ) |
||
| 180 | |||
| 181 | clipboard.clear() |
||
| 182 | clipboard.to_response(response) |
||
| 183 | return response |
||
| 184 | |||
| 266 |