Debian Codegraph @

gavo/user/plainui.py

raw
1 """
2 Observers for running interactive programs in the terminal.
3 """
4
5 #c Copyright 2008-2025, the GAVO project <[email protected]>
6 #c
7 #c This program is free software, covered by the GNU GPL. See the
8 #c COPYING file in the source distribution.
9
10
11 import sys
12
13 from gavo import base
14
15
16 class StingyPlainUI(base.ObserverBase):
17 """An Observer swallowing infos, warnings, and the like.
18
19 This is to configure the UI. Enable it by calling
20 ``api.StingyPlainUI(api.ui)``.
21 """
22 def __init__(self, eh):
23 self.curIndent = ""
24 base.ObserverBase.__init__(self, eh)
25
26 def showMsg(self, msg):
27 print(self.curIndent+msg, file=sys.stderr)
28
29 def pushIndent(self):
30 self.curIndent = self.curIndent+" "
31
32 def popIndent(self):
33 self.curIndent = self.curIndent[:-2]
34
35 @base.listensTo("SourceError")
36 def announceSourceError(self, srcString):
37 self.showMsg("Failed source %s"%srcString)
38
39 @base.listensTo("Error")
40 def printErrMsg(self, errMsg):
41 self.showMsg("*X*X* "+errMsg)
42
43
44 class SemiStingyPlainUI(StingyPlainUI):
45 """A StingyPlainUI that at least displays warnings.
46
47 This is to configure the UI. Enable it by calling
48 ``api.SemiStingyPlainUI(api.ui)``.
49 """
50 @base.listensTo("Warning")
51 def printWarning(self, message):
52 self.showMsg("** WARNING: "+message)
53
54
55 class PlainUI(SemiStingyPlainUI):
56 """An Observer spitting out most info to the screen.
57
58 This is to configure the UI. Enable it by calling ``api.PlainUI(api.ui)``.
59 """
60 @base.listensTo("NewSource")
61 def announceNewSource(self, srcString):
62 self.showMsg("Starting %s"%srcString)
63 self.pushIndent()
64
65 @base.listensTo("SourceFinished")
66 def announceSourceFinished(self, srcString):
67 self.popIndent()
68 self.showMsg("Done %s, read %d"%(srcString, self.dispatcher.totalRead))
69
70 @base.listensTo("SourceError")
71 def announceSourceError(self, srcString):
72 self.popIndent()
73 self.showMsg("Failed %s"%srcString)
74
75 @base.listensTo("Shipout")
76 def announceShipout(self, noShipped):
77 self.showMsg("Shipped %d/%d"%(
78 noShipped, self.dispatcher.totalShippedOut))
79
80 @base.listensTo("IndexCreation")
81 def announceIndexing(self, indexName):
82 self.showMsg("Create index %s"%indexName)
83
84 @base.listensTo("ScriptRunning")
85 def announceScriptRunning(self, arg):
86 script, source = arg
87 fromNote = ""
88 if source:
89 fromNote = " on %s"%(getattr(source, "id", None) or repr(source))
90 self.showMsg("%s executing script %s%s"%(
91 script.__class__.__name__, script.name, fromNote))
92
93 @base.listensTo("Info")
94 def printInfo(self, message):
95 self.showMsg(message)
96
97 @base.listensTo("ProcessStarts")
98 def openProcess(self, procId):
99 sys.stderr.write(f"{self.curIndent}{procId}... ")
100 sys.stderr.flush()
101 self.clearer = ""
102
103 @base.listensTo("Progress")
104 def reportProgress(self, nextMessage):
105 sys.stderr.write(self.clearer+nextMessage)
106 sys.stderr.flush()
107 nChar = len(nextMessage)
108 self.clearer = ("\b"*nChar+" "*nChar+"\b"*nChar)
109
110 @base.listensTo("ProcessEnded")
111 def closeProcess(self, procId):
112 sys.stderr.write(f"{self.clearer}Done.\n")