Top > OOoPython > mytools > sstdout

sstdout Edit

標準出力を OOo のウィンドウに表示する。Windows では普通にすると標準出力が表示されないためログを取ったり sys.stdout を置き換える必要がある。

sys.stdout を置き換えて OOo のウィンドウに表示することで別のアプリケーションやファイルを使用せずに標準出力を参照できるように。

GUI を使用したサービスなどを作成するときに順次リアルタイムで標準出力を参照したいときなどに利用する。

filesstdout.py

使用方法:
mytools.window が必要です。

sys.stdout を置き換えます。必要なら sys.stderr も同様にして置き換えます。

def sstd_test():
	ctx = XSCRIPTCONTEXT.getComponentContext()
	from mytools import sstdout
	#reload(sstdout)
	logger = sstdout.sstdout(ctx)
	
	import sys
	sys.stdout = logger

	print u'log...'
	print u'2nd line'

sstdout.py

  0
  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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# -*- coding: utf_8 -*-
 
import unohelper
 
from mytools import window as mytools_window
 
from com.sun.star.awt.WindowAttribute import \
    MOVEABLE as WA_MOVEABLE, CLOSEABLE as WA_CLOSEABLE, \
    BORDER as WA_BORDER, SHOW as WA_SHOW, SIZEABLE as WA_SIZEABLE
from com.sun.star.awt.WindowClass import TOP as WC_TOP
from com.sun.star.awt.PosSize import POSSIZE as PS_POSSIZE, SIZE as PS_SIZE
from com.sun.star.awt import XWindowListener, XTopWindowListener, XMenuListener
from com.sun.star.awt import Selection
from com.sun.star.ui.dialogs.ExecutableDialogResults import OK as EDR_OK
from com.sun.star.ui.dialogs.TemplateDescription import \
    FILESAVE_SIMPLE as TD_FILESAVE_SIMPLE
 
 
Width = 500
Height = 300
Font_Name = u'Courier'
Font_Height = 10
 
 
class sstdout:
    """stdout."""
    def __init__(self, ctx):
        self.length = 0
        self.ctx = ctx
        self.edit_out = self._create_sstd(ctx)
    
    def write(self, s):
        """For I/O."""
        self.log(s, False)
    
    def clear(self):
        if self.edit_out:
            self.edit_out.setText(u'')
    
    def log(self, s, lf=True):
        l = self.length
        self.length += len(s)
        if lf:
            self.length += 1
            s = s + u'\n'
        selection = self.create_Selection(l, l)
        self.edit_out.insertText(selection, s)
    
    def create_Selection(self, Min, Max):
        s = Selection()
        s.Min = Min
        s.Max = Max
        return s
    
    def _create_sstd(self, ctx):
        h = Height
        
        smgr = ctx.ServiceManager
        desktop = smgr.createInstanceWithContext(
            u'com.sun.star.frame.Desktop', ctx)
        
        toolkit = smgr.createInstanceWithContext(
            u'com.sun.star.awt.Toolkit', ctx)
        window = mytools_window.createWindow(
            toolkit, toolkit.getDesktopWindow(),
            WC_TOP, u'window', WA_MOVEABLE + WA_CLOSEABLE + WA_BORDER + WA_SIZEABLE,
            0, 0, Width, h)
        
        
        frame = smgr.createInstanceWithContext( 
            u'com.sun.star.frame.Frame', ctx)
        frame.initialize(window)
        frame.setCreator(desktop)
        frame.setName(u'sstdout')
        frame.Title = u'sstdout'
        
        menubar = smgr.createInstanceWithContext(u'com.sun.star.awt.MenuBar', ctx)
        menubar.removeItem(0,menubar.getItemCount())
        menubar.insertItem(0,u'~File',0,0)
        menubar.insertItem(1,u'~Edit',0,1)
        window.setMenuBar(menubar)
        
        cpm = mytools_window.create_popupmenu
        filemenu = cpm(smgr,ctx,(
            (0,u'~Save As',0,0,u'mytools.sstd:f_saveas'),
            #(-1,1),
        ) )
        
        editmenu = cpm(smgr,ctx,(
            (0,u'~Clear',0,0,u'mytools.sstd:e_clear'),
            #(-1,1),
        ) )
        menubar.setPopupMenu(0,filemenu)
        menubar.setPopupMenu(1,editmenu)
        
        h = window.OutputSize.Height
        
        cont = mytools_window.createCtrlContainer(smgr, ctx)
        cont.createPeer(toolkit, window)
        cont.setPosSize(0, 0, Width, h, PS_POSSIZE)
        frame.setComponent(cont, None)
        
        edit_out = mytools_window.createControl(
            smgr, ctx, u'Edit', 0, 0, Width, h,
            (u'FontHeight', u'FontName', u'MultiLine', u'VScroll'),
            (Font_Height, Font_Name, True, True))
        
        cont.addControl(u'edit_out', edit_out)
        
        ml = MenuListener(self)
        menubar.addMenuListener(ml)
        menubar.getPopupMenu(0).addMenuListener(ml)
        menubar.getPopupMenu(1).addMenuListener(ml)
        
        wl = WindowListener(cont)
        window.addWindowListener(wl)
        window.setVisible(True)
        tl = TopWindowListener(self)
        window.addTopWindowListener(tl)
        
        self.window = window
        self.frame = frame
        self.menu = menubar
        return edit_out
 
 
