Skip to content

Commit

Permalink
Use f-strings everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
mshak2 committed Jul 12, 2024
1 parent 3ca4c83 commit f8cb9e8
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions sarracenia/rabbitmq_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,24 @@ def exec_rabbitmqadmin(url, options, simulate=False):
command += ' --ssl --port=15671 '
command += ' ' + options

logger.debug("command = %s" % command)
logger.debug(f"command = {command}")
if sys.version_info.major < 3 or (sys.version_info.major == 3
and sys.version_info.minor < 5):
if logger: logger.debug("using subprocess.getstatusoutput")

if simulate:
print("dry_run: %s" % ' '.join(command))
print(f"dry_run: {' '.join(command)}")
return 0, None

return subprocess.getstatusoutput(command)
else:
cmdlin = command.replace("'", '')
cmdlst = cmdlin.split()
if logger:
logger.debug("using subprocess.run cmdlst=%s" %
' '.join(cmdlst))
logger.debug(f"using subprocess.run cmdlst={' '.join(cmdlst)}")

if simulate:
print("dry_run: %s" % cmdlin)
print(f"dry_run: {cmdlin}")
return 0, None

rclass = subprocess.run(cmdlst, stdout=subprocess.PIPE)
Expand All @@ -69,10 +68,10 @@ def exec_rabbitmqadmin(url, options, simulate=False):
except:
if sys.version_info.major < 3 or (sys.version_info.major == 3
and sys.version_info.minor < 5):
if logger: logger.error("trying run command %s %s" % command)
if logger: logger.error(f"trying run command {command[0]} {command[1]}")
else:
if logger:
logger.error("trying run command %s %s" % ' '.join(cmdlst))
logger.error(f"trying run command {' '.join(cmdlst)}")
if logger: logger.debug('Exception details:', exc_info=True)

return 0, None
Expand All @@ -83,9 +82,9 @@ def add_user(url, role, user, passwd, simulate):
add the given user with the given credentials.
"""

declare = "declare user name='%s' password=" % user
declare = f"declare user name='{user}' password="

if passwd != None: declare += "\'%s\'" % urllib.parse.unquote(passwd)
if passwd != None: declare += f"\'{urllib.parse.unquote(passwd)}\'"
if role == 'admin': declare += " tags=administrator "
else: declare += ' tags="" '

Expand All @@ -97,9 +96,8 @@ def add_user(url, role, user, passwd, simulate):
c = "configure=.*"
w = "write=.*"
r = "read=.*"
logger.info("permission user \'%s\' role %s %s %s %s " %
(user + '@' + url.hostname, 'feeder', c, w, r))
declare = "declare permission vhost=/ user=%s %s %s %s" % (user, c, w, r)
logger.info(f"permission user \'{user + '@' + url.hostname}\' role feeder {c} {w} {r} ")
declare = f"declare permission vhost=/ user={user} {c} {w} {r}"
dummy = run_rabbitmqadmin(url, declare, simulate)
return

Expand Down Expand Up @@ -129,8 +127,8 @@ def del_user(url, user, simulate):
"""
delete user from the given broker.
"""
logger.info("deleting user %s" % user)
delete = "delete user name='%s'" % user
logger.info(f"deleting user {user}")
delete = f"delete user name='{user}'"
dummy = run_rabbitmqadmin(url, delete, simulate)


Expand Down Expand Up @@ -301,12 +299,11 @@ def user_access(url, user):
lex = list(
map(lambda x: x['name'],
json.loads(exec_rabbitmqadmin(url, "list exchanges name")[1])))
print("exchanges: %s\n\n" % lex)
print(f"exchanges: {lex}\n\n")

u = 'tsource'
up = rabbitmq_user_access(url, u)
print("permissions for %s: \nqueues: %s\nexchanges: %s\nbindings %s" %
(u, up['queues'], up['exchanges'], up['bindings']))
print(f"permissions for {u}: \nqueues: {up['queues']}\nexchanges: {up['exchanges']}\nbindings {up['bindings']}")
#print( "\n\nbindings: %s" % json.loads(exec_rabbitmqadmin(url,"list bindings")[1]) )


Expand All @@ -316,7 +313,7 @@ def run_rabbitmqadmin(url, options, simulate=False):
capture result.
"""

logger.debug("sr_rabbit run_rabbitmqadmin %s" % options)
logger.debug(f"sr_rabbit run_rabbitmqadmin {options}")
try:
(status, answer) = exec_rabbitmqadmin(url, options, simulate)

Expand All @@ -338,7 +335,6 @@ def run_rabbitmqadmin(url, options, simulate=False):
return lst

except:
logger.error("sr_rabbit/run_rabbitmqadmin failed with option '%s'" %
options)
logger.error(f"sr_rabbit/run_rabbitmqadmin failed with option '{options}'")
logger.debug('Exception details: ', exc_info=True)
return []

0 comments on commit f8cb9e8

Please sign in to comment.