File: __main__.py

package info (click to toggle)
python-adventure 1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 760 kB
  • ctags: 327
  • sloc: python: 1,991; makefile: 36; sh: 33
file content (112 lines) | stat: -rw-r--r-- 2,648 bytes parent folder | download | duplicates (2)
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
"""Offer Adventure at a custom command prompt.

Copyright 2010-2015 Brandon Rhodes.  Licensed as free software under the
Apache License, Version 2.0 as detailed in the accompanying README.txt.

"""

import argparse
import os
import re
import readline
import sys
from time import sleep
from . import load_advent_dat
from .game import Game

BAUD = 1200


def baudout(s):
    for c in s:
        sleep(9. / BAUD)  # 8 bits + 1 stop bit @ the given baud rate
        sys.stdout.write(c)
        sys.stdout.flush()


class AdventureArgumentParser(argparse.ArgumentParser):
    """ Command-line argument parser for this program. """

    default_description = 'Adventure into the Colossal Caves.'

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if self.description is None:
            self.description = self.default_description

        self._add_arguments()

    def _add_arguments(self):
        """ Add arguments specific to this program. """

        self.add_argument(
            'savefile', nargs='?',
            help='The filename of game you have saved.')


def make_or_resume_game(gamefile_path=None):
    """ Make a new game, or resume from the `gamefile_path`.

        :param gamefile_path: The filesystem path of the saved game to
            restore, or ``None`` to create a new game.
        :return: The `Game` instance.

        """
    if gamefile_path is None:
        game = Game()
        load_advent_dat(game)
        game.start()
        baudout(game.output)
    else:
        game = Game.resume(gamefile_path)
        baudout('GAME RESTORED\n')

    return game


def loop(game):
    """ The main Read-Eval-Print Loop for this program.

        :param game: The `Game` instance to play.
        :return: None.

        """
    while not game.is_finished:
        line = input('> ')
        words = re.findall(r'\w+', line)
        if words:
            baudout(game.do_command(words))


def main(argv=None):
    """ Main-line code for this program.

        :param argv: Sequence of command-line arguments to the process.
            If ``None``, defaults to `sys.argv`.
        :return: Exit status (integer) for the process.

        """
    if argv is None:
        argv = sys.argv

    exit_status = 0

    try:
        parser = AdventureArgumentParser(prog=argv[0])
        options = parser.parse_args(argv[1:])

        game = make_or_resume_game(options.savefile)

        loop(game)

    except EOFError:
        # End of command input.
        exit_status = 0

    return exit_status


if __name__ == '__main__':
    exit_status = main(sys.argv)
    sys.exit(exit_status)