class WindowListener(unohelper.Base, XWindowListener):
    
    def __init__(self, cast):
        self.cast = cast
    def disposing(self,eventObject): pass
    def windowMoved(self,windowEvent): pass
    def windowShown(self,windowEvent): pass
    def windowHidden(self,windowEvent): pass
    def windowResized(self,windowEvent):
        w = windowEvent.Width
        h = windowEvent.Source.OutputSize.Height
        edit_out = self.cast.getControl(u'edit_out')
        edit_out.setPosSize(0,0, w, h, PS_SIZE)
 
 
class MenuListener(unohelper.Base, XMenuListener):
    def __init__(self,cast):
        self.cast = cast
        self.functions = {
            'f_': self._file_menu, 'e_': self._edit_menu}
    def disposing(self,eventObject): pass
    def activate(self,menuEvent): pass
    def deactivate(self,menuEvent): pass
    def highlight(self,menuEvent): pass
    def select(self,menuEvent):
        mid = menuEvent.MenuId
        cmd = menuEvent.Source.getCommand(mid)[13:]
        print cmd
        if cmd == u'f_saveas':
            self._file_menu()
        elif cmd == u'e_clear':
            self._edit_menu()
    
    def _file_menu(self):
        ctx = self.cast.ctx
        smgr = ctx.ServiceManager
        s = get_fileurl(smgr, ctx)
        if s == False: return
        text = self.cast.edit_out.getText()
        self._write_file(ctx, s, text)
    
    
    def _edit_menu(self):
        self.cast.clear()
    
    def _write_file(self, ctx, name, text):
        smgr = ctx.ServiceManager
        sfa = smgr.createInstanceWithContext(
            u'com.sun.star.ucb.SimpleFileAccess', ctx)
        output = sfa.openFileWrite(name)
        writer = smgr.createInstanceWithContext(
            u'com.sun.star.io.TextOutputStream', ctx)
        writer.setOutputStream(output)
        writer.setEncoding(u'utf_8')
        writer.writeString(text)
 
 
class TopWindowListener(unohelper.Base, XTopWindowListener):
    def __init__(self,cast):
        self.cast = cast
    def disposing(self,ev): pass
    def windowOpened(self,ev): pass
    def windowClosing(self,ev): pass
    def windowMinimized(self,ev): pass
    def windowNormalized(self,ev): pass
    def windowActivated(self,ev): pass
    def windowDeactivated(self,ev): pass
    def windowClosed(self,ev):
        cast = self.cast
        menu = self.cast.menu
        menu.getPopupMenu(0).setPopupMenu(0, None)
        cast.window.setMenuBar(None) # !
        
        cast.edit_out = None
        cast.menu = None
        cast.window = None
        cast.frame = None
        self.cast = None
 
 
def get_fileurl(smgr, ctx):
    fp = smgr.createInstanceWithContext( 
        u'com.sun.star.ui.dialogs.FilePicker', ctx)
    fp.initialize((TD_FILESAVE_SIMPLE,))
    fp.setMultiSelectionMode(False)
    fp.appendFilter(u'All Files (*.*)', u'*.*')
    fp.setCurrentFilter(u'All Files (*.*)')
    
    filepath = ""
    if fp.execute() == EDR_OK:
        filepaths = fp.getFiles()
        filepath = filepaths[0]
        return filepath
    else:
        return False

Attach file: filesstdout.py 758 download [Information]

Reload   New Lower page making Edit Freeze Diff Upload Copy Rename   Front page List of pages Search Recent changes Backup   Help   RSS of recent changes