Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/io/io_common.py: 90%
20 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-23 12:34 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-23 12:34 +0000
1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6'''
7IO related exception definitions and utilities.
8'''
10import os
13class FileError(Exception):
14 '''
15 Base class for errors occurring when loading or saving data.
16 '''
17 def __init__(self, *args, **kwargs):
18 Exception.__init__(self, *args, **kwargs)
19 self.context = {}
21 def set_context(self, k, v):
22 self.context[k] = v
24 def __str__(self):
25 s = Exception.__str__(self)
26 if not self.context:
27 return s
29 lines = []
30 for k in sorted(self.context.keys()):
31 lines.append('%s: %s\n' % (k, self.context[k]))
33 return '%s\n%s' % (s, '\n'.join(lines))
36class FileLoadError(FileError):
37 '''
38 Raised when a problem occurred while loading of a file.
39 '''
42class FileSaveError(FileError):
43 '''
44 Raised when a problem occurred while saving of a file.
45 '''
48def get_stats(path):
49 try:
50 s = os.stat(path)
51 return float(s.st_mtime), s.st_size
52 except OSError as e:
53 raise FileLoadError(e)
56def touch(path):
57 try:
58 with open(path, 'a'):
59 os.utime(path)
61 except OSError as e:
62 raise FileLoadError(e)