File: txtui.py

package info (click to toggle)
solfege 2.0.4-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,200 kB
  • ctags: 1,844
  • sloc: python: 12,160; xml: 5,458; ansic: 1,486; makefile: 562; sh: 233
file content (100 lines) | stat: -rw-r--r-- 3,681 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
# GNU Solfege - eartraining for GNOME
# Copyright (C) 2000, 2001, 2002, 2003  Tom Cato Amundsen
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import sys, string, re
import app, mpd

import src.rcfile
src.rcfile.copy_rc_file()

try:
    import readline
except:
    print "Failed to import readline"

del sys.argv[sys.argv.index('--txtui')]
import harmonicinterval
import configureoutput

whitespace_re = re.compile("\s+")

class UI:
    def __init__(self, argv):
        self.m_prompt = "solfege>"
        self.m_app = app.SolfegeApp(argv, self)
        self.mainloop()
    def display_docfile(self, fn):
        print "display file (not finished yet!):" , fn
    def display_error_message(self, txt):
        print "Error message:\n", txt
    def mainloop(self):
        print "Welcome to GNU Solfege %s\nType 'help' to list commands." % configureoutput.VERSION_STRING
        teacher = None
        while 1:
            s = raw_input(self.m_prompt)
            s = string.rstrip(s)
            v = whitespace_re.split(s)
            if s == '':
                print "Type 'help' to list commands."
                continue
            if s in ('exit', 'bye', 'quit'):
                break
            if s == 'help':
                print """
start harm            start the harmonic exercise
new                   new question
repeat                repeat question
repeat melodic        repeat melodic
guess xxx             xxx is short name of interval, for example
                      m2 for minor second, M3 for major third,
                      4 for perfect fourth, d5 for diminished fifth.
exit                  quit the program.
"""
            elif v[0] == 'start':
                if len(v) < 2:
                    print "You must tell what exercise to start."
                    print "This release only support 'start harm' that will start the"
                    print "harmonic interval exercise."
                    continue
                if v[1] == 'harm' and teacher == None:
                    self.m_prompt = "harmonic-interval>"
                    teacher = harmonicinterval.Teacher('harmonic-interval', self.m_app)
            elif v[0] == 'new':
                if not teacher:
                    print 'start an exercise first'
                    continue
                teacher.new_question()
                teacher.play_question()
            elif v[0] == 'repeat':
                if len(v) == 1:
                    teacher.play_question()
                elif v[1] == 'melodic':
                    teacher.play_melodic()
            elif v[0] == 'guess':
                if len(v) < 2:
                    print "missing argument"
                    continue
                i = mpd.Interval()
                i.set_from_string(v[1])
                if teacher.guess_answer(i.get_intvalue()):
                    print "Correct"
                else:
                    print "Wrong"
            else:
                print "sorry, I don't understand you."

w = UI(sys.argv)