File: load_examples.pl

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (403 lines) | stat: -rwxr-xr-x 10,681 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/local/apps/perl/current/bin/perl -I/usr/local/lib/metaps/perl

use Getopt::Long;
use strict;

#--------------------------------------------------
#
#--------------------------------------------------

#The root page of examples in confluence!!!
my $rootPage="API examples";

my @langKeys=("f","p","c");

my %langName = ("f" => "Fortran 90",
             "c"  => "C",
             "p"  => "Python");

my %langType = ("f" => "none",
             "c"  => "cpp",
             "p"  => "python");

my %langSuffix = ("f" => "f90",
             "c"  => "c",
             "p"  => "py");

my %langDir = ("f" => "F90",
             "c"  => "C",
             "p"  => "python");

my %langComment = ("f" => "!",
             "c"  => "*",
             "p"  => "#");


my $CONF=$ENV{CONF};
die "Env var CONF should point to confluence.sh script" if ( $CONF eq "" || ! -f "$CONF" );

my $inRootDir;
my $confSpace="ECC";

#---------------------------------
# Read arguments
#---------------------------------

GetOptions("inDir=s" => \$inRootDir,
           "confSpace=s" => \$confSpace)
 	or die("Error in command line arguments\n");

print "confluence space=".$confSpace."\n";

#----------------------------------
# Dirs
#----------------------------------

#The root dir of the examples
$inRootDir="../examples";

#The genareted html files are stored here
my $htmlDir=$ENV{TMPDIR}."/res_html";


my %allExamples=read_examples();

#----------------------------------------
# Preparations
#----------------------------------------

unless ( -d  $htmlDir ) {
    mkdir $htmlDir	
}

#---------------------------------------------------
# Loop for the code types (e.g. grib, bufr, etc.)
#---------------------------------------------------

foreach my $cType (keys %allExamples) {

    print "----------------------------------------\n";
    print "  Processing examples for: $cType\n";
    print "----------------------------------------\n";

    #Get the examples
    my @examples=@{$allExamples{$cType}};

    if($#examples+1 == 0) {
        die "No examples are defined!\n";
    }

    #---------------------------------------------------
    # Create a parent page for the examples of this type
    #---------------------------------------------------

    my $parentPage=parentPageTitle($cType);
    makeParentPage($rootPage,$parentPage,$htmlDir,$cType,@examples);

    #--------------------------------
    # Loop for the examples
    #--------------------------------

    foreach my $name (@examples) {

        my $fOut=$htmlDir."/".$cType."_".$name.".html";
        open(OUT,">$fOut") or die "$fOut: $!";

        #Description
        print OUT "<h3>Description</h3>";
        print OUT "This example shows: <i>".getDescription($name)."</i>\n";

        #Create a tab container
        print OUT "<h3>Source code</h3>";
        print OUT "<ac:structured-macro ac:name=\"auitabs\"><ac:parameter ac:name=\"direction\">horizontal</ac:parameter><ac:rich-text-body>";

        #Add a tab for each language
        foreach my $i (@langKeys) {

            my $tabName=$langName{$i};
            my $fName=$name.".".$langSuffix{$i};
            my $fPath=$inRootDir."/".$langDir{$i}."/".$fName;
            my $lType=$langType{$i};

            #Read sourcode into html format
            my $c=getSourceCode($fName,$fPath,$lType);

            #If there is a source creates a tab for it
            if(length($c) > 0) {
                print OUT "<ac:structured-macro ac:name=\"auitabspage\"><ac:parameter ac:name=\"title\">".$tabName."</ac:parameter>
                <ac:rich-text-body>".$c."</ac:rich-text-body></ac:structured-macro>";
            }
        }

        #Close tab container
        print OUT  "</ac:rich-text-body></ac:structured-macro>";

        close OUT;

        #Upload the file to confluence
        my $pageTitle=examplePageTitle($name);
        loadToConf($fOut,$pageTitle,$parentPage);
    }
}

#===========================================================
#===========================================================
#
# FUNCTIONS
#
#===========================================================
#===========================================================

#====================================================
#
# Functions related to the examples
#
#====================================================

#------------------------------------------
# Read in example list
#------------------------------------------

