Skip to content

Commit c1133e1

Browse files
authored
Merge pull request #114 from consideRatio/pr/var-name-refactor
refactor: rename two local variables for readability
2 parents 387ee96 + 270beec commit c1133e1

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

chartpress.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -512,19 +512,33 @@ def build_images(prefix, images, tag=None, push=False, force_push=False, force_b
512512

513513
def _update_values_file_with_modifications(name, modifications):
514514
"""
515-
Update <name>/values.yaml file with a dictionary of modifications.
515+
Update <name>/values.yaml file with a dictionary of modifications with its
516+
root level keys representing a path within the values.yaml file.
517+
518+
Example of a modifications dictionary:
519+
520+
{
521+
"server.image": {
522+
"repository": "my-docker-org/server",
523+
"tag": "v1.0.0",
524+
},
525+
"server.initContainers.0.image": {
526+
"repository": "my-docker-org/server-init",
527+
"tag": "v1.0.0",
528+
}
529+
}
516530
"""
517531
values_file = os.path.join(name, 'values.yaml')
518532

519533
with open(values_file) as f:
520534
values = yaml.load(f)
521535

522-
for key, value in modifications.items():
523-
if not isinstance(value, dict) or set(value.keys()) != {'repository', 'tag'}:
524-
raise ValueError(f"I only understand image updates with 'repository', 'tag', not: {value!r}")
525-
parts = key.split('.')
536+
for path_key, path_value in modifications.items():
537+
if not isinstance(path_value, dict) or set(path_value.keys()) != {'repository', 'tag'}:
538+
raise ValueError(f"I only understand image updates with 'repository', 'tag', not: {path_value!r}")
539+
526540
mod_obj = parent = values
527-
for p in parts:
541+
for p in path_key.split('.'):
528542
if p.isdigit():
529543
# integers are indices in lists
530544
p = int(p)
@@ -537,32 +551,32 @@ def _update_values_file_with_modifications(name, modifications):
537551
if keys:
538552
for repo_key in keys:
539553
before = mod_obj.get(repo_key, None)
540-
if before != value['repository']:
541-
_log(f"Updating {values_file}: {key}.{repo_key}: {value}")
542-
mod_obj[repo_key] = value['repository']
554+
if before != path_value['repository']:
555+
_log(f"Updating {values_file}: {path_key}.{repo_key}: {path_value}")
556+
mod_obj[repo_key] = path_value['repository']
543557
else:
544558
possible_keys = ' or '.join(IMAGE_REPOSITORY_KEYS)
545559
raise KeyError(
546-
f'Could not find {possible_keys} in {values_file}:{key}'
560+
f'Could not find {possible_keys} in {values_file}:{path_key}'
547561
)
548562

549563
before = mod_obj.get('tag', None)
550-
if before != value['tag']:
551-
_log(f"Updating {values_file}: {key}.tag: {value}")
552-
mod_obj['tag'] = value['tag']
564+
if before != path_value['tag']:
565+
_log(f"Updating {values_file}: {path_key}.tag: {path_value}")
566+
mod_obj['tag'] = path_value['tag']
553567
elif isinstance(mod_obj, str):
554568
# scalar image string, not dict with separate repository, tag keys
555-
image = "{repository}:{tag}".format(**value)
569+
image = "{repository}:{tag}".format(**path_value)
556570
try:
557571
before = parent[last_part]
558572
except (KeyError, IndexError):
559573
before = None
560574
if before != image:
561-
_log(f"Updating {values_file}: {key}: {image}")
575+
_log(f"Updating {values_file}: {path_key}: {image}")
562576
parent[last_part] = image
563577
else:
564578
raise TypeError(
565-
f'The key {key} in {values_file} must be a mapping or string, not {type(mod_obj)}.'
579+
f'{path_key} in {values_file} must be a mapping or string, not {type(mod_obj)}.'
566580
)
567581

568582
with open(values_file, 'w') as f:

0 commit comments

Comments
 (0)