1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6import os
9class FileError(Exception):
10 def __init__(self, *args, **kwargs):
11 Exception.__init__(self, *args, **kwargs)
12 self.context = {}
14 def set_context(self, k, v):
15 self.context[k] = v
17 def __str__(self):
18 s = Exception.__str__(self)
19 if not self.context:
20 return s
22 lines = []
23 for k in sorted(self.context.keys()):
24 lines.append('%s: %s\n' % (k, self.context[k]))
26 return '%s\n%s' % (s, '\n'.join(lines))
29class FileLoadError(FileError):
30 '''
31 Raised when a problem occurred while loading of a file.
32 '''
35class FileSaveError(FileError):
36 '''
37 Raised when a problem occurred while saving of a file.
38 '''
41def get_stats(path):
42 try:
43 s = os.stat(path)
44 return float(s.st_mtime), s.st_size
45 except OSError as e:
46 raise FileLoadError(e)
49def touch(path):
50 try:
51 with open(path, 'a'):
52 os.utime(path)
54 except OSError as e:
55 raise FileLoadError(e)