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
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
10# Exports
12__all__ = (
13 "factory",
14 "get_releases",
15 "Release",
16)
18# Functions
21def factory(identifier, path=None):
22 """Load a release.
24 :param identifier: The release identifier (same as directory name).
25 :type identifier: str
27 :param path: The path to the releases directory.
28 :type path: str
30 :rtype: Release | None
32 """
33 _path = path or RELEASE_PATH
35 if not os.path.exists(os.path.join(_path, identifier)):
36 return None
38 _kwargs = dict()
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)
45 for key, value in ini.items("release"):
46 # Identifier is passed as arg below.
47 if key == "identifier":
48 continue
50 _kwargs[key] = value
52 notes_path = os.path.join(_path, identifier, "notes.markdown")
53 if os.path.exists(notes_path):
54 _kwargs['content'] = read_file(notes_path)
56 return Release(identifier, **_kwargs)
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
71def get_releases(limit=10, path=None):
72 """Load release data.
74 :param limit: The total number of releases to return.
75 :type limit: int
77 :param path: The path to the releases directory.
78 :type path: str
80 :rtype: list[Release]
82 """
83 _path = path or RELEASE_PATH
85 if not os.path.exists(_path):
86 return list()
88 a = list()
89 count = 0
90 for root, dirs, files in os.walk(_path):
91 dirs.sort(reverse=True)
93 for d in dirs:
94 release = factory(d, path=_path)
95 if release:
96 a.append(release)
98 count += 1
100 if count == limit:
101 break
103 return a
106# Classes
109class Release(object):
110 """Encapsulate release information."""
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
119 def __repr__(self):
120 return "<%s %s>" % (self.__class__.__name__, self.identifier)
122 def __str__(self):
123 return self.identifier
125 def to_html(self):
126 """Export release content as HTML.
128 :rtype: str
130 """
131 return mark_safe(markdown(self.content))