forked from soundmonster/samoklava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_ses.py
132 lines (105 loc) · 4.17 KB
/
import_ses.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
#
# UI automation script to run DRC on a KiCad PCBNew layout
# Sadly it is not possible to run DRC with the PCBNew Python API since the
# code is to tied in to the UI. Might change in the future.
#
# Copyright 2019 Productize SPRL
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import os
import logging
import argparse
import time
from xvfbwrapper import Xvfb
pcbnew_dir = os.path.dirname(os.path.abspath(__file__))
repo_root = os.path.dirname(pcbnew_dir)
sys.path.append(repo_root)
from util import file_util
from util.ui_automation import (
PopenContext,
xdotool,
wait_for_window,
recorded_xvfb,
clipboard_store
)
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def run_export(pcb_file, ses_file, output_file, record=True):
recording_file = os.path.join('/tmp', 'import_ses_screencast.ogv')
ses_input_file = os.path.abspath(ses_file)
save_as = False
pcb_output_file = None
if output_file is not None:
save_as = True
pcb_output_file = os.path.abspath(output_file)
if os.path.exists(pcb_output_file):
logger.error('PCB file already exists')
exit(-1)
xvfb_kwargs = {
'width': 800,
'height': 600,
'colordepth': 24,
}
with recorded_xvfb(recording_file, **xvfb_kwargs) if record else Xvfb(**xvfb_kwargs):
with PopenContext(['pcbnew', pcb_file], close_fds=True) as pcbnew_proc:
clipboard_store(ses_input_file)
window = wait_for_window('pcbnew', 'Pcbnew', 10, False)
logger.info('Focus main pcbnew window')
wait_for_window('pcbnew', 'Pcbnew')
# Needed to rebuild the menu, making sure it is actually built
xdotool(['windowsize', '--sync', window, '750', '600'])
wait_for_window('pcbnew', 'Pcbnew')
logger.info('File Menu')
xdotool(['keydown', 'alt'])
xdotool(['key', 'f'])
xdotool(['keyup', 'alt'])
xdotool(['key', 'i', 's'])
logger.info('Focus Import modal window')
wait_for_window('Import', 'Merge Specctra Session file')
logger.info('Pasting SES input file')
xdotool(['keydown', 'ctrl'])
xdotool(['key', 'a'])
xdotool(['key', 'v'])
xdotool(['keyup', 'ctrl'])
xdotool(['key', 'Return'])
# give the app some time to import
time.sleep(2)
if save_as:
logger.info('Saving as new file')
clipboard_store(pcb_output_file)
xdotool(['key', 'ctrl+shift+s'])
xdotool(['keydown', 'ctrl'])
xdotool(['key', 'a'])
xdotool(['key', 'v'])
xdotool(['keyup', 'ctrl'])
xdotool(['key', 'Return'])
else:
logger.info('Saving into existing file')
xdotool(['key', 'ctrl+s'])
time.sleep(2)
pcbnew_proc.terminate()
return ses_input_file
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='KiCad automated DRC runner')
parser.add_argument('kicad_pcb_file', help='KiCad layout file')
parser.add_argument('input_file', help='Input Specctra SES file')
parser.add_argument('--output-file', help='Output PCB file (optional, will save to original file if not set)',
required=False)
parser.add_argument('--record', help='Record the UI automation',
action='store_true'
)
args = parser.parse_args()
export_result = run_export(args.kicad_pcb_file, args.input_file, args.output_file, args.record)
logging.info(export_result);