Coverage for /media/ldata/code/tendril/tendril/boms/electronics.py: 0%

# Coverage for /media/ldata/code/tendril/tendril/boms/electronics.py : 0%

## 272 statements 0 run 272 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | #!/usr/bin/env python # encoding: utf-8 # Copyright (C) 2015 Chintalagiri Shashank # # This file is part of Tendril. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """ Electronic Boms Module documentation (:mod:`boms.electronics`) ============================================================== This module contains various classes for handling structured data present in elctronics BOMs. The module is built for use with gEDA with specific additions and/or constraints to the standard gEDA project structure. Use with other EDA tools should be possible by replacing ``gedaif`` with an EDA tool specific package. Module Summary: --------------- .. autosummary:: EntityBase EntityElnComp EntityElnGroup import_pcb Module Members: --------------- """ from tendril.utils import log logger = log.get_logger(__name__, log.INFO) import os import copy import tendril.gedaif.bomparser import tendril.gedaif.conffile import tendril.conventions.electronics from outputbase import OutputElnBomDescriptor from outputbase import OutputBom from entitybase import EntityBase from entitybase import EntityGroupBase from entitybase import EntityBomBase class EntityElnComp(EntityBase): """Object containing a single electronic component. Accept a ``gedaif.bomparser.BomLine`` generated through gnetlist's ``bom`` backend. The ``attribs`` file should atleast contain: * device * value * footprint * fillstatus :param item: BomLine containing details of component to be created :type item: gedaif.bomparser.BomLine """ def __init__(self, item=None): super(EntityElnComp, self).__init__() self._device = "" self._value = "" self._footprint = "" self._fillstatus = "" if item is not None: self.define(item.data['refdes'], item.data['device'], item.data['value'], item.data['footprint'], item.data['fillstatus']) def define(self, refdes, device, value, footprint="", fillstatus=""): """ Define the component. Can be used directly for special cases when there is no `tendril.gedaif.bomparser.BomLine` to pass to the class `__init__` function. :param refdes: Refdes string. :param device: Device string. :param value: Value string. :param footprint: Footprint string. Optional. :param fillstatus: Fillstatus string. Optional. """ self.refdes = refdes self.device = device self.value = value self.footprint = footprint self.fillstatus = fillstatus self._defined = True @property def fillstatus(self): """ Fillstatus string. When ``fillstatus`` is ``DNP``, the component is not included in BOMs irrespective of other configuration states. """ return self._fillstatus @fillstatus.setter def fillstatus(self, value): if value == 'DNP': self._fillstatus = 'DNP' elif value in ['unknown', '']: self._fillstatus = '' elif value == 'CONF': self._fillstatus = 'CONF' else: logger.warning("Unsupported fillstatus: " + value) @property def ident(self): return tendril.conventions.electronics.ident_transform(self.device, self.value, self.footprint) @property def device(self): """ Component device string. """ return self._device @device.setter def device(self, value): self._device = value @property def value(self): """ Component value string. """ return self._value @value.setter def value(self, value): self._value = value @property def footprint(self): """ Component footprint string. ``MY-`` at the beginning of the footprint string is stripped away automatically. """ return self._footprint @footprint.setter def footprint(self, value): if value is None: return if value[0:3] == "MY-": self._footprint = value[3:] else: self._footprint = value class EntityElnGroup(EntityGroupBase): """ Container for a group of EntityElnComp objects. :ivar groupname: Name of the group :ivar eln_comp_list: List of EntityElnComp objects """ def __init__(self, groupname, contextname): super(EntityElnGroup, self).__init__(groupname, contextname) pass def insert(self, item): """ Insert an electronic component into the EntityGroup. Accept a BomLine and generate an EntityElnComp to represent the component if it's ``fillstatus`` is not ``DNP``. Insert the created EntityElnComp object into the group. :param item: ``BomLine`` representing the item to insert. :type item: gedaif.bomparser.BomLine """ if item.data['fillstatus'] != 'DNP': x = EntityElnComp(item) if x.defined: self.complist.append(x) def insert_eln_comp(self, comp): """ Insert a manually created component into the EntityGroup. This should be used for components not originating directly from gEDA's gnetlist output. :param comp: Existing component to insert :type comp: EntityElnComp """ if comp.defined: self.complist.append(comp) class EntityElnBomConf(object): def __init__(self, configfile): super(EntityElnBomConf, self).__init__() self._configfile = configfile configdata = configfile.configdata self.pcbname = configdata["pcbname"] self.rawconfig = configdata @property def grouplist(self): return self._configfile.grouplist @property def motiflist(self): return self._configfile.motiflist @property def sjlist(self): return self._configfile.sjlist @property def configurations(self): return self._configfile.configurations @property def configsections(self): return self._configfile.configsections def get_configurations(self): rval = [] for configuration in self.configurations: rval.append(configuration["configname"]) return rval def get_configsections(self): return self._configfile.get_configsections def get_sec_groups(self, sectionname, config): return self._configfile.get_sec_groups(sectionname, config) def get_configuration(self, configname): return self._configfile.config_grouplist(configname) def get_configuration_motifs(self, configname): for configuration in self.configurations: if configuration["configname"] == configname: try: return configuration["motiflist"] except KeyError: logger.debug( "Configuration missing motiflist : " + configname ) return None raise ValueError def get_configuration_gens(self, configname): for configuration in self.configurations: if configuration["configname"] == configname: try: return configuration["genlist"] except KeyError: logger.debug( "Configuration missing genlist : " + configname ) return None raise ValueError def get_configuration_sjs(self, configname): if self.sjlist is not None: sjlist = copy.copy(self.sjlist) else: sjlist = None for configuration in self.configurations: if configuration['configname'] == configname: try: if configuration['sjlist'] is not None: sjlist.update(configuration['sjlist']) return sjlist return configuration['sjlist'] except KeyError: logger.debug( "Configuration missing SJ list : " + configname ) return sjlist raise ValueError class EntityElnBom(EntityBomBase): def __init__(self, configfile): """ :type configfile: gedaif.conffile.ConfigsFile """ super(EntityElnBom, self).__init__() self.pcbname = configfile.configdata["pcbname"] self.projfile = configfile.configdata["projfile"] self.projfolder = configfile.projectfolder self.configurations = EntityElnBomConf(configfile) self._motifs = [] self._configured_for = None self.create_groups() self.populate_bom() def create_groups(self): groupnamelist = [x['name'] for x in self.configurations.grouplist] if 'default' not in groupnamelist: x = EntityElnGroup('default', self.pcbname) self.grouplist.append(x) for group in groupnamelist: logger.debug("Creating Group: " + str(group)) x = EntityElnGroup(group, self.pcbname) self.grouplist.append(x) def find_tgroup(self, item): """ :rtype : EntityElnGroup """ gname = item.data['group'] if gname == 'unknown': gname = 'default' if gname not in [x['name'] for x in self.configurations.grouplist]: logger.warning( "Could not find group in config file : " + gname ) gname = 'default' for group in self.grouplist: if group.groupname == gname: return group def find_group(self, groupname): """ :rtype : EntityElnGroup """ for group in self.grouplist: if group.groupname == groupname: return group def populate_bom(self): if self.configurations.pcbname is not None: tgroup = self.find_group('default') comp = EntityElnComp() comp.define('PCB', 'PCB', self.configurations.pcbname) tgroup.insert_eln_comp(comp) parser = tendril.gedaif.bomparser.MotifAwareBomParser( self.projfolder, "bom" ) for item in parser.line_gen: if item.data['device'] == 'TESTPOINT': continue tgroup = self.find_tgroup(item) skip = False if item.data['footprint'] == 'MY-TO220': comp = EntityElnComp() comp.define('HS' + item.data['refdes'][1:], 'HEAT SINK', 'TO220') tgroup.insert_eln_comp(comp) if item.data['device'] == 'MODULE LCD' and \ item.data['value'] == "CHARACTER PARALLEL 16x2" and \ item.data['footprint'] == "MY-MTA100-16": comp = EntityElnComp() comp.define( item.data['refdes'], 'CONN SIP', "16PIN PM ST", "MY-MTA100-16" ) tgroup.insert_eln_comp(comp) skip = True if not skip: tgroup.insert(item) for item in parser.motif_gen: self._motifs.append(item) def get_motif_by_refdes(self, refdes): for motif in self._motifs: if refdes == motif.refdes: return motif return None @property def configured_for(self): return self._configured_for def configure_motifs(self, configname): self._configured_for = configname motifconfs = self.configurations.get_configuration_motifs(configname) if motifconfs is not None: for key, motifconf in motifconfs.iteritems(): motif = self.get_motif_by_refdes(key) if motif is None: logger.error("Motif not defined : " + key) continue motifconf_act = motif.get_configdict_stub() if self.configurations.motiflist is not None: basemotifconfs = self.configurations.motiflist for bkey, baseconf in basemotifconfs.iteritems(): if bkey == key: logger.debug( "Found Base Configuration for : " + key ) motifconf_act.update(baseconf) motifconf_act.update(motifconf) motif.configure(motifconf_act) def create_output_bom(self, configname): if configname not in self.configurations.get_configurations(): raise ValueError outbomdescriptor = OutputElnBomDescriptor( self.pcbname, self.projfolder, configname, self.configurations ) outbom = OutputBom(outbomdescriptor) outgroups = self.configurations.get_configuration(configname) genlist = self.configurations.get_configuration_gens(configname) gen_refdeslist = None if genlist is not None: gen_refdeslist = genlist.keys() sjlist = self.configurations.get_configuration_sjs(configname) sj_refdeslist = None if sjlist is not None: sj_refdeslist = sjlist.keys() for group in outgroups: grpobj = self.find_group(group) if grpobj is None: logger.critical("outgroups : " + str(outgroups)) logger.critical( "grpobj not found : " + str(group) + ":for " + configname ) for comp in grpobj.complist: if gen_refdeslist is not None and \ comp.refdes in gen_refdeslist: if tendril.conventions.electronics.fpiswire(comp.device): comp.footprint = genlist[comp.refdes] else: comp.value = genlist[comp.refdes] if sj_refdeslist is not None and comp.refdes in sj_refdeslist: if not comp.fillstatus == 'CONF': logger.error("sjlist attempts to change " "non-configurable SJ : " + comp.refdes) raise AttributeError if sjlist[comp.refdes]: logger.debug("Setting Fillstatus : " + comp.refdes) comp.fillstatus = '' else: logger.debug("Clearing Fillstatus : " + comp.refdes) comp.fillstatus = 'DNP' outbom.insert_component(comp) motifconfs = self.configurations.get_configuration_motifs(configname) if motifconfs is None: outbom.sort_by_ident() return outbom self.configure_motifs(configname) for key, motifconf in motifconfs.iteritems(): motif = self.get_motif_by_refdes(key) if motif is None: logger.error("Motif not defined : " + key) continue for item in motif.get_line_gen(): if item.data['group'] in outgroups and \ item.data['fillstatus'] != 'DNP': outbom.insert_component(EntityElnComp(item)) outbom.sort_by_ident() return outbom def import_pcb(cardfolder): """Import PCB and return a populated EntityBom Accept cardfolder as an argument and return a populated EntityBOM. The cardfolder should be the path to a PCB folder, containing the file structure described in ``somewhere``. .. seealso:: - ``gedaif.projfile.GedaProjectFile`` - ``gEDA Project Folder Structure`` :param cardfolder: PCB folder (containing schematic, pcb, gerber) :type cardfolder: str :return: Populated EntityBom :rtype: EntityBom :Example: >>> import tendril.boms.electronics >>> bom = tendril.boms.electronics.import_pcb('path/to/cardfolder') """ cardfolder = os.path.abspath(cardfolder) pcbbom = None configfile = tendril.gedaif.conffile.ConfigsFile(cardfolder) if configfile.configdata is not None: pcbbom = EntityElnBom(configfile) return pcbbom if __name__ == "__main__": pass