filesystem.py 14.6 KB
Newer Older
Marcel's avatar
Marcel committed
1
# -*- coding: utf-8 -*-
2

Marcel's avatar
Marcel committed
3
# imports
4
5
from StringIO import StringIO
from distutils.spawn import find_executable
6
7
from mimetypes import guess_type
from zipfile import ZipFile
8
import json
9
10
11
12
import logging
import os
import re
import shutil
Gero Müller's avatar
Gero Müller committed
13
import stat
14
15
16
17
import subprocess

try:
    import Image
Gero Müller's avatar
Gero Müller committed
18
    import ImageFilter
19
20
21
22
23
24
25
26
27
28
    HAVE_PIL = True
except:
    HAVE_PIL = False

if not HAVE_PIL:
    convert_executable = find_executable('convert')
    if convert_executable and os.path.isfile(convert_executable):
        HAVE_CONVERT = True
    else:
        HAVE_CONVERT = False
29

30
31
logger = logging.getLogger(__name__)

32

33
34
class FileSystem(object):

35
36
    FILE_EXTENSIONS = ["png", "jpg", "jpeg", "bmp", "ps", "eps", "pdf",
                       "txt", "xml", "py", "c", "cpp", "root", "pxlio"]
Marcel's avatar
Marcel committed
37
38
39
    BROWSER_EXTENSIONS = ["png", "jpg", "jpeg", "bmp"]
    ADDITIONAL_MIMES = {
        "pxlio": "text/plain",
40
        "root": "text/plain"
Marcel's avatar
Marcel committed
41
    }
42
43
44
45
46

    def __init__(self):
        # allowed extensions
        self.allowed_extensions = FileSystem.FILE_EXTENSIONS

47
    def setup(self, basedir=None):
Marcel's avatar
Marcel committed
48
49
        if basedir is None:
            basedir = self.expand('~/')
murban's avatar
murban committed
50
        if not os.path.isdir(basedir):
Marcel's avatar
Marcel committed
51
            raise Exception("Basedir '%s' does not exist!" % basedir)
murban's avatar
murban committed
52
53
        # the basedir
        self.basedir = os.path.join(basedir, ".vispa")
54
55
        if os.path.isdir(self.basedir):
            return "Basedir already exists"
56
        else:
Gero Müller's avatar
Gero Müller committed
57
            os.makedirs(self.basedir, 0o700)
58
            return "Basedir now exists"
59

murban's avatar
murban committed
60
    def get_mime_type(self, filepath):
Marcel's avatar
Marcel committed
61
        filepath = self.expand(filepath)
62
63
64
65
        mime, encoding = guess_type(filepath)
        if mime is not None:
            return mime
        ext = filepath.split(".")[-1]
Gero Müller's avatar
Gero Müller committed
66
67
        if ext is not None and ext != "" and ext.lower(
        ) in FileSystem.ADDITIONAL_MIMES.keys():
68
69
70
            return FileSystem.ADDITIONAL_MIMES[ext]
        return None

murban's avatar
murban committed
71
    def check_file_extension(self, path, extensions=[]):
Marcel's avatar
Marcel committed
72
        path = self.expand(path)
73
74
75
76
77
78
79
80
        if (len(extensions) == 0):
            return True
        for elem in extensions:
            elem = elem if elem.startswith(".") else "." + elem
            if path.lower().endswith(elem.lower()):
                return True
        return False

Marcel's avatar
Marcel committed
81
82
    def exists(self, path, type=None):
        # type may be 'f' or 'd'
Marcel's avatar
Marcel committed
83
        path = self.expand(path)
84
85
        # path exists physically?
        if not os.path.exists(path):
Marcel's avatar
Marcel committed
86
87
            return None
        # type correct?
88
89
90
91
92
93
94
        target_type = 'd' if os.path.isdir(path) else 'f'
        if not type:
            return target_type
        type = type.lower()
        if type not in ['f', 'd']:
            return None
        return target_type if target_type == type else None
