summaryrefslogtreecommitdiff
path: root/lorem
blob: ddfb7dfae186813bc3424dcecd936ef16f008774 (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
#!/usr/bin/python3

import cliapp
import random
import textwrap


wrapped_text = """
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
"""


class Lorem(cliapp.Application):

    max_sentences = 8

    def add_settings(self):
        self.settings.integer(
            ["paragraphs", "p"],
            "number of paragraphs to produce, approximately; each paragraph "
            "is of a random, but fairly short length "
            "(default is infinite)",
            metavar="N",
        )

    def process_args(self, args):
        text = " ".join(x for x in wrapped_text.split("\n") if x)
        sentences = [x for x in text.split(".") if x]
        sentences = [" ".join(x.split()) + "." for x in sentences]

        i = 0
        paragraphs = self.settings["paragraphs"]
        while True:
            n = random.randint(1, self.max_sentences)
            paragraph = "\n".join(random.choice(sentences) for i in range(n))
            self.output.write("\n".join(textwrap.wrap(paragraph)))
            self.output.write("\n")
            i += 1
            if paragraphs and i >= paragraphs:
                break
            # Output empty line only between paragraphs, not after the last.
            self.output.write("\n")


Lorem().run()