Skip to content

Commit

Permalink
Formatted some more Python code with black
Browse files Browse the repository at this point in the history
We currently don't format all the python code automatically but we
think it may be a good idea to do it more and more.

Eventually we'll get to a point where all the code has the same coding
standards (although some of the formatting decisions may make the code
slightly less readable, e.g. see `test_tool.py` file in this commit)

NOTE: Keeping these formatting changes in separate commits for the
moment to make it explicit in Git history. Would help when investigating
bugs with with Git bisect/blame and reduce the time spent getting to the
real commit which may have caused it and the reasons why these changes
were made.

Keeping these formatting changes with other changes may confusing for
the person investigating.
  • Loading branch information
xoen committed Jun 26, 2020
1 parent 5327586 commit 0228d73
Showing 1 changed file with 30 additions and 34 deletions.
64 changes: 30 additions & 34 deletions controlpanel/api/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,24 @@ class HelmError(APIException):


class Helm(object):

@classmethod
def execute(cls, *args, check=True, **kwargs):
should_wait = False
if 'timeout' in kwargs:
if "timeout" in kwargs:
should_wait = True
timeout = kwargs.pop('timeout')
timeout = kwargs.pop("timeout")

try:
log.debug(' '.join(['helm', *args]))
log.debug(" ".join(["helm", *args]))
env = os.environ.copy()
# helm checks for existence of DEBUG env var
if 'DEBUG' in env:
del env['DEBUG']
if "DEBUG" in env:
del env["DEBUG"]
proc = subprocess.Popen(
["helm", *args],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
encoding='utf8',
encoding="utf8",
env=env,
**kwargs,
)
Expand Down Expand Up @@ -92,63 +91,61 @@ def parse_upgrade_output(output):
section = None
columns = None
last_deployed = None
namespace = ''
namespace = ""
resource_type = None
resources = {}
notes = []

for line in output.split('\n'):
for line in output.split("\n"):

if line.startswith('LAST DEPLOYED:'):
if line.startswith("LAST DEPLOYED:"):
last_deployed = datetime.strptime(
line.split(':', 1)[1],
' %a %b %d %H:%M:%S %Y',
line.split(":", 1)[1], " %a %b %d %H:%M:%S %Y",
)
continue

if line.startswith('NAMESPACE:'):
namespace = line.split(':', 1)[1].strip()
if line.startswith("NAMESPACE:"):
namespace = line.split(":", 1)[1].strip()
continue

if line.startswith('==> ') and section == 'RESOURCES':
resource_type = line.split(' ', 1)[1].strip()
if line.startswith("==> ") and section == "RESOURCES":
resource_type = line.split(" ", 1)[1].strip()
continue

if line.startswith('RESOURCES:'):
section = 'RESOURCES'
if line.startswith("RESOURCES:"):
section = "RESOURCES"
continue

if line.startswith('NAME') and resource_type:
if line.startswith("NAME") and resource_type:
columns = line.lower()
columns = re.split(r'\s+', columns)
columns = re.split(r"\s+", columns)
continue

if section == 'NOTES':
if section == "NOTES":
notes.append(line)
continue

if line.startswith('NOTES:'):
section = 'NOTES'
if line.startswith("NOTES:"):
section = "NOTES"
continue

if section and line.strip():
row = re.split(r'\s+', line)
row = re.split(r"\s+", line)
row = dict(zip(columns, row))
resources[resource_type] = [
*resources.get(resource_type, []),
*[row],
]

return {
'last_deployed': last_deployed,
'namespace': namespace,
'resources': resources,
'notes': '\n'.join(notes),
"last_deployed": last_deployed,
"namespace": namespace,
"resources": resources,
"notes": "\n".join(notes),
}


class Chart(object):

def __init__(self, name, description, version, app_version):
self.name = name
self.description = description
Expand All @@ -162,10 +159,7 @@ class HelmRepository(object):

HELM_HOME = Helm.execute("home").stdout.read().strip()
REPO_PATH = os.path.join(
HELM_HOME,
"repository",
"cache",
f"{settings.HELM_REPO}-index.yaml",
HELM_HOME, "repository", "cache", f"{settings.HELM_REPO}-index.yaml",
)

_updated_at = None
Expand All @@ -186,7 +180,9 @@ def _load(cls):
cls._repository = yaml.load(f, Loader=yaml.FullLoader)
except Exception as err:
wrapped_err = HelmError(err)
wrapped_err.detail = f"Error while opening/parsing helm repository cache: '{cls.REPO_PATH}'"
wrapped_err.detail = (
f"Error while opening/parsing helm repository cache: '{cls.REPO_PATH}'"
)
raise HelmError(wrapped_err)

@classmethod
Expand Down

0 comments on commit 0228d73

Please sign in to comment.