Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright (C) Citrix Systems Inc. 

2# 

3# This program is free software; you can redistribute it and/or modify 

4# it under the terms of the GNU Lesser General Public License as published 

5# by the Free Software Foundation; version 2.1 only. 

6# 

7# This program is distributed in the hope that it will be useful, 

8# but WITHOUT ANY WARRANTY; without even the implied warranty of 

9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

10# GNU Lesser General Public License for more details. 

11# 

12# You should have received a copy of the GNU Lesser General Public License 

13# along with this program; if not, write to the Free Software Foundation, Inc., 

14# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 

15# 

16# File-based journaling 

17 

18import os 

19import errno 

20 

21import util 

22 

23SEPARATOR = "_" 

24 

25class JournalerException(util.SMException): 

26 pass 

27 

28 

29class Journaler: 

30 """Simple file-based journaler. A journal is a id-value pair, and there 

31 can be only one journal for a given id.""" 

32 

33 def __init__(self, dir): 

34 self.dir = dir 

35 

36 def create(self, type, id, val): 

37 """Create an entry of type "type" for "id" with the value "val". 

38 Error if such an entry already exists.""" 

39 valExisting = self.get(type, id) 

40 if valExisting: 

41 raise JournalerException("Journal already exists for '%s:%s': %s" \ 

42 % (type, id, valExisting)) 

43 path = self._getPath(type, id) 

44 f = open(path, "w") 

45 f.write(val) 

46 f.close() 

47 

48 def remove(self, type, id): 

49 """Remove the entry of type "type" for "id". Error if the entry doesn't 

50 exist.""" 

51 val = self.get(type, id) 

52 if not val: 

53 raise JournalerException("No journal for '%s:%s'" % (type, id)) 

54 path = self._getPath(type, id) 

55 os.unlink(path) 

56 

57 def get(self, type, id): 

58 """Get the value for the journal entry of type "type" for "id". 

59 Return None if no such entry exists""" 

60 path = self._getPath(type, id) 

61 if not util.pathexists(path): 

62 return None 

63 try: 

64 f = open(path, "r") 

65 except IOError as e: 

66 if e.errno == errno.ENOENT: 

67 # the file can disappear any time, since there is no locking 

68 return None 

69 raise 

70 val = f.readline() 

71 return val 

72 

73 def getAll(self, type): 

74 """Get a mapping id->value for all entries of type "type" """ 

75 fileList = os.listdir(self.dir) 

76 entries = dict() 

77 for fileName in fileList: 

78 if not fileName.startswith(type): 

79 continue 

80 parts = fileName.split(SEPARATOR, 2) 

81 if len(parts) != 2: 81 ↛ 82line 81 didn't jump to line 82, because the condition on line 81 was never true

82 raise JournalerException("Bad file name: %s" % fileName) 

83 t, id = parts 

84 if t != type: 

85 continue 

86 val = self.get(type, id) 

87 if val: 

88 entries[id] = val 

89 return entries 

90 

91 def _getPath(self, type, id): 

92 name = "%s%s%s" % (type, SEPARATOR, id) 

93 path = os.path.join(self.dir, name) 

94 return path