Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(910)

Unified Diff: chrome/common/extensions/docs/server2/file_system.py

Issue 149513014: Docserver: Add FileSystem.Exists method and start sanity checking path format (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/docs/server2/file_system.py
diff --git a/chrome/common/extensions/docs/server2/file_system.py b/chrome/common/extensions/docs/server2/file_system.py
index 6686f4c99f2b10922daf84e89b05bf62ccf1cbe0..d4becf75211333c83d1f6fcc7e0fef9ccedf326d 100644
--- a/chrome/common/extensions/docs/server2/file_system.py
+++ b/chrome/common/extensions/docs/server2/file_system.py
@@ -2,7 +2,10 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
+import posixpath
+
from future import Gettable, Future
+from path_util import IsDirectory, SplitParent, ToDirectory
class _BaseFileSystemException(Exception):
@@ -77,6 +80,27 @@ class FileSystem(object):
read_single = self.Read([path])
return Future(delegate=Gettable(lambda: read_single.Get()[path]))
+ def Exists(self, path):
+ '''Returns a Future to the existence of |path|; True if |path| exists,
+ False is not. This method will not throw a FileNotFoundError unlike
Yoyo Zhou 2014/02/04 00:27:20 s/is/if/
not at google - send to devlin 2014/02/04 16:14:40 Done.
+ the Read* methods, however it may still throw a FileSystemError.
+
+ There are several ways to implement this method via the interface but this
+ method exists to do so in a canonical and most efficient way for caching.
+ '''
+ if path in ('', '/'):
+ # There is always a root directory.
+ return Future(value=True)
+
+ parent, base = SplitParent(path)
+ list_future = self.ReadSingle(ToDirectory(parent))
+ def resolve():
+ try:
+ return base in list_future.Get()
+ except FileNotFoundError:
+ return False
+ return Future(delegate=Gettable(resolve))
+
def Refresh(self):
'''Asynchronously refreshes the content of the FileSystem, returning a
future to its completion.
@@ -105,17 +129,22 @@ class FileSystem(object):
def Walk(self, root):
'''Recursively walk the directories in a file system, starting with root.
- Emulates os.walk from the standard os module.
- If the root cannot be found, raises a FileNotFoundError.
+ Behaviour is very similar to os.walk from the standard os module, yielding
Yoyo Zhou 2014/02/04 00:27:20 nice spelling =)
not at google - send to devlin 2014/02/04 16:14:40 are you referring to my favourite way of spelling
+ (base, files, dirs) recursively, where |base| is the base path of |files|
Yoyo Zhou 2014/02/04 00:30:03 Seems like this is actually base, dirs, files.
not at google - send to devlin 2014/02/04 16:14:40 yes. Thank you.
+ and |dirs| is relative to |root|, and |files| and |dirs| are the files and
Yoyo Zhou 2014/02/04 00:27:20 delete 'is'
not at google - send to devlin 2014/02/04 16:14:40 Done.
+ dirs in |base| respectively.
Yoyo Zhou 2014/02/04 00:27:20 Clarify that files and dirs are lists.
not at google - send to devlin 2014/02/04 16:14:40 Done.
+
+ Note that directories will always end with a '/', files never will.
+
+ If |root| cannot be found, raises a FileNotFoundError.
For any other failure, raises a FileSystemError.
'''
- basepath = root.rstrip('/') + '/'
+ root = ToDirectory(root) # TODO(kalman): assert IsDirectory(root)
+ basepath = root
def walk(root):
- if not root.endswith('/'):
- root += '/'
-
+ assert IsDirectory(root), root
dirs, files = [], []
for f in self.ReadSingle(root).Get():
« no previous file with comments | « chrome/common/extensions/docs/server2/cron.yaml ('k') | chrome/common/extensions/docs/server2/file_system_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698