Firemen/ParseHelp/Creo3.pm

filter: Statementstmt 100% 39 / 39 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 100% 52 / 52 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Firemen/Change/Type.pm ↑ Summary
1 #!/usr/bin/perl
2 ######################
3 #
4 # Copyright (C) 2015 TU Clausthal, Institut fuer Maschinenwesen, Joachim Langenbach
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 ######################
20  
21 # Pod::Weaver infos
22 # ABSTRACT: Shared functions to exctract options and their values, default value and descriptions (Creo 3 and later).
23  
24 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 package CAD::Firemen::ParseHelp::Creo3;
28 {
29 $CAD::Firemen::ParseHelp::Creo3::VERSION = '0.7.1';
30 }
31 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 our @EXPORT_OK = qw(
33 extractHelpCreo3
34 );
35  
36 4 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use Tie::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use Term::ProgressBar;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use JSON::Parse 'parse_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use IO::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use HTML::TreeBuilder 5 -weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶use CAD::Firemen;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 use CAD::Firemen::Common qw(
46 strip
47 testPassed
48 testFailed
49 4 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 sub findOptionPage {
52 my $cdbOptions = shift;
53 my $optionsInfo = shift;
54 my $optionsValues = shift;
55 my $optionsDefault = shift;
56 my $errors = shift;
57 my $file = shift;
58 my $app = shift;
59 my $verbose = shift;
60 if(!defined($verbose)){
61 $verbose = 0;
62 }
63  
64 my @toc;
65 if(!tie(@toc, 'Tie::File', $file, recsep => "\n")){
66 return (0, "Could not open file to find option page: ". $file);
67 }
68  
69 my ($fileName, $filePath, $fileSuffix) = fileparse($file);
70  
71 # just to avoid unused warning in release tests
72 $fileName = undef;
73 $fileSuffix = undef;
74  
75 my $lines = scalar(@toc);
76 my $progress = Term::ProgressBar->new({name => "Parsing index file for app ". $app, count => $lines});
77 $progress->minor(0);
78 my $i = 0;
79 my $json;
80 foreach my $line (@toc){
81 my $line = strip($line);
82 if($i != 0 && substr($line, 0, 1) ne ";"){
83 $json .= $line ."\n";
84 }
85 $i++;
86 $progress->update($i);
87 }
88 if($i < $lines){
89 $progress->update($lines);
90 }
91 print "\n";
92  
93 my $jsonContent = parse_json($json);
94  
95 my $items = scalar(keys($jsonContent->{"words"}));
96 $progress = Term::ProgressBar->new({name => "Processing ". scalar(keys($jsonContent->{"words"})) ." keywords and ". scalar(keys($jsonContent->{"pages"})) ." pages for app ". $app, count => $items});
97 $progress->minor(0);
98 $i = 0;
99 foreach my $key (keys($jsonContent->{"words"})){
100 foreach my $option (keys(%{$cdbOptions})){
101 if(!exists($optionsInfo->{$option})){
102 if($key =~ m/$option/i){
103 if(scalar($jsonContent->{"words"}->{$key}) < 2){
104 return (0, "Malformed index found --> Stopping here");
105 }
106  
107 for(my $i = 0; $i < scalar(@{$jsonContent->{"words"}->{$key}}) - 1; $i = $i + 2){
108 my $pagesRow = $jsonContent->{"words"}->{$key}->[$i];
109 my $pagesColumn = $jsonContent->{"words"}->{$key}->[$i + 1] - 1;
110 if($pagesColumn != 0){
111 # skip all references, which does not reference the html page (first column in $pages
112 next;
113 }
114 my $page = $jsonContent->{"pages"}->[$pagesRow]->[$pagesColumn];
115 my $result= extractHelp($cdbOptions, $optionsInfo, $optionsValues, $optionsDefault, $errors, $option, $filePath ."/". $page, $verbose);
116 if($verbose > 2){
117 if($result){
118 print $option ." --> ". $page ." OK\n";
119 }
120 else{
121 print $option ." --> ". $page ." - NOT FOUND\n";
122 }
123 }
124 }
125 }
126 }
127 }
128 $i++;
129 $progress->update($i);
130 }
131 if($i < $items){
132 $progress->update($items);
133 }
134 print "\n";
135 return $errors;
136 }
137  
138 sub extractHelp {
139 my $cdbOptions = shift;
140 my $optionsInfo = shift;
141 my $optionsValues = shift;
142 my $optionsDefault = shift;
143 my $errors = shift;
144 my $option = uc(shift);
145 my $file = shift;
146 my $verbose = shift;
147 if(!defined($verbose)){
148 $verbose = 0;
149 }
150  
151 my $optionLowered = lc($option);
152  
153 my @htmlContent;
154 #if(!tie(@htmlContent, 'Tie::File', $file, recsep => "\x0D\x0A", discipline => ':encoding(utf8)')){
155 if(!tie(@htmlContent, 'Tie::File', $file)){
156 if(!exists($errors->{$option})){
157 $errors->{$option} = "";
158 }
159 $errors->{$option} .= "Could not open HTML file ". $file;
160 return 0;
161 }
162 my $htmlTree = HTML::TreeBuilder->new;
163 if(!$htmlTree->parse_file(html_file($file))){
164 if(!exists($errors->{$option})){
165 $errors->{$option} = "";
166 }
167 $errors->{$option} .= "Could not open file ". $file;
168 return 0;
169 }
170  
171 my $optionFound = 0;
172 my $regexSection = qr/<div class=\"Section_Title\" id=\"([^\"]+)\"><span class=\"option\">$optionLowered<\/span><\/div>/;
173 my @sectionTitles = $htmlTree->look_down("_tag" => "div", "class" => "Section_Title");
174 if(scalar(@sectionTitles) < 1 && $verbose > 2){
175 print "Found no section titles in file ". $file ."\n";
176 }
177 foreach my $sectionTitle (@sectionTitles){
178 if($sectionTitle->as_HTML() =~ m/$regexSection/){
179 my $id = $1;
180 $id =~ s/(.+)_[0-9]+/$1/;
181 $optionFound = 1;
182  
183 my @bodies = $htmlTree->look_down("_tag" => "div", "id" => qr/$id[_0-9]+/);
184 foreach my $body (@bodies){
185 my @values = $body->as_HTML() =~ m/<span class=\"codeph\">([^>]+)<\/span>/g;
186 if(scalar(@values) > 0){
187 for(my $i = 0; $i < scalar(@values); $i++){
188 $values[$i] = strip($values[$i]);
189 }
190 $optionsValues->{$option} = [ @values ];
191 # check extracted values against those from cdb
192 if(scalar($optionsValues->{$option}) == scalar(keys(%{$cdbOptions->{$option}}))){
193 if(!exists($errors->{$option})){
194 $errors->{$option} = "";
195 }
196 $errors->{$option} .= "Found different values for option ". $option ."(Expected: ". scalar(keys(%{$cdbOptions->{$option}})) .", Got: ". scalar($optionsValues->{$option}) .")";
197 }
198 if($body->as_HTML() =~ m/<span class=\"codeph\">([^>]+)<\/span><span class=\"Superscript\">\*<\/span>/){
199 # match default option
200 $optionsDefault->{$option} = uc(strip($1));
201 }
202 }
203 else{
204 if(!exists($optionsInfo->{$option})){
205 $optionsInfo->{$option} = "";
206 }
207 $optionsInfo->{$option} .= " ". $body->as_text_trimmed();
208 $optionsInfo->{$option} = strip($optionsInfo->{$option});
209 }
210 }
211 last;
212 }
213 }
214 if(!$optionFound){
215 my @divs = $htmlTree->look_down("_tag" => "div");
216 if(scalar(@divs) < 1 && $verbose > 2){
217 print "Found no divs in file ". $file ."\n";
218 }
219 my $regexOption = qr/<span class=\"option\">$optionLowered<\/span>[\):\s]{0,}([^<]+)/;
220 foreach my $div (@divs){
221 if($div->as_HTML() =~ m/$regexOption/){
222 $optionFound = 1;
223 if(!exists($optionsInfo->{$option})){
224 $optionsInfo->{$option} = "";
225 }
226 $optionsInfo->{$option} .= " ". $1;
227 $optionsInfo->{$option} = strip($optionsInfo->{$option});
228 }
229 }
230 }
231 return $optionFound;
232 }
233  
234 sub extractHelpCreo3 {
235 my $directory = shift;
236 my $language = shift;
237 my $cdbOptions = shift;
238 my $verbose = shift;
239 my %optionsInfo = ();
240 my %optionsValues = ();
241 my %optionsDefault = ();
242 my %errors = ();
243  
244 if(!-d $directory){
245 testFailed("Examine help structure");
246 if($verbose > 0){
247 print "Directory does not exist: ". $directory ."\n";
248 }
249 return ({}, {}, {}, {});
250 }
251 $directory =~ s/[\/\\]+$//;
252 my $dir;
253 if(!opendir($dir, $directory)){
254 testFailed("Examine help structure");
255 if($verbose > 0){
256 print "Could not open directory: ". $directory ."\n";
257 }
258 return ({}, {}, {}, {});
259 }
260 while(my $entry = readdir($dir)){
261 if($entry !~ m/^\./){
262 my $subDir = $directory ."/". $entry;
263 if(-d $subDir){
264 my @tmp = split(/_/, $entry);
265 if(scalar(@tmp) < 1){
266 if($verbose > 1){
267 print "Error determining app of directory entry ". $entry ."\n";
268 exit 1;
269 }
270 }
271 my $app = $tmp[scalar(@tmp) - 1];
272 my $file = $subDir ."/". $language ."/". $app ."_sx.js";
273 if(!-f $file){
274 if($verbose > 1){
275 print "Index file does not exists: ". $file ."\n";
276 }
277 exit 1;
278 }
279 my $errors = findOptionPage($cdbOptions, \%optionsInfo, \%optionsValues, \%optionsDefault, \%errors, $file, $app, $verbose);
280 if(scalar(keys(%{$errors})) > 0){
281 if($verbose > 1){
282 print "The following errors occured parsing directory ". $subDir .":\n";
283 foreach my $option (sort(keys(%{$errors}))){
284 print " ". $option .": ". $errors->{$option} ."\n";
285 }
286 }
287 }
288 }
289 }
290 }
291 closedir($dir);
292 return (\%optionsInfo, \%optionsValues, \%optionsDefault, \%errors);
293 }
294  
295 1;
296  
297 __END__
« Firemen/Change/Type.pm ↑ Summary