File: CustomizetoJSON.pm

package info (click to toggle)
polymake 4.12-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 35,992 kB
  • sloc: cpp: 168,768; perl: 43,375; javascript: 31,575; ansic: 3,007; java: 2,654; python: 633; sh: 268; xml: 117; makefile: 61
file content (150 lines) | stat: -rw-r--r-- 4,673 bytes parent folder | download | duplicates (2)
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
#  Copyright (c) 1997-2024
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------

use strict;
use namespaces;
use warnings qw(FATAL void syntax misc);

package Polymake::Core::CustomizetoJSON;

sub convert_file {
   my ($text, $init_script) = @_;
   pos($text) = 0;
   my ($pkg, $app, %result);
   my %unrecognized;

   while (pos($text) < length($text)) {

      # skip empty or comment lines, as well as the version number
      next if $text =~ m{\G \s* (?> (?: \# | \$version\s*=) .* )? \n}xmgc;

      if ($text =~ m{\G \s* package \s+ ($qual_id_re) \s*; }xomgc) {
         $pkg = $1;
         undef $app;
         next;
      }

      if ($text =~ m{\G \s* application \s+ ($id_re) \s*; }xomgc) {
         $app = $1;
         $pkg = "Polymake::$app";
         next;
      }

      if (defined($app)) {
         if ($text =~ m{\G \s* prefer \s+ (.+)(?<=\S) \s*; }xomgc) {
            my $expr = eval "$1";
            if (!$@ && defined($app)) {
               push @{$result{Polymake::Core::Preference::settings_key}{$app}}, $expr;
            }
            next;
         }
      } else {
         if ($text =~ m{\G \s* (?: ARCH\s*\(\s* $quoted_re \s*\)\s+ and \s+ )?
                        (?'var' $var_sigil_re $qual_id_re) \s*=\s*
                        (?('scalar') (?'value' .+)(?<=\S)
                            | (?=\() (?'value' $confined_multiline_re)) \s*; }xomgc) {

            my ($arch, $var, $value) = @+{qw(quoted var value)};
            my ($key, $next_level);
            if ($var =~ /::/) {
               $key = substr($var, 1);
            } elsif ($pkg eq "Polymake::User" && $var eq '%disabled_extensions') {
               $key = "_extensions::disabled";
            } elsif ($pkg eq "Polymake::Core::CPlusPlus") {
               $key = "_C++::" . substr($var, 1);
            } elsif ($var eq '%configured' && $pkg =~ /^Polymake::(\w+)$/) {
               $key = "_applications::configured";
               $next_level = $1;
            } else {
               $key = $pkg . "::" . substr($var, 1);
            }
            if (defined($arch)) {
               $key .= "#$arch";
            }
            if (substr($var, 0, 1) eq '$') {
               $value = eval $value;
            } elsif (substr($var, 0, 1) eq '@') {
               my @list = eval $value;
               $value = \@list;
            } else {
               my %hash = eval $value;
               $value = \%hash;
            }
            unless ($@) {
               if (defined($next_level)) {
                  $result{$key}{$next_level} = $value;
               } else {
                  $result{$key} = $value;
               }
            }
            next;
         }
      }

      $text =~ m{\G .* \n}xmgc;
      if (defined($init_script)) {
         my $unrecognized = $&;
         if ($unrecognized =~ /^[ \t]*[^\s\#]/m) {
            $unrecognized{$app // ""} .= $unrecognized;
         }
      }
   }

   if (keys %unrecognized) {
      open my $F, ">", $init_script or die "can't write to $init_script: $!\n";
      print $F <<".";
print STDERR <<'===== cut out up to this line =====';  return;


***** ATTENTION *****

Some start-up commands were rescued from your old preferences file.
They have been stored in $init_script, but not executed yet.
Please revise them and/or disable the script as described in the preamble.

*********************

===== cut out up to this line =====

# This script will be executed at the beginning of every polymake session.
# To run something else instead, set the custom variable:
#   set_custom \$init_script="path/to/script.pl";
#   
# To disable it altogether:
#   reset_custom \$init_script;
#

# Rescued commands follow:

.
      for $app (sort keys %unrecognized) {
         if (length($app)) {
            print $F "\nuse application '$app';\n";
         }
         print $F $unrecognized{$app};
      }
      close $F;
   }

   return \%result;
}

1

# Local Variables:
# cperl-indent-level:3
# indent-tabs-mode:nil
# End: