delorie.com/archives/browse.cgi   search  
Mail Archives: geda-user/2016/01/19/08:09:53

X-Authentication-Warning: delorie.com: mail set sender to geda-user-bounces using -f
X-Recipient: geda-user AT delorie DOT com
X-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=mime-version:in-reply-to:references:date:message-id:subject:from:to
:content-type;
bh=ohphr8kqGo/1Ehw3tirGYDLtLd0PkGSU68hHQIHj0D8=;
b=yI1gfM6ARXA7n9NBqi8KyrpJLExOEv3tPK6wsDqe5pr3hFgCwe8pe09jZW34SxXdEQ
LZg8Vkhq4Y8YAnQWzCba5uOlea3ujeSHCRaEyKewnnRbSj1l4I78nYhEzWAFaf1hOlU7
JHO6vkTxMPKQaqWzwLzfjpP/5opwE1TkfApxjVlxfksDfNF/wrLhmXlz2dtyUyfgYtgX
sQCoDi7WRnU1WDfNiXf+gX1YRz/2d/T9/5MkAYp8CZ97qjJsg7EYUyemBUGIEVsZuq5Y
yRJPqQCb510y2zZlr8yImZOriBy8GWYbAqFB7zySTwG0MlBwkm4kqk7/ODZ2rAhCtwi6
uthA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20130820;
h=x-gm-message-state:mime-version:in-reply-to:references:date
:message-id:subject:from:to:content-type;
bh=ohphr8kqGo/1Ehw3tirGYDLtLd0PkGSU68hHQIHj0D8=;
b=UI+lzHzzrPCvf8DXOsB8KLFHwqz33wqjMD9dzh88w/GI3JZld5Abed5nNSzwz8Dc7O
ryGlGgTzygbWOiNHX5Fje/5xn5DWa93g3HLIJisYdMrUwM+l4pYIZhyGzaWdYlOQuqHr
0E+hhywU3jY1BOKRzM4a62mGFhBSFFP+CEWDlkH889o1j5IYrDdlGo7cOkfoELiy6q7b
6H3n8Yu4DRKeTZ8ODVeiTuGWE4woYeWzvnYV4+FM8UAyY72BLkfn3cYclzsheFxA4yFM
xhkhPm2ErUqcwxBQT/Q3wsFKguYBDkNzspQw7KEDQ/EQYu87yjp6hViJL2szul1EPeiM
MPTQ==
X-Gm-Message-State: AG10YOSoESp5JOX6BUufFjIDulQhePvdn0k8l3mRqY6dnNDSLBm7Fd93jbwdictE5aPn8FYxdEESKTj1ovG/Hg==
MIME-Version: 1.0
X-Received: by 10.28.97.11 with SMTP id v11mr20397929wmb.42.1453208925204;
Tue, 19 Jan 2016 05:08:45 -0800 (PST)
In-Reply-To: <20160103192006.1FBFE809D79B@turkos.aspodata.se>
References: <20160102091556 DOT BBC6D809D79B AT turkos DOT aspodata DOT se>
<CAMvDHVAoAnt3g6JBvgbnrR53LG6KpzWKBTmQgo1j+7ERvbESpg AT mail DOT gmail DOT com>
<20160103192006 DOT 1FBFE809D79B AT turkos DOT aspodata DOT se>
Date: Tue, 19 Jan 2016 16:08:44 +0300
Message-ID: <CAMvDHVDSVB=vJPrwqzBJPM5bMoSAJkixJp1ZMw3yZxAqEwPNkw@mail.gmail.com>
Subject: Re: [geda-user] Howto make a script in gaf (Was should we broaden
scope of libgeda)
From: "Vladimir Zhbanov (vzhbanov AT gmail DOT com) [via geda-user AT delorie DOT com]" <geda-user AT delorie DOT com>
To: geda-user AT delorie DOT com
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

Karl,

On 1/3/16, karl AT aspodata DOT se <karl AT aspodata DOT se> wrote:
...

