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# Imports 

2 

3from configparser import ConfigParser 

4from django.utils.safestring import mark_safe 

5from markdown import markdown 

6from superdjango.utils import read_file 

7import os 

8from ..constants import RELEASE_PATH 

9 

10# Exports 

11 

12__all__ = ( 

13 "factory", 

14 "get_releases", 

15 "Release", 

16) 

17 

18# Functions 

19 

20 

21def factory(identifier, path=None): 

22 """Load a release. 

23 

24 :param identifier: The release identifier (same as directory name). 

25 :type identifier: str 

26 

27 :param path: The path to the releases directory. 

28 :type path: str 

29 

30 :rtype: Release | None 

31 

32 """ 

33 _path = path or RELEASE_PATH 

34 

35 if not os.path.exists(os.path.join(_path, identifier)): 

36 return None 

37 

38 _kwargs = dict() 

39 

40 meta_path = os.path.join(_path, identifier, "meta.ini") 

41 if os.path.exists(meta_path): 

42 ini = ConfigParser() 

43 ini.read(meta_path) 

44 

45 for key, value in ini.items("release"): 

46 # Identifier is passed as arg below. 

47 if key == "identifier": 

48 continue 

49 

50 _kwargs[key] = value 

51 

52 notes_path = os.path.join(_path, identifier, "notes.markdown") 

53 if os.path.exists(notes_path): 

54 _kwargs['content'] = read_file(notes_path) 

55 

56 return Release(identifier, **_kwargs) 

57 

58 # extensions = ["markdown", "md"] 

59 # for extension in extensions: 

60 # notes_path = os.path.join(_path, "notes.%s" % extension) 

61 # if os.path.exists(notes_path): 

62 # description_path = os.path.join(_path, "description.txt") 

63 # if os.path.exists(description_path): 

64 # description = read_file(description_path) 

65 # 

66 # return Release(identifier, content=read_file(notes_path), description=description) 

67 # 

68 # return None 

69 

70 

71def get_releases(limit=10, path=None): 

72 """Load release data. 

73 

74 :param limit: The total number of releases to return. 

75 :type limit: int 

76 

77 :param path: The path to the releases directory. 

78 :type path: str 

79 

80 :rtype: list[Release] 

81 

82 """ 

83 _path = path or RELEASE_PATH 

84 

85 if not os.path.exists(_path): 

86 return list() 

87 

88 a = list() 

89 count = 0 

90 for root, dirs, files in os.walk(_path): 

91 dirs.sort(reverse=True) 

92 

93 for d in dirs: 

94 release = factory(d, path=_path) 

95 if release: 

96 a.append(release) 

97 

98 count += 1 

99 

100 if count == limit: 

101 break 

102 

103 return a 

104 

105 

106# Classes 

107 

108 

109class Release(object): 

110 """Encapsulate release information.""" 

111 

112 def __init__(self, identifier, content=None, date=None, description=None, scope=None): 

113 self.content = content 

114 self.date = date 

115 self.description = description 

116 self.identifier = identifier 

117 self.scope = scope 

118 

119 def __repr__(self): 

120 return "<%s %s>" % (self.__class__.__name__, self.identifier) 

121 

122 def __str__(self): 

123 return self.identifier 

124 

125 def to_html(self): 

126 """Export release content as HTML. 

127 

128 :rtype: str 

129 

130 """ 

131 return mark_safe(markdown(self.content))