File: Custom.pm

package info (click to toggle)
libstring-license-perl 0.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,900 kB
  • sloc: perl: 1,309; cpp: 360; ansic: 210; python: 27; ruby: 21; xml: 16; sh: 8; makefile: 3
file content (227 lines) | stat: -rw-r--r-- 5,230 bytes parent folder | download
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
use v5.20;
use utf8;
use warnings;
use feature qw(signatures);
no warnings qw(experimental::signatures);

use Feature::Compat::Class 0.07;

=head1 NAME

String::License::Naming - names of licenses and license naming schemes

=head1 VERSION

Version v0.0.11

=head1 SYNOPSIS

    use String::License::Naming::Custom;

    my $obj = String::License::Naming::Custom->new( schemes => [qw(spdx internal)] );

    my $schemes = [ $obj->list_schemes ];  # => is_deeply [ 'spdx', 'internal' ]

    my $license = [ grep { /^(Expat|Perl)$/ } $obj->list_licenses ];  # => is_deeply ['Perl']

    # use and prefer Debian-specific identifiers
    $schemes = [ $obj->add_scheme('debian') ];  # => is_deeply [ 'debian', 'spdx', 'internal' ]

    $license = [ grep { /^(Expat|Perl)$/ } $obj->list_licenses ];  # => is_deeply [ 'Expat', 'Perl' ]

=head1 DESCRIPTION

L<String::License::Naming> enumerates supported licenses
matching an ordered set of naming schemes,
or enumerates the names of supported license naming schemes.

Some licenses are known by different names.
E.g. the license "MIT" according to SPDX
is named "Expat" in Debian.

Some licenses are not always represented.
E.g. "Perl" is a (discouraged) license in Debian
while it is a relationship of several licenses with SPDX
(and that expression is recommended in Debian as well).

By default,
licenses are matched using naming schemes C<[ 'spdx', 'internal' ]>,
which lists all supported licenses,
preferrably by their SPDX name
or as fallback by an internal name.

=cut

package String::License::Naming::Custom v0.0.11;

use Carp       qw(croak);
use Log::Any   ();
use List::Util qw(uniq);
use Regexp::Pattern::License 3.4.0;

use namespace::clean;

class String::License::Naming::Custom : isa(String::License::Naming);

field $log;

=head1 CONSTRUCTOR

=over

=item new

    my $names = String::License::Naming->new;

    my $spdx_names = String::License::Naming->new( schemes => ['spdx'] );

Constructs and returns a String::License::Naming object.

Takes an optional array as named argument B<schemes>.
both ordering by which name licenses should be presented,
and limiting which licenses to cover.

When omitted,
the default schemes array C<[ 'spdx', 'internal' ]> is used,
which includes all supported licenses,
and they are presented by their SPDX name when defined
or otherwise by a semi-stable internal name.

When passing an empty array reference,
all supported licenses are included,
presented by a semi-stable internal potentially multi-word description.

=back

=cut

field $schemes : param = undef;

# TODO: maybe support seeding explicit keys
field $keys;

ADJUST {
	$log = Log::Any->get_logger;

	if ( defined $schemes ) {

		croak $log->fatal('parameter "schemes" must be an array reference')
			unless ref $schemes eq 'ARRAY';

		# TODO: die unless each arrayref entry is a string and supported

		my @uniq_schemes = uniq @$schemes;
		if ( join( ' ', @$schemes ) ne join( ' ', @uniq_schemes ) ) {
			$log->warn("duplicate scheme(s) omitted");
			@$schemes = \@uniq_schemes;
		}
	}
	else {
		$schemes = [];
	}

	$keys = [
		String::License::Naming::resolve_shortnames( $keys, $schemes, 1 ) ];
}

=head1 FUNCTIONS

=over

=item add_scheme

Takes a string representing a license naming scheme to use,
favored over existing schemes in use.

Returns array of schemes in use after addition.

=cut

method add_scheme ($new_scheme)
{
	if ( grep { $_ eq $new_scheme } @$schemes ) {
		$log->warn("already included scheme $new_scheme not added");
		return @$schemes;
	}

	# TODO: validate new entry is string and supported, or die
	unshift @$schemes, $new_scheme;

	return @$schemes;
}

=item list_schemes

Returns a list of license naming schemes in use.

=cut

method list_schemes ()
{
	return @$schemes;
}

=item list_available_schemes

Returns a list of all license naming schemes available.

=cut

method list_available_schemes ()
{
	my ( $_prop, $_any, @result );

	$_prop = '(?:[a-z][a-z0-9_]*)';
	$_any  = '[a-z0-9_.()]';

	@result = uniq sort
		map  {/^(?:name|caption)\.alt\.org\.($_prop)$_any*/}
		map  { keys %{ $Regexp::Pattern::License::RE{$_} } }
		grep {/^[a-z]/} keys %Regexp::Pattern::License::RE;

	return @result;
}

=item list_licenses

Returns a list of licensing patterns covered by this object instance,
each labeled by shortname according to current set of schemes.

=cut

method list_licenses ()
{
	return String::License::Naming::resolve_shortnames( $keys, $schemes );
}

=back

=encoding UTF-8

=head1 AUTHOR

Jonas Smedegaard C<< <dr@jones.dk> >>

=head1 COPYRIGHT AND LICENSE

  Copyright © 2023 Jonas Smedegaard

This program is free software:
you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License
as published by the Free Software Foundation,
either version 3, or (at your option) any later version.

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 Affero General Public License for more details.

You should have received a copy
of the GNU Affero General Public License along with this program.
If not, see <https://www.gnu.org/licenses/>.

=cut

1;