Skip to content

Commit

Permalink
Merge pull request #14 from alltilla/global-options
Browse files Browse the repository at this point in the history
Add global option support
  • Loading branch information
alltilla authored Aug 13, 2023
2 parents a2cbc23 + ed6a427 commit 5628fb1
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 42 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@ jobs:

- name: mypy
run: make mypy

- name: mypy
run: make pyright
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ style-check: black-check pylint pycodestyle
mypy:
poetry run mypy $(SOURCEDIRS)

linters: mypy style-check
pyright:
poetry run pyright $(SOURCEDIRS)

linters: mypy pyright style-check

check: pytest linters

Expand Down
106 changes: 76 additions & 30 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "syslog-ng-cfg-helper"
version = "1.1.0"
version = "1.2.0"
description = "Configuration helper for syslog-ng."
authors = ["Attila Szakacs <szakacs.attila96@gmail.com>"]
readme = "README.md"
Expand All @@ -22,6 +22,7 @@ include = [

[tool.poetry.dependencies]
python = "^3.8"
pyright = "^1.1.322"

[tool.poetry.group.dev.dependencies]
neologism = "^0.0.8"
Expand Down
15 changes: 14 additions & 1 deletion syslog_ng_cfg_helper/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Optional

from syslog_ng_cfg_helper.driver_db import DriverDB, Driver
from syslog_ng_cfg_helper.driver_db.utils import color_red
from syslog_ng_cfg_helper.driver_db.utils import color_red, unindent


def colorize_context_name(name: str, colored: bool = True) -> str:
Expand All @@ -27,6 +27,15 @@ def open_db() -> DriverDB:
return driver_db


def print_global_options(driver_db: DriverDB, colored: bool) -> None:
driver = driver_db.get_driver("options", DriverDB.GLOBAL_OPTIONS_DRIVER_NAME)
global_options_str = driver.colored_str() if colored else str(driver)
global_options_str = unindent(global_options_str)
global_options_str_lines = global_options_str.split("\n")

print("\n".join(global_options_str_lines[1:-1]))


def print_options(driver_db: DriverDB, context_name: str, driver_name: str, colored: bool) -> None:
if context_name not in driver_db.contexts:
print(f"The context '{context_name}' is not in the database.")
Expand Down Expand Up @@ -68,6 +77,10 @@ def print_contexts(driver_db: DriverDB, colored: bool) -> None:


def query(driver_db: DriverDB, context: Optional[str], driver: Optional[str], colored: bool) -> None:
if context == "options":
print_global_options(driver_db, colored)
return

if context and driver:
print_options(driver_db, context, driver, colored)
return
Expand Down
2 changes: 2 additions & 0 deletions syslog_ng_cfg_helper/driver_db/driver_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def __str__(self) -> str:


class DriverDB:
GLOBAL_OPTIONS_DRIVER_NAME = "global-options"

def __init__(self) -> None:
self.__contexts: Dict[str, Dict[str, Driver]] = {}

Expand Down
6 changes: 6 additions & 0 deletions syslog_ng_cfg_helper/driver_db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ def indent(string: str) -> str:
return "\n".join(indented_lines)


def unindent(string: str) -> str:
lines = string.split("\n")
unindented_lines = [line[__INDENTATION:] if line.startswith(" " * __INDENTATION) else line for line in lines]
return "\n".join(unindented_lines)


def diff_indent(string: str) -> str:
lines = string.split("\n")
indented_lines = []
Expand Down
34 changes: 31 additions & 3 deletions syslog_ng_cfg_helper/module_loader/load_modules.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from typing import Dict, List, Set
from typing import Dict, List, Optional, Set
from pathlib import Path
from neologism import DCFG, Rule

Expand Down Expand Up @@ -52,8 +52,11 @@ def __get_token_resolutions(parser_file: Path) -> Dict[str, Set[str]]:
return resolutions


def __resolve_tokens_to_keywords(grammar: DCFG, common_parser_file: Path, parser_file: Path) -> None:
parser_files: List[Path] = [common_parser_file, parser_file]
def __resolve_tokens_to_keywords(grammar: DCFG, common_parser_file: Path, parser_file: Optional[Path] = None) -> None:
parser_files: List[Path] = [common_parser_file]
if parser_file:
parser_files.append(parser_file)

for file in parser_files:
for token, resolutions in __get_token_resolutions(file).items():
for resolution in resolutions:
Expand Down Expand Up @@ -169,11 +172,36 @@ def __load_drivers_in_module(module_source_dir: Path, common_parser_file: Path)
return drivers


def __load_common_grammar_file(lib_dir: Path, common_parser_file: Path) -> DriverDB:
grammar = DCFG()
grammar.load_yacc_file(str(lib_dir / "cfg-grammar.y"))
__format_types(grammar)
__remove_ifdef(grammar)
__resolve_tokens_to_keywords(grammar, common_parser_file)

driver_db = DriverDB()
global_options = Driver("options", DriverDB.GLOBAL_OPTIONS_DRIVER_NAME)
driver_db.add_driver(global_options)

for sentence in grammar.sentences:
if not sentence or sentence[0] != "options":
continue
try:
driver_slice = parse_sentence(sentence)
driver_db.add_driver(driver_slice)
except ParseError as exception:
print(f" Cannot parse sentence '{' '.join(sentence)}': {exception}")

return driver_db


def load_modules(lib_dir: Path, modules_dir: Path) -> DriverDB:
common_parser_file = lib_dir / "cfg-parser.c"
driver_db = DriverDB()
module_source_dirs: List[Path] = list(filter(lambda path: path.is_dir(), modules_dir.glob("*")))

driver_db.merge(__load_common_grammar_file(lib_dir, common_parser_file))

for module_source_dir in module_source_dirs:
print(f"Loading module '{module_source_dir.name}'.")

Expand Down
33 changes: 27 additions & 6 deletions syslog_ng_cfg_helper/module_loader/parse_sentence.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Set, Tuple
from syslog_ng_cfg_helper.driver_db import Block, Driver, Option
from syslog_ng_cfg_helper.driver_db import Block, DriverDB, Driver, Option


class ParseError(Exception):
Expand Down Expand Up @@ -110,6 +110,7 @@ def __parse_options_in_block(sentence: Tuple[str, ...], target_block: Block) ->
if i in processed:
continue

number_of_parsed_symbols = 0
rest_of_sentence = sentence[i:]
if __is_block(rest_of_sentence):
block, number_of_parsed_symbols = __parse_block(rest_of_sentence)
Expand All @@ -124,21 +125,41 @@ def __parse_options_in_block(sentence: Tuple[str, ...], target_block: Block) ->
__mark_n_symbols_as_processed(processed, i, number_of_parsed_symbols)


def __parse_common_global_options(sentence: Tuple[str, ...]) -> Driver:
if sentence[-1] != ";":
raise ParseError("Common global options sentence does not end with ';'.")

if not (sentence[1] == "{" and sentence[-2] == "}"):
raise ParseError("Common global options curly braces are missing.")

driver = Driver("options", DriverDB.GLOBAL_OPTIONS_DRIVER_NAME)
option_sentence = sentence[1:-2]
__parse_options_in_block(option_sentence, driver)

return driver


def parse_sentence(sentence: Tuple[str, ...]) -> Driver:
if len(sentence) < 4:
raise ParseError("Too short sentence.")

if sentence[0] == "options":
return __parse_common_global_options(sentence)

if not sentence[0].startswith("LL_CONTEXT_"):
raise ParseError("Context is missing.")

if not (sentence[2] == "(" and sentence[-1] == ")"):
raise ParseError("Braces are missing, probably not a driver.")

context = sentence[0].replace("LL_CONTEXT_", "").replace("_", "-").lower()
name = sentence[1]

driver = Driver(context, name)

__parse_options_in_block(sentence[3:-1], driver)
if context == "options":
driver = Driver(context, DriverDB.GLOBAL_OPTIONS_DRIVER_NAME)
option_sentence = sentence[1:]
else:
driver = Driver(context, sentence[1])
option_sentence = sentence[3:-1]

__parse_options_in_block(option_sentence, driver)

return driver

0 comments on commit 5628fb1

Please sign in to comment.