Title: [90702] trunk/Tools
Revision
90702
Author
[email protected]
Date
2011-07-10 18:59:57 -0700 (Sun, 10 Jul 2011)

Log Message

Reviewed by Tony Chang.

Complete functions in filesystem.py
https://bugs.webkit.org/show_bug.cgi?id=63528

- missing text file functions added
- functions sorted
- removed 'append' optional parameters (were unused)
- adapted filesystem_mock in the same way

* Scripts/webkitpy/common/system/filesystem.py:
* Scripts/webkitpy/common/system/filesystem_mock.py:
* Scripts/webkitpy/common/system/filesystem_unittest.py:

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (90701 => 90702)


--- trunk/Tools/ChangeLog	2011-07-11 01:47:21 UTC (rev 90701)
+++ trunk/Tools/ChangeLog	2011-07-11 01:59:57 UTC (rev 90702)
@@ -1,3 +1,19 @@
+2011-07-11  Roland Steiner  <[email protected]>
+
+        Reviewed by Tony Chang.
+
+        Complete functions in filesystem.py
+        https://bugs.webkit.org/show_bug.cgi?id=63528
+
+        - missing text file functions added
+        - functions sorted
+        - removed 'append' optional parameters (were unused)
+        - adapted filesystem_mock in the same way
+
+        * Scripts/webkitpy/common/system/filesystem.py:
+        * Scripts/webkitpy/common/system/filesystem_mock.py:
+        * Scripts/webkitpy/common/system/filesystem_unittest.py:
+
 2011-07-10  Adam Barth  <[email protected]>
 
         Clean up style in fallback path calculation

Modified: trunk/Tools/Scripts/webkitpy/common/system/filesystem.py (90701 => 90702)


--- trunk/Tools/Scripts/webkitpy/common/system/filesystem.py	2011-07-11 01:47:21 UTC (rev 90701)
+++ trunk/Tools/Scripts/webkitpy/common/system/filesystem.py	2011-07-11 01:59:57 UTC (rev 90702)
@@ -191,12 +191,6 @@
         f = os.fdopen(temp_fd, 'wb')
         return f, temp_name
 
-    def open_text_file_for_writing(self, path, append=False):
-        mode = 'w'
-        if append:
-            mode = 'a'
-        return codecs.open(path, mode, 'utf8')
-
     def open_binary_file_for_reading(self, path):
         return codecs.open(path, 'rb')
 
@@ -205,6 +199,16 @@
         with file(path, 'rb') as f:
             return f.read()
 
