Skip to content

Commit

Permalink
style: Run latest version of Black against all files
Browse files Browse the repository at this point in the history
  • Loading branch information
btorresgil committed Jan 18, 2024
1 parent debadc8 commit 17c2cf0
Show file tree
Hide file tree
Showing 31 changed files with 1,028 additions and 258 deletions.
6 changes: 4 additions & 2 deletions docs/configtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ def node_style(cls):
style = "style=filled " + nodestyle[module] + " "
except:
pass
result = ' {0} [{1}URL="../module-{2}.html#panos.{3}" target="_top"];\n'.format(
cls_name, style, module, cls
result = (
' {0} [{1}URL="../module-{2}.html#panos.{3}" target="_top"];\n'.format(
cls_name, style, module, cls
)
)
else:
if style:
Expand Down
7 changes: 5 additions & 2 deletions examples/dyn_address_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@


def main():

# Get command line arguments
parser = argparse.ArgumentParser(
description="Tag an IP address on a Palo Alto Networks Next generation Firewall"
Expand Down Expand Up @@ -111,7 +110,11 @@ def main():
logging.basicConfig(format=logging_format, level=logging_level)

# Connect to the device and determine its type (Firewall or Panorama).
device = PanDevice.create_from_device(args.hostname, args.username, args.password,)
device = PanDevice.create_from_device(
args.hostname,
args.username,
args.password,
)

# Panorama does not have a userid API, so exit.
# You can use the userid API on a firewall with the Panorama 'target'
Expand Down
7 changes: 5 additions & 2 deletions examples/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@


def main():

# Get command line arguments
parser = argparse.ArgumentParser(
description="Upgrade a Palo Alto Networks Firewall or Panorama to the specified version"
Expand Down Expand Up @@ -95,7 +94,11 @@ def main():

# Connect to the device and determine its type (Firewall or Panorama).
# This is important to know what version to upgrade to next.
device = PanDevice.create_from_device(args.hostname, args.username, args.password,)
device = PanDevice.create_from_device(
args.hostname,
args.username,
args.password,
)

# Perform the upgrades in sequence with reboots between each upgrade
device.software.upgrade_to_version(args.version, args.dryrun)
Expand Down
7 changes: 5 additions & 2 deletions examples/userid.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@


def main():

# Get command line arguments
parser = argparse.ArgumentParser(
description="Update User-ID by adding or removing a user-to-ip mapping"
Expand Down Expand Up @@ -90,7 +89,11 @@ def main():
logging.basicConfig(format=logging_format, level=logging_level)

# Connect to the device and determine its type (Firewall or Panorama).
device = PanDevice.create_from_device(args.hostname, args.username, args.password,)
device = PanDevice.create_from_device(
args.hostname,
args.username,
args.password,
)

logging.debug("Detecting type of device")

Expand Down
38 changes: 22 additions & 16 deletions panos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,12 @@ def __repr__(self):
return "PanOSVersion ('%s')" % str(self)

def __lt__(self, other):

# Handle 'latest' is always higher
if isstring(other) and other == "latest":
return True

other = stringToVersion(other)
for (x, y) in zip(self.mainrelease, other.mainrelease):
for x, y in zip(self.mainrelease, other.mainrelease):
if x < y:
return True
if x > y:
Expand All @@ -203,7 +202,6 @@ def __ge__(self, other):
return not self.__lt__(other)

def __eq__(self, other):

# Handle 'latest' which is always different
if isstring(other) and other == "latest":
return False
Expand Down Expand Up @@ -241,7 +239,9 @@ def tree_legend_dot():
result += (
'{module} [style=filled fillcolor={color} URL="{url}'
'/module-{module}.html" target="_blank"];'.format(
module=module, color=node_color(module), url=DOCUMENTATION_URL,
module=module,
color=node_color(module),
url=DOCUMENTATION_URL,
)
)
result += "}"
Expand Down Expand Up @@ -286,7 +286,11 @@ def string_or_list(value):
value,
]
return (
list(value) if "__iter__" in dir(value) else [value,]
list(value)
if "__iter__" in dir(value)
else [
value,
]
)


Expand Down Expand Up @@ -463,8 +467,8 @@ def node_color(module):

def object_classes():
import inspect
from panos import errors
from panos import base

from panos import base, errors

current_module = sys.modules[__name__]

Expand All @@ -476,15 +480,17 @@ def object_classes():
if the_cls not in omits:
omits.append(the_cls)

from panos import device
from panos import firewall
from panos import ha
from panos import network
from panos import objects
from panos import panorama
from panos import plugins
from panos import policies
from panos import predefined
from panos import (
device,
firewall,
ha,
network,
objects,
panorama,
plugins,
policies,
predefined,
)

classes = {}
for pkg in (device, firewall, ha, network, objects, panorama, policies, predefined):
Expand Down
82 changes: 61 additions & 21 deletions panos/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ def element(self, with_children=True, comparable=False):
nextelement = root

for section in path:

if section.find("|") != -1:
# This is an element variable, so create an element containing
# the variables's value
Expand Down Expand Up @@ -1648,7 +1647,13 @@ def _set_reference(
if var_type == "list":
if var is None:
update_needed = True
setattr(obj, reference_var, [self,])
setattr(
obj,
reference_var,
[
self,
],
)
if update:
obj.update(reference_var)
elif not isinstance(var, list):
Expand Down Expand Up @@ -1992,7 +1997,9 @@ def delete_similar(self):
for chunk in chunk_instances_for_delete_similar(instances):
dev.xapi.delete(
"{0}/{1}[{2}]".format(
xpath, prefix, " or ".join(joiner.format(x.uid) for x in chunk),
xpath,
prefix,
" or ".join(joiner.format(x.uid) for x in chunk),
),
retry_on_peer=self.HA_SYNC,
)
Expand Down Expand Up @@ -2124,14 +2131,13 @@ def hierarchy_info(self):
dict: Hierarchy information about this object.
"""
from panos.firewall import Firewall
from panos.panorama import DeviceGroup
from panos.panorama import Panorama
from panos.panorama import Template
from panos.panorama import TemplateStack
from panos.panorama import DeviceGroup, Panorama, Template, TemplateStack

classes = panos.object_classes()
configs = [
[self.__class__,],
[
self.__class__,
],
]
updated_configs = []

Expand All @@ -2143,7 +2149,10 @@ def hierarchy_info(self):
configs.pop(num)
for p in parents:
configs.append(
chain + [p,]
chain
+ [
p,
]
)
break
else:
Expand Down Expand Up @@ -2801,7 +2810,8 @@ def __getattr__(self, name):

raise AttributeError(
"'{0}' object has no attribute '{1}'".format(
self.__class__.__name__, str(name),
self.__class__.__name__,
str(name),
)
)

Expand Down Expand Up @@ -2886,9 +2896,7 @@ def __repr__(self):


class ValueEntry(VersionedPanObject):
"""Base class for objects that only have a value element.
"""
"""Base class for objects that only have a value element."""

ROOT = Root.VSYS
SUFFIX = ENTRY
Expand Down Expand Up @@ -3722,7 +3730,12 @@ def get_device_version(self):

@classmethod
def create_from_device(
cls, hostname, api_username=None, api_password=None, api_key=None, port=443,
cls,
hostname,
api_username=None,
api_password=None,
api_key=None,
port=443,
):
"""Factory method to create a :class:`panos.firewall.Firewall`
or :class:`panos.panorama.Panorama` object from a live device
Expand All @@ -3744,18 +3757,33 @@ def create_from_device(
# Create generic PanDevice to connect and get information
from panos import firewall, panorama

device = PanDevice(hostname, api_username, api_password, api_key, port,)
device = PanDevice(
hostname,
api_username,
api_password,
api_key,
port,
)
system_info = device.refresh_system_info()
version = system_info[0]
model = system_info[1]
if model == "Panorama" or model.startswith("M-"):
instance = panorama.Panorama(
hostname, api_username, api_password, device.api_key, port,
hostname,
api_username,
api_password,
device.api_key,
port,
)
else:
serial = system_info[2]
instance = firewall.Firewall(
hostname, api_username, api_password, device.api_key, serial, port,
hostname,
api_username,
api_password,
device.api_key,
serial,
port,
)
instance._set_version_and_version_info(version)
return instance
Expand Down Expand Up @@ -3903,10 +3931,16 @@ def method(self, *args, **kwargs):

def classify_exception(self, e):
if str(e) == "Invalid credentials.":
return err.PanInvalidCredentials(str(e), pan_device=self.pan_device,)
return err.PanInvalidCredentials(
str(e),
pan_device=self.pan_device,
)
elif str(e).startswith("URLError:"):
if str(e).endswith("timed out"):
return err.PanConnectionTimeout(str(e), pan_device=self.pan_device,)
return err.PanConnectionTimeout(
str(e),
pan_device=self.pan_device,
)
else:
# This could be that we have an old version of OpenSSL
# that doesn't support TLSv1.1, so check for that and give
Expand Down Expand Up @@ -4836,7 +4870,12 @@ def _commit(
logger.debug(
self.id
+ ": commit requested: commit_all:%s sync:%s sync_all:%s cmd:%s"
% (str(commit_all), str(sync), str(sync_all), cmd,)
% (
str(commit_all),
str(sync),
str(sync_all),
cmd,
)
)
if commit_all:
action = "all"
Expand Down Expand Up @@ -5603,7 +5642,8 @@ def is_ready(self, minutes=None, seconds=None):
end = None
if minutes is not None or seconds is not None:
end = datetime.datetime.now() + datetime.timedelta(
minutes=minutes or 0, seconds=seconds or 0,
minutes=minutes or 0,
seconds=seconds or 0,
)

while True:
Expand Down
Loading

0 comments on commit 17c2cf0

Please sign in to comment.