Преглед на файлове

Helper scripts: Port to Python, fix perlcritic errors (#1431)

* Convert 'd.text/test.pl' to Python script
* Fix perlcritic warnings for 'utils/copywrite.pl' and 'vector/v.clean/test/v.rand.poly'
Owen Smith преди 4 години
родител
ревизия
27aa771f5e
променени са 4 файла, в които са добавени 113 реда и са изтрити 92 реда
  1. 0 59
      display/d.text/test.pl
  2. 79 0
      display/d.text/test.py
  3. 24 25
      utils/copywrite.pl
  4. 10 8
      vector/v.clean/test/v.rand.poly

+ 0 - 59
display/d.text/test.pl

@@ -1,59 +0,0 @@
-#!/usr/bin/env perl
-my @fonts = ("cyrilc","gothgbt","gothgrt","gothitt","greekc","greekcs","greekp",
-	"greeks","italicc","italiccs","italict","romanc","romancs","romand",
-	"romans","romant","scriptc","scripts","cyrilc","gothgbtlu");
-my @colors = ("red","orange","yellow","green","blue","indigo","violet", "black",
-	"gray","brown","magenta","aqua","grey","cyan","purple");
-
-sub rc{
-	printf ".X %s\n.Y %s\n", shift, shift;
-}
-sub xy{
-	printf ".X %s%\n.Y %s%\n", shift, shift;
-}
-sub font{
-	printf ".F %s\n", shift;
-}
-sub size{
-	printf ".S %s\n", shift;
-}
-sub color{
-	printf ".C %s\n", shift;
-}
-sub rotate{
-	printf ".R %s\n", shift;
-}
-sub align{
-	printf ".A %s\n", shift;
-}
-sub text{
-	$_ = shift;
-	s/^\./../mg;
-	s/\n$//;
-	print "$_\n";
-}
-
-size 4;
-for(my $i = 0; $i < 36; $i++){
-	font $fonts[$i%$#fonts];
-	size ((($i>=9&&$i<18)||$i>27?36-$i:$i)%9);
-	rotate $i*10;
-	color $colors[$i%$#colors];
-	xy 80+10*cos($i*10/180*3.141593), 50+10*640/480*sin($i*10/180*3.141593);
-	text ". $fonts[$i%$#fonts]";
-}
-size 2;
-rotate 0;
-font "romans";
-color "gray";
-rc 1, 1;
-
-undef $/;
-open FH, "test.pl";
-my $src = <FH>;
-close FH;
-
-$src =~ s/\n/\n.L 1\n.L 0\n/g;
-$src =~ s/(".*?")/\n.C red\n$1\n.C gray\n/g;
-$src = ".L 0\n$src";
-print $src;

+ 79 - 0
display/d.text/test.py

@@ -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)),
+    )
+)

+ 24 - 25
utils/copywrite.pl

@@ -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
 #
@@ -37,9 +43,9 @@ my $editor	= "xemacs";
 #   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
@@ -52,13 +58,6 @@ my $editor	= "xemacs";
 #
 #-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#-=-#
 
-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>};
@@ -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;
@@ -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;
@@ -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;
@@ -123,10 +122,10 @@ 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));
@@ -134,25 +133,25 @@ sub evaluate_file {
     # 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: $!";
 
@@ -181,7 +180,7 @@ __END__
  * 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

+ 10 - 8
vector/v.clean/test/v.rand.poly

@@ -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`;
@@ -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++){
@@ -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;
 
@@ -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
@@ -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
@@ -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";
 
     }
 }