|
| 1 | + |
| 2 | +import os |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +import logging |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +from kiplot import kiplot |
| 10 | +from kiplot import config_reader |
| 11 | + |
| 12 | + |
| 13 | +KICAD_PCB_EXT = '.kicad_pcb' |
| 14 | + |
| 15 | + |
| 16 | +class KiPlotTestContext(object): |
| 17 | + |
| 18 | + def __init__(self, test_name): |
| 19 | + self.cfg = None |
| 20 | + |
| 21 | + # The name used for the test output dirs and other logging |
| 22 | + self.test_name = test_name |
| 23 | + |
| 24 | + # The name of the PCB board file (will be interpolated into the plot |
| 25 | + # files by pcbnewm so we need to know |
| 26 | + self.board_name = None |
| 27 | + |
| 28 | + # The actual board file that will be loaded |
| 29 | + self.board_file = None |
| 30 | + |
| 31 | + # The directory under which to place plots (None: use a temp dir) |
| 32 | + self.plot_dir = pytest.config.getoption('plot_dir') |
| 33 | + |
| 34 | + # The actual output dir for this plot run |
| 35 | + self._output_dir = None |
| 36 | + # Clean the output dir afterwards (true for temp dirs) |
| 37 | + self._del_dir_after = self.plot_dir is None |
| 38 | + |
| 39 | + def _get_text_cfg_dir(self): |
| 40 | + |
| 41 | + this_dir = os.path.dirname(os.path.realpath(__file__)) |
| 42 | + |
| 43 | + return os.path.join(this_dir, '../yaml_samples') |
| 44 | + |
| 45 | + def _get_board_cfg_dir(self): |
| 46 | + |
| 47 | + this_dir = os.path.dirname(os.path.realpath(__file__)) |
| 48 | + |
| 49 | + return os.path.join(this_dir, '../board_samples') |
| 50 | + |
| 51 | + def load_yaml_config_file(self, filename): |
| 52 | + """ |
| 53 | + Reads a config from a YAML file |
| 54 | + """ |
| 55 | + |
| 56 | + cfg_file = os.path.join(self._get_text_cfg_dir(), filename) |
| 57 | + |
| 58 | + cr = config_reader.CfgYamlReader() |
| 59 | + |
| 60 | + with open(cfg_file) as cf_file: |
| 61 | + cfg = cr.read(cf_file) |
| 62 | + |
| 63 | + self.cfg = cfg |
| 64 | + |
| 65 | + def _load_board_file(self, filename=None): |
| 66 | + """ |
| 67 | + Load the named board. |
| 68 | +
|
| 69 | + @param filename: a filename to load, or None to load the relevant |
| 70 | + board name from the board sample dir |
| 71 | + """ |
| 72 | + |
| 73 | + if filename is None: |
| 74 | + self.board_file = os.path.join(self._get_board_cfg_dir(), |
| 75 | + self.board_name + KICAD_PCB_EXT) |
| 76 | + else: |
| 77 | + self.board_file = filename |
| 78 | + |
| 79 | + assert os.path.isfile(self.board_file) |
| 80 | + |
| 81 | + def _set_up_output_dir(self): |
| 82 | + |
| 83 | + if not self.plot_dir: |
| 84 | + # create a tmp dir |
| 85 | + self.output_dir = tempfile.mkdtemp( |
| 86 | + prefix='tmp_kiplot_{}'.format(self.test_name)) |
| 87 | + |
| 88 | + else: |
| 89 | + self.output_dir = os.path.join(self.plot_dir, self.test_name) |
| 90 | + # just create the dir |
| 91 | + if os.path.isdir(self.output_dir): |
| 92 | + # exists, that's OK |
| 93 | + pass |
| 94 | + else: |
| 95 | + os.makedirs(self.output_dir) |
| 96 | + |
| 97 | + self.cfg.outdir = self.output_dir |
| 98 | + logging.info(self.output_dir) |
| 99 | + |
| 100 | + def clean_up(self): |
| 101 | + |
| 102 | + if self._del_dir_after: |
| 103 | + shutil.rmtree(self.output_dir) |
| 104 | + |
| 105 | + def do_plot(self): |
| 106 | + |
| 107 | + self.cfg.validate() |
| 108 | + |
| 109 | + self._load_board_file(self.board_file) |
| 110 | + |
| 111 | + self._set_up_output_dir() |
| 112 | + |
| 113 | + plotter = kiplot.Plotter(self.cfg) |
| 114 | + plotter.plot(self.board_file) |
0 commit comments