Total Complexity | 43 |
Total Lines | 144 |
Duplicated Lines | 0 % |
Complex classes like browsepy.File 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 |
||
28 | class File(object): |
||
29 | re_charset = re.compile('; charset=(?P<charset>[^;]+)') |
||
30 | def __init__(self, path=None, app=None): |
||
31 | self.path = path |
||
32 | self.app = current_app if app is None else app |
||
33 | |||
34 | def remove(self): |
||
35 | if not self.can_remove: |
||
36 | raise OutsideRemovableBase("File outside removable base") |
||
37 | if self.is_directory: |
||
38 | shutil.rmtree(self.path) |
||
39 | else: |
||
40 | os.unlink(self.path) |
||
41 | |||
42 | def download(self): |
||
43 | if self.is_directory: |
||
44 | stream = TarFileStream( |
||
45 | self.path, |
||
46 | self.app.config["directory_tar_buffsize"] |
||
47 | ) |
||
48 | return Response(stream, mimetype="application/octet-stream") |
||
49 | directory, name = os.path.split(self.path) |
||
50 | return send_from_directory(directory, name, as_attachment=True) |
||
51 | |||
52 | def contains(self, filename): |
||
53 | return os.path.exists(os.path.join(self.path, filename)) |
||
54 | |||
55 | def choose_filename(self, filename, attempts=999): |
||
56 | new_filename = filename |
||
57 | for attempt in range(2, attempts+1): |
||
58 | if not self.contains(new_filename): |
||
59 | return new_filename |
||
60 | new_filename = alternative_filename(filename, attempt) |
||
61 | while self.contains(new_filename): |
||
62 | new_filename = alternative_filename(filename) |
||
63 | return new_filename |
||
64 | |||
65 | @property |
||
66 | def plugin_manager(self): |
||
67 | return self.app.extensions['plugin_manager'] |
||
68 | |||
69 | @property |
||
70 | def actions(self): |
||
71 | return self.plugin_manager.get_actions(self.mimetype) |
||
72 | |||
73 | @cached_property |
||
74 | def can_download(self): |
||
75 | return self.app.config['directory_downloadable'] or not self.is_directory |
||
76 | |||
77 | @cached_property |
||
78 | def can_remove(self): |
||
79 | dirbase = self.app.config["directory_remove"] |
||
80 | if dirbase: |
||
81 | return self.path.startswith(dirbase + os.sep) |
||
82 | return False |
||
83 | |||
84 | @cached_property |
||
85 | def can_upload(self): |
||
86 | dirbase = self.app.config["directory_upload"] |
||
87 | if self.is_directory and dirbase: |
||
88 | return dirbase == self.path or self.path.startswith(dirbase + os.sep) |
||
89 | return False |
||
90 | |||
91 | @cached_property |
||
92 | def stats(self): |
||
93 | return os.stat(self.path) |
||
94 | |||
95 | @cached_property |
||
96 | def mimetype(self): |
||
97 | if self.is_directory: |
||
98 | return 'inode/directory' |
||
99 | return self.plugin_manager.get_mimetype(self.path) |
||
100 | |||
101 | @cached_property |
||
102 | def is_directory(self): |
||
103 | return os.path.isdir(self.path) |
||
104 | |||
105 | @cached_property |
||
106 | def is_file(self): |
||
107 | return os.path.isfile(self.path) |
||
108 | |||
109 | @cached_property |
||
110 | def is_empty(self): |
||
111 | return not self._listdir |
||
112 | |||
113 | @cached_property |
||
114 | def parent(self): |
||
115 | if self.path == self.app.config['directory_base']: |
||
116 | return None |
||
117 | return self.__class__(os.path.dirname(self.path), self.app) |
||
118 | |||
119 | @cached_property |
||
120 | def ancestors(self): |
||
121 | ancestors = [] |
||
122 | parent = self.parent |
||
123 | while parent: |
||
124 | ancestors.append(parent) |
||
125 | parent = parent.parent |
||
126 | return tuple(ancestors) |
||
127 | |||
128 | @property |
||
129 | def modified(self): |
||
130 | return datetime.datetime.fromtimestamp(self.mtime).strftime('%Y.%m.%d %H:%M:%S') |
||
131 | |||
132 | @property |
||
133 | def size(self): |
||
134 | size, unit = fmt_size(self.stats.st_size, self.app.config["use_binary_multiples"]) |
||
135 | if unit == binary_units[0]: |
||
136 | return "%d %s" % (size, unit) |
||
137 | return "%.2f %s" % (size, unit) |
||
138 | |||
139 | @property |
||
140 | def urlpath(self): |
||
141 | return abspath_to_urlpath(self.path, self.app.config['directory_base']) |
||
142 | |||
143 | @property |
||
144 | def name(self): |
||
145 | return os.path.basename(self.path) |
||
146 | |||
147 | @property |
||
148 | def type(self): |
||
149 | return self.mimetype.split(";", 1)[0] |
||
150 | |||
151 | @property |
||
152 | def encoding(self): |
||
153 | if ";" in self.mimetype: |
||
154 | match = self.re_charset.search(self.mimetype) |
||
155 | gdict = match.groupdict() if match else {} |
||
156 | return gdict.get("charset") or "default" |
||
157 | return "default" |
||
158 | |||
159 | def listdir(self): |
||
160 | content = [ |
||
161 | self.__class__(path=os.path.join(self.path, path), app=self.app) |
||
162 | for path in os.listdir(self.path) |
||
163 | ] |
||
164 | content.sort(key=lambda f: (f.is_directory, f.name.lower())) |
||
165 | return content |
||
166 | |||
167 | @classmethod |
||
168 | def from_urlpath(cls, path, app=None): |
||
169 | app = app or current_app |
||
170 | base = app.config['directory_base'] |
||
171 | return cls(path=urlpath_to_abspath(path, base), app=app) |
||
172 | |||
427 |