Skip to content

Commit

Permalink
filter: added --enable-only & --disabled-only
Browse files Browse the repository at this point in the history
- Added new arguments to the 'filter' command allowing including or excluding
  stanzas based on the boolean value of the 'disabled' attribute
  • Loading branch information
lowell80 committed Mar 4, 2022
1 parent 9365edd commit 3a52fef
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
9 changes: 8 additions & 1 deletion docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ Ksconf 0.9
This new option can be used to control the level of detail in the output.


Ksconf v0.9.2 (2022-03-04)
~~~~~~~~~~~~~~~~~~~~~~~~~~
- The ``filter`` command can now include/exclude stanzas based on the boolean value of ``disabled`` using the new ``--enabled-only`` or ``--disabled-only`` arguments.
The default behavior remains the same, that is, the ``disabled`` attribute is compleelty ignored.
Thanks to John B Splunker for inspiring this feature!


Ksconf v0.9.1 (2022-03-03)
~~~~~~~~~~~~~~~~~~~~~~~~~~
- Ksconf now tries harder to preserve file modification times. This is supported in ``merge``, ``combine``` and ``pacakge`` commands.
- Ksconf now tries harder to preserve file modification times. This is supported in ``merge``, ``combine``` and ``package`` commands.
Specifically, merged ``.conf`` files and concatenated files will keep the most recent modification time in the destination.
This should make the output of ``combine`` and ``package`` (by extention) more deterministic in many scenarios.

Expand Down
7 changes: 6 additions & 1 deletion docs/source/dyn/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ ksconf filter
[--match {regex,wildcard,string}] [--ignore-case]
[--invert-match] [--files-with-matches]
[--count | --brief] [--stanza PATTERN]
[--attr-present ATTR] [--keep-attrs WC-ATTR]
[--attr-present ATTR] [-e | -d] [--keep-attrs WC-ATTR]
[--reject-attrs WC-ATTR]
CONF [CONF ...]
Expand Down Expand Up @@ -364,6 +364,11 @@ ksconf filter
--attr-present ATTR Match any stanza that includes the ATTR attribute.
ATTR supports bulk attribute patterns via the
'file://' prefix.
-e, --enabled-only Keep only enabled stanzas. Any stanza containing
'disabled = 1' will be removed. The value of
'disabled' is assumed to be false by default.
-d, --disabled-only Keep disabled stanzas only. The value of the
`disabled` attribute is interpreted as a boolean.
Attribute selection:
Include or exclude attributes passed through. By default, all attributes
Expand Down
30 changes: 29 additions & 1 deletion ksconf/commands/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
import sys

from ksconf.commands import ConfFileType, KsconfCmd, dedent
from ksconf.conf.parser import PARSECONF_MID_NC, write_conf_stream
from ksconf.conf.parser import PARSECONF_MID_NC, conf_attr_boolean, write_conf_stream
from ksconf.consts import EXIT_CODE_SUCCESS
from ksconf.filter import FilteredList, FilteredListWildcard, create_filtered_list
from ksconf.util.completers import conf_files_completer


def is_disabled(attributes):
# type: (dict) -> bool
return conf_attr_boolean(attributes.get("disabled", False))


class FilterCmd(KsconfCmd):
help = "A stanza-aware GREP tool for conf files"
description = dedent("""
Expand Down Expand Up @@ -121,6 +126,18 @@ def register_args(self, parser):
PATTERN supports the special ``file://`` syntax.""")
'''

pg_eod = pg_sel.add_mutually_exclusive_group()
pg_eod.add_argument("-e", "--enabled-only", action="store_true",
help=dedent("""
Keep only enabled stanzas. Any stanza containing ``disabled = 1`` will be removed.
The value of ``disabled`` is assumed to be false by default.
"""))

pg_eod.add_argument("-d", "--disabled-only", action="store_true",
help=dedent("""
Keep disabled stanzas only.
The value of the `disabled` attribute is interpreted as a boolean. """))

pg_con = parser.add_argument_group("Attribute selection", dedent("""\
Include or exclude attributes passed through.
By default, all attributes are preserved.
Expand Down Expand Up @@ -149,6 +166,13 @@ def prep_filters(self, args):
self.attr_presence_filters = create_filtered_list(args.match, flags)
self.attr_presence_filters.feedall(args.attr_present)

if args.enabled_only:
self.disabled_filter = lambda attrs: not is_disabled(attrs)
elif args.disabled_only:
self.disabled_filter = is_disabled
else:
self.disabled_filter = lambda attrs: True

if args.keep_attrs or args.reject_attrs:
self.attrs_keep_filter = FilteredListWildcard(flags)
for attrs in args.keep_attrs:
Expand All @@ -162,6 +186,10 @@ def prep_filters(self, args):

def _test_stanza(self, stanza, attributes):
if self.stanza_filters.match_stanza(stanza):
# Exclude based on value of 'disabled' attribute
if not self.disabled_filter(attributes):
return False

# If there are no attribute level filters, automatically keep (preserves empty stanzas)
if not self.attr_presence_filters.has_rules:
return True
Expand Down

0 comments on commit 3a52fef

Please sign in to comment.