File: scripts.py

package info (click to toggle)
python-pweave 0.30.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 5,064 kB
  • sloc: python: 30,281; makefile: 167
file content (126 lines) | stat: -rw-r--r-- 5,776 bytes parent folder | download
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
import sys
from optparse import OptionParser
import pweave


def weave():
    if len(sys.argv) == 1:
        print("This is pweave %s, enter Pweave -h for help" % pweave.__version__)
        sys.exit()

    # Command line options
    parser = OptionParser(usage="pweave [options] sourcefile", version="Pweave " + pweave.__version__)
    parser.add_option("-f", "--format", dest="doctype", default=None,
                      help="The output format. Available formats: " + pweave.PwebFormats.shortformats() +
                           " Use Pweave -l to list descriptions or see http://mpastell.com/pweave/formats.html")
    parser.add_option("-i", "--input-format", dest="informat", default=None,
                      help="Input format: noweb, markdown, notebook or script")
    parser.add_option("-k", "--kernel", dest="kernel", default='python3',
                      help="Jupyter kernel used to run code: default is python3")
    parser.add_option("-o", "--output", dest="output", default=None,
                      help="Name of the output file")
    parser.add_option("-l", "--list-formats", dest="listformats", action="store_true", default=False,
                      help="List output formats")
    parser.add_option("-m", "--matplotlib", dest="plot", default=True, action="store_false",
                      help="Disable matplotlib")
    parser.add_option("-d", "--documentation-mode", dest="docmode",
                      action="store_true", default=False,
                      help="Use documentation mode, chunk code and results will be loaded from cache and inline code will be hidden")
    parser.add_option("-c", "--cache-results", dest="cache",
                      action="store_true", default=False,
                      help="Cache results to disk for documentation mode")
    parser.add_option("-F", "--figure-directory", dest="figdir", default='figures',
                      help="Directory path for matplolib graphics: Default 'figures'")
    parser.add_option("--cache-directory", dest="cachedir", default='cache',
                      help="Directory path for cached results used in documentation mode: Default 'cache'")
    parser.add_option("-g", "--figure-format", dest="figformat", default=None,
                      help="Figure format for matplotlib graphics: Defaults to 'png' for rst and Sphinx html documents and 'pdf' for tex")
    parser.add_option("-t", "--mimetype", dest="mimetype", default=None,
                      help="Source document's text mimetype. This is used to set cell " +
                           "type in Jupyter notebooks")

    (options, args) = parser.parse_args()

    try:
        infile = args[0]
    except IndexError:
        infile = ""

    opts_dict = vars(options)
    if options.figformat is not None:
        opts_dict["figformat"] = ('.%s' % options.figformat)

    pweave.weave(infile, **opts_dict)


def publish():
    if len(sys.argv) == 1:
        print("Publish a python script. Part of Pweave %s, use -h for help" % pweave.__version__)
        sys.exit()

    parser = OptionParser(usage="pypublish [options] sourcefile", version="Part of Pweave " + pweave.__version__)
    parser.add_option("-f", "--format", dest="format", default='html',
                      help="Output format html or pdf, pdf output requires pandoc and pdflatex")
    parser.add_option("-e", "--latex_engine", dest = "latex_engine", default = "pdflatex",
                      help = "The command for running latex.")
    parser.add_option("-t", "--theme", dest = "theme", default = "skeleton",
                      help = "Theme for HTML output")
    parser.add_option("-o", "--output", dest="output", default=None,
                      help="Name of the output file")

    (options, args) = parser.parse_args()

    try:
        infile = args[0]
    except IndexError:
        infile = ""

    pweave.publish(infile, options.format, options.theme, options.latex_engine, options.output)


def tangle():
    if len(sys.argv) == 1:
        print("This is ptangle %s, enter ptangle -h for help" % pweave.__version__)
        sys.exit()

    parser = OptionParser(usage="ptangle sourcefile", version="Pweave " + pweave.__version__)
    parser.add_option("-i", "--input-format", dest="informat", default=None,
                      help="Input format: noweb, markdown, notebook or script")

    (options, args) = parser.parse_args()

    try:
        infile = args[0]
    except IndexError:
        infile = ""

    pweave.tangle(infile, options.informat)


def convert():
    if len(sys.argv) == 1:
        print("This is Pweave document converter %s. Enter pweave-convert -h for help " % pweave.__version__)
        sys.exit()

    parser = OptionParser(usage="pweave-convert [options] sourcefile", version="Part of Pweave " + pweave.__version__)
    parser.add_option("-i", "--input-format", dest="informat", default='noweb',
                      help="Input format: noweb, notebook or script")
    parser.add_option("-f", "--output-format", dest="outformat", default='html',
                      help="Output format script, noweb or notebook")
    parser.add_option("-l", "--list-formats", dest="listformats", action="store_true", default=False,
                      help="List input / output formats")
    parser.add_option("-p", "--pandoc", dest="pandoc_args", default=None,
                      help="passed to pandoc for converting doc chunks")

    (options, args) = parser.parse_args()

    try:
        infile = args[0]
    except IndexError:
        infile = ""

    pweave.convert(file=infile,
                   informat=options.informat,
                   outformat=options.outformat,
                   pandoc_args=options.pandoc_args,
                   listformats=options.listformats)