Changeset 90702 in webkit


Ignore:
Timestamp:
Jul 10, 2011 6:59:57 PM (13 years ago)
Author:
rolandsteiner@chromium.org
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:
Location:
trunk/Tools
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r90697 r90702  
     12011-07-11  Roland Steiner  <rolandsteiner@chromium.org>
     2
     3        Reviewed by Tony Chang.
     4
     5        Complete functions in filesystem.py
     6        https://bugs.webkit.org/show_bug.cgi?id=63528
     7
     8        - missing text file functions added
     9        - functions sorted
     10        - removed 'append' optional parameters (were unused)
     11        - adapted filesystem_mock in the same way
     12
     13        * Scripts/webkitpy/common/system/filesystem.py:
     14        * Scripts/webkitpy/common/system/filesystem_mock.py:
     15        * Scripts/webkitpy/common/system/filesystem_unittest.py:
     16
    1172011-07-10  Adam Barth  <abarth@webkit.org>
    218
  • trunk/Tools/Scripts/webkitpy/common/system/filesystem.py

    r90050 r90702  
    192192        return f, temp_name
    193193
    194     def open_text_file_for_writing(self, path, append=False):
    195         mode = 'w'
    196         if append:
    197             mode = 'a'
    198         return codecs.open(path, mode, 'utf8')
    199 
    200194    def open_binary_file_for_reading(self, path):
    201195        return codecs.open(path, 'rb')
     
    206200            return f.read()
    207201
     202    def write_binary_file(self, path, contents):
     203        with file(path, 'wb') as f:
     204            f.write(contents)
     205
     206    def open_text_file_for_reading(self, path):
     207        return codecs.open(path, 'r', 'utf8')
     208
     209    def open_text_file_for_writing(self, path):
     210        return codecs.open(path, 'w', 'utf8')
     211
    208212    def read_text_file(self, path):
    209213        """Return the contents of the file at the given path as a Unicode string.
     
    212216        with codecs.open(path, 'r', 'utf8') as f:
    213217            return f.read()
     218
     219    def write_text_file(self, path, contents):
     220        """Write the contents to the file at the given location.
     221
     222        The file is written encoded as UTF-8 with no BOM."""
     223        with codecs.open(path, 'w', 'utf8') as f:
     224            f.write(contents)
    214225
    215226    def relpath(self, path, start='.'):
     
    253264        """Return (dirname + os.sep + basename, '.' + ext)"""
    254265        return os.path.splitext(path)
    255 
    256     def write_binary_file(self, path, contents):
    257         with file(path, 'wb') as f:
    258             f.write(contents)
    259 
    260     def write_text_file(self, path, contents):
    261         """Write the contents to the file at the given location.
    262 
    263         The file is written encoded as UTF-8 with no BOM."""
    264         with codecs.open(path, 'w', 'utf8') as f:
    265             f.write(contents)
  • trunk/Tools/Scripts/webkitpy/common/system/filesystem_mock.py

    r90543 r90702  
    254254    def open_binary_tempfile(self, suffix=''):
    255255        path = self._mktemp(suffix)
    256         return (WritableFileObject(self, path), path)
    257 
    258     def open_text_file_for_writing(self, path, append=False):
    259         return WritableFileObject(self, path, append)
    260 
    261     def read_text_file(self, path):
    262         return self.read_binary_file(path).decode('utf-8')
     256        return (WritableBinaryFileObject(self, path), path)
    263257
    264258    def open_binary_file_for_reading(self, path):
    265259        if self.files[path] is None:
    266260            self._raise_not_found(path)
    267         return ReadableFileObject(self, path, self.files[path])
     261        return ReadableBinaryFileObject(self, path, self.files[path])
    268262
    269263    def read_binary_file(self, path):
     
    273267        return self.files[path]
    274268
     269    def write_binary_file(self, path, contents):
     270        self.files[path] = contents
     271        self.written_files[path] = contents
     272
     273    def open_text_file_for_reading(self, path):
     274        if self.files[path] is None:
     275            self._raise_not_found(path)
     276        return ReadableTextFileObject(self, path)
     277
     278    def open_text_file_for_writing(self, path):
     279        return WritableTextFileObject(self, path)
     280
     281    def read_text_file(self, path):
     282        return self.read_binary_file(path).decode('utf-8')
     283
     284    def write_text_file(self, path, contents):
     285        return self.write_binary_file(path, contents.encode('utf-8'))
     286
    275287    def relpath(self, path, start='.'):
    276288        return ospath.relpath(path, start, self.abspath, self.sep)
     
    303315        return (path[0:idx], path[idx:])
    304316
    305     def write_text_file(self, path, contents):
    306         return self.write_binary_file(path, contents.encode('utf-8'))
    307 
    308     def write_binary_file(self, path, contents):
    309         self.files[path] = contents
    310         self.written_files[path] = contents
    311 
    312 
    313 class WritableFileObject(object):
    314     def __init__(self, fs, path, append=False, encoding=None):
     317
     318class WritableBinaryFileObject(object):
     319    def __init__(self, fs, path):
    315320        self.fs = fs
    316321        self.path = path
    317322        self.closed = False
    318         if path not in self.fs.files or not append:
    319             self.fs.files[path] = ""
     323        self.fs.files[path] = ""
    320324
    321325    def __enter__(self):
     
    333337
    334338
    335 class ReadableFileObject(object):
     339class WritableTextFileObject(WritableBinaryFileObject):
     340    def write(self, str):
     341        WritableBinaryFileObject.write(self, str.encode('utf-8'))
     342
     343
     344class ReadableBinaryFileObject(object):
    336345    def __init__(self, fs, path, data=""):
    337346        self.fs = fs
     
    356365        self.offset += bytes
    357366        return self.data[start:self.offset]
     367
     368
     369class ReadableTextFileObject(ReadableBinaryFileObject):
     370    def read(self, bytes=None):
     371        return ReadableBinaryFileObject.read(self, bytes).decode('utf-8')
  • trunk/Tools/Scripts/webkitpy/common/system/filesystem_unittest.py

    r89944 r90702  
    143143                os.rmdir(sub_dir)
    144144
     145    def test_read_and_write_text_file(self):
     146        fs = FileSystem()
     147        text_path = None
     148
     149        unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D'
     150        hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD'
     151        try:
     152            text_path = tempfile.mktemp(prefix='tree_unittest_')
     153            file = fs.open_text_file_for_writing(text_path)
     154            file.write(unicode_text_string)
     155            file.close()
     156
     157            file = fs.open_text_file_for_reading(text_path)
     158            read_text = file.read()
     159            file.close()
     160
     161            self.assertEqual(read_text, unicode_text_string)
     162        finally:
     163            if text_path and fs.isfile(text_path):
     164                os.remove(text_path)
     165
    145166    def test_read_and_write_file(self):
    146167        fs = FileSystem()
     
    148169        binary_path = None
    149170
    150         unicode_text_string = u'Ūnĭcōde̽'
     171        unicode_text_string = u'\u016An\u012Dc\u014Dde\u033D'
    151172        hex_equivalent = '\xC5\xAA\x6E\xC4\xAD\x63\xC5\x8D\x64\x65\xCC\xBD'
    152173        try:
Note: See TracChangeset for help on using the changeset viewer.