X-Authentication-Warning: delorie.com: mail set sender to geda-user-bounces using -f X-Recipient: geda-user AT delorie DOT com Date: Mon, 11 Jan 2016 16:26:38 +0100 (CET) From: Roland Lutz To: "Matt Rhys-Roberts (matt DOT rhys-roberts AT envinsci DOT co DOT uk) [via geda-user AT delorie DOT com]" Subject: Re: [geda-user] Searching for refdes's having a specific attribute? In-Reply-To: <568F9808.5040808@envinsci.co.uk> Message-ID: References: <568F9808 DOT 5040808 AT envinsci DOT co DOT uk> User-Agent: Alpine 2.11 (DEB 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII 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 Precedence: bulk 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.