> But how do I rean in an existing file so I can scan it for
> components ?

Typically, you doesn't want to read files yourself, because our
programs (gaf, gschem, gnetlist, ...) already do it
themselves. However, for dynamic processing, you may want it.

Currently, libgeda has no guile function to read a schematic file,
and this is probably right decision, because guile is not limited
in I/O by file I/O, but can use various ports. However, there are
two functions in libgeda which let you do what you want:

  string->page for input
    and
  page->string for output

So what you probably need is just some means to read strings from
files and save them to files. Perhaps the simplest way is using
the read-string function from the rdelim module. Run 'gaf shell':

$ gaf shell
scheme@(guile-user)> (use-modules (ice-9 rdelim) (geda page))
scheme@(guile-user)> (define (read-page name . port) (string->page
  name (apply read-string port)))
scheme@(guile-user)> (define (file->page name)
  (with-input-from-file name (lambda () (read-page name))))
scheme@(guile-user)> (active-pages)
$1 = ()
scheme@(guile-user)> (file->page "test.sch")
** Message: ...
$2 = #<geda-page 0x8425ae8>
scheme@(guile-user)> (active-pages)
$3 = (#<geda-page 0x8425ae8>)
scheme@(guile-user)>

As you can see, I've loaded two modules:
  (ice-9 rdelim) which makes available some simple means for eof
    delimited io and
  (geda page) which is responsible for working with pages (or
    files, if you want to name them so) in geda

Then I defined helper i/o functions as follows:
------------------------------------------------------------------
; Reads information from PORT and transforms its into a page NAME
(define (read-page name . port)
  (string->page name
    (apply rdelim:read-string port)))

; Outputs schematic PAGE to file NAME
(define (page->file page name)
  (with-output-to-file name
    (lambda () (display (page->string page)))))

; Reads file NAME and outputs PAGE
(define (file->page name)
  (with-input-from-file name (lambda () (read-page name))))
------------------------------------------------------------------

or even simpler:

------------------------------------------------------------------
(define (file->page name)
  (with-input-from-file
    name
    (lambda () (string->page name (read-string)))))
------------------------------------------------------------------

lambda() is here because the with-input-from-file function
requires the second argument to be a thunk (function without
arguments).


That's all!

You can sure use other means/ways to get strings, or use other i/o,
such as pipes (say, current input or output port, as gnetlist uses
internally for backends). You can save the functions into your own
module and use it afterwards.

Next, the 'active-pages' function shows the pages loaded.

To find components on the first page we first need to take
it. That is use (car (active-pages)). Or load the (srfi srfi-1)
module and use their selectors: first == car, second == cadr and
so on. Or just assign a name to the file->page result:

  (define mypage (file->page "mytest.sch"))

To sort out components, you have to load another module:

scheme@(guile-user)> (use-modules (geda object))
scheme@(guile-user)> (filter component? (page-contents mypage))
$2 = (#<geda-object 0x9f01538> #<geda-object 0xa0a0e60>
#<geda-object 0xa0ab008> #<geda-object 0xa094408> #<geda-object
0xa099310> #<geda-object 0xa099f28> #<geda-object 0xa2979f8>
#<geda-object 0xa298f00> #<geda-object 0xa29c4a0> #<geda-object
0xa29c568> #<geda-object 0xa2a18f8>)

The same way just assign the variable name to this list and you'll
be able to work only with components:

  (define components (filter component? (page-contents mypage)))

If you want to find their attached attributes, use the (geda attrib)
module and the 'object-attribs' function. For example, to get list
of lists of attribs for all page components, define the
'components' variable as above, and use:

  (map object-attribs components)

All the above will also work in gschem's REPL, and I believe in
gnetlist's one, too.

You can find several other tips/tricks here [1] and there [2].

Cheers,
  Vladimir

[1] http://wiki.geda-project.org/geda:guile_scripting
[2] https://github.com/vzh?tab=repositories

- Raw text -


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