Skip to content

Commit

Permalink
make pylint happy by cleaning up globals usage
Browse files Browse the repository at this point in the history
  • Loading branch information
k-rister committed Nov 13, 2023
1 parent 4dd8197 commit 0b688ac
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 60 deletions.
86 changes: 39 additions & 47 deletions roadblocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,9 @@
import shlex

from pathlib import Path
from dataclasses import dataclass

from roadblock import roadblock

# define some global variables
@dataclass
class global_vars:
'''Global variables'''

args = None
log = None
wait_for_cmd = None

# log formatting variables
log_debug_format = '[CODE][%(module)s %(funcName)s:%(lineno)d]\n[%(asctime)s][%(levelname) 8s] %(message)s'
log_normal_format = '[%(asctime)s][%(levelname) 8s] %(message)s'


def process_options ():
'''Define the CLI argument parsing options'''
Expand Down Expand Up @@ -132,56 +118,62 @@ def process_options ():
help = argparse.SUPPRESS,
action = "store_true")

t_global.args = parser.parse_args()
args = parser.parse_args()

if t_global.args.wait_for is not None and t_global.args.wait_for_log is None:
if args.wait_for is not None and args.wait_for_log is None:
parser.error("When --wait-for is defined then --wait-for-log must also be defined")

if t_global.args.wait_for is not None:
t_global.wait_for_cmd = shlex.split(t_global.args.wait_for)
p = Path(t_global.wait_for_cmd[0])
if args.wait_for is not None:
cmd = shlex.split(args.wait_for)
p = Path(cmd[0])
if not p.exists():
parser.error(f"The specified --wait-for command does not exist [{t_global.wait_for_cmd[0]}]")
parser.error(f"The specified --wait-for command does not exist [{cmd[0]}]")
if not p.is_file():
parser.error(f"The specified --wait-for command is not a file [{t_global.wait_for_cmd[0]}]")

if t_global.args.log_level == 'debug':
logging.basicConfig(level = logging.DEBUG, format = t_global.log_debug_format, stream = sys.stdout)
elif t_global.args.log_level == 'normal':
logging.basicConfig(level = logging.INFO, format = t_global.log_normal_format, stream = sys.stdout)
parser.error(f"The specified --wait-for command is not a file [{cmd[0]}]")

t_global.log = logging.getLogger(__file__)
return args


def main():
'''Main control block'''

process_options()
args = process_options()

# log formatting variables
log_debug_format = '[CODE][%(module)s %(funcName)s:%(lineno)d]\n[%(asctime)s][%(levelname) 8s] %(message)s'
log_normal_format = '[%(asctime)s][%(levelname) 8s] %(message)s'

if args.log_level == 'debug':
logging.basicConfig(level = logging.DEBUG, format = log_debug_format, stream = sys.stdout)
elif args.log_level == 'normal':
logging.basicConfig(level = logging.INFO, format = log_normal_format, stream = sys.stdout)

logger = logging.getLogger(__file__)

log_debug = False
if t_global.args.log_level == "debug":
if args.log_level == "debug":
log_debug = True

rb = roadblock(t_global.log, log_debug)
rb.set_uuid(t_global.args.roadblock_uuid)
rb.set_role(t_global.args.roadblock_role)
rb.set_follower_id(t_global.args.roadblock_follower_id)
rb.set_leader_id(t_global.args.roadblock_leader_id)
rb.set_timeout(t_global.args.roadblock_timeout)
rb.set_redis_server(t_global.args.roadblock_redis_server)
rb.set_redis_password(t_global.args.roadblock_redis_password)
rb.set_followers(t_global.args.roadblock_followers)
rb.set_abort(t_global.args.abort)
rb.set_message_log(t_global.args.message_log)
rb.set_user_messages(t_global.args.user_messages)
rb.set_message_validation(t_global.args.message_validation)
rb.set_connection_watchdog(t_global.args.connection_watchdog)
rb.set_wait_for_cmd(t_global.wait_for_cmd)
rb.set_wait_for_log(t_global.args.wait_for_log)
rb.set_simulate_heartbeat_timeout(t_global.args.simulate_heartbeat_timeout)
rb = roadblock(logger, log_debug)
rb.set_uuid(args.roadblock_uuid)
rb.set_role(args.roadblock_role)
rb.set_follower_id(args.roadblock_follower_id)
rb.set_leader_id(args.roadblock_leader_id)
rb.set_timeout(args.roadblock_timeout)
rb.set_redis_server(args.roadblock_redis_server)
rb.set_redis_password(args.roadblock_redis_password)
rb.set_followers(args.roadblock_followers)
rb.set_abort(args.abort)
rb.set_message_log(args.message_log)
rb.set_user_messages(args.user_messages)
rb.set_message_validation(args.message_validation)
rb.set_connection_watchdog(args.connection_watchdog)
if args.wait_for is not None:
rb.set_wait_for_cmd(shlex.split(args.wait_for))
rb.set_wait_for_log(args.wait_for_log)
rb.set_simulate_heartbeat_timeout(args.simulate_heartbeat_timeout)

return rb.run_it()

if __name__ == "__main__":
t_global = global_vars()
sys.exit(main())
17 changes: 4 additions & 13 deletions utilities/redis-monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@
import sys
import argparse

from dataclasses import dataclass

import redis


@dataclass
class global_vars:
'''Global variables'''

args=None

def process_options ():
'''Define the CLI argument parsing options'''

Expand All @@ -33,17 +25,17 @@ def process_options ():
default = 'foobar',
type = str)

t_global.args = parser.parse_args()
return parser.parse_args()

def main():
'''Main control block'''

process_options()
args = process_options()

try:
redcon = redis.Redis(host = t_global.args.redis_server,
redcon = redis.Redis(host = args.redis_server,
port = 6379,
password = t_global.args.redis_password,
password = args.redis_password,
health_check_interval = 0)
redcon.ping()
except redis.exceptions.ConnectionError as con_error:
Expand All @@ -60,5 +52,4 @@ def main():
return 0

if __name__ == "__main__":
t_global = global_vars()
sys.exit(main())

0 comments on commit 0b688ac

Please sign in to comment.