95

96
97
98
99
100
101
102
    def get_file_count(self, path):
        path = self.expand(path)
        if os.path.exists(path):
            return len(os.listdir(path))
        else:
            return -1

Gero Müller's avatar
Gero Müller committed
103
104
105
    def get_file_list(self, path, deep=False,
                      filter=None, reverse=False,
                      hide_hidden=True, encode_json=True):
106
        filter = map(re.compile, filter) or []
107
        filelist = []
Marcel's avatar
Marcel committed
108
        path_expand = self.expand(path)
109
110
111
112
113
114
115
116
117
        try:
            for elem in os.listdir(path_expand):
                # hide hidden files?
                if elem.startswith('.') and hide_hidden:
                    continue

                # excluded by filters?
                match = False
                for filter_elem in filter:
118
                    if filter_elem.search(elem):
119
120
121
122
                        match = True
                if match != reverse:
                    continue

Gero Müller's avatar
Gero Müller committed
123
124
125
                info = {
                    'name': elem
                }
126

Gero Müller's avatar
Gero Müller committed
127
                fullpath = os.path.join(path_expand, elem)
Gero Müller's avatar
Gero Müller committed
128
                stats = os.lstat(fullpath)
Gero Müller's avatar
Gero Müller committed
129
                is_symlink = stat.S_ISLNK(stats.st_mode)
Gero Müller's avatar
Gero Müller committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
                if is_symlink:
                    realfullpath = os.path.realpath(fullpath)
                    info.update({'symlink': True, 'realpath': realfullpath})
                    if os.path.exists(realfullpath):
                        stats = os.stat(realfullpath)

                info.update({
                    'size': stats.st_size,
                    'mtime': stats.st_mtime,
                    'type': 'd' if stat.S_ISDIR(stats.st_mode) else 'f'
                })

                filelist.append(info)
                if deep:
                    filelist.extend(self.get_file_list(fullpath, deep,
                                                       filter, reverse))
        except:
            pass
148
149

        # Determine the parent
Gero Müller's avatar
Gero Müller committed
150
        # parentpath = path_expand[:-1] if path_expand.endswith(os.sep) and
Gero Müller's avatar
Gero Müller committed
151
        #    path_expand != os.sep else path_expand
152
        parentpath = os.path.dirname(path_expand)
153
154
155
        data = {'filelist': filelist, 'parentpath': parentpath}
        if encode_json:
            return json.dumps(data)
156
157
        else:
            return data
158

Gero Müller's avatar
Gero Müller committed
159
160
    def get_suggestions(
            self, path, length=1, append_hidden=True, encode_json=True):
Marcel's avatar
Marcel committed
161
162
163
        suggestions = []
        source, filter = None, None
        # does the path exist?
Marcel's avatar
Marcel committed
164
165
        path_expanded = self.expand(path)
        if os.path.exists(path_expanded):
Marcel's avatar
Marcel committed
166
            # dir case
Marcel's avatar
Marcel committed
167
            if os.path.isdir(path_expanded):
Marcel's avatar
Marcel committed
168
169
170
171
172
173
174
175
176
177
178
                if path.endswith('/'):
                    source = path
                else:
                    suggestions.append(path + os.sep)
                    return suggestions
            # file case
            else:
                return suggestions
        else:
            # try to estimate source and filter
            head, tail = os.path.split(path)
Marcel's avatar
Marcel committed
179
            if os.path.isdir(os.path.expanduser(os.path.expandvars(head))):
Marcel's avatar
Marcel committed
180
181
182
183
184
185
186
                source = head
                filter = tail

        # return empty suggestions when source is not set
        if not source:
            return suggestions

Marcel's avatar
Marcel committed
187
        files = os.listdir(os.path.expanduser(os.path.expandvars(source)))
