Skip to content

Commit

Permalink
Helper scripts: Port to Python, fix perlcritic errors (#1431)
Browse files Browse the repository at this point in the history
* Convert 'd.text/test.pl' to Python script
* Fix perlcritic warnings for 'utils/copywrite.pl' and 'vector/v.clean/test/v.rand.poly'
  • Loading branch information
Owen Smith committed Apr 1, 2021
1 parent f190e2d commit 27aa771
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 92 deletions.
59 changes: 0 additions & 59 deletions display/d.text/test.pl

This file was deleted.

79 changes: 79 additions & 0 deletions display/d.text/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
# Author: Owen Smith - Rewritten from test.pl by Huidae Cho
# Run: d.mon start=wx0 && ./test.py | d.text at=0,100
import math
import re

# Quiet black syntax checking for fonts and colors to keep the code printed to
# the display vertically short.
# fmt: off
fonts = ("cyrilc", "gothgbt", "gothgrt", "gothitt", "greekc", "greekcs",
"greekp", "greeks", "italicc", "italiccs", "italict", "romanc",
"romancs", "romand", "romans", "romant", "scriptc", "scripts",
"cyrilc", "gothgbtlu")
colors = ("red", "orange", "yellow", "green", "blue", "indigo", "violet",
"black", "gray", "brown", "magenta", "aqua", "grey", "cyan",
"purple")
# fmt: on


def rc(r, c):
print(f".X {r}\n.Y {c}")


def xy(x, y):
print(f".X {x}%\n.Y {y}%")


def font(f):
print(f".F {f}")


def size(s):
print(f".S {s}")


def color(c):
print(f".C {c}")


def rotate(r):
print(f".R {r}")


def align(a):
print(f".A {a}")


def text(in_text):
print(f"{in_text}")


for i in range(36):
font(fonts[int(i % len(fonts))])
size(((36 - i if (i >= 9 and i <= 18 or i > 27) else i) % 9))
rotate(i * 10)
color(colors[i % len(colors)])
xy(
(80 + 10 * math.cos(i * 10 / 180 * 3.141593)),
(50 + 10 * 640 / 480 * math.sin(i * 10 / 180 * 3.141593)),
)
text(fonts[int(i % len(fonts))])

size(2)
rotate(0)
font("romans")
color("gray")
rc(1, 1)

with open(__file__) as f:
src = f.read()

print(
".L 0\n"
+ re.sub(
'(".*?")',
"\n.C red\n,\\g<0>\n.C gray\n",
re.sub("\n", "\n.L 1\n.L 0\n", re.sub("(?m)^#.*\n?", "", src)),
)
)
49 changes: 24 additions & 25 deletions utils/copywrite.pl
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@
## Please run from top source code directory.
## For usage details, see below.

use File::Find;
use File::Basename;
use strict;
use warnings;
use Cwd;
use File::Basename;

#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#
#
# edit this, and edit the bottom of the file after __END__

# my $editor = "vi -o";
#my $editor = "nedit";
# my $editor = "nedit";
my $editor = "xemacs";

# don't forget to get a copy of
# don't forget to get a copy of
# http://www.red-bean.com/cvs2cl/cvs2cl.pl
# and put it in the same directory as this script
#
Expand All @@ -37,9 +43,9 @@
# the format of the file should be "username,identity", one per line
# The contributors.csv is already in the main directory of the GRASS
# source code, so it should be found.
#

####
# to work offline:
# to work offline:
# $ export PATH=$PATH:$(pwd)
# $ find . -name main.c | while read i; do \
# echo $i; (cd `dirname $i`; cvs2cl.pl -f ChangeLog.tmp main.c); done
Expand All @@ -52,13 +58,6 @@
#
#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#

use File::Find;
use File::Basename;
use Cwd;
use strict;
use warnings;
use File::Basename;

my $ChangeLog = "ChangeLog.tmp";
my $cvs2cl = getcwd . "/cvs2cl.pl -f $ChangeLog";
my $template = do {local $/; <DATA>};
Expand All @@ -73,7 +72,7 @@ sub evaluate_file {
warn "+ Reading $name\n";

# slurp main.c
my $main_c = do {local $/; open F, $_ or die "reading $name: $!"; <F>};
my $main_c = do {local $/; open my $F, '<', $_ or die "reading $name: $!"; <$F>};

# continue if main.c is 25 lines or longer
return unless ($main_c =~ y/\n/\n/) >= 25;
Expand All @@ -95,8 +94,8 @@ sub evaluate_file {
{
# Read whole paras at once
local $/ = "";
open LOG, $ChangeLog or die "$ChangeLog: $!";
while (my $line = <LOG>) {
open my $LOG, '<', $ChangeLog or die "$ChangeLog: $!";
while (my $line = $LOG) {

# Hack out the pretty printing
$line =~ s/\s+/ /gos;
Expand All @@ -110,7 +109,7 @@ sub evaluate_file {
$user = $2;
$username{$user}++;
}

# Note when a user did the initial check in
# if ($line =~ /^\s+\*\s+[^:]+:\s+(?:initial|new$)/gios) {
# $original_author = $user;
Expand All @@ -123,36 +122,36 @@ sub evaluate_file {
}
}
}

# don't duplicate the original author
delete $username{$original_author} if $original_author;

#figure out module name
my $fullname = $name;
my $mymodule = basename(dirname($fullname));

# append the list of contributors
my @years = sort map {$_ + 0} keys %year;
my @authors = sort keys %username;

# map committers to identities
@authors = map { exists( $identity{$_} ) ? $identity{$_} : $_ } @authors;
$original_author = $identity{$original_author}
if exists $identity{$original_author};

# execute the template
{
{
local $" = ", ";
$main_c = eval(
"<<END_OF_TEMPLATE\n" . $template . "\nEND_OF_TEMPLATE" );
$main_c = eval{
"<<END_OF_TEMPLATE\n" . $template . "\nEND_OF_TEMPLATE" };
}

# write the new version of main.c
warn "+ Rewriting $name\n";
open F, ">$_" or die "writing $name: $!";
open my $F, '>', $_ or die "writing $name: $!";
print F $main_c;
close F;

# load it up in an editor for vetting
system "$editor $_ $ChangeLog description.html" and die "$editor $_ $ChangeLog: $!";

Expand Down Expand Up @@ -181,7 +180,7 @@ sub evaluate_file {
* MODULE: $mymodule
* AUTHOR(S): $original_author (original contributor)
* @authors
* PURPOSE:
* PURPOSE:
* COPYRIGHT: (C) $years[0]-$years[-1] by the GRASS Development Team
*
* This program is free software under the GNU General Public
Expand Down
18 changes: 10 additions & 8 deletions vector/v.clean/test/v.rand.poly
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/perl
# Generate GRASS 5.7 ascii file containing random closed polygons
# Generated vector may be used for tests of v.clean
# Generated vector may be used for tests of v.clean

use strict;

$gisdbase = `g.gisenv get=GISDBASE`;
$location_name = `g.gisenv get=LOCATION_NAME`;
Expand All @@ -11,7 +13,7 @@ chomp ($gisdbase); chomp ($location_name); chomp ($mapset);
$loc = "$gisdbase/$location_name/$mapset";

$npoly = 100; # number of polygons
$size = 0.1; # maximum size of polygon as a portion of region
$size = 0.1; # maximum size of polygon as a portion of region
$nseg = 50; # number of segments in polygon

for($i=0; $i<=$#ARGV; $i++){
Expand All @@ -25,7 +27,7 @@ $ymin = `g.region -g | grep ^s= `; chomp $ymin; $ymin =~ s/s=(.*)/$1/;
$xmax = `g.region -g | grep ^e= `; chomp $xmax; $xmax =~ s/e=(.*)/$1/;
$xmin = `g.region -g | grep ^w= `; chomp $xmin; $xmin =~ s/w=(.*)/$1/;

# Center
# Center
$xc = ($xmax + $xmin) / 2;
$yc = ($ymax + $ymin) / 2;

Expand All @@ -39,12 +41,12 @@ $maxsize = $dmin * $size;
$minsize = $maxsize * 0.7;

# Minimum and maximum radius of centers of polygons
$rcmax = $dmin / 2 - $maxsize;
$rcmax = $dmin / 2 - $maxsize;
$rcmin = $rcmax * 0.2;

$fascii = "$loc/dig_ascii/clean.test";
mkdir "$loc/dig_ascii/";
open(ASCII, ">$fascii") or die "Cannot open: $fascii\n";
open my $ASCII, '>', $fascii or die "Cannot open: $fascii\n";

print ASCII "ORGANIZATION: GRASS Development Team
DIGIT DATE: 1/9/2002
Expand All @@ -55,7 +57,7 @@ MAP SCALE: 10000
OTHER INFO: Random closed polygons
ZONE: 0
MAP THRESH: 0.500000
VERTI:\n";
VERTI:\n";

for ( $i = 0; $i < $npoly; $i++ ) {
# random center of polygon
Expand All @@ -74,11 +76,11 @@ for ( $i = 0; $i < $npoly; $i++ ) {
$r = rand ( $maxsize - $minsize ) + $minsize;
} else {
$ang = 0; # last point
$r = ($maxsize + $minsize) / 2;
$r = ($maxsize + $minsize) / 2;
}
$x = $xpc + $r * cos ( $ang );
$y = $ypc + $r * sin ( $ang );
print ASCII " $y $x\n";
print ASCII " $y $x\n";

}
}
Expand Down

0 comments on commit 27aa771

Please sign in to comment.