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