Marcel's avatar
Marcel committed
188
189
        # resort?
        if append_hidden:
Gero Müller's avatar
Gero Müller committed
190
191
            files = sorted(
                map(lambda f: str(f), files), cmp=file_compare, key=str.lower)
192
        while (len(suggestions) < length or length == 0) and len(files):
Marcel's avatar
Marcel committed
193
194
195
196
            file = files.pop(0)
            if filter and not file.startswith(filter):
                continue
            suggestion = os.path.join(source, file)
Gero Müller's avatar
Gero Müller committed
197
198
            if not suggestion.endswith(
                    '/') and os.path.isdir(os.path.expanduser(os.path.expandvars(suggestion))):
Marcel's avatar
Marcel committed
199
200
201
                suggestion += '/'
            suggestions.append(suggestion)

202
        return suggestions if not encode_json else json.dumps(suggestions)
203

murban's avatar
murban committed
204
    def cut_slashs(self, path):
Marcel's avatar
Marcel committed
205
        path = self.expand(path)
murban's avatar
murban committed
206
        path = path[1:] if path.startswith(os.sep) else path
207
208
        if path == "":
            return path
murban's avatar
murban committed
209
        path = path[:-1] if path.endswith(os.sep) else path
210
211
        return path

murban's avatar
murban committed
212
    def create_folder(self, path, name):
Marcel's avatar
Marcel committed
213
        path = self.expand(path)
Gero Müller's avatar
Gero Müller committed
214
215
        name = self.expand(name)

216
        # folder with the same name existent?
217
        name = self.handle_file_name_collision(name, path)
Marcel's avatar
Marcel committed
218
        fullpath = os.path.join(path, name)
219
220
221
        try:
            os.mkdir(fullpath)
        except Exception as e:
222
            # raise Exception("You don't have the permission to create this folder!")
223
224
            raise Exception(str(e))

murban's avatar
murban committed
225
    def create_file(self, path, name):
Marcel's avatar
Marcel committed
226
        path = self.expand(path)
Gero Müller's avatar
Gero Müller committed
227
228
        name = self.expand(name)

229
        # file with the same name existent?
230
        name = self.handle_file_name_collision(name, path)
Marcel's avatar
Marcel committed
231
        fullpath = os.path.join(path, name)
232
233
234
235
236
237
        try:
            f = file(fullpath, "w")
            f.close()
        except Exception as e:
            raise Exception(str(e))

238
    def rename(self, path, name, new_name, force=False):
Gero Müller's avatar
Gero Müller committed
239
240
241
242
        path = self.expand(path)
        name = self.expand(name)
        new_name = self.expand(new_name)

243
244
245
246
247
248
249
        try:
            if force == False:
                new_name = self.handle_file_name_collision(new_name, path)
            name = os.path.join(path, name)
            new_name = os.path.join(path, new_name)
            os.renames(name, new_name)
            return
Gero Müller's avatar
Gero Müller committed
250
        except Exception as e:
251
            return str(e)
252

murban's avatar
murban committed
253
254
    def remove(self, path):
        if isinstance(path, list):
255
256
257
            for p in path:
                self.remove(p)
        else:
Marcel's avatar
Marcel committed
258
259
260
261
262
            path = self.expand(path)
            if os.path.isdir(path):
                shutil.rmtree(path)
            else:
                os.remove(path)
263
264

    def compress(self, path, paths, name):
Gero Müller's avatar
Gero Müller committed
265
266
        # paths has to be a list of strings
        paths = paths if isinstance(paths, (list, tuple)) else [paths]
Gero Müller's avatar
Gero Müller committed
267
        paths = [self.expand(p) for p in paths]
Gero Müller's avatar
Gero Müller committed
268
269
270

        path = path if not path.endswith(os.sep) else path[:-1]
        path = self.expand(path)
Martin Urban's avatar
Martin Urban committed
271

