Source code for superdjango.contrib.releases.library.releases

# Imports

from configparser import ConfigParser
from django.utils.safestring import mark_safe
from markdown import markdown
from superdjango.utils import read_file
import os
from ..constants import RELEASE_PATH

# Exports

__all__ = (
    "factory",
    "get_releases",
    "Release",
)

# Functions


[docs]def factory(identifier, path=None): """Load a release. :param identifier: The release identifier (same as directory name). :type identifier: str :param path: The path to the releases directory. :type path: str :rtype: Release | None """ _path = path or RELEASE_PATH if not os.path.exists(os.path.join(_path, identifier)): return None _kwargs = dict() meta_path = os.path.join(_path, identifier, "meta.ini") if os.path.exists(meta_path): ini = ConfigParser() ini.read(meta_path) for key, value in ini.items("release"): # Identifier is passed as arg below. if key == "identifier": continue _kwargs[key] = value notes_path = os.path.join(_path, identifier, "notes.markdown") if os.path.exists(notes_path): _kwargs['content'] = read_file(notes_path) return Release(identifier, **_kwargs)
# extensions = ["markdown", "md"] # for extension in extensions: # notes_path = os.path.join(_path, "notes.%s" % extension) # if os.path.exists(notes_path): # description_path = os.path.join(_path, "description.txt") # if os.path.exists(description_path): # description = read_file(description_path) # # return Release(identifier, content=read_file(notes_path), description=description) # # return None
[docs]def get_releases(limit=10, path=None): """Load release data. :param limit: The total number of releases to return. :type limit: int :param path: The path to the releases directory. :type path: str :rtype: list[Release] """ _path = path or RELEASE_PATH if not os.path.exists(_path): return list() a = list() count = 0 for root, dirs, files in os.walk(_path): dirs.sort(reverse=True) for d in dirs: release = factory(d, path=_path) if release: a.append(release) count += 1 if count == limit: break return a
# Classes
[docs]class Release(object): """Encapsulate release information."""
[docs] def __init__(self, identifier, content=None, date=None, description=None, scope=None): self.content = content self.date = date self.description = description self.identifier = identifier self.scope = scope
def __repr__(self): return "<%s %s>" % (self.__class__.__name__, self.identifier) def __str__(self): return self.identifier
[docs] def to_html(self): """Export release content as HTML. :rtype: str """ return mark_safe(markdown(self.content))