Skip to content

Commit

Permalink
fix: ignore comments in hosts file
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed May 13, 2024
1 parent 7794498 commit b2c47fd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 36 deletions.
7 changes: 3 additions & 4 deletions src/gvm_sync_targets/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
# SPDX-License-Identifier: MIT

import logging
from typing import Optional, TextIO, cast
from typing import TextIO

import click
from gvm.connections import DebugConnection, UnixSocketConnection
from gvm.protocols.gmp import Gmp
from gvm.transforms import EtreeCheckCommandTransform

from gvm_sync_targets import __version__
from gvm_sync_targets.models import ModelTransform
from gvm_sync_targets.models.assets_response import GetAssetsResponse
from gvm_sync_targets.util import Element, target_in_use, to_str
from gvm_sync_targets.util import read_lines


@click.group(
Expand All @@ -40,7 +39,7 @@ def gvm_sync_targets(
DebugConnection(UnixSocketConnection()),
transform=ModelTransform(),
) as gmp:
hosts = hosts_file.read().splitlines()
hosts = read_lines(hosts_file.read())
gmp.authenticate(username, password)
existing_hosts: GetAssetsResponse = gmp.get_hosts(details=True)
to_add = hosts.copy()
Expand Down
39 changes: 7 additions & 32 deletions src/gvm_sync_targets/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,14 @@
Element: TypeAlias = "_Element"

__all__ = [
"to_str",
"target_in_use",
"Element",
"read_lines",
]


def to_str(text: Union[str, bytes, bytearray, memoryview]) -> str:
"""Convert bytes, bytearray, memoryview, or str to str.
Args:
text: Input value
Returns:
decoded output
Examples:
>>> to_str("aaa")
'aaa'
>>> to_str(b'bb')
'bb'
>>> to_str(memoryview(b'bb'))
'bb'
>>> to_str(bytearray(b'cc'))
'cc'
"""
if isinstance(text, (bytes, bytearray)):
return text.decode()

if isinstance(text, memoryview):
return to_str(text.tobytes())

return text


def target_in_use(target: Element) -> bool:
return bool(target.xpath("boolean(in_use[text()='1'])"))
def read_lines(data: str, ignore_comments: bool = True) -> list[str]:
return [
line
for line in data.splitlines()
if not (ignore_comments and line.lstrip().startswith("#"))
]

0 comments on commit b2c47fd

Please sign in to comment.