Gero Müller's avatar
Gero Müller committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
        name = name if name.endswith(".zip") else name + ".zip"
        name = self.handle_file_name_collision(name, path)

        fullpath = os.path.join(path, name)

        with ZipFile(fullpath, "w") as archive:
            i = 0
            while i < len(paths):
                if not paths[i]:
                    i += 1
                    continue
                p = self.expand(paths[i])
                i += 1

                if os.path.isdir(p):
                    for elem in os.listdir(p):
                        fullp = os.path.join(p, elem)
                        if os.path.isdir(fullp):
                            paths.append(fullp)
                        else:
Gero Müller's avatar
Gero Müller committed
292
293
                            ap = fullp[
                                len(path):] if fullp.startswith(path) else fullp
Gero Müller's avatar
Gero Müller committed
294
295
296
                            logger.debug(fullp)
                            archive.write(fullp, ap)
                else:
Martin Urban's avatar
Martin Urban committed
297
                    ap = p[len(path):] if p.startswith(path) else p
Gero Müller's avatar
Gero Müller committed
298
                    logger.debug(p)
Martin Urban's avatar
Martin Urban committed
299
                    archive.write(p, ap)
300

301
    def paste(self, path, fullsrc, cut):
Marcel's avatar
Marcel committed
302
        # TODO
303
304
305
306
        path = self.expand(path)
        if isinstance(fullsrc, (list, tuple)):
            for p in fullsrc:
                p = self.expand(p)
murban's avatar
murban committed
307
                self.paste(path, p, cut)
308
309
            return True

310
311
        # fulltarget = os.path.join(path, fullsrc.split(os.sep)[-1])
        logger.error("")
Gero Müller's avatar
Gero Müller committed
312
313
        target = self.handle_file_name_collision(
            fullsrc.split(os.sep)[-1], path)
314
        fulltarget = os.path.join(path, target)
315

316
317
        if os.path.isdir(fullsrc):
            shutil.copytree(fullsrc, fulltarget)
318
            if cut:
319
                shutil.rmtree(fullsrc)
320
        else:
321
            shutil.copy2(fullsrc, fulltarget)
322
            if cut:
323
                os.remove(fullsrc)
324

Marcel's avatar
Marcel committed
325
    def save_file(self, path, content, binary=False, force=True):
326
327
328
        logger.info(path)
        logger.info("\n\n")
        return
Marcel's avatar
Marcel committed
329
        path = self.expand(path)
Marcel's avatar
Marcel committed
330
        # check if file already exists
murban's avatar
murban committed
331
        if os.path.exists(path) and not force:
Marcel's avatar
Marcel committed
332
            return False
Marcel's avatar
Marcel committed
333
        if binary:
334
335
336
337
338
            out = file(path, "wb")
        else:
            out = file(path, "w")
        logger.info("saved")
        logger.info("\n\n")
Marcel's avatar
Marcel committed
339
        out.write(content)
340
        out.close()
Marcel's avatar
Marcel committed
341
        return True
342

Gero Müller's avatar
Gero Müller committed
343
    def save_file_content(self, filename, content,
Gero Müller's avatar
Gero Müller committed
344
                          path=None, force=True, append=False):
345
346
        # check write permissions
        # if not self.checkPermission(username, vPath, 'w'):
Gero Müller's avatar
Gero Müller committed
347
348
349
350
351
352
        #    return False, "Permission denied!"

        if path:
            filename = os.path.join(path, filename)
        filename = self.expand(filename)

353
        # check if file already exists
Gero Müller's avatar
Gero Müller committed
354
355
356
357
358
359
360
361
362
        if os.path.exists(filename) and not force:
            return False, "The file '%s' already exists!" % filename

        out = open(filename, "ab+" if append else "wb")
        out.write(content)
        out.close()

        return True, "File saved!"

murban's avatar
murban committed
363
    def get_file_content(self, path):
Marcel's avatar
Marcel committed
364
        path = self.expand(path)
