Change --comments behaviour.

--comments defaulted to true, but --nocomments was
enforced if --filetemplate was set.

Remove enforcement, but default to --nocomments
if --filetemplate is set. Default behaviour is
unchanged, but it's now possible to have
comments in files.

Change handling of captions accordingly (must
be sent to output handle now).

Update POD.

Signed-off-by: Thomas Hochstein <thh@thh.name>
This commit is contained in:
Thomas Hochstein 2025-05-11 19:56:17 +02:00
parent 7dd8a95be3
commit 28157570f1
4 changed files with 34 additions and 36 deletions

View file

@ -50,11 +50,8 @@ GetOptions ('c|captions!' => \$OptCaptions,
'h|help' => \&ShowPOD, 'h|help' => \&ShowPOD,
'V|version' => \&ShowVersion) or exit 1; 'V|version' => \&ShowVersion) or exit 1;
# parse parameters # parse parameters
# TODO: $OptSums is currently a no-op # $OptComments defaults to TRUE if --filetemplate is not used
# $OptComments defaults to TRUE $OptComments = 1 if (!$OptFileTemplate && !defined($OptComments));
$OptComments = 1 if (!defined($OptComments));
# force --nocomments when --filetemplate is used
$OptComments = 0 if ($OptFileTemplate);
# parse $OptType # parse $OptType
if ($OptType) { if ($OptType) {
if ($OptType =~ /(host|server)s?/i) { if ($OptType =~ /(host|server)s?/i) {
@ -166,24 +163,25 @@ $DBQuery->execute(@SQLBindNames)
# set default to 'pretty' # set default to 'pretty'
$OptFormat = 'pretty' if !$OptFormat; $OptFormat = 'pretty' if !$OptFormat;
# print captions if --caption is set # print captions if --caption is set
my $LeadIn;
if ($OptCaptions && $OptComments) { if ($OptCaptions && $OptComments) {
# print time period with report type # print time period with report type
my $CaptionReportType = '(number of postings for each month)'; my $CaptionReportType = '(number of postings for each month)';
if ($OptReportType and $OptReportType ne 'default') { if ($OptReportType and $OptReportType ne 'default') {
$CaptionReportType = '(number of all postings for that time period)'; $CaptionReportType = '(number of all postings for that time period)';
} }
printf("# ----- Report for %s %s\n",$CaptionPeriod,$CaptionReportType); $LeadIn .= sprintf("# ----- Report for %s %s\n",$CaptionPeriod,$CaptionReportType);
# print name list if --names is set # print name list if --names is set
printf("# ----- Names: %s\n",join(',',split(/:/,$OptNames))) $LeadIn .= sprintf("# ----- Names: %s\n",join(',',split(/:/,$OptNames)))
if $OptNames; if $OptNames;
# print boundaries, if set # print boundaries, if set
my $CaptionBoundary= '(counting only month fulfilling this condition)'; my $CaptionBoundary= '(counting only month fulfilling this condition)';
printf("# ----- Threshold: %s %s x %s %s %s\n", $LeadIn .= sprintf("# ----- Threshold: %s %s x %s %s %s\n",
$LowBound ? $LowBound : '',$LowBound ? '=>' : '', $LowBound ? $LowBound : '',$LowBound ? '=>' : '',
$UppBound ? '<=' : '',$UppBound ? $UppBound : '',$CaptionBoundary) $UppBound ? '<=' : '',$UppBound ? $UppBound : '',$CaptionBoundary)
if ($LowBound or $UppBound); if ($LowBound or $UppBound);
# print primary and secondary sort order # print primary and secondary sort order
printf("# ----- Grouped by %s (%s), sorted %s%s\n", $LeadIn .= sprintf("# ----- Grouped by %s (%s), sorted %s%s\n",
($GroupBy eq 'month') ? 'Months' : 'Names', ($GroupBy eq 'month') ? 'Months' : 'Names',
($OptGroupBy and $OptGroupBy =~ /-?desc$/i) ? 'descending' : 'ascending', ($OptGroupBy and $OptGroupBy =~ /-?desc$/i) ? 'descending' : 'ascending',
($OptOrderBy and $OptOrderBy =~ /posting/i) ? 'by number of postings ' : '', ($OptOrderBy and $OptOrderBy =~ /posting/i) ? 'by number of postings ' : '',
@ -191,7 +189,7 @@ if ($OptCaptions && $OptComments) {
} }
# output data # output data
&OutputData($OptFormat,$OptComments,$GroupBy,$Precision,'', &OutputData($OptFormat,$OptComments,$GroupBy,$Precision,'',$LeadIn,
$OptFileTemplate,$DBQuery,$MaxLength,$MaxValLength); $OptFileTemplate,$DBQuery,$MaxLength,$MaxValLength);
### close handles ### close handles
@ -261,7 +259,6 @@ using B<--nocomments>.
Last but not least you can redirect all output to a number of files, e.g. Last but not least you can redirect all output to a number of files, e.g.
one for each month, by submitting the B<--filetemplate> option, see below. one for each month, by submitting the B<--filetemplate> option, see below.
Captions and comments are automatically disabled in this case.
=head2 Configuration =head2 Configuration
@ -429,10 +426,11 @@ False by default.
=item B<--comments|--nocomments> =item B<--comments|--nocomments>
Add comments (group headers) to I<dump> and I<pretty> output. True by default. Add comments (group headers) to I<dump> and I<pretty> output. True by default
as logn as B<--filetemplate> is not set.
Use I<--nocomments> to suppress anything except newsgroup names/months and Use I<--nocomments> to suppress anything except host/client names or months and
numbers of postings. This is enforced when using B<--filetemplate>, see below. numbers of postings.
=item B<--filetemplate> I<filename template> =item B<--filetemplate> I<filename template>
@ -446,8 +444,6 @@ example with B<--filetemplate> I<stats>:
stats-2012-02 stats-2012-02
... and so on ... and so on
B<--nocomments> is enforced, see above.
=item B<--db> I<database table> =item B<--db> I<database table>
Override I<DBTableHosts> or I<DBTableClnts> from F<newsstats.conf>. Override I<DBTableHosts> or I<DBTableClnts> from F<newsstats.conf>.

View file

@ -51,10 +51,8 @@ GetOptions ('b|boundary=s' => \$OptBoundType,
'h|help' => \&ShowPOD, 'h|help' => \&ShowPOD,
'V|version' => \&ShowVersion) or exit 1; 'V|version' => \&ShowVersion) or exit 1;
# parse parameters # parse parameters
# $OptComments defaults to TRUE # $OptComments defaults to TRUE if --filetemplate is not used
$OptComments = 1 if (!defined($OptComments)); $OptComments = 1 if (!$OptFileTemplate && !defined($OptComments));
# force --nocomments when --filetemplate is used
$OptComments = 0 if ($OptFileTemplate);
# parse $OptBoundType # parse $OptBoundType
if ($OptBoundType) { if ($OptBoundType) {
if ($OptBoundType =~ /level/i) { if ($OptBoundType =~ /level/i) {
@ -226,6 +224,7 @@ $DBQuery->execute(@SQLBindNewsgroups)
# set default to 'pretty' # set default to 'pretty'
$OptFormat = 'pretty' if !$OptFormat; $OptFormat = 'pretty' if !$OptFormat;
# print captions if --caption is set # print captions if --caption is set
my $LeadIn;
if ($OptCaptions && $OptComments) { if ($OptCaptions && $OptComments) {
# print time period with report type # print time period with report type
my $CaptionReportType= '(number of postings for each month)'; my $CaptionReportType= '(number of postings for each month)';
@ -235,9 +234,9 @@ if ($OptCaptions && $OptComments) {
$CaptionReportType= '(number of all postings for that time period)' $CaptionReportType= '(number of all postings for that time period)'
if $OptReportType eq 'sum'; if $OptReportType eq 'sum';
} }
printf("# ----- Report for %s %s\n",$CaptionPeriod,$CaptionReportType); $LeadIn .= sprintf("# ----- Report for %s %s\n",$CaptionPeriod,$CaptionReportType);
# print newsgroup list if --newsgroups is set # print newsgroup list if --newsgroups is set
printf("# ----- Newsgroups: %s\n",join(',',split(/:/,$OptNewsgroups))) $LeadIn .= sprintf("# ----- Newsgroups: %s\n",join(',',split(/:/,$OptNewsgroups)))
if $OptNewsgroups; if $OptNewsgroups;
# print boundaries, if set # print boundaries, if set
my $CaptionBoundary= '(counting only month fulfilling this condition)'; my $CaptionBoundary= '(counting only month fulfilling this condition)';
@ -246,12 +245,12 @@ if ($OptCaptions && $OptComments) {
$CaptionBoundary= '(on average)' if $OptBoundType eq 'average'; $CaptionBoundary= '(on average)' if $OptBoundType eq 'average';
$CaptionBoundary= '(all month summed up)' if $OptBoundType eq 'sum'; $CaptionBoundary= '(all month summed up)' if $OptBoundType eq 'sum';
} }
printf("# ----- Threshold: %s %s x %s %s %s\n", $LeadIn .= sprintf("# ----- Threshold: %s %s x %s %s %s\n",
$LowBound ? $LowBound : '',$LowBound ? '=>' : '', $LowBound ? $LowBound : '',$LowBound ? '=>' : '',
$UppBound ? '<=' : '',$UppBound ? $UppBound : '',$CaptionBoundary) $UppBound ? '<=' : '',$UppBound ? $UppBound : '',$CaptionBoundary)
if ($LowBound or $UppBound); if ($LowBound or $UppBound);
# print primary and secondary sort order # print primary and secondary sort order
printf("# ----- Grouped by %s (%s), sorted %s%s\n", $LeadIn .= sprintf("# ----- Grouped by %s (%s), sorted %s%s\n",
($GroupBy eq 'month') ? 'Months' : 'Newsgroups', ($GroupBy eq 'month') ? 'Months' : 'Newsgroups',
($OptGroupBy and $OptGroupBy =~ /-?desc$/i) ? 'descending' : 'ascending', ($OptGroupBy and $OptGroupBy =~ /-?desc$/i) ? 'descending' : 'ascending',
($OptOrderBy and $OptOrderBy =~ /posting/i) ? 'by number of postings ' : '', ($OptOrderBy and $OptOrderBy =~ /posting/i) ? 'by number of postings ' : '',
@ -260,7 +259,7 @@ if ($OptCaptions && $OptComments) {
# output data # output data
&OutputData($OptFormat,$OptComments,$GroupBy,$Precision, &OutputData($OptFormat,$OptComments,$GroupBy,$Precision,
$OptCheckgroupsFile ? $ValidGroups : '', $OptCheckgroupsFile ? $ValidGroups : '',$LeadIn,
$OptFileTemplate,$DBQuery,$MaxLength,$MaxValLength); $OptFileTemplate,$DBQuery,$MaxLength,$MaxValLength);
### close handles ### close handles
@ -336,7 +335,6 @@ using B<--nocomments>.
Last but not least you can redirect all output to a number of files, e.g. Last but not least you can redirect all output to a number of files, e.g.
one for each month, by submitting the B<--filetemplate> option, see below. one for each month, by submitting the B<--filetemplate> option, see below.
Captions and comments are automatically disabled in this case.
=head2 Configuration =head2 Configuration
@ -589,10 +587,11 @@ False by default.
=item B<--comments|--nocomments> =item B<--comments|--nocomments>
Add comments (group headers) to I<dump> and I<pretty> output. True by default. Add comments (group headers) to I<dump> and I<pretty> output. True by default
as logn as B<--filetemplate> is not set.
Use I<--nocomments> to suppress anything except newsgroup names/months and Use I<--nocomments> to suppress anything except newsgroup names/months and
numbers of postings. This is enforced when using B<--filetemplate>, see below. numbers of postings.
=item B<--filetemplate> I<filename template> =item B<--filetemplate> I<filename template>
@ -606,8 +605,6 @@ example with B<--filetemplate> I<stats>:
stats-2012-02 stats-2012-02
... and so on ... and so on
B<--nocomments> is enforced, see above.
=item B<--groupsdb> I<database table> =item B<--groupsdb> I<database table>
Override I<DBTableGrps> from F<newsstats.conf>. Override I<DBTableGrps> from F<newsstats.conf>.

View file

@ -16,6 +16,8 @@ NewsStats 0.3.0 (unreleased)
- Fallback to last month if no month is given. - Fallback to last month if no month is given.
- Add option handling, import VERSION, add POD. - Add option handling, import VERSION, add POD.
* Update README, INSTALL and ChangeLog. * Update README, INSTALL and ChangeLog.
* Don't enforce --nocomment for --filetemplate, just default to it.
Change caption handling, update documentation accordingly.
NewsStats 0.2.0 (2025-05-10) NewsStats 0.2.0 (2025-05-10)
* Redo directory structure: * Redo directory structure:

View file

@ -430,12 +430,13 @@ sub OutputData {
### $GroupBy : primary sorting order (month or key) ### $GroupBy : primary sorting order (month or key)
### $Precision: number of digits right of decimal point (0 or 2) ### $Precision: number of digits right of decimal point (0 or 2)
### $ValidKeys: reference to a hash containing all valid keys ### $ValidKeys: reference to a hash containing all valid keys
### $LeadIn : print at start of output
### $FileTempl: file name template (--filetemplate): filetempl-YYYY-MM ### $FileTempl: file name template (--filetemplate): filetempl-YYYY-MM
### $DBQuery : database query handle with executed query, ### $DBQuery : database query handle with executed query,
### containing $Month, $Key, $Value ### containing $Month, $Key, $Value
### $PadField : padding length for key field (optional) for 'pretty' ### $PadField : padding length for key field (optional) for 'pretty'
### $PadValue : padding length for value field (optional) for 'pretty' ### $PadValue : padding length for value field (optional) for 'pretty'
my ($Format, $Comments, $GroupBy, $Precision, $ValidKeys, $FileTempl, my ($Format, $Comments, $GroupBy, $Precision, $ValidKeys, $LeadIn, $FileTempl,
$DBQuery, $PadField, $PadValue) = @_; $DBQuery, $PadField, $PadValue) = @_;
my %ValidKeys = %{$ValidKeys} if $ValidKeys; my %ValidKeys = %{$ValidKeys} if $ValidKeys;
my ($FileName, $Handle, $OUT); my ($FileName, $Handle, $OUT);
@ -478,8 +479,8 @@ sub OutputData {
$FileName)); $FileName));
$Handle = $OUT; $Handle = $OUT;
}; };
print $Handle &FormatOutput($Format, $Comments, $Caption, $Key, $Value, print $Handle &FormatOutput($Format, $Comments, $LeadIn, $Caption,
$Precision, $PadField, $PadValue); $Key, $Value, $Precision, $PadField, $PadValue);
$LastIteration = $Caption; $LastIteration = $Caption;
}; };
close $OUT if ($FileTempl); close $OUT if ($FileTempl);
@ -498,7 +499,7 @@ sub FormatOutput {
### $PadField : padding length for key field (optional) for 'pretty' ### $PadField : padding length for key field (optional) for 'pretty'
### $PadValue : padding length for value field (optional) for 'pretty' ### $PadValue : padding length for value field (optional) for 'pretty'
### OUT: $Output: formatted output ### OUT: $Output: formatted output
my ($Format, $Comments, $Caption, $Key, $Value, $Precision, $PadField, my ($Format, $Comments, $LeadIn, $Caption, $Key, $Value, $Precision, $PadField,
$PadValue) = @_; $PadValue) = @_;
my ($Output); my ($Output);
# keep last caption in mind # keep last caption in mind
@ -514,8 +515,10 @@ sub FormatOutput {
$Output = sprintf ("%s %s %u\n",$Caption,$Key,$Value); $Output = sprintf ("%s %s %u\n",$Caption,$Key,$Value);
} elsif ($Format eq 'pretty') { } elsif ($Format eq 'pretty') {
# output as a table # output as a table
$Output = sprintf ("# ----- %s:\n",$Caption) if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration)) {
if ($Comments and (!defined($LastIteration) or $Caption ne $LastIteration)); $Output = $LeadIn;
$Output .= sprintf ("# ----- %s:\n",$Caption);
}
# increase $PadValue for numbers with decimal point # increase $PadValue for numbers with decimal point
$PadValue += $Precision+1 if $Precision; $PadValue += $Precision+1 if $Precision;
# add padding if $PadField is set; $PadValue HAS to be set then # add padding if $PadField is set; $PadValue HAS to be set then