OLD | NEW |
1 # Copyright (C) 2009 Google Inc. All rights reserved. | 1 # Copyright (C) 2009 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 def _mktemp(self, suffix='', prefix='tmp', dir=None, **kwargs): | 233 def _mktemp(self, suffix='', prefix='tmp', dir=None, **kwargs): |
234 if dir is None: | 234 if dir is None: |
235 dir = self.sep + '__im_tmp' | 235 dir = self.sep + '__im_tmp' |
236 curno = self.current_tmpno | 236 curno = self.current_tmpno |
237 self.current_tmpno += 1 | 237 self.current_tmpno += 1 |
238 self.last_tmpdir = self.join(dir, '%s_%u_%s' % (prefix, curno, suffix)) | 238 self.last_tmpdir = self.join(dir, '%s_%u_%s' % (prefix, curno, suffix)) |
239 return self.last_tmpdir | 239 return self.last_tmpdir |
240 | 240 |
241 def mkdtemp(self, **kwargs): | 241 def mkdtemp(self, **kwargs): |
242 class TemporaryDirectory(object): | 242 class TemporaryDirectory(object): |
| 243 |
243 def __init__(self, fs, **kwargs): | 244 def __init__(self, fs, **kwargs): |
244 self._kwargs = kwargs | 245 self._kwargs = kwargs |
245 self._filesystem = fs | 246 self._filesystem = fs |
246 self._directory_path = fs._mktemp(**kwargs) | 247 self._directory_path = fs._mktemp(**kwargs) |
247 fs.maybe_make_directory(self._directory_path) | 248 fs.maybe_make_directory(self._directory_path) |
248 | 249 |
249 def __str__(self): | 250 def __str__(self): |
250 return self._directory_path | 251 return self._directory_path |
251 | 252 |
252 def __enter__(self): | 253 def __enter__(self): |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 # (see http://docs.python.org/library/os.path.html#os.path.abspath ) | 345 # (see http://docs.python.org/library/os.path.html#os.path.abspath ) |
345 # it also removes trailing slashes and converts forward and backward | 346 # it also removes trailing slashes and converts forward and backward |
346 # slashes to the preferred slash os.sep. | 347 # slashes to the preferred slash os.sep. |
347 start = self.abspath(start) | 348 start = self.abspath(start) |
348 path = self.abspath(path) | 349 path = self.abspath(path) |
349 | 350 |
350 common_root = start | 351 common_root = start |
351 dot_dot = '' | 352 dot_dot = '' |
352 while not common_root == '': | 353 while not common_root == '': |
353 if path.startswith(common_root): | 354 if path.startswith(common_root): |
354 break | 355 break |
355 common_root = self.dirname(common_root) | 356 common_root = self.dirname(common_root) |
356 dot_dot += '..' + self.sep | 357 dot_dot += '..' + self.sep |
357 | 358 |
358 rel_path = path[len(common_root):] | 359 rel_path = path[len(common_root):] |
359 | 360 |
360 if not rel_path: | 361 if not rel_path: |
361 return '.' | 362 return '.' |
362 | 363 |
363 if rel_path[0] == self.sep: | 364 if rel_path[0] == self.sep: |
364 # It is probably sufficient to remove just the first character | 365 # It is probably sufficient to remove just the first character |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 return (path[:idx], path[(idx + 1):]) | 409 return (path[:idx], path[(idx + 1):]) |
409 | 410 |
410 def splitext(self, path): | 411 def splitext(self, path): |
411 idx = path.rfind('.') | 412 idx = path.rfind('.') |
412 if idx == -1: | 413 if idx == -1: |
413 idx = len(path) | 414 idx = len(path) |
414 return (path[0:idx], path[idx:]) | 415 return (path[0:idx], path[idx:]) |
415 | 416 |
416 | 417 |
417 class WritableBinaryFileObject(object): | 418 class WritableBinaryFileObject(object): |
| 419 |
418 def __init__(self, fs, path): | 420 def __init__(self, fs, path): |
419 self.fs = fs | 421 self.fs = fs |
420 self.path = path | 422 self.path = path |
421 self.closed = False | 423 self.closed = False |
422 self.fs.files[path] = "" | 424 self.fs.files[path] = "" |
423 | 425 |
424 def __enter__(self): | 426 def __enter__(self): |
425 return self | 427 return self |
426 | 428 |
427 def __exit__(self, type, value, traceback): | 429 def __exit__(self, type, value, traceback): |
428 self.close() | 430 self.close() |
429 | 431 |
430 def close(self): | 432 def close(self): |
431 self.closed = True | 433 self.closed = True |
432 | 434 |
433 def write(self, str): | 435 def write(self, str): |
434 self.fs.files[self.path] += str | 436 self.fs.files[self.path] += str |
435 self.fs.written_files[self.path] = self.fs.files[self.path] | 437 self.fs.written_files[self.path] = self.fs.files[self.path] |
436 | 438 |
437 | 439 |
438 class WritableTextFileObject(WritableBinaryFileObject): | 440 class WritableTextFileObject(WritableBinaryFileObject): |
| 441 |
439 def write(self, str): | 442 def write(self, str): |
440 WritableBinaryFileObject.write(self, str.encode('utf-8')) | 443 WritableBinaryFileObject.write(self, str.encode('utf-8')) |
441 | 444 |
442 | 445 |
443 class ReadableBinaryFileObject(object): | 446 class ReadableBinaryFileObject(object): |
| 447 |
444 def __init__(self, fs, path, data): | 448 def __init__(self, fs, path, data): |
445 self.fs = fs | 449 self.fs = fs |
446 self.path = path | 450 self.path = path |
447 self.closed = False | 451 self.closed = False |
448 self.data = data | 452 self.data = data |
449 self.offset = 0 | 453 self.offset = 0 |
450 | 454 |
451 def __enter__(self): | 455 def __enter__(self): |
452 return self | 456 return self |
453 | 457 |
454 def __exit__(self, type, value, traceback): | 458 def __exit__(self, type, value, traceback): |
455 self.close() | 459 self.close() |
456 | 460 |
457 def close(self): | 461 def close(self): |
458 self.closed = True | 462 self.closed = True |
459 | 463 |
460 def read(self, bytes=None): | 464 def read(self, bytes=None): |
461 if not bytes: | 465 if not bytes: |
462 return self.data[self.offset:] | 466 return self.data[self.offset:] |
463 start = self.offset | 467 start = self.offset |
464 self.offset += bytes | 468 self.offset += bytes |
465 return self.data[start:self.offset] | 469 return self.data[start:self.offset] |
466 | 470 |
467 | 471 |
468 class ReadableTextFileObject(ReadableBinaryFileObject): | 472 class ReadableTextFileObject(ReadableBinaryFileObject): |
| 473 |
469 def __init__(self, fs, path, data): | 474 def __init__(self, fs, path, data): |
470 super(ReadableTextFileObject, self).__init__(fs, path, StringIO.StringIO
(data.decode("utf-8"))) | 475 super(ReadableTextFileObject, self).__init__(fs, path, StringIO.StringIO
(data.decode("utf-8"))) |
471 | 476 |
472 def close(self): | 477 def close(self): |
473 self.data.close() | 478 self.data.close() |
474 super(ReadableTextFileObject, self).close() | 479 super(ReadableTextFileObject, self).close() |
475 | 480 |
476 def read(self, bytes=-1): | 481 def read(self, bytes=-1): |
477 return self.data.read(bytes) | 482 return self.data.read(bytes) |
478 | 483 |
479 def readline(self, length=None): | 484 def readline(self, length=None): |
480 return self.data.readline(length) | 485 return self.data.readline(length) |
481 | 486 |
482 def __iter__(self): | 487 def __iter__(self): |
483 return self.data.__iter__() | 488 return self.data.__iter__() |
484 | 489 |
485 def next(self): | 490 def next(self): |
486 return self.data.next() | 491 return self.data.next() |
487 | 492 |
488 def seek(self, offset, whence=os.SEEK_SET): | 493 def seek(self, offset, whence=os.SEEK_SET): |
489 self.data.seek(offset, whence) | 494 self.data.seek(offset, whence) |
OLD | NEW |