Gero Müller's avatar
Gero Müller committed
365
        f = open(path, "rb")
366
367
        content = f.read()
        f.close()
Marcel's avatar
Marcel committed
368
        return content
369

Marcel's avatar
Marcel committed
370
    def get_mtime(self, path):
Marcel's avatar
Marcel committed
371
        path = self.expand(path)
Marcel's avatar
Marcel committed
372
373
        return os.path.getmtime(path)

murban's avatar
murban committed
374
    def is_browser_file(self, path):
Marcel's avatar
Marcel committed
375
        path = self.expand(path)
376
        extension = path.split(".")[-1]
377
        return extension.lower() in FileSystem.BROWSER_EXTENSIONS
378

murban's avatar
murban committed
379
    def handle_file_name_collision(self, name, path):
380
        # collision?
murban's avatar
murban committed
381
        files = os.listdir(path)
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
        if name not in files:
            return name

        # when this line is reached, there is a collision!

        # cut the file extension
        extension = name.split(".")[-1]
        prename = None
        if name == extension:
            extension = ""
            prename = name
        else:
            prename = name.split("." + extension)[0]

        # has the name already a counter at its end?
        hasCounter = False
        preprename = None
        counter = prename.split("_")[-1]

        if counter != prename:
            try:
                counter = int(counter)
                hasCounter = True
                preprename = "_".join(prename.split("_")[:-1])
            except:
                pass

        if hasCounter:
            # increment and try again
            counter += 1
Gero Müller's avatar
Gero Müller committed
412
413
414
            newname = "%s_%d%s" % (preprename,
                                   counter,
                                   "" if extension == "" else "." + extension)
415
        else:
Gero Müller's avatar
Gero Müller committed
416
417
            newname = "%s_1%s" % (
                prename, "" if extension == "" else "." + extension)
418
419

        # return
murban's avatar
murban committed
420
        return self.handle_file_name_collision(newname, path)
Marcel's avatar
Marcel committed
421

Marcel's avatar
Marcel committed
422
423
    def expand(self, path):
        return os.path.expanduser(os.path.expandvars(path))
Marcel's avatar
Marcel committed
424

Gero Müller's avatar
Gero Müller committed
425
    def thumbnail(self, path, width=100, height=100, sharpen=True):
426
427
428
        path = self.expand(path)
        if HAVE_PIL:
            output = StringIO()
Gero Müller's avatar
Gero Müller committed
429
            img = Image.open(path)
Gero Müller's avatar
Gero Müller committed
430
            img.thumbnail((width, height), Image.ANTIALIAS)
Gero Müller's avatar
Gero Müller committed
431
            if sharpen:
432
                img.filter(ImageFilter.SHARPEN)
Gero Müller's avatar
Gero Müller committed
433
            img.save(output, "JPEG")
434
435
436
437
438
439
440
441
442
443
444
445
446
            contents = output.getvalue()
            output.close()
            return contents
        elif HAVE_CONVERT:
            cmd = ['convert', path,
                   '-thumbnail', '%dx%d' % (width, height),
                   'jpeg:-']
            p = subprocess.Popen(cmd, stdin=None, stdout=subprocess.PIPE)
            return p.communicate()[0]
        else:
            return self.get_file_content(path)


Marcel's avatar
Marcel committed
447
448
449
450
451
452
453
454
def string_compare(a, b):
    if a == b:
        return 0
    elif a > b:
        return 1
    else:
        return -1

455

Marcel's avatar
Marcel committed
456
457
458
459
460
461
462
463
464
def file_compare(a, b):
    if not a.startswith('.') and not b.startswith('.'):
        return string_compare(a, b)
    elif a.startswith('.') and b.startswith('.'):
        return string_compare(a, b)
    elif a.startswith('.') and not b.startswith('.'):
        return 1
    elif not a.startswith('.') and b.startswith('.'):
        return -1