Chromium Code Reviews| Index: tools/telemetry/telemetry/util/file_handle.py |
| diff --git a/tools/telemetry/telemetry/util/file_handle.py b/tools/telemetry/telemetry/util/file_handle.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2eb8b8dae33ac54dbf4fec71a2241acff291e7f4 |
| --- /dev/null |
| +++ b/tools/telemetry/telemetry/util/file_handle.py |
| @@ -0,0 +1,66 @@ |
| +# Copyright 2014 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import os |
| + |
| +_next_file_id = 0 |
| + |
| +class FileHandle(object): |
| + def __init__(self, tf, path=None): |
| + """Constructs a FileHandle object. |
| + |
| + This constructor should not be used by the user; rather it is preferred to |
| + use the module-level GetRelPath and FromTempFile functions. |
| + |
| + Args: |
| + tempfile: An instance of a temporary file object. |
| + path: A path; should not be passed if tempfile is and vice-versa. |
| + """ |
| + # We should never build a file handle from nothing. |
| + assert (path is None) != (tf is None) |
| + self._tf = tf |
| + self._path = path |
|
nduca
2014/09/16 20:56:24
self._path -> self._absolute_path
|
| + |
| + global _next_file_id |
| + self._id = _next_file_id |
| + _next_file_id += 1 |
| + |
| + @property |
| + def id(self): |
| + return self._id |
|
nednguyen
2014/09/16 16:15:04
I am still not convinced that we need the id for t
nduca
2014/09/16 20:56:24
as coded, this i think this is the right approach.
|
| + |
| + def GetRelPath(self, path='.'): |
|
nduca
2014/09/16 20:56:24
i think path=None is the right behavior here
rena
nduca
2014/09/16 20:56:24
i think path=None is the right behavior here
|
| + """Returns the path to the pointed-to file relative to the given path. |
| + |
| + Args: |
| + path: A string representing a path. |
| + Returns: |
| + A string giving the relative path from path to this file. |
| + """ |
| + if self._tf: |
| + name = self._tf.name |
| + else: |
| + name = self._path |
| + |
| + return os.path.relpath(name, path) |
| + |
| + @staticmethod |
| + def FromTempFile(tf): |
| + """Constructs a FileHandle pointing to a temporary file. |
| + |
| + Returns: |
| + A FileHandle referring to a named temporary file. |
| + """ |
| + return FileHandle(tf) |
| + |
| + @staticmethod |
| + def FromPath(path): |
| + """Constructs a FileHandle from a path. |
| + |
| + Args: |
| + path: A string giving the path to a file. |
| + Returns: |
| + A FileHandle referring to the file at the specified path. |
| + """ |
| + return FileHandle(None, path) |
|
nduca
2014/09/16 20:56:24
pass in os.path.abspath(path)
|