kapsikkum-unmanic – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #!/usr/bin/env python3 |
2 | # -*- coding: utf-8 -*- |
||
3 | |||
4 | """ |
||
5 | unmanic.fileinfo.py |
||
6 | |||
7 | Written by: Wagner Caixeta <wagner.caixeta@gmail.com> |
||
8 | Date: 23 Apr 2020, (15:25:00 PM) |
||
9 | |||
10 | Copyright: |
||
11 | Copyright (C) Josh Sunnex - All Rights Reserved |
||
12 | |||
13 | Permission is hereby granted, free of charge, to any person obtaining a copy |
||
14 | of this software and associated documentation files (the "Software"), to deal |
||
15 | in the Software without restriction, including without limitation the rights |
||
16 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
17 | copies of the Software, and to permit persons to whom the Software is |
||
18 | furnished to do so, subject to the following conditions: |
||
19 | |||
20 | The above copyright notice and this permission notice shall be included in all |
||
21 | copies or substantial portions of the Software. |
||
22 | |||
23 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
||
24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
||
26 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
||
27 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
||
28 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
||
29 | OR OTHER DEALINGS IN THE SOFTWARE. |
||
30 | |||
31 | """ |
||
32 | |||
33 | import os |
||
34 | import re |
||
35 | |||
36 | """ |
||
37 | |||
38 | An object to represent file_info (Filebot pattern to keep filename history) |
||
39 | |||
40 | """ |
||
41 | |||
42 | |||
43 | class FileInfo(object): |
||
44 | """ |
||
45 | FileInfo |
||
46 | |||
47 | Attributes: |
||
48 | path (string): Path of file_info |
||
49 | entries (array of Entry): Filename history |
||
50 | """ |
||
51 | |||
52 | def __init__(self, path): |
||
53 | self.path = path |
||
54 | self.entries = [] |
||
55 | |||
56 | def append(self, newname, originalname): |
||
57 | self.entries.append(Entry(newname, self._find_oldest_name(originalname))) |
||
58 | |||
59 | def load(self): |
||
60 | if os.path.exists(self.path): |
||
61 | try: |
||
62 | f = open(self.path) |
||
63 | self.entries = [] |
||
64 | for line in f: |
||
65 | m = re.search('(.+)="(.+)"', line) |
||
66 | if m.group(1) is not None and m.group(2) is not None: |
||
67 | self.entries.append(Entry(m.group(1), m.group(2))) |
||
68 | except IOError: |
||
69 | print("File not accessible") |
||
70 | finally: |
||
71 | f.close() |
||
72 | |||
73 | def save(self): |
||
74 | try: |
||
75 | f = open(self.path, "w") |
||
76 | for entry in self.entries: |
||
77 | f.write('%s="%s"\n' % (entry.newname, entry.originalname)) |
||
78 | except IOError: |
||
79 | print("File not accessible") |
||
80 | finally: |
||
81 | f.close() |
||
82 | |||
83 | def _find_oldest_name(self, filename): |
||
84 | for entry in self.entries: |
||
85 | if entry.newname == filename: |
||
86 | return entry.originalname |
||
87 | return filename |
||
88 | |||
89 | |||
90 | """ |
||
91 | |||
92 | An object to keep a pair of newname and originalname |
||
93 | |||
94 | """ |
||
95 | |||
96 | |||
97 | class Entry(object): |
||
98 | """ |
||
99 | Entry |
||
100 | |||
101 | Attributes: |
||
102 | newname (string): New name of file |
||
103 | originalname (string): Old name of file |
||
104 | """ |
||
105 | |||
106 | def __init__(self, newname, originalname): |
||
107 | self.newname = newname |
||
108 | self.originalname = originalname |