1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

# http://pyrocko.org - GPLv3 

# 

# The Pyrocko Developers, 21st Century 

# ---|P------/S----------~Lg---------- 

 

import os 

 

 

class FileError(Exception): 

def __init__(self, *args, **kwargs): 

Exception.__init__(self, *args, **kwargs) 

self.context = {} 

 

def set_context(self, k, v): 

self.context[k] = v 

 

def __str__(self): 

s = Exception.__str__(self) 

if not self.context: 

return s 

 

lines = [] 

for k in sorted(self.context.keys()): 

lines.append('%s: %s\n' % (k, self.context[k])) 

 

return '%s\n%s' % (s, '\n'.join(lines)) 

 

 

class FileLoadError(FileError): 

'''Raised when a problem occurred while loading of a file.''' 

 

 

class FileSaveError(FileError): 

'''Raised when a problem occurred while saving of a file.''' 

 

 

def get_stats(path): 

try: 

s = os.stat(path) 

return float(s.st_mtime), s.st_size 

except OSError as e: 

raise FileLoadError(e) 

 

 

def touch(path): 

try: 

with open(path, 'a'): 

os.utime(path) 

 

except OSError as e: 

raise FileLoadError(e)