summaryrefslogtreecommitdiff
path: root/jt.py
blob: 392af40e08bca818f045ab8ad44a550c016c3b6a (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import logging
import os
import subprocess
import sys

import fable

def _jt_path():
    return os.environ['JT_PATH']

def _runcmd(ctx, argv):
    logging.debug('Executing %r', argv)
    p = subprocess.Popen(
        argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    out, err = p.communicate()
    ctx["stdout"] = out
    ctx["stderr"] = err
    ctx["exit_code"] = p.returncode
    logging.debug('Result: exit_code=%r', ctx['exit_code'])
    if ctx['exit_code'] != 0:
        logging.warning(
            'jt failed, stdout=%r stderr=%r', ctx['stdout'], ctx['stderr'])

def create_empty_journal(ctx):
    ctx['journal'] = os.path.abspath('journal')
    ctx['blog'] = os.path.join(ctx['journal'], 'blog')
    os.mkdir(ctx['journal'])

    ctx['config'] = os.path.abspath('jt.conf')
    with open(ctx['config'], 'w') as f:
        f.write('[config]\n')
        f.write('source = %s\n' % ctx['journal'])
        f.write('git = no\n')
        f.write('push = no\n')
        f.write('notes-dir = blog\n')
        f.write('editor = /bin/true {editor}\n')
        f.write('log = %s.log\n' % ctx['journal'])
    logging.debug('Created config file %s', ctx['config'])


def run_jt(ctx, command=None):
    try_jt(ctx, command=command)
    fable.assertEqual(ctx['exit_code'], 0)

def try_jt(ctx, command=None):
    args = command.split()
    assert args[0] == 'jt'

    opts = [
        '--no-default-config',
        '--config', ctx['config'],
    ]

    argv = [_jt_path()] + opts + args[1:]
    argv = ['/bin/sh', '-c', ' '.join(argv)]
    _runcmd(ctx, argv)

def create_empty_file(ctx, filename=None):
    with open(filename, 'w') as f:
        pass

def jt_failed(ctx):
    fable.assertNotEqual(ctx['exit_code'], 0)

def _find_drafts(ctx):
    draftsdir = os.path.join(ctx['journal'], 'drafts')
    if not os.path.isdir(draftsdir):
        return []
    drafts = [f for f in _find_files(draftsdir) if f.endswith('.mdwn')]
    logging.debug('drafts = %r', drafts)
    return drafts

def no_drafts(ctx):
    drafts = _find_drafts(ctx)
    fable.assertEqual(drafts, [])

def one_draft(ctx):
    drafts = _find_drafts(ctx)
    fable.assertEqual(len(drafts), 1)

def one_draft_with_attachment(ctx, filename=None):
    drafts = _find_drafts(ctx)
    fable.assertEqual(len(drafts), 1)
    filename = drafts[0]
    fable.assertTrue(filename.endswith('.mdwn'))
    dirname = filename[:-len('.mdwn')]
    fable.assertTrue(os.path.isdir(dirname))
    attachment = os.path.join(dirname, filename)
    fable.assertTrue(os.path.exists(attachment))

def n_drafts(ctx, count=None):
    n = int(count)
    drafts = _find_drafts(ctx)
    fable.assertEqual(len(drafts), n)

def _get_draft(ctx, id):
    filename = os.path.join(ctx['journal'], 'drafts', '%s.mdwn' % id)
    return _cat(filename)

def draft_has_title(ctx, id=None, title=None):
    draft = _get_draft(ctx, id)
    fable.assertTrue(title in draft)

def _find_files(dirname):
    for d, s, f in os.walk(dirname):
        for filename in f:
            yield os.path.join(d, filename)

def _find_entries(ctx):
    blogdir = ctx['blog']
    logging.debug('blogdir: %r', blogdir)
    if not os.path.isdir(blogdir):
        return []
    entries = [f for f in _find_files(blogdir) if f.endswith('.mdwn')]
    logging.debug('entries = %r', entries)
    return entries

def one_entry(ctx):
    entries = _find_entries(ctx)
    fable.assertEqual(len(entries), 1)

def n_entries(ctx, count=None):
    n = int(count)
    entries = _find_entries(ctx)
    fable.assertEqual(len(entries), n)

def no_entries(ctx):
    entries = _find_entries(ctx)
    fable.assertEqual(len(entries), 0)

def _find_topics(ctx):
    dirname = os.path.join(ctx['journal'], 'topics')
    filenames = [f for f in _find_files(dirname) if f.endswith('.mdwn')]
    logging.debug('topics: %r', filenames)
    return filenames

def _cat(filename):
    with open(filename) as f:
        return f.read()

def one_topic(ctx, topic=None, title=None):
    topics = _find_topics(ctx)
    fable.assertEqual(len(topics), 1)
    topic = _cat(topics[0])
    fable.assertTrue(title in topic)