sub read_examples {

	my %res=();
	my $fIn="examples.par";
	open(IN,"<$fIn") or die "$fIn: $!";
	
    my $line;	
    my $actType;
    while (defined ($line = <IN>)) {
		chomp($line);
		if ($line =~ /.*#/) {next;}
		
        #print $line."\n";

        $line =~ s/\"//g;

        my ($a)=($line =~ /(\S+)\s*\(/);
        if($a and $a !~ /\!/) {
            $actType=$a;
            #print "type: ".$a."\n";
            #$res{$actType};
            next;
        }

        if( $line =~ /\)/) {
            $actType="";
            next;
        }

        if(length($actType) > 0) {
            my ($b) =($line =~ /(\S+)/);
            #print "ex: ".$b."\n";
            if($b) {
                push(@{$res{$actType}},$b);
            }
        }
	}	

    close IN;

	return %res;
}

#-----------------------------------------------------------------------------
# Genarate the page title for the parent page for a given type of examples
#-----------------------------------------------------------------------------

sub parentPageTitle {

   my ($codeType) = @_;
   return $codeType." examples";
}

#--------------------------------------------------------
# Create parent page for a given type of examples
#--------------------------------------------------------

sub makeParentPage {
    my ($rootPage,$pageTitle,$outDir,$cType,@xmp) = @_;

    my $f=$outDir."/parent_".$cType."html";

    open(OUT,">$f") or die "$f: $!";

    my $str="<table><tbody>";
    $str=$str."<tr><th>Example</th><th>Description</th></tr>";

    foreach my $xm (@xmp) {
        $str=$str."<tr><td>".linkToPage($xm,examplePageTitle($xm))."</td><td>".getDescription($xm)."</td></tr>";
    }
    $str=$str."</tbody></table>";

    print OUT $str;

    close OUT;

    #Load it to confluence
    loadToConf($f,$pageTitle,$rootPage);
}

#-----------------------------------------------------------------------------
# Genarate the page title for a given example
#-----------------------------------------------------------------------------
sub examplePageTitle {

   my ($exName) = @_;
   return $exName;
}

#-----------------------------------------------------------
# Get the file path for an example with the given language
#-----------------------------------------------------------

sub examplePath {
    my ($name,$lang) = @_;
    return $inRootDir."/".$langDir{$lang}."/".$name.".".$langSuffix{$lang};
}

#-----------------------------------------------------------
# Check if the file for a given example exists.
#-----------------------------------------------------------

sub hasExample {

    my ($name,$lang) = @_;
    my $f=examplePath($name,$lang);
    if( -e $f ) {return 1};

    print "$f no!!!\n";

    return 0;
}

#------------------------------------------------------------
# Generate confluence formatted html string form a source file
#------------------------------------------------------------

sub getSourceCode {

    my ($title,$fPath,$lang) = @_;
    my $codeTxt;

    print $fPath."\n";

    my $str;

    if($lang eq "") {
        $codeTxt=fortran_file_to_string($fPath);
        #return "<p /><code><ac:macro ac:name=\"html\"><ac:plain-text-body><![CDATA[".$codeTxt."]]></ac:plain-text-body></ac:macro></code>";
        return "<p /><pre>".$codeTxt."</pre>";
    } else {
        open(IN,"<$fPath") or  return ""; # die "$fPath: $!";
        $codeTxt=join("", <IN>);
        close IN;
    }

    $str="
    <ac:structured-macro ac:name=\"code\">
        <ac:parameter ac:name=\"title\">".$title."</ac:parameter>
        <ac:parameter ac:name=\"linenumbers\">false</ac:parameter>
        <ac:parameter ac:name=\"language\">".$lang."</ac:parameter>
        <ac:plain-text-body><![CDATA[".$codeTxt."]]></ac:plain-text-body>
    </ac:structured-macro>";

    return $str;
}

#--------------------------------------------------------
# Extract the description of a given example
#--------------------------------------------------------

sub getDescription {

    my ($name) = @_;
    my $res;  

    foreach my $lang (@langKeys) {
            
        if(hasExample($name,$lang) == 1 ) { 

            my $f=examplePath($name,$lang);

            my $found=0;
            open (IN,"<$f") or return "";
            while (defined (my $line = <IN>)) {
                my $comment=$langComment{$lang};

                if($comment ne "*") {
                    $line =~ s/\Q${comment}/\*/g;
                }
         
                #First row
                if($found==0) {
                    if($line =~ /\*\s*Description:/) {
                        $found=1;
                        my ($r)=($line =~ /\*\s*Description:\s*(.+)/);
                        if(length($r) > 2) {
                            $res=$r;
                        }
                    }
                } else {
                    my ($r)=($line =~ /\*\s+(.+)/);
                    if(length($r) > 2) {
                        $res=$res." ".$r;
                    } else {
                        last;
                    }
                }
            }

            if($res) {
                return $res;
            }

        }
        
        #print $f."\n";
        #print "descr: ".$res."\n";
    }

    return $res;
}

#====================================================
#
# Generic functions related to confluence
#
#====================================================

#--------------------------------
# Upload a page to confluence
#--------------------------------

sub loadToConf {

    my ($fHtml,$title,$parentPage) = @_;
    system("$CONF -a addPage --noConvert --replace --space \"$confSpace\" --title \"$title\" --parent \"$parentPage\" --file $fHtml");
}

#---------------------------------------
# Create link to a page
#---------------------------------------

sub linkToPage {

    my ($page,$label) = @_;

    my $str="<ac:link>
  <ri:page ri:content-title=\"".$page."\"/>
  <ac:plain-text-link-body>
    <![CDATA[".$label."]]>
  </ac:plain-text-link-body>
  </ac:link>";

   return $str;
}

sub makeInfo {

    my ($text) = @_;
    return "<ac:structured-macro ac:name=\"info\"><ac:parameter ac:name=\"icon\">false</ac:parameter>".
              "<ac:rich-text-body>".$text."</ac:rich-text-body></ac:structured-macro>";
}