Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / chroot_file_system.py
1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import posixpath
6
7 from docs_server_utils import StringIdentity
8 from file_system import FileSystem
9 from future import Future
10
11
12 class ChrootFileSystem(FileSystem):
13   '''ChrootFileSystem(fs, path) exposes a FileSystem whose root is |path| inside
14   |fs|, so ChrootFileSystem(fs, 'hello').Read(['world']) is equivalent to
15   fs.Read(['hello/world']) with the 'hello' prefix stripped from the result.
16   '''
17
18   def __init__(self, file_system, root):
19     '''Parameters:
20     |file_system| The FileSystem instance to transpose paths of.
21     |root|        The path to transpose all Read/Stat calls by.
22     '''
23     self._file_system = file_system
24     self._root = root.strip('/')
25
26   def Read(self, paths, skip_not_found=False):
27     # Maintain reverse mapping so the result can be mapped to the original
28     # paths given (the result from |file_system| will include |root| in the
29     # result, which would be wrong).
30     prefixed_paths = {}
31     def prefix(path):
32       prefixed = posixpath.join(self._root, path)
33       prefixed_paths[prefixed] = path
34       return prefixed
35     def next(results):
36       return dict((prefixed_paths[path], content)
37                   for path, content in results.iteritems())
38     return self._file_system.Read(tuple(prefix(path) for path in paths),
39                                   skip_not_found-skip_not_found).Then(next)
40
41   def Refresh(self):
42     return self._file_system.Refresh()
43
44   def Stat(self, path):
45     return self._file_system.Stat(posixpath.join(self._root, path))
46
47   def GetIdentity(self):
48     return StringIdentity(
49         '%s/%s' % (self._file_system.GetIdentity(), self._root))
50
51   def __repr__(self):
52     return 'ChrootFileSystem(%s, %s)' % (
53             self._root, repr(self._file_system))