# Write everything to disk that is in the write queue
self.collecty.write_queue.commit_file(self.file)
+ # Convenience functions for plugin authors
+
+ def read_file(self, *args, strip=True):
+ """
+ Reads the content of the given file
+ """
+ filename = os.path.join(*args)
+
+ with open(filename) as f:
+ value = f.read()
+
+ # Strip any excess whitespace
+ if strip:
+ value = value.strip()
+
+ return value
+
+ def read_file_integer(self, filename):
+ """
+ Reads the content from a file and returns it as an integer
+ """
+ value = self.read_file(filename)
+
+ try:
+ return int(value)
+ except ValueError:
+ return None
+
class GraphTemplate(object):
# A unique name to identify this graph template.
@property
def core_id(self):
- return self.read_file("topology/core_id")
+ return self.read_file(self.sys_path, "topology/core_id")
def is_cpufreq_supported(self):
path = os.path.join(self.sys_path, "cpufreq")
self.read_frequency("cpufreq/cpuinfo_max_freq"),
)
- def read_file(self, filename):
- file = os.path.join(self.sys_path, filename)
-
- with open(file, "r") as f:
- return f.read().strip()
-
def read_frequency(self, filename):
- val = self.read_file(filename)
+ val = self.read_file(self.sys_path, filename)
# Convert from kHz to Hz
return int(val) * 1000
for file in files:
path = os.path.join(interface_path, "statistics", file)
- # Open file and read it's content.
- with open(path) as f:
- line = f.readline()
- line = line.strip()
- ret.append(line)
+ ret.append(
+ self.read_file_integer(path),
+ )
return ret