delorie.com/archives/browse.cgi   search  
Mail Archives: geda-user/2016/01/11/11:01:10

X-Authentication-Warning: delorie.com: mail set sender to geda-user-bounces using -f
X-Recipient: geda-user AT delorie DOT com
Subject: Re: [geda-user] Searching for refdes's having a specific attribute?
To: geda-user AT delorie DOT com
References: <568F9808 DOT 5040808 AT envinsci DOT co DOT uk>
<alpine DOT DEB DOT 2 DOT 11 DOT 1601111626180 DOT 4406 AT nimbus>
From: "M. J. Everitt (m DOT j DOT everitt AT iee DOT org) [via geda-user AT delorie DOT com]" <geda-user AT delorie DOT com>
Message-ID: <5693D1A8.2040703@iee.org>
Date: Mon, 11 Jan 2016 16:00:40 +0000
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101
Thunderbird/38.2.0
MIME-Version: 1.0
In-Reply-To: <alpine.DEB.2.11.1601111626180.4406@nimbus>
X-Provags-ID: V03:K0:udZaW/ChPrjY5dk5Tjb8KAHPXnZDg9ikE5qRtDmJK+18bz3aMYv
mu/MbJ8BB/gzKKgDcQeF4nX4U9RZTuq5hFXZ+2l1HHpg1Q26yQuDpOpR+zKhmTipr4235UO
5+N/+3x4ttTZuUlmAtZEUfvBZa/dxMUQoOwj3K+Qe0LLJ3v4ZTEeZr+MoSoNuojI2s6LK5a
zqhGRvx/3u7jEl6QAfAnw==
X-UI-Out-Filterresults: notjunk:1;V01:K0:iJ7OasBEi9M=:GHnIe0t2NBrcBvQQ/vXDEB
2OUPuc21nEfVFuoHGmiypXOy8ZNUMtpLA2893PhXyeaQoD8TJ62O4Yn0vOQaMOVrDSPbdz9pU
0iSH42HhrsW9iJa2pXxSujzOsRvlhuCTO6L5JhpoTkoojWxaCOfNPqTfvgtwI2xU+liKQ1E2b
Lud/42T1zMy9juIGXNQVFoG62h4euhPK+L00YCyq4IOAyZcGZD9JDKUTojKAvUNjT0bXXG/bI
PB+j7OxbSKbG2r8ET0noHTT2QetOLNZPiixwj3AhXfiZg4GnU3YjuxowWVH+nQFkUpsRc5jt+
pXwaYCkhTcETSJ9m2oLQ87jyMWO+jSnEbbw1Xzl8Vi8fCBJ89mANKRCuMN2iNgqeipgb4x7kL
OAQcOSpZjqGKyR9+VbthxV7DiGz4RnWLQHRRnFbl8STM/zSt9H5oBzcP29WonvLD+AHRFrN6O
M/dN/M9GwNCrqlJsl90zph2c+6dyywghmISJmlrmq2Jk93zjUxFmSnY4gOQ8O9YDiTk2SZBEm
sixsnPuaMc24ampvIietJobGjKcXkKiJoeCrP0YBuxi+6QEfUq71cdY4wi5ePu7ryIjop8ivd
iIDbwOk00P+57mRHFDLRjgQJy7KYPse1zXDE/yBQn+8x9UU7YRFTCL+6twbLbzcB1RoJ3PzKx
zJWqyEh9RVETRwGqEGT/CpUOEBqgCFCf/3neLoQqqI9gEWgm5Zb4Go3YRVxh/6XJxzycfsDxF
UzLoHzdmB25zTDtD
Reply-To: geda-user AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: geda-user AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On 11/01/16 15:26, Roland Lutz wrote:
> On Fri, 8 Jan 2016, Matt Rhys-Roberts
> (matt DOT rhys-roberts AT envinsci DOT co DOT uk) [via geda-user AT delorie DOT com] wrote:
>> How could I search a directory of .sch schematic sheets for
>> components that contain a specific attribute, e.g. OrderCode=1234567,
>> please?
>
> This is a nice example of where the new Xorn libraries come in handy. :)
>
> To use this, you need a recent git version of gEDA/gaf, and you have
> to either install gEDA/gaf to a standard location or add the
> subdirectory "xorn/built-packages" to the environment variable
> PYTHONPATH.  Then you can search for matching components like this:
>
>
> import xorn.geda.attrib
> import xorn.geda.read
>
> sch = xorn.geda.read.read('schematic.sch')
> for ob in sch.toplevel_objects():
>     if isinstance(ob.data(), xorn.storage.Component) and \
>         '1234567' in xorn.geda.attrib.search_attached(ob, 'OrderCode'):
>             print xorn.geda.attrib.search_attached(ob, 'refdes')[0]
>
>
> If you want to search a complete directory, here is how you could
> write a command-line utility which does that:
>
>
> #!/usr/bin/python2.7
> import os, sys
> import xorn.geda.attrib
> import xorn.geda.read
>
> path = sys.argv[1]
> name = sys.argv[2]
> value = sys.argv[3]
>
> for dirpath, dirnames, filenames in os.walk(path):
>     for filename in filenames:
>         if not filename.endswith('.sch'):
>             continue
>         sch = xorn.geda.read.read(os.path.join(path, filename))
>         for ob in sch.toplevel_objects():
>             if isinstance(ob.data(), xorn.storage.Component) and \
>                value in xorn.geda.attrib.search_attached(ob, name):
>                 print xorn.geda.attrib.search_attached(ob, 'refdes')[0]
>
>
> You can call it like "./ordercode . OrderCode 1234567" to search the
> current directory for components with OrderCode=1234567.
>
>> Basically, I need to fit components manually to a board, one specific
>> type at a time, to keep production neat. So I need to list them by
>> unique order code, ideally.
>
> So what you ultimately want is a list of refdes's for each order code?
> You could compile that list like this:
>
>
> #!/usr/bin/python2.7
> import os, sys
> import xorn.geda.attrib
> import xorn.geda.read
>
> path = sys.argv[1]
> name = sys.argv[2]
> result = {}
>
> for dirpath, dirnames, filenames in os.walk(path):
>     for filename in filenames:
>         if not filename.endswith('.sch'):
>             continue
>         sch = xorn.geda.read.read(os.path.join(path, filename))
>         for ob in sch.toplevel_objects():
>             if not isinstance(ob.data(), xorn.storage.Component):
>                 continue
>             try:
>                 refdes = xorn.geda.attrib.search_attached(ob,
> 'refdes')[0]
>             except IndexError:
>                 refdes = None
>             for value in xorn.geda.attrib.search_attached(ob, name):
>                 if value not in result:
>                     result[value] = []
>                 result[value].append(refdes)
>
> for k in sorted(result.keys()):
>     print '%s: %s' % (k, ' '.join(result[k]))
>
>
> Call this script like "./ordercodes . OrderCode" to print a list of
> order codes and associated refdes' for the current directory.
>
> Please note that, like any of the proposed solutions, this does not
> find OrderCode= attributes which are inherited from non-embedded
> symbols.  If you want to search these, too, have a look at this mail:
>
>   http://article.gmane.org/gmane.comp.cad.geda.user/50021
>
> If you loaded the schematic this way, you can replace
> "search_attached" with "search_all" to get inherited attributes, too.
>
Roland, I commend your python, but I fear it's beyond the reach of the
average user who wants a 2 minute solution!! Assuming your code does
exactly as the OP suggested, thankfully you've brought down the several
hours of work to get to that point to a miniscule 5 minutes of
"copy/paste/execute" :) .

Matt, if you're still with us, did you fix your need appropriately, and
what solution did you end up using?!

MJE

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019