Skip to content

Commit

Permalink
refactor: remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Rys authored Jul 12, 2021
2 parents f29671b + c0a6b1d commit 0958f66
Show file tree
Hide file tree
Showing 42 changed files with 154 additions and 258 deletions.
2 changes: 1 addition & 1 deletion poetry.lock

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

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ license = "APACHE-2.0"
python = "^3.7"
solnlib = "^4.0.0"
splunk-sdk = "1.6.16"
future = "^0"
splunktalib = "^2.0.0"

[tool.poetry.dev-dependencies]
Expand Down
14 changes: 6 additions & 8 deletions splunktaucclib/alert_actions_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
#
# SPDX-License-Identifier: Apache-2.0

from __future__ import print_function
from splunktaucclib.splunk_aoblib.setup_util import Setup_Util
from splunktaucclib.splunk_aoblib.rest_helper import TARestHelper
import logging
from splunktaucclib.logging_helper import get_logger
from splunktaucclib.cim_actions import ModularAction
import requests
from builtins import str
import csv
import gzip
import sys
Expand All @@ -28,7 +26,7 @@ def __init__(self, ta_name, alert_name):
# self._logger_name = "modalert_" + alert_name
self._logger_name = alert_name + "_modalert"
self._logger = get_logger(self._logger_name)
super(ModularAlertBase, self).__init__(
super().__init__(
sys.stdin.read(), self._logger, alert_name
)
self.setup_util_module = None
Expand Down Expand Up @@ -111,16 +109,16 @@ def _get_proxy_uri(self):
if proxy and proxy.get("proxy_url") and proxy.get("proxy_type"):
uri = proxy["proxy_url"]
if proxy.get("proxy_port"):
uri = "{0}:{1}".format(uri, proxy.get("proxy_port"))
uri = "{}:{}".format(uri, proxy.get("proxy_port"))
if proxy.get("proxy_username") and proxy.get("proxy_password"):
uri = "{0}://{1}:{2}@{3}/".format(
uri = "{}://{}:{}@{}/".format(
proxy["proxy_type"],
proxy["proxy_username"],
proxy["proxy_password"],
uri,
)
else:
uri = "{0}://{1}".format(proxy["proxy_type"], uri)
uri = "{}://{}".format(proxy["proxy_type"], uri)
return uri

def send_http_request(
Expand Down Expand Up @@ -231,7 +229,7 @@ def get_events(self):
self.pre_handle(num, result)
for num, result in enumerate(csv.DictReader(self.result_handle))
)
except IOError:
except OSError:
msg = "Error: {}."
self.log_error(msg.format("No search result. Cannot send alert action."))
sys.exit(2)
Expand Down Expand Up @@ -266,7 +264,7 @@ def run(self, argv):

try:
status = self.process_event()
except IOError:
except OSError:
msg = "Error: {}."
self.log_error(msg.format("No search result. Cannot send alert action."))
sys.exit(2)
Expand Down
34 changes: 14 additions & 20 deletions splunktaucclib/cim_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
#
# SPDX-License-Identifier: Apache-2.0

from builtins import str
from builtins import next
from builtins import range
from six import string_types as basestring
import six
from builtins import object
import collections
import csv
import json
Expand All @@ -34,7 +28,7 @@ class InvalidResultID(Exception):
pass


class ModularAction(object):
class ModularAction:
DEFAULT_MSGFIELDS = [
"signature",
"action_name",
Expand Down Expand Up @@ -111,7 +105,7 @@ def __init__(self, settings, logger, action_name="unknown"):
## if sid contains rt_scheduler with snapshot-sid; drop snapshot-sid
## sometimes self.sid may be an integer (1465593470.1228)
try:
rtsid = re.match("^(rt_scheduler.*)\.(\d+)$", self.sid)
rtsid = re.match(r"^(rt_scheduler.*)\.(\d+)$", self.sid)
if rtsid:
self.sid = rtsid.group(1)
self.sid_snapshot = rtsid.group(2)
Expand Down Expand Up @@ -155,7 +149,7 @@ def __init__(self, settings, logger, action_name="unknown"):
## use | sendalert param.action_name=$action_name$
self.action_name = self.configuration.get("action_name") or action_name
## use sid to determine action_mode
if isinstance(self.sid, basestring) and "scheduler" in self.sid:
if isinstance(self.sid, str) and "scheduler" in self.sid:
self.action_mode = "saved"
else:
self.action_mode = "adhoc"
Expand All @@ -178,7 +172,7 @@ def addinfo(self):
"""
if self.info_file:
try:
with open(self.info_file, "rU") as fh:
with open(self.info_file) as fh:
self.info = next(csv.DictReader(fh))
except Exception as e:
self.message("Could not retrieve info.csv", level=logging.WARN)
Expand Down Expand Up @@ -242,7 +236,7 @@ def message(self, signature, status=None, rids=None, level=logging.INFO, **kwarg
if (x not in ModularAction.DEFAULT_MSGFIELDS) and re.match("[A-Za-z_]+", x)
]
## MSG
msg = "%s %s" % (
msg = "{} {}".format(
ModularAction.DEFAULT_MESSAGE,
" ".join(['{i}="{{d[{i}]}}"'.format(i=i) for i in newargs]),
)
Expand Down Expand Up @@ -280,7 +274,7 @@ def message(self, signature, status=None, rids=None, level=logging.INFO, **kwarg
## attributes of "argsdict"
message = msg.format(d=argsdict)
## prune empty string key-value pairs
for match in re.finditer('[A-Za-z_]+=""(\s|$)', message):
for match in re.finditer(r'[A-Za-z_]+=""(\s|$)', message):
message = message.replace(match.group(0), "", 1)
message = message.strip()
self.logger.log(level, message)
Expand Down Expand Up @@ -319,10 +313,10 @@ def update(self, result):
self.orig_sid = result.get("orig_sid", "")
## This is for events/results that were created as the result of a previous action
self.orig_rid = result.get("orig_rid", "")
if "rid" in result and isinstance(result["rid"], (basestring, int)):
if "rid" in result and isinstance(result["rid"], (str, int)):
self.rid = str(result["rid"])
if self.sid_snapshot:
self.rid = "%s.%s" % (self.rid, self.sid_snapshot)
self.rid = "{}.{}".format(self.rid, self.sid_snapshot)
## add result info to list of named tuples
self.rids.append(self.rid_ntuple(self.orig_sid, self.rid, self.orig_rid))
else:
Expand Down Expand Up @@ -390,7 +384,7 @@ def result2stash(
if (
key.startswith("__mv_")
and val
and isinstance(val, basestring)
and isinstance(val, str)
and val.startswith("$")
and val.endswith("$")
):
Expand All @@ -415,13 +409,13 @@ def result2stash(
if key.startswith("__mv"):
val = val.replace("$$", "$")
## escape quotes
if isinstance(val, basestring):
if isinstance(val, str):
val = val.replace('"', r"\"")
## check map
if mapexp(real_key):
_raw += ', %s="%s"' % ("orig_" + real_key.lstrip("_"), val)
_raw += ', {}="{}"'.format("orig_" + real_key.lstrip("_"), val)
else:
_raw += ', %s="%s"' % (real_key, val)
_raw += ', {}="{}"'.format(real_key, val)
processed_keys.append(real_key)

return _raw
Expand Down Expand Up @@ -499,7 +493,7 @@ def get_string(input, default):

if self.events:
## sanitize file extension
if not fext or not re.match("^[\w-]+$", fext):
if not fext or not re.match(r"^[\w-]+$", fext):
self.logger.warn(
"Requested file extension was ignored due to invalid characters"
)
Expand All @@ -523,7 +517,7 @@ def get_string(input, default):
fout = header_line + default_breaker + (default_breaker).join(chunk)
## write output string
try:
fn = "%s_%s.stash_%s" % (
fn = "{}_{}.stash_{}".format(
mktimegm(time.gmtime()),
random.randint(0, 100000),
fext,
Expand Down
7 changes: 3 additions & 4 deletions splunktaucclib/common/rwlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
This module provides Read-Write lock.
"""

from builtins import object
import threading


class _ReadLocker(object):
class _ReadLocker:
def __init__(self, lock):
self.lock = lock

Expand All @@ -22,7 +21,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
return False


class _WriteLocker(object):
class _WriteLocker:
def __init__(self, lock):
self.lock = lock

Expand All @@ -34,7 +33,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
return False


class RWLock(object):
class RWLock:
"""Simple Read-Write lock.
Allow multiple read but only one writing concurrently.
Expand Down
11 changes: 3 additions & 8 deletions splunktaucclib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
The load/save action is based on specified schema.
"""

from __future__ import absolute_import

from future import standard_library

standard_library.install_aliases()
import sys
from builtins import object
import json
import logging
import traceback
Expand Down Expand Up @@ -54,10 +49,10 @@ def log(msg, msgx="", level=logging.INFO, need_tb=False):
return

msgx = " - " + msgx if msgx else ""
content = "UCC Config Module: %s%s" % (msg, msgx)
content = "UCC Config Module: {}{}".format(msg, msgx)
if need_tb:
stack = "".join(traceback.format_stack())
content = "%s\r\n%s" % (content, stack)
content = "{}\r\n{}".format(content, stack)
stulog.logger.log(level, content, exc_info=1)


Expand All @@ -67,7 +62,7 @@ class ConfigException(UCCException):
pass


class Config(object):
class Config:
"""UCC Config Module"""

# Placeholder stands for any field
Expand Down
9 changes: 2 additions & 7 deletions splunktaucclib/data_collection/ta_checkpoint_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@
#
# SPDX-License-Identifier: Apache-2.0

from __future__ import absolute_import
from future import standard_library

standard_library.install_aliases()
from builtins import object
from . import ta_consts as c
import splunktalib.state_store as ss
import splunktaucclib.common.log as stulog
import re
import urllib.request, urllib.parse, urllib.error


class TACheckPointMgr(object):
class TACheckPointMgr:
SEPARATOR = "___"

def __init__(self, meta_config, task_config):
Expand Down Expand Up @@ -57,7 +52,7 @@ def key_formatter(self):
key_str = TACheckPointMgr.SEPARATOR.join(divide_value)
qualified_key_str = ""
for i in range(len(key_str)):
if re.match("[^\w]", key_str[i]):
if re.match(r"[^\w]", key_str[i]):
qualified_key_str += urllib.parse.quote(key_str[i])
else:
qualified_key_str += key_str[i]
Expand Down
5 changes: 1 addition & 4 deletions splunktaucclib/data_collection/ta_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0

from __future__ import absolute_import
from __future__ import division
import sys
from builtins import object
import socket
from . import ta_consts as c
import os.path as op
Expand All @@ -18,7 +15,7 @@
basestring = str if sys.version_info[0] == 3 else basestring

# methods can be overrided by subclass : process_task_configs
class TaConfig(object):
class TaConfig:
_current_hostname = socket.gethostname()
_appname = util.get_appname_from_path(op.abspath(__file__))

Expand Down
8 changes: 3 additions & 5 deletions splunktaucclib/data_collection/ta_data_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#
# SPDX-License-Identifier: Apache-2.0

from builtins import next
from builtins import object
from splunktaucclib.data_collection import ta_checkpoint_manager as cp
import splunktaucclib.data_collection.ta_data_collector as tdc

Expand All @@ -27,7 +25,7 @@ def build_event(
)


class TaDataClient(object):
class TaDataClient:
def __init__(
self,
all_conf_contents,
Expand Down Expand Up @@ -70,7 +68,7 @@ def create_data_collector(
def client_adapter(job_func):
class TaDataClientAdapter(TaDataClient):
def __init__(self, all_conf_contents, meta_config, task_config, ckpt, chp_mgr):
super(TaDataClientAdapter, self).__init__(
super().__init__(
all_conf_contents, meta_config, task_config, ckpt, chp_mgr
)
self._execute_times = 0
Expand All @@ -82,7 +80,7 @@ def stop(self):
"""

# normaly base class just set self._stop as True
super(TaDataClientAdapter, self).stop()
super().stop()

def get(self):
"""
Expand Down
4 changes: 1 addition & 3 deletions splunktaucclib/data_collection/ta_data_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#
# SPDX-License-Identifier: Apache-2.0

from __future__ import absolute_import
from builtins import object
import time
import threading
from . import ta_consts as c
Expand Down Expand Up @@ -51,7 +49,7 @@
)


class TADataCollector(object):
class TADataCollector:
def __init__(
self,
tconfig,
Expand Down
Loading

0 comments on commit 0958f66

Please sign in to comment.