Skip to content

Commit

Permalink
refactor: apply stricter mypy rules (#847)
Browse files Browse the repository at this point in the history
* refactor: GlobalConfig does parsing in __init__ method

* refactor: fix all the mypy's reported issues

* refactor: properly type re.compile variable

* chore: add mypy.ini

* chore: add mypy.ini to licenses configuration
  • Loading branch information
artemrys committed Sep 27, 2023
1 parent 266b28f commit 91ff2a5
Show file tree
Hide file tree
Showing 49 changed files with 346 additions and 309 deletions.
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ header:
- "splunk_add_on_ucc_framework/commands/import_from_aob.sh"
- "splunk_add_on_ucc_framework/commands/imports.py"
- "scripts/compare_different_ucc_versions_output.sh"
- "mypy.ini"
10 changes: 10 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[mypy]
check_untyped_defs = true
disallow_any_generics = true
disallow_incomplete_defs = true
disallow_untyped_defs = true
warn_redundant_casts = true
warn_unused_ignores = true

[mypy-tests.*]
disallow_untyped_defs = false
2 changes: 1 addition & 1 deletion splunk_add_on_ucc_framework/app_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


class AppConf:
def __init__(self):
def __init__(self) -> None:
self._app_conf = conf_parser.TABConfigParser()

def read(self, path: str) -> None:
Expand Down
23 changes: 10 additions & 13 deletions splunk_add_on_ucc_framework/app_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#

import json
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Any

APP_MANIFEST_SCHEMA_VERSION = "2.0.0"
APP_MANIFEST_SUPPORTED_DEPLOYMENTS = frozenset(
Expand Down Expand Up @@ -43,8 +43,13 @@ class AppManifestFormatException(Exception):


class AppManifest:
def __init__(self):
self._manifest = None
def __init__(self, content: str) -> None:
try:
self._manifest = json.loads(content)
except json.JSONDecodeError:
raise AppManifestFormatException(
"Could not parse app.manifest, not a correct JSON file"
)

def get_addon_name(self) -> str:
return self._manifest["info"]["id"]["name"]
Expand Down Expand Up @@ -77,21 +82,13 @@ def _get_target_workloads(self) -> Optional[List[str]]:
return self._manifest.get("targetWorkloads")

@property
def manifest(self) -> Dict:
def manifest(self) -> Dict[str, Any]:
return self._manifest

def read(self, content: str) -> None:
try:
self._manifest = json.loads(content)
except json.JSONDecodeError:
raise AppManifestFormatException(
"Could not parse app.manifest, not a correct JSON file"
)

def update_addon_version(self, version: str) -> None:
self._manifest["info"]["id"]["version"] = version

def validate(self):
def validate(self) -> None:
schema_version = self._get_schema_version()
if schema_version != APP_MANIFEST_SCHEMA_VERSION:
raise AppManifestFormatException(
Expand Down
24 changes: 13 additions & 11 deletions splunk_add_on_ucc_framework/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import os
import shutil
import sys
from typing import Optional
from typing import Optional, List

from openapi3 import OpenAPI

Expand Down Expand Up @@ -60,7 +60,7 @@

def _modify_and_replace_token_for_oauth_templates(
ta_name: str, global_config: global_config_lib.GlobalConfig, outputdir: str
):
) -> None:
"""
Rename templates with respect to addon name if OAuth is configured.
Expand Down Expand Up @@ -125,7 +125,7 @@ def _modify_and_replace_token_for_oauth_templates(

def _add_modular_input(
ta_name: str, global_config: global_config_lib.GlobalConfig, outputdir: str
):
) -> None:
for service in global_config.inputs:
input_name = service.get("name")
class_name = input_name.upper()
Expand Down Expand Up @@ -167,7 +167,9 @@ def _add_modular_input(
config.write(configfile)


def _get_ignore_list(addon_name: str, ucc_ignore_path: str, output_directory: str):
def _get_ignore_list(
addon_name: str, ucc_ignore_path: str, output_directory: str
) -> List[str]:
"""
Return path of files/folders to be removed.
Expand All @@ -193,7 +195,7 @@ def _get_ignore_list(addon_name: str, ucc_ignore_path: str, output_directory: st
return ignore_list


def _remove_listed_files(ignore_list):
def _remove_listed_files(ignore_list: List[str]) -> None:
"""
Return path of files/folders to removed in output folder.
Expand All @@ -220,7 +222,7 @@ def generate_data_ui(
addon_name: str,
include_inputs: bool,
include_dashboard: bool,
):
) -> None:
# Create directories in the output folder for add-on's UI nav and views.
os.makedirs(
os.path.join(output_directory, addon_name, "default", "data", "ui", "nav"),
Expand Down Expand Up @@ -289,9 +291,8 @@ def _get_app_manifest(source: str) -> app_manifest_lib.AppManifest:
)
with open(app_manifest_path) as manifest_file:
app_manifest_content = manifest_file.read()
app_manifest = app_manifest_lib.AppManifest()
try:
app_manifest.read(app_manifest_content)
app_manifest = app_manifest_lib.AppManifest(app_manifest_content)
app_manifest.validate()
return app_manifest
except app_manifest_lib.AppManifestFormatException as e:
Expand All @@ -318,7 +319,7 @@ def generate(
addon_version: Optional[str] = None,
output_directory: Optional[str] = None,
python_binary_name: str = "python3",
):
) -> None:
logger.info(f"ucc-gen version {__version__} is used")
logger.info(f"Python binary name to use: {python_binary_name}")
output_directory = _get_build_output_path(output_directory)
Expand Down Expand Up @@ -347,8 +348,9 @@ def generate(

if os.path.isfile(config_path):
logger.info(f"Using globalConfig file located @ {config_path}")
global_config = global_config_lib.GlobalConfig()
global_config.parse(config_path, is_global_config_yaml)
global_config = global_config_lib.GlobalConfig(
config_path, is_global_config_yaml
)
try:
validator = global_config_validator.GlobalConfigValidator(
internal_root_dir, global_config
Expand Down
2 changes: 1 addition & 1 deletion splunk_add_on_ucc_framework/commands/import_from_aob.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
logger = logging.getLogger("ucc_gen")


def import_from_aob(addon_name: str):
def import_from_aob(addon_name: str) -> None:
addon_name_directory = os.path.join(os.getcwd(), addon_name)
if not os.path.isdir(addon_name_directory):
logger.error(f"No such directory {addon_name_directory}")
Expand Down
8 changes: 4 additions & 4 deletions splunk_add_on_ucc_framework/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations
import logging
import os
import sys
import re
from typing import Optional

import shutil

Expand All @@ -34,7 +34,7 @@
ADDON_INPUT_NAME_RE = re.compile(ADDON_INPUT_NAME_RE_STR)


def _is_valid(pattern: re.Pattern, string: str) -> bool:
def _is_valid(pattern: re.Pattern[str], string: str) -> bool:
result = pattern.search(string)
if result is None:
return False
Expand All @@ -60,7 +60,7 @@ def _generate_addon(
addon_display_name: str,
addon_input_name: str,
addon_version: str,
addon_rest_root: Optional[str] = None,
addon_rest_root: str | None = None,
overwrite: bool = False,
) -> str:
generated_addon_path = os.path.join(
Expand Down Expand Up @@ -151,7 +151,7 @@ def init(
addon_display_name: str,
addon_input_name: str,
addon_version: str,
addon_rest_root: Optional[str] = None,
addon_rest_root: str | None = None,
overwrite: bool = False,
) -> str:
if not _is_valid_addon_name(addon_name):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
from os import linesep
from os import path as op
from typing import Dict, Any

from jinja2 import Environment, FileSystemLoader

Expand All @@ -37,9 +38,9 @@
class AlertActionsConfGeneration:
def __init__(
self,
input_setting,
package_path,
):
input_setting: Dict[str, Any],
package_path: str,
) -> None:
self._alert_conf_name = "alert_actions.conf"
self._alert_spec_name = "alert_actions.conf.spec"
self._eventtypes_conf = "eventtypes.conf"
Expand All @@ -63,20 +64,20 @@ def __init__(
"icon_path": "alerticon.png",
}

def get_local_conf_file_path(self, conf_name):
def get_local_conf_file_path(self, conf_name: str) -> str:
local_path = op.join(self._package_path, "default")
if not op.exists(local_path):
os.makedirs(local_path)

return op.join(local_path, conf_name)

def get_spec_file_path(self):
def get_spec_file_path(self) -> str:
readme_path = op.join(self._package_path, "README")
if not op.exists(readme_path):
os.makedirs(readme_path)
return op.join(readme_path, self._alert_spec_name)

def generate_conf(self):
def generate_conf(self) -> None:
logger.info(
'status="starting", operation="generate", '
+ 'object="alert_actions.conf", object_type="file"'
Expand All @@ -94,7 +95,7 @@ def generate_conf(self):
"index",
]
)
alerts = {}
alerts: Dict[str, Any] = {}
for alert in self._alert_settings:
alert_name = alert["short_name"]
alerts[alert_name] = []
Expand Down Expand Up @@ -139,7 +140,7 @@ def generate_conf(self):
+ 'object="alert_actions.conf", object_type="file"'
)

def generate_eventtypes(self):
def generate_eventtypes(self) -> None:
logger.info(
'status="starting", operation="generate", '
+ 'object="eventtypes.conf", object_type="file"'
Expand All @@ -162,7 +163,7 @@ def generate_eventtypes(self):
+ 'object="eventtypes.conf", object_type="file"'
)

def generate_tags(self):
def generate_tags(self) -> None:
logger.info(
'status="starting", operation="generate", '
+ 'object="tags.conf", object_type="file"'
Expand All @@ -185,7 +186,7 @@ def generate_tags(self):
+ 'object="tags.conf", object_type="file"'
)

def generate_spec(self):
def generate_spec(self) -> None:
logger.info(
'status="starting", operation="generate", '
+ 'object="alert_actions.conf.spec", object_type="file"'
Expand All @@ -200,7 +201,7 @@ def generate_spec(self):
"dropdownlist_splunk_search": "list",
"radio": "list",
}
alerts = {}
alerts: Dict[str, Any] = {}
for alert in self._alert_settings:
alert_name = alert["short_name"]
alerts[alert_name] = []
Expand Down Expand Up @@ -236,14 +237,14 @@ def generate_spec(self):
+ 'object="alert_actions.conf.spec", object_type="file"'
)

def handle(self):
def handle(self) -> None:
self.add_default_settings()
self.generate_conf()
self.generate_spec()
self.generate_eventtypes()
self.generate_tags()

def add_default_settings(self):
def add_default_settings(self) -> None:
for alert in self._alert_settings:
if ac.ALERT_PROPS not in list(alert.keys()):
alert[ac.ALERT_PROPS] = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
class AlertHtmlGenerator:
def __init__(
self,
input_setting,
package_path,
):
input_setting: Dict[str, Any],
package_path: str,
) -> None:
self._all_setting = input_setting
self._package_path = package_path
# nosemgrep: splunk.autoescape-disabled, python.jinja2.security.audit.autoescape-disabled.autoescape-disabled
Expand Down Expand Up @@ -86,7 +86,7 @@ def handle_alert(self, alert: Dict[str, Any]) -> None:
text,
)

def handle(self):
def handle(self) -> None:
logger.info("Started generating alert actions HTML files")
for alert in self._alert_actions_setting:
alert_short_name = alert["short_name"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
import os
from os.path import basename as bn
from typing import Any

import addonfactory_splunk_conf_parser_lib as conf_parser

Expand All @@ -32,7 +33,7 @@
logger = logging.getLogger("ucc_gen")


def remove_alert_from_conf_file(alert, conf_file):
def remove_alert_from_conf_file(alert: Any, conf_file: str) -> None:
if not alert or not conf_file:
logger.info('alert="%s", conf_file="%s"', alert, conf_file)
return
Expand Down Expand Up @@ -65,7 +66,9 @@ def remove_alert_from_conf_file(alert, conf_file):
parser.write(cf)


def merge_conf_file(src_file, dst_file, merge_mode="stanza_overwrite"):
def merge_conf_file(
src_file: str, dst_file: str, merge_mode: str = "stanza_overwrite"
) -> None:
if not os.path.isfile(src_file):
return
if not os.path.isfile(dst_file):
Expand Down

0 comments on commit 91ff2a5

Please sign in to comment.