| Conditions | 9 |
| Total Lines | 72 |
| 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:
| 1 | #!/usr/bin/python |
||
| 9 | def add(filepath, transient=False, provenance=None, |
||
| 10 | dependencies=Dependencies()): |
||
| 11 | """ |
||
| 12 | Simply register the file. |
||
| 13 | |||
| 14 | Inspects the file and makes it known to the provenance data, such that |
||
| 15 | image files can be logged that have been created using this file. |
||
| 16 | Useful also for temporary files. |
||
| 17 | |||
| 18 | Example: |
||
| 19 | (provenance, status) = niprov.add('/path/to/my.nii') |
||
| 20 | |||
| 21 | Args: |
||
| 22 | filepath (str): Path to the newly created file. |
||
| 23 | transient (bool, optional): Set this to True to indicate that the file |
||
| 24 | is only temporary and future checks should not expect it to be |
||
| 25 | physically present. Defaults to False, assuming that the file |
||
| 26 | remains. |
||
| 27 | provenance (dict, optional): Add the key-value pairs in this dictionary |
||
| 28 | to the provenance record for the new file. |
||
| 29 | |||
| 30 | Returns: |
||
| 31 | tuple: Tuple of new provenance and status. Status is a string with one |
||
| 32 | of the following values: |
||
| 33 | 'new': File was not known yet and has been added. |
||
| 34 | 'series': The file was deemed part of a series and has been added. |
||
| 35 | 'failed': There was an error inspecting the file. |
||
| 36 | 'known': The file was already known to niprov, nothing happened. |
||
| 37 | 'dryrun': Function called with config.dryrun, database not touched. |
||
| 38 | """ |
||
| 39 | config = dependencies.getConfiguration() |
||
| 40 | file = dependencies.getFileFactory() |
||
| 41 | repository = dependencies.getRepository() |
||
| 42 | listener = dependencies.getListener() |
||
| 43 | filesys = dependencies.getFilesystem() |
||
| 44 | |||
| 45 | if provenance is None: |
||
| 46 | provenance = {} |
||
| 47 | provenance['transient'] = transient |
||
| 48 | provenance['added'] = datetime.now() |
||
| 49 | provenance['id'] = shortuuid.uuid()[:6] |
||
| 50 | |||
| 51 | filepath = os.path.abspath(filepath) |
||
| 52 | img = file.locatedAt(filepath, provenance=provenance) |
||
| 53 | if config.dryrun: |
||
| 54 | status = 'dryrun' |
||
| 55 | elif repository.knows(img): |
||
| 56 | listener.knownFile(img.path) |
||
| 57 | img = repository.byLocation(img.location.toString()) |
||
| 58 | status = 'known' |
||
| 59 | elif repository.knowsSeries(img): |
||
| 60 | series = repository.getSeries(img) |
||
| 61 | series.addFile(img) |
||
| 62 | repository.update(series) |
||
| 63 | listener.fileFoundInSeries(img, series) |
||
| 64 | status = 'series' |
||
| 65 | else: |
||
| 66 | if not transient: |
||
| 67 | if not filesys.fileExists(filepath): |
||
| 68 | raise IOError(errno.ENOENT, 'File not found', filepath) |
||
| 69 | try: |
||
| 70 | img.inspect() |
||
| 71 | except: |
||
| 72 | listener.fileError(img.path) |
||
| 73 | status = 'failed' |
||
| 74 | return (img, status) |
||
| 75 | if config.attach: |
||
| 76 | img.attach(config.attach_format) |
||
| 77 | repository.add(img) |
||
| 78 | listener.fileFound(img) |
||
| 79 | status = 'new' |
||
| 80 | return (img, status) |
||
| 81 | |||
| 85 |