Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / local_file_system.py
blob9b405adbdcf5b4245cb3a0c8e1ceee6996a9b120
1 # Copyright (c) 2012 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.
5 import os
6 import sys
8 from docs_server_utils import StringIdentity
9 from file_system import FileSystem, FileNotFoundError, StatInfo
10 from future import Gettable, Future
12 def _ConvertToFilepath(path):
13 return path.replace('/', os.sep)
15 def _ConvertFromFilepath(path):
16 return path.replace(os.sep, '/')
18 def _ReadFile(filename):
19 try:
20 with open(filename, 'rb') as f:
21 return f.read()
22 except IOError as e:
23 raise FileNotFoundError('Read failed for %s: %s' % (filename, e))
25 def _ListDir(dir_name):
26 all_files = []
27 try:
28 files = os.listdir(dir_name)
29 except OSError as e:
30 raise FileNotFoundError('os.listdir failed for %s: %s' % (dir_name, e))
31 for os_path in files:
32 posix_path = _ConvertFromFilepath(os_path)
33 if os_path.startswith('.'):
34 continue
35 if os.path.isdir(os.path.join(dir_name, os_path)):
36 all_files.append(posix_path + '/')
37 else:
38 all_files.append(posix_path)
39 return all_files
41 def _CreateStatInfo(path):
42 try:
43 path_mtime = os.stat(path).st_mtime
44 if os.path.isdir(path):
45 child_versions = dict((_ConvertFromFilepath(filename),
46 os.stat(os.path.join(path, filename)).st_mtime)
47 for filename in os.listdir(path))
48 # This file system stat mimics subversion, where the stat of directories
49 # is max(file stats). That means we need to recursively check the whole
50 # file system tree :\ so approximate that by just checking this dir.
51 version = max([path_mtime] + child_versions.values())
52 else:
53 child_versions = None
54 version = path_mtime
55 return StatInfo(version, child_versions)
56 except OSError as e:
57 raise FileNotFoundError('os.stat failed for %s: %s' % (path, e))
59 class LocalFileSystem(FileSystem):
60 '''FileSystem implementation which fetches resources from the local
61 filesystem.
62 '''
63 def __init__(self, base_path):
64 self._base_path = _ConvertToFilepath(base_path)
66 @staticmethod
67 def Create(root=''):
68 return LocalFileSystem(
69 os.path.join(sys.path[0], '..', '..', '..', '..', '..', root))
71 def Read(self, paths):
72 def resolve():
73 result = {}
74 for path in paths:
75 full_path = os.path.join(self._base_path,
76 _ConvertToFilepath(path).lstrip(os.sep))
77 if path == '' or path.endswith('/'):
78 result[path] = _ListDir(full_path)
79 else:
80 result[path] = _ReadFile(full_path)
81 return result
82 return Future(delegate=Gettable(resolve))
84 def Refresh(self):
85 return Future(value=())
87 def Stat(self, path):
88 full_path = os.path.join(self._base_path,
89 _ConvertToFilepath(path).lstrip(os.sep))
90 return _CreateStatInfo(full_path)
92 def GetIdentity(self):
93 return '@'.join((self.__class__.__name__, StringIdentity(self._base_path)))
95 def __repr__(self):
96 return 'LocalFileSystem(%s)' % self._base_path