+    def write_binary_file(self, path, contents):
+        with file(path, 'wb') as f:
+            f.write(contents)
+
+    def open_text_file_for_reading(self, path):
+        return codecs.open(path, 'r', 'utf8')
+
+    def open_text_file_for_writing(self, path):
+        return codecs.open(path, 'w', 'utf8')
+
     def read_text_file(self, path):
         """Return the contents of the file at the given path as a Unicode string.
 
@@ -212,6 +216,13 @@
         with codecs.open(path, 'r', 'utf8') as f:
             return f.read()
 
+    def write_text_file(self, path, contents):
+        """Write the contents to the file at the given location.
+
+        The file is written encoded as UTF-8 with no BOM."""
+        with codecs.open(path, 'w', 'utf8') as f:
+            f.write(contents)
+
     def relpath(self, path, start='.'):
         return ospath.relpath(path, start)
 
@@ -252,14 +263,3 @@
     def splitext(self, path):
         """Return (dirname + os.sep + basename, '.' + ext)"""
         return os.path.splitext(path)
-
-    def write_binary_file(self, path, contents):
-        with file(path, 'wb') as f:
-            f.write(contents)
-
-    def write_text_file(self, path, contents):
-        """Write the contents to the file at the given location.
-
-        The file is written encoded as UTF-8 with no BOM."""
-        with codecs.open(path, 'w', 'utf8') as f:
-            f.write(contents)

Modified: trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py (90701 => 90702)


--- trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py	2011-07-11 01:47:21 UTC (rev 90701)
+++ trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py	2011-07-11 01:59:57 UTC (rev 90702)
@@ -253,18 +253,12 @@
 
     def open_binary_tempfile(self, suffix=''):
         path = self._mktemp(suffix)
-        return (WritableFileObject(self, path), path)
+        return (WritableBinaryFileObject(self, path), path)
 
-    def open_text_file_for_writing(self, path, append=False):
-        return WritableFileObject(self, path, append)
-
-    def read_text_file(self, path):
-        return self.read_binary_file(path).decode('utf-8')
-
     def open_binary_file_for_reading(self, path):
         if self.files[path] is None:
             self._raise_not_found(path)
-        return ReadableFileObject(self, path, self.files[path])
+        return ReadableBinaryFileObject(self, path, self.files[path])
 
     def read_binary_file(self, path):
         # Intentionally raises KeyError if we don't recognize the path.
@@ -272,6 +266,24 @@
             self._raise_not_found(path)
         return self.files[path]
 
+    def write_binary_file(self, path, contents):
+        self.files[path] = contents
+        self.written_files[path] = contents
+
+    def open_text_file_for_reading(self, path):
+        if self.files[path] is None:
+            self._raise_not_found(path)
+        return ReadableTextFileObject(self, path)
+
+    def open_text_file_for_writing(self, path):
+        return WritableTextFileObject(self, path)
+
+    def read_text_file(self, path):
+        return self.read_binary_file(path).decode('utf-8')
+
+    def write_text_file(self, path, contents):
+        return self.write_binary_file(path, contents.encode('utf-8'))
+
     def relpath(self, path, start='.'):
         return ospath.relpath(path, start, self.abspath, self.sep)
 
@@ -302,21 +314,13 @@
             idx = 0
         return (path[0:idx], path[idx:])
 
-    def write_text_file(self, path, contents):
-        return self.write_binary_file(path, contents.encode('utf-8'))
 
-    def write_binary_file(self, path, contents):
-        self.files[path] = contents
-        self.written_files[path] = contents
-
-
-class WritableFileObject(object):
-    def __init__(self, fs, path, append=False, encoding=None):
+class WritableBinaryFileObject(object):
+    def __init__(self, fs, path):
         self.fs = fs
         self.path = path
         self.closed = False
-        if path not in self.fs.files or not append:
-            self.fs.files[path] = ""
+        self.fs.files[path] = ""
 
     def __enter__(self):
         return self
@@ -332,7 +336,12 @@
         self.fs.written_files[self.path] = self.fs.files[self.path]
 
 
-class ReadableFileObject(object):
+class WritableTextFileObject(WritableBinaryFileObject):
+    def write(self, str):
+        WritableBinaryFileObject.write(self, str.encode('utf-8'))
+
+
+class ReadableBinaryFileObject(object):
     def __init__(self, fs, path, data=""
         self.fs = fs
         self.path = path
@@ -355,3 +364,8 @@
         start = self.offset
         self.offset += bytes
         return self.data[start:self.offset]
+
+
+class ReadableTextFileObject(ReadableBinaryFileObject):
+    def read(self, bytes=None):
+        return ReadableBinaryFileObject.read(self, bytes).decode('utf-8')

Modified: trunk/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py (90701 => 90702)


--- trunk/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py	2011-07-11 01:47:21 UTC (rev 90701)
+++ trunk/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py	2011-07-11 01:59:57 UTC (rev 90702)
@@ -142,12 +142,33 @@
             if os.path.exists(sub_dir):
                 os.rmdir(sub_dir)
 
+    def test_read_and_write_text_file(self):
+        fs = FileSystem()
+        text_path = None
+
+        unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D'
+        hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD'
+        try:
+            text_path = tempfile.mktemp(prefix='tree_unittest_')
+            file = fs.open_text_file_for_writing(text_path)
+            file.write(unicode_text_string)
+            file.close()
+
+            file = fs.open_text_file_for_reading(text_path)
+            read_text = file.read()
+            file.close()
+
+            self.assertEqual(read_text, unicode_text_string)
+        finally:
+            if text_path and fs.isfile(text_path):
+                os.remove(text_path)
+
     def test_read_and_write_file(self):
         fs = FileSystem()
         text_path = None
         binary_path = None
 
-        unicode_text_string = u'Ūnĭcōde̽'
+        unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D'
         hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD'
         try:
             text_path = tempfile.mktemp(prefix='tree_unittest_')
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to