Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python2.6 format fields #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions roles/lib_yaml_editor/library/yedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ class YeditException(Exception):
# pylint: disable=too-many-public-methods,too-many-instance-attributes
class Yedit(object):
''' Class to modify yaml files '''
re_valid_key = r"(((\[-?\d+\])|([0-9a-zA-Z%s/_-]+)).?)+$"
re_key = r"(?:\[(-?\d+)\])|([0-9a-zA-Z{}/_-]+)"
re_valid_key = r"(((\[-?\d+\])|([0-9a-zA-Z{0}/_-]+)).?)+$"
re_key = r"(?:\[(-?\d+)\])|([0-9a-zA-Z{0}/_-]+)"
com_sep = set(['.', '#', '|', ':'])

# pylint: disable=too-many-arguments
Expand All @@ -211,7 +211,7 @@ def __init__(self,
content=None,
content_type='yaml',
separator='.',
backup_ext=".{}".format(time.strftime("%Y%m%dT%H%M%S")),
backup_ext=".{0}".format(time.strftime("%Y%m%dT%H%M%S")),
backup=False):
self.content = content
self._separator = separator
Expand Down Expand Up @@ -267,7 +267,7 @@ def remove_entry(data, key, index=None, value=None, sep='.'):
if value is not None:
data.pop(value)
elif index is not None:
raise YeditException("remove_entry for a dictionary does not have an index {}".format(index))
raise YeditException("remove_entry for a dictionary does not have an index {0}".format(index))
else:
data.clear()

Expand Down Expand Up @@ -339,7 +339,7 @@ def add_entry(data, key, item=None, sep='.'):

elif data and not isinstance(data, dict):
raise YeditException("Unexpected item type found while going through key " +
"path: {} (at key: {})".format(key, dict_key))
"path: {0} (at key: {1})".format(key, dict_key))

data[dict_key] = {}
data = data[dict_key]
Expand All @@ -348,7 +348,7 @@ def add_entry(data, key, item=None, sep='.'):
int(arr_ind) <= len(data) - 1):
data = data[int(arr_ind)]
else:
raise YeditException("Unexpected item type found while going through key path: {}".format(key))
raise YeditException("Unexpected item type found while going through key path: {0}".format(key))

if key == '':
data = item
Expand All @@ -366,7 +366,7 @@ def add_entry(data, key, item=None, sep='.'):
# so we must have been provided some syntax like a.b.c[<int>] = "data" for a
# non-existent array
else:
raise YeditException("Error adding to object at path: {}".format(key))
raise YeditException("Error adding to object at path: {0}".format(key))

return data

Expand Down Expand Up @@ -426,7 +426,7 @@ def write(self):
raise YeditException('Please specify a filename.')

if self.backup and self.file_exists():
shutil.copy(self.filename, '{}{}'.format(self.filename, self.backup_ext))
shutil.copy(self.filename, '{0}{1}'.format(self.filename, self.backup_ext))

# Try to set format attributes if supported
try:
Expand All @@ -443,7 +443,7 @@ def write(self):
elif self.content_type == 'json':
Yedit._write(self.filename, json.dumps(self.yaml_dict, indent=4, sort_keys=True))
else:
raise YeditException('Unsupported content_type: {}.'.format(self.content_type) +
raise YeditException('Unsupported content_type: {0}.'.format(self.content_type) +
'Please specify a content_type of yaml or json.')

return (True, self.yaml_dict)
Expand Down Expand Up @@ -506,7 +506,7 @@ def load(self, content_type='yaml'):
self.yaml_dict = json.loads(contents)
except yaml.YAMLError as err:
# Error loading yaml or json
raise YeditException('Problem with loading yaml file. {}'.format(err))
raise YeditException('Problem with loading yaml file. {0}'.format(err))

return self.yaml_dict

Expand Down Expand Up @@ -626,7 +626,7 @@ def update(self, path, value, index=None, curr_value=None):
# pylint: disable=maybe-no-member
if not isinstance(value, dict):
raise YeditException('Cannot replace key, value entry in dict with non-dict type. ' +
'value=[{}] type=[{}]'.format(value, type(value)))
'value=[{0}] type=[{1}]'.format(value, type(value)))

entry.update(value)
return (True, self.yaml_dict)
Expand Down Expand Up @@ -755,7 +755,7 @@ def parse_value(inc_value, vtype=''):
# we will convert to bool if it matches any of the above cases
if isinstance(inc_value, str) and 'bool' in vtype:
if inc_value not in true_bools and inc_value not in false_bools:
raise YeditException('Not a boolean type. str=[{}] vtype=[{}]'.format(inc_value, vtype))
raise YeditException('Not a boolean type. str=[{0}] vtype=[{1}]'.format(inc_value, vtype))
elif isinstance(inc_value, bool) and 'str' in vtype:
inc_value = str(inc_value)

Expand All @@ -768,7 +768,7 @@ def parse_value(inc_value, vtype=''):
inc_value = yaml.safe_load(inc_value)
except Exception:
raise YeditException('Could not determine type of incoming value. ' +
'value=[{}] vtype=[{}]'.format(type(inc_value), vtype))
'value=[{0}] vtype=[{1}]'.format(type(inc_value), vtype))

return inc_value

Expand Down Expand Up @@ -817,7 +817,7 @@ def run_ansible(params):

if yamlfile.yaml_dict is None and state != 'present':
return {'failed': True,
'msg': 'Error opening file [{}]. Verify that the '.format(params['src']) +
'msg': 'Error opening file [{0}]. Verify that the '.format(params['src']) +
'file exists, that it is has correct permissions, and is valid yaml.'}

if state == 'list':
Expand Down Expand Up @@ -926,7 +926,7 @@ def main():
choices=['yaml', 'json', 'str'],
type='str'),
backup=dict(default=False, type='bool'),
backup_ext=dict(default=".{}".format(time.strftime("%Y%m%dT%H%M%S")), type='str'),
backup_ext=dict(default=".{0}".format(time.strftime("%Y%m%dT%H%M%S")), type='str'),
separator=dict(default='.', type='str'),
edits=dict(default=None, type='list'),
),
Expand Down