Make conditionals more Pythonic

This commit is contained in:
Joshua Boniface 2019-06-24 13:37:56 -04:00
parent 93be983b39
commit b52cf01ecc
5 changed files with 76 additions and 76 deletions

View File

@ -1021,9 +1021,9 @@ def get_list_osd(zk_conn, limit):
if limit:
try:
# Implicitly assume fuzzy limits
if re.match('\^.*', limit) == None:
if not re.match('\^.*', limit):
limit = '.*' + limit
if re.match('.*\$', limit) == None:
if not re.match('.*\$', limit):
limit = limit + '.*'
except Exception as e:
return False, 'Regex Error: {}'.format(e)
@ -1031,7 +1031,7 @@ def get_list_osd(zk_conn, limit):
for osd in full_osd_list:
valid_osd = False
if limit:
if re.match(limit, osd['osd_id']) != None:
if re.match(limit, osd['osd_id']):
valid_osd = True
else:
valid_osd = True
@ -1112,9 +1112,9 @@ def get_list_pool(zk_conn, limit):
if limit:
try:
# Implicitly assume fuzzy limits
if re.match('\^.*', limit) == None:
if not re.match('\^.*', limit):
limit = '.*' + limit
if re.match('.*\$', limit) == None:
if not re.match('.*\$', limit):
limit = limit + '.*'
except Exception as e:
return False, 'Regex Error: {}'.format(e)
@ -1122,7 +1122,7 @@ def get_list_pool(zk_conn, limit):
for pool in full_pool_list:
valid_pool = False
if limit:
if re.match(limit, pool['pool_id']) != None:
if re.match(limit, pool['pool_id']):
valid_pool = True
else:
valid_pool = True
@ -1207,9 +1207,9 @@ def get_list_volume(zk_conn, pool, limit):
if limit:
try:
# Implicitly assume fuzzy limits
if re.match('\^.*', limit) == None:
if not re.match('\^.*', limit):
limit = '.*' + limit
if re.match('.*\$', limit) == None:
if not re.match('.*\$', limit):
limit = limit + '.*'
except Exception as e:
return False, 'Regex Error: {}'.format(e)
@ -1217,7 +1217,7 @@ def get_list_volume(zk_conn, pool, limit):
for volume in full_volume_list:
valid_volume = False
if limit:
if re.match(limit, volume) != None:
if re.match(limit, volume):
valid_volume = True
else:
valid_volume = True
@ -1304,9 +1304,9 @@ def get_list_snapshot(zk_conn, pool, volume, limit):
if limit:
try:
# Implicitly assume fuzzy limits
if re.match('\^.*', limit) == None:
if not re.match('\^.*', limit):
limit = '.*' + limit
if re.match('.*\$', limit) == None:
if not re.match('.*\$', limit):
limit = limit + '.*'
except Exception as e:
return False, 'Regex Error: {}'.format(e)
@ -1314,7 +1314,7 @@ def get_list_snapshot(zk_conn, pool, volume, limit):
for snapshot in full_snapshot_list:
valid_snapshot = False
if limit:
if re.match(limit, snapshot) != None:
if re.match(limit, snapshot):
valid_snapshot = True
else:
valid_snapshot = True

View File

