-
Notifications
You must be signed in to change notification settings - Fork 6
/
genscreenshot.py
115 lines (98 loc) · 3.15 KB
/
genscreenshot.py
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
#!/usr/bin/env python
#
# Copyright (c) 2022, Alliance for Sustainable Energy
#
# This software is released under the BSD 3-clause license. See LICENSE file
# for more details.
#
import logging
import threading
import os, sys, tempfile
from PIL import Image
from xvfbwrapper import Xvfb
import yaml
"""
Note that this script relies on:
- xvfbwrapper
- xwd
- convert
On ubuntu systems, install with
sudo apt install x11-apps imagemagick-6.q16
"""
# Set the location of amrwind_frontend.py script
scriptpath=os.path.dirname(os.path.realpath(__file__))
sys.path.insert(1, scriptpath)
import amrwind_frontend as amrwind
# Set title and local configuration directory
title='AMR-Wind'
localconfigdir=os.path.join(scriptpath,'local')
def start_Xvfb(**kwargs):
vdisplay = Xvfb(**kwargs)
vdisplay.start()
return vdisplay
def Xvfb_screenshot(filename, crop=None, log=True):
"""
Saves a screen shot of the Xvfb screen buffer to file filename
"""
# See
# https://unix.stackexchange.com/questions/365268/how-do-i-take-a-screen-shot-of-my-xvfb-buffer
# https://www.geeksforgeeks.org/python-pil-image-crop-method/
if crop is not None:
tmp = tempfile.NamedTemporaryFile()
filename1 = tmp.name
#print(filename1)
else:
filename1 = filename
# Dump the screenshot
display=os.environ['DISPLAY']
xwdcmd="xwd -display %s -root -silent | convert xwd:- png:%s"%(display, filename1)
os.system(xwdcmd)
if crop is not None:
im = Image.open(filename1)
im1 = im.crop(crop)
im1.save(filename)
if log:
logging.info("genscreenshot: saving %s"%filename)
return
def start_instance(name, casedict, lock):
lock.acquire()
logging.info("Thread %s: starting", name)
case=amrwind.MyApp(configyaml=os.path.join(scriptpath,'config.yaml'),
localconfigdir=localconfigdir,
scriptpath=scriptpath,
title=title)
# -- maximize window ---
w,h=case.winfo_screenwidth(),case.winfo_screenheight()
case.geometry("%dx%d+0+0" % (w, h))
casedict[name]=case
lock.release()
case.mainloop()
logging.info("Thread %s: finishing", name)
def getwinpos(win):
"""
Returns the window position
"""
win.update()
x0, y0 = win.winfo_rootx(), win.winfo_rooty()
w, h = win.winfo_width(), win.winfo_height()
return (x0, y0, w, h)
def scrollcanvas(canvas, ypos):
canvas.update()
canvas.yview_moveto(ypos)
def gethelpmesg(yamlfile, key, basekey='helpwindows', mesgkey='mesg'):
with open(yamlfile) as stream:
try:
yamldata=yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
return yamldata[basekey][key][mesgkey]
def setAMRWindInputString(case, casename, key, extra='', comment=''):
"""
Returns the string
casename.setAMRWindInput('key', value)
"""
extraargs = '' if extra=='' else ', '+extra
addcomment = '' if comment=='' else ' # '+comment
valstr = repr(case.getAMRWindInput(key))
outstr = """%s.setAMRWindInput('%s', %s%s)%s\n"""
return outstr%(casename, key, valstr, extraargs, addcomment)