Example #1
0
 def test_get_filename(self):
     s = "Document"
     self.assertEqual(fsutil.get_filename(s), "Document")
     s = "Document.txt"
     self.assertEqual(fsutil.get_filename(s), "Document.txt")
     s = ".Document.txt"
     self.assertEqual(fsutil.get_filename(s), ".Document.txt")
     s = "/root/a/b/c/Document.txt"
     self.assertEqual(fsutil.get_filename(s), "Document.txt")
     s = "https://domain-name.com/Document.txt?p=1"
     self.assertEqual(fsutil.get_filename(s), "Document.txt")
Example #2
0
 def test_get_filename(self):
     s = 'Document'
     self.assertEqual(fsutil.get_filename(s), 'Document')
     s = 'Document.txt'
     self.assertEqual(fsutil.get_filename(s), 'Document.txt')
     s = '.Document.txt'
     self.assertEqual(fsutil.get_filename(s), '.Document.txt')
     s = '/root/a/b/c/Document.txt'
     self.assertEqual(fsutil.get_filename(s), 'Document.txt')
     s = 'https://domain-name.com/Document.txt?p=1'
     self.assertEqual(fsutil.get_filename(s), 'Document.txt')
Example #3
0
 def test_copy_dir(self):
     fsutil.create_file(self.temp_path("a/b/f-1.txt"))
     fsutil.create_file(self.temp_path("a/b/f-2.txt"))
     fsutil.create_file(self.temp_path("a/b/f-3.txt"))
     fsutil.copy_dir(self.temp_path("a/b"), self.temp_path("x/y/z"))
     filepaths = fsutil.list_files(self.temp_path("a/b"))
     filenames = [fsutil.get_filename(filepath) for filepath in filepaths]
     self.assertEqual(len(filepaths), 3)
     self.assertEqual(filenames, ["f-1.txt", "f-2.txt", "f-3.txt"])
     filepaths = fsutil.list_files(self.temp_path("x/y/z/b/"))
     filenames = [fsutil.get_filename(filepath) for filepath in filepaths]
     self.assertEqual(len(filepaths), 3)
     self.assertEqual(filenames, ["f-1.txt", "f-2.txt", "f-3.txt"])
Example #4
0
 def test_copy_dir(self):
     fsutil.create_file(self.temp_path('a/b/f-1.txt'))
     fsutil.create_file(self.temp_path('a/b/f-2.txt'))
     fsutil.create_file(self.temp_path('a/b/f-3.txt'))
     fsutil.copy_dir(self.temp_path('a/b'), self.temp_path('x/y/z'))
     filepaths = fsutil.list_files(self.temp_path('a/b'))
     filenames = [fsutil.get_filename(filepath) for filepath in filepaths]
     self.assertEqual(len(filepaths), 3)
     self.assertEqual(filenames, ['f-1.txt', 'f-2.txt', 'f-3.txt'])
     filepaths = fsutil.list_files(self.temp_path('x/y/z/b/'))
     filenames = [fsutil.get_filename(filepath) for filepath in filepaths]
     self.assertEqual(len(filepaths), 3)
     self.assertEqual(filenames, ['f-1.txt', 'f-2.txt', 'f-3.txt'])
Example #5
0
        def wrapped(*args, **kwargs):

            # Execute the wrapped function
            result = func(*args, **kwargs)

            extra = {
                "history": True,
                "successful": False,
                "result": None,
                "uuid": shortuuid.uuid()[:8],
            }

            if result:
                extra["successful"] = True
                extra["result"] = result

            logger_ = logger.opt(depth=1).bind(**extra)

            command_args = sys.argv

            # First arg will be the command
            # Remove full path, just save the command/binary
            command_args[0] = fsutil.get_filename(command_args[0])

            full_command = " ".join(command_args)

            logger_.info(full_command)

            return result
Example #6
0
 def __init__(self, relative, absolute, url):
     self.url = url
     self.absolute = absolute
     self.relative = relative
     self.name = fsutil.get_filename(absolute)
     self.is_exist = fsutil.exists(absolute)
     self.is_file = fsutil.is_file(absolute)
     self.is_dir = fsutil.is_dir(absolute)
     self.__content = None
Example #7
0
 def test_list_files(self):
     for i in range(0, 5):
         fsutil.create_dir(self.temp_path('a/b/c/d-{}'.format(i)))
         fsutil.create_file(self.temp_path('a/b/c/f-{}.txt'.format(i)),
                            content='{}'.format(i))
     filepaths = fsutil.list_files(self.temp_path('a/b/c'))
     filenames = [fsutil.get_filename(filepath) for filepath in filepaths]
     self.assertEqual(len(filepaths), 5)
     self.assertEqual(
         filenames, ['f-0.txt', 'f-1.txt', 'f-2.txt', 'f-3.txt', 'f-4.txt'])
Example #8
0
def list_dir(request):
    path = request.GET.get('path', '')
    with_dirs = request.GET.get('dirs', 1)
    with_files = request.GET.get('files', 0)
    node = Node.parse(relative=path)

    result = []

    if not node.is_exist or node.is_file:
        raise Http404

    if with_dirs:
        try:
            folders = fsutil.list_dirs(node.absolute)
        except FileNotFoundError:
            raise Http404
        for i, folder in enumerate(folders):
            relative_path = folder.replace(base_files_dir, '')
            id_path = '/'.join(fsutil.split_path(relative_path))
            folders[i] = {
                'id':
                hashlib.sha1(folder.encode('utf-8')).hexdigest(),
                'load':
                reverse('bloom:file_manager:list_dir',
                        params={'path': id_path}),
                'class':
                'folder',
                'children':
                len(fsutil.list_dirs(folder)) > 0,
                'text':
                fsutil.get_filename(folder),
                'url':
                static(relative_path),
                'list':
                reverse('bloom:file_manager:list_dir',
                        params={
                            'path': id_path,
                            'files': 1
                        }),
            }
        result.extend(folders)

    if with_files:
        try:
            files = fsutil.list_files(node.absolute)
        except FileNotFoundError:
            raise Http404
        for i, file in enumerate(files):
            files[i] = {
                'id': fsutil.get_file_hash(file, 'sha1'),
                'class': 'file ex-' + fsutil.get_file_extension(file),
                'text': fsutil.get_filename(file),
                'url': static(file.replace(static_dir, '')),
            }
        result.extend(files)

    if not path and not with_files:
        result = {
            'id': 'root',
            'text': 'Root Folder',
            'children': result,
            'state': {
                'opened': True,
                'selected': True
            },
            'list': reverse('bloom:file_manager:list_dir', params={'files':
                                                                   1}),
        }

    return JsonResponse(result, safe=False)