@ -453,10 +453,10 @@ def formatACLList(zk_conn, vni, _direction, acl_list):
description = dict()
rule = dict()
if _direction is None:
directions = ['in', 'out']
else:
if _direction:
directions = [_direction]
else:
directions = ['in', 'out']
# Gather information for printing
for acl in acl_list:
@ -535,10 +535,10 @@ def isValidMAC(macaddr):
""",
re.VERBOSE|re.IGNORECASE)
if allowed.match(macaddr) is None:
return False
else:
if allowed.match(macaddr):
return True
else:
return False
def isValidIP(ipaddr):
ip4_blocks = str(ipaddr).split(".")
@ -608,34 +608,34 @@ def add_network(zk_conn, vni, description, nettype,
def modify_network(zk_conn, vni, **parameters):
# Add the new network to Zookeeper
zk_data = dict()
if parameters['description'] != None:
if parameters['description']:
zk_data.update({'/networks/{}'.format(vni): parameters['description']})
if parameters['domain'] != None:
if parameters['domain']:
zk_data.update({'/networks/{}/domain'.format(vni): parameters['domain']})
if parameters['ip4_network'] != None:
if parameters['ip4_network']:
zk_data.update({'/networks/{}/ip4_network'.format(vni): parameters['ip4_network']})
if parameters['ip4_gateway'] != None:
if parameters['ip4_gateway']:
zk_data.update({'/networks/{}/ip4_gateway'.format(vni): parameters['ip4_gateway']})
if parameters['ip6_network'] != None:
if parameters['ip6_network']:
zk_data.update({'/networks/{}/ip6_network'.format(vni): parameters['ip6_network']})
if parameters['ip6_network'] != '':
if parameters['ip6_network']:
zk_data.update({'/networks/{}/dhcp6_flag'.format(vni): 'True'})
else:
zk_data.update({'/networks/{}/dhcp6_flag'.format(vni): 'False'})
if parameters['ip6_gateway'] != None:
if parameters['ip6_gateway']:
zk_data.update({'/networks/{}/ip6_gateway'.format(vni): parameters['ip6_gateway']})
else:
# If we're changing the network, but don't also specify the gateway,
# generate a new one automatically
if parameters['ip6_network'] != None:
if parameters['ip6_network']:
ip6_netpart, ip6_maskpart = parameters['ip6_network'].split('/')
ip6_gateway = '{}1'.format(ip6_netpart)
zk_data.update({'/networks/{}/ip6_gateway'.format(vni): ip6_gateway})
if parameters['dhcp_flag'] != None:
if parameters['dhcp_flag']:
zk_data.update({'/networks/{}/dhcp_flag'.format(vni): parameters['dhcp_flag']})
if parameters['dhcp_start'] != None:
if parameters['dhcp_start']:
zk_data.update({'/networks/{}/dhcp_start'.format(vni): parameters['dhcp_start']})
if parameters['dhcp_end'] != None:
if parameters['dhcp_end']:
zk_data.update({'/networks/{}/dhcp_end'.format(vni): parameters['dhcp_end']})
zkhandler.writedata(zk_conn, zk_data)
@ -658,7 +658,7 @@ def remove_network(zk_conn, network):
def add_dhcp_reservation(zk_conn, network, ipaddress, macaddress, hostname):
# Validate and obtain standard passed value
net_vni = getNetworkVNI(zk_conn, network)
if net_vni == None:
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
# Use lowercase MAC format exclusively
@ -688,7 +688,7 @@ def add_dhcp_reservation(zk_conn, network, ipaddress, macaddress, hostname):
def remove_dhcp_reservation(zk_conn, network, reservation):
# Validate and obtain standard passed value
net_vni = getNetworkVNI(zk_conn, network)
if net_vni == None:
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
match_description = ''
@ -715,7 +715,7 @@ def remove_dhcp_reservation(zk_conn, network, reservation):
def add_acl(zk_conn, network, direction, description, rule, order):
# Validate and obtain standard passed value
net_vni = getNetworkVNI(zk_conn, network)
if net_vni == None:
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
# Change direction to something more usable
@ -731,7 +731,7 @@ def add_acl(zk_conn, network, direction, description, rule, order):
full_acl_list = getNetworkACLs(zk_conn, net_vni, direction)
acl_list_length = len(full_acl_list)
# Set order to len
if order == None or int(order) > acl_list_length:
if not order or int(order) > acl_list_length:
order = acl_list_length
# Convert passed-in order to an integer
else:
@ -771,7 +771,7 @@ def add_acl(zk_conn, network, direction, description, rule, order):
def remove_acl(zk_conn, network, rule, direction):
# Validate and obtain standard passed value
net_vni = getNetworkVNI(zk_conn, network)
if net_vni == None:
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
# Change direction to something more usable
@ -816,7 +816,7 @@ def remove_acl(zk_conn, network, rule, direction):
def get_info(zk_conn, network, long_output):
# Validate and obtain alternate passed value
net_vni = getNetworkVNI(zk_conn, network)
if net_vni == None:
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
information = formatNetworkInformation(zk_conn, net_vni, long_output)
@ -831,17 +831,17 @@ def get_list(zk_conn, limit):
for net in full_net_list:
description = zkhandler.readdata(zk_conn, '/networks/{}'.format(net))
if limit != None:
if limit:
try:
# Implcitly assume fuzzy limits
if re.match('\^.*', limit) == None:
if not re.match('\^.*', limit):
limit = '.*' + limit
if re.match('.*\$', limit) == None:
if not re.match('.*\$', limit):
limit = limit + '.*'
if re.match(limit, net) != None:
if re.match(limit, net):
net_list.append(net)
if re.match(limit, description) != None:
if re.match(limit, description):
net_list.append(net)
except Exception as e:
return False, 'Regex Error: {}'.format(e)
@ -856,7 +856,7 @@ def get_list(zk_conn, limit):
def get_list_dhcp(zk_conn, network, limit, only_static=False):
# Validate and obtain alternate passed value
net_vni = getNetworkVNI(zk_conn, network)
if net_vni == None:
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
dhcp_list = []
@ -871,9 +871,9 @@ def get_list_dhcp(zk_conn, network, limit, only_static=False):
if limit:
try:
# Implcitly assume fuzzy limits
if re.match('\^.*', limit) == None:
if not re.match('\^.*', limit):
limit = '.*' + limit
if re.match('.*\$', limit) == None:
if not re.match('.*\$', limit):
limit = limit + '.*'
except Exception as e:
return False, 'Regex Error: {}'.format(e)
@ -882,9 +882,9 @@ def get_list_dhcp(zk_conn, network, limit, only_static=False):
for lease in full_dhcp_list:
valid_lease = False
if limit:
if re.match(limit, lease) != None:
if re.match(limit, lease):
valid_lease = True
if re.match(limit, lease) != None:
if re.match(limit, lease):
valid_lease = True
else:
valid_lease = True
@ -900,7 +900,7 @@ def get_list_dhcp(zk_conn, network, limit, only_static=False):
def get_list_acl(zk_conn, network, limit, direction):
# Validate and obtain alternate passed value
net_vni = getNetworkVNI(zk_conn, network)
if net_vni == None:
if not net_vni:
return False, 'ERROR: Could not find network "{}" in the cluster!'.format(network)
# Change direction to something more usable
@ -917,9 +917,9 @@ def get_list_acl(zk_conn, network, limit, direction):
if limit:
try:
# Implcitly assume fuzzy limits
if re.match('\^.*', limit) == None:
if not re.match('\^.*', limit):
limit = '.*' + limit
if re.match('.*\$', limit) == None:
if not re.match('.*\$', limit):
limit = limit + '.*'
except Exception as e:
return False, 'Regex Error: {}'.format(e)
@ -927,7 +927,7 @@ def get_list_acl(zk_conn, network, limit, direction):
for acl in full_acl_list:
valid_acl = False
if limit:
if re.match(limit, acl['description']) != None:
if re.match(limit, acl['description']):
valid_acl = True
else:
valid_acl = True

View File

@ -221,7 +221,7 @@ def get_info(zk_conn, node):
# Get information about node in a pretty format
node_information = getInformationFromNode(zk_conn, node)
if node_information == None:
if not node_information:
return False, 'ERROR: Could not get information about node "{}".'.format(node)
return True, node_information
@ -231,15 +231,15 @@ def get_list(zk_conn, limit):
node_list = []
full_node_list = zkhandler.listchildren(zk_conn, '/nodes')
for node in full_node_list:
if limit != None:
if limit:
try:
# Implcitly assume fuzzy limits
if re.match('\^.*', limit) == None:
if not re.match('\^.*', limit):
limit = '.*' + limit
if re.match('.*\$', limit) == None:
if not re.match('.*\$', limit):
limit = limit + '.*'
if re.match(limit, node) != None:
if re.match(limit, node):
node_list.append(getInformationFromNode(zk_conn, node))
except Exception as e:
return False, 'Regex Error: {}'.format(e)

View File

@ -63,7 +63,7 @@ def getInformationFromXML(zk_conn, uuid):
domain_disks = common.getDomainDisks(parsed_xml)
domain_controllers = common.getDomainControllers(parsed_xml)
if domain_lastnode != '':
if domain_lastnode:
domain_migrated = 'from {}'.format(domain_lastnode)
else:
domain_migrated = 'no'
@ -166,7 +166,7 @@ def define_vm(zk_conn, config_data, target_node, selector):
dom_uuid = parsed_xml.uuid.text
dom_name = parsed_xml.name.text
if target_node == None:
if not target_node:
target_node = common.findTargetNode(zk_conn, selector, dom_uuid)
else:
# Verify node is valid
@ -327,7 +327,7 @@ def move_vm(zk_conn, domain, target_node, selector):
current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
if target_node == None:
if not target_node:
target_node = common.findTargetNode(zk_conn, selector, dom_uuid)
else:
# Verify node is valid
@ -372,7 +372,7 @@ def migrate_vm(zk_conn, domain, target_node, selector, force_migrate, is_cli=Fal
current_node = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(dom_uuid))
last_node = zkhandler.readdata(zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
if last_node != '' and force_migrate != True:
if last_node and not force_migrate:
if is_cli:
click.echo('ERROR: VM "{}" has been previously migrated.'.format(dom_uuid))
click.echo('> Last node: {}'.format(last_node))
@ -382,7 +382,7 @@ def migrate_vm(zk_conn, domain, target_node, selector, force_migrate, is_cli=Fal
else:
return False, 'ERROR: VM "{}" has been previously migrated.'.format(dom_uuid)
if target_node == None:
if not target_node:
target_node = common.findTargetNode(zk_conn, selector, dom_uuid)
else:
# Verify node is valid
@ -490,7 +490,7 @@ def follow_console_log(zk_conn, domain, lines=10):
# Remove the old lines from the new log
diff_console_log = new_console_log.replace(old_console_log, "")
# If there's a difference, print it out
if diff_console_log != "":
if diff_console_log:
print(diff_console_log, end='')
# Wait a second
time.sleep(1)
@ -506,19 +506,19 @@ def get_info(zk_conn, domain):
# Gather information from XML config and print it
domain_information = getInformationFromXML(zk_conn, dom_uuid)
if domain_information == None:
if not domain_information:
return False, 'ERROR: Could not get information about VM "{}".'.format(domain)
return True, domain_information
def get_list(zk_conn, node, state, limit):
if node != None:
if node:
# Verify node is valid
valid_node = common.verifyNode(zk_conn, target_node)
if not valid_node:
return False, "Specified node {} is invalid.".format(target_node)
if state != None:
if state:
valid_states = [ 'start', 'restart', 'shutdown', 'stop', 'failed', 'migrate', 'unmigrate' ]
if not state in valid_states:
return False, 'VM state "{}" is not valid.'.format(state)
@ -527,12 +527,12 @@ def get_list(zk_conn, node, state, limit):
vm_list = []
# Set our limit to a sensible regex
if limit != None:
if limit:
try:
# Implcitly assume fuzzy limits
if re.match('\^.*', limit) == None:
if not re.match('\^.*', limit):
limit = '.*' + limit
if re.match('.*\$', limit) == None:
if not re.match('.*\$', limit):
limit = limit + '.*'
except Exception as e:
return False, 'Regex Error: {}'.format(e)
@ -546,17 +546,17 @@ def get_list(zk_conn, node, state, limit):
vm_node[vm] = zkhandler.readdata(zk_conn, '/domains/{}/node'.format(vm))
vm_state[vm] = zkhandler.readdata(zk_conn, '/domains/{}/state'.format(vm))
# Handle limiting
if limit != None:
if limit:
try:
if re.match(limit, vm) != None:
if node == None and state == None:
if re.match(limit, vm):
if not node and not state:
vm_list.append(getInformationFromXML(zk_conn, vm))
else:
if vm_node[vm] == node or vm_state[vm] == state:
vm_list.append(getInformationFromXML(zk_conn, vm))
if re.match(limit, name) != None:
if node == None and state == None:
if re.match(limit, name):
if not node and not state:
vm_list.append(getInformationFromXML(zk_conn, vm))
else:
if vm_node[vm] == node or vm_state[vm] == state:
@ -565,7 +565,7 @@ def get_list(zk_conn, node, state, limit):
return False, 'Regex Error: {}'.format(e)
else:
# Check node to avoid unneeded ZK calls
if node == None and state == None:
if not node and not state:
vm_list.append(getInformationFromXML(zk_conn, vm))
else:
if vm_node[vm] == node or vm_state[vm] == state:
@ -614,7 +614,7 @@ def format_info(zk_conn, domain_information, long_output):
ainformation.append('{}Previous Node:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['last_node']))
# Get a failure reason if applicable
if domain_information['failed_reason'] != '':
if domain_information['failed_reason']:
ainformation.append('')
ainformation.append('{}Failure reason:{} {}'.format(ansiprint.purple(), ansiprint.end(), domain_information['failed_reason']))

View File

@ -28,10 +28,10 @@ import client_lib.ansiprint as ansiprint
# Exists function
def exists(zk_conn, key):
stat = zk_conn.exists(key)
if stat is None:
return False
else:
if stat:
return True
else:
return False
# Child list function
def listchildren(zk_conn, key):