[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: gEDA-user: non-interactive attribute editor



On 4/21/06, Levente <levente.kovacs@xxxxxxxxxxxx> wrote:
> Hi,

> My task is to add a few, same attribute to a bunch of symbols.
>
> Levente

The script below will output a symbol (STDOUT) consisting of the original symbol
file followed by new attribute lines. The new attribute lines are
defined in the __DATA__
section of the script.

N.B. This script was written on 21-Apr-2006 B.C. (Before Cappuccino). Use with
        caution.

(* jcl *)

--
http://www.luciani.org

#!/usr/bin/perl

# Copyright (C) 2006 John C. Luciani Jr.

### Permission is granted to make and distribute verbatim copies of
### this software provided that (1) no fee is charged and (2) the
### original copyright notice and this permission notice are preserved
### on all copies.

### Permission is granted to make and distribute modified versions of
### this software under the conditions for verbatim copying provided
### that the entire resulting derived work is distributed under terms
### of a permission notice identical to this one.

use strict;
use warnings;
use Tie::IxHash;
use Data::Dumper;

use Carp;

# Reads a gschem symbol and adds new attribute lines at the end.  Each
# new line is placed above the last attribute line that was read.

# The new attribute lines are defined in the __DATA__ section.

use constant YINC => 500; # spacing for the new attribute lines

my @Attr_lines;

# see http://geda.seul.org/docs/current/fileformats/node12.html for
# a description of the values

my %Value;
tie %Value, "Tie::IxHash";

%Value = map { my @v = split; splice @v, 0, 2  } << "END" =~ /(.+)/g;
type           T                  # text
x              value
y              value
color           5       # ATTRIBUTE_COLOR
size           10       # Size in points
visibility      0       # =0 INVISIBLE =1 VISIBLE
show_name_value 0       # =0 SHOW_NAME_VALUE =1 SHOW_VALUE =2 SHOW_NAME
angle           0
alignment       0       # lower left
num_lines       1
END

# read the attribute lines

while (<DATA>) {
    s/\#.*//; # Remove comments
    s/^\s*//; # Remove leading spaces
    s/\s*$//; # Revove trailing spaces
    next unless length; # Skip empty lines
    push @Attr_lines, $_;
}

@ARGV = $ARGV[0] || 'test.sym';

my %Pos; # last X value

while (<>) {
    next unless /^\s*T\s+(\S+)\s+(\S+)/;
    ($Pos{x}, $Pos{y}) = ($1, $2); # save the last X and Y value
} continue {
    print;
}

foreach my $attr_line (@Attr_lines) {
    $Pos{y} += YINC;
    printf("%s\n", join(' ', map { /^(x|y)$/ ? $Pos{$1} : $Value{$_} }
keys %Value));
    print "$attr_line\n";
}


__DATA__
attr1=line 1
attr2=line 2


# Style (adapted from the Perl Cookbook, First Edition, Recipe 12.4)

# 1. Names of functions and local variables are all lowercase.
# 2. The program's persistent variables (either file lexicals
#    or package globals) are capitalized.
# 3. Identifiers with multiple words have each of these
#    separated by an underscore for readability.
# 4. Constants are all uppercase.
# 5. If the arrow operator (->) is followed by either a
#    method name or a variable containing a method name then
#    there is a space before and after the operator.