[go: up one dir, main page]

Menu

[390b88]: / transf / lib / ui.py  Maximize  Restore  History

Download this file

55 lines (38 with data), 1.2 kB

 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
'''User data input transformations.'''
import sys
from transf import exception
from transf import transformation
from transf.lib import build
class Inputter(object):
'''Base class for all user data inputters.'''
def inputStr(self, title, text):
raise NotImplementedError
# TODO: other kind of inputs
# TODO: dinamically generate complex dialogs from a term
# description
class CliInputter(Inputter):
'''Simple command-line-interface inputter.'''
def inputStr(self, title, text):
sys.stdout.write(title + '\n')
sys.stdout.write(text + '\n')
return sys.stdin.readline()[:-1]
inputter = Inputter()
class Str(transformation.Transformation):
def __init__(self, title=None, text=None):
transformation.Transformation.__init__(self)
if title is None:
self.title = build.empty
else:
self.title = title
if text is None:
self.text = build.empty
else:
self.text = text
def apply(self, trm, ctx):
title = self.title.apply(trm, ctx)
text = self.text.apply(trm, ctx)
result = inputter.inputStr(title.value, text.value)
if result is None:
raise exception.Failure
else:
return trm.factory.makeStr(result)