aboutsummaryrefslogtreecommitdiffstats
path: root/view_functions.py
blob: b2e07201d3eed282c842327299ddf3c57f99bb08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
view_functions.py - defines functions called by views to display the correct data
about files and paths.
"""
import os
import subprocess
from siteconfig import siteconfig, config


def default_context():
    """
    default_context - returns the minimum info needed to render a template--the
    domain name (for the home directory), and the top site directories which
    make up the navbar
    """
    return {
        'domain': siteconfig.DOMAIN,
        'navbar': siteconfig.MAIN_SITE_DIRS,
    }


def file_last_modified(path):
    git_time = f"git log -n1 --pretty=%aD {path}".split()
    try:
        mod_time = subprocess.check_output(
            git_time, cwd=config.BASE_DIR
        )
    except:
        # File is not in the git log, no biggie, just blank the date
        return None

    # Git outputs in RFC2822 format
    return mod_time.decode('ascii').strip()


def index_dir(path):
    """
    index_dir - Given a directory at `path`, list it's contents,
    and sort each item as a file or a directory or a special file.

    return - a tuple with the values:
            > a list of directories in `path`,
            > a list of files in `path`,
            > (if present), a list of external links to add to the index.html of the
            directory
            > (if present), a short description (string) of what the directory contains

    Lists are sorted alphabetically.

    *Special files include '.links' and '.description' the format of these
    files is unquoted CSV and text/html. Both are displayed on the index.html
    for the given `path`
    """
    dirs = []
    files = []
    links = []
    description = False
    contents = os.listdir(path)
    for obj in contents:
        if os.path.isfile(path + '/' + obj):
            if obj == siteconfig.LINKS_FILE:
                with open(path + '/' + obj) as f:
                    links = f.readlines()
            elif obj == siteconfig.DESC_FILE:
                description = True
            elif not obj.startswith('.'):
                files.append(obj)
        elif os.path.isdir(path + '/' + obj):
            if not obj.startswith('.'):
                dirs.append(obj)

    return sorted(dirs), sorted(files), sorted(links), description


def is_hidden_path(path):
    """
    Tests if the URI path requested by the user, has any hidden paths
    which should not be rendered
    """
    for d in path.split('/'):
        if d.startswith('.'):
            return True
    return False


def load_description(parent_dir):
    description = None
    description_file = parent_dir + '/' + siteconfig.DESC_FILE
    if os.path.isfile(description_file):
        with open(description_file, "r") as f:
            description = f.read().strip()
    return description