-
Notifications
You must be signed in to change notification settings - Fork 0
/
_cogutils.py
71 lines (51 loc) · 1.64 KB
/
_cogutils.py
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
import os
import subprocess
from itertools import imap, chain
try:
import cog
except ImportError:
pass
SAMPLE = 'sample.py'
def sample_run_lines(args):
command = ['python', SAMPLE] + args
proc = subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=os.path.dirname(__file__) or None)
return chain([' '.join(['$'] + command)],
read_striped_lines(proc.stdout))
def read_striped_lines(file):
return imap(str.rstrip, iter(file.readline, ''))
def indent_lines(lines, indent=2):
temp = " " * indent + "{0}"
for l in lines:
yield temp.format(l) if l else l
def inject_literal_block(desc, lines):
cog.out("\n{0}::\n\n".format(desc))
cog.outl("\n".join(indent_lines(lines)))
cog.outl()
def inject_sample_run():
inject_literal_block(
'Example run',
chain(
sample_run_lines(['--help']),
[''],
sample_run_lines(['--yes', '--choice', 'a']),
[''],
sample_run_lines(['--inum', 'invalid_argument'])))
def inject_sample_source():
inject_literal_block('Source code', read_striped_lines(open(SAMPLE)))
def inject_sample_doc():
inject_sample_source()
inject_sample_run()
def inject_readme():
cog.outl(
'"""\n{0}\n"""'
.format("".join(strip_cog_lines(file('README.rst').readlines()))))
def strip_cog_lines(lines):
for l in lines:
if '[[[cog' in l: # assuming no multiple cog line!
continue
if '[[[end]]]' in l:
continue
yield l