| Total Complexity | 5 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | from coala_utils.decorators import generate_eq |
||
| 4 | @generate_eq("filename") |
||
| 5 | class Fileproxy: |
||
| 6 | """ |
||
| 7 | The file proxy is a wrapper object to wrap every file. The file proxy is an |
||
| 8 | object that contains properties about the file currently processed (such as |
||
| 9 | the filename). It contains: |
||
| 10 | * The filename, this is the starting point for every other attribute. |
||
| 11 | * The content of the file as a string. |
||
| 12 | * The content of the file, line-splitted. |
||
| 13 | |||
| 14 | Note: Other properties can be included later as and when required. |
||
| 15 | |||
| 16 | The equality of the object is checked just based on it's filename. |
||
| 17 | """ |
||
| 18 | |||
| 19 | def __init__(self, filename): |
||
| 20 | """ |
||
| 21 | Constructs a new fileproxy object. |
||
| 22 | :param filename: The name of the file to load. |
||
| 23 | """ |
||
| 24 | self.filename = filename |
||
| 25 | with open(self.filename, "r", encoding="utf-8") as filehandle: |
||
| 26 | self.content = filehandle.read() |
||
| 27 | self.lines = tuple(self.content.splitlines(True)) |
||
| 28 | |||
| 29 | def __iter__(self): |
||
| 30 | """ |
||
| 31 | :return: A list of lines of the file to iterate over. |
||
| 32 | """ |
||
| 33 | return iter(self.lines) |
||
| 34 | |||
| 35 | def __hash__(self): |
||
| 36 | """ |
||
| 37 | :return: Only hashes the filename. |
||
| 38 | """ |
||
| 39 | return hash(self.filename) # pragma: no cover |
||
| 40 | |||
| 41 | def __str__(self): |
||
| 42 | """ |
||
| 43 | :return: A string of contents of the whole file. |
||
| 44 | """ |
||
| 45 | return self.content |
||
| 46 |