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

Unified Diff: tools/telemetry/telemetry/util/file_handle.py

Issue 545523002: [Telemetry] Add capability for values to reference external files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address staticmethod nit Created 6 years, 3 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: 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)

Powered by Google App Engine
This is Rietveld 408576698