1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import os 

7 

8 

9class FileError(Exception): 

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

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

12 self.context = {} 

13 

14 def set_context(self, k, v): 

15 self.context[k] = v 

16 

17 def __str__(self): 

18 s = Exception.__str__(self) 

19 if not self.context: 

20 return s 

21 

22 lines = [] 

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

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

25 

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

27 

28 

29class FileLoadError(FileError): 

30 ''' 

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

32 ''' 

33 

34 

35class FileSaveError(FileError): 

36 ''' 

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

38 ''' 

39 

40 

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) 

47 

48 

49def touch(path): 

50 try: 

51 with open(path, 'a'): 

52 os.utime(path) 

53 

54 except OSError as e: 

55 raise FileLoadError(e)