Skip to content

Commit

Permalink
Refactoring role spec stuff into a dedicated parsing class
Browse files Browse the repository at this point in the history
Also reworking tests to cut down on the number of patches required
by sub-classing the DataLoader() class and reworking the base object's
structure a bit to allow its use
  • Loading branch information
jimi-c committed Oct 30, 2014
1 parent bd203a4 commit 3b0e641
Show file tree
Hide file tree
Showing 13 changed files with 912 additions and 566 deletions.
11 changes: 10 additions & 1 deletion v2/ansible/parsing/yaml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ def load_from_file(self, file_name):

return parsed_data

def path_exists(self, path):
return os.path.exists(path)

def is_directory(self, path):
return os.path.isdir(path)

def is_file(self, path):
return os.path.isfile(path)

def _safe_load(self, stream):
''' Implements yaml.safe_load(), except using our custom loader class. '''
return load(stream, AnsibleLoader)
Expand All @@ -100,7 +109,7 @@ def _get_file_contents(self, file_name):
Reads the file contents from the given file name, and will decrypt them
if they are found to be vault-encrypted.
'''
if not os.path.exists(file_name) or not os.path.isfile(file_name):
if not self.path_exists(file_name) or not self.is_file(file_name):
raise AnsibleParserError("the file_name '%s' does not exist, or is not readable" % file_name)

show_content = True
Expand Down
20 changes: 13 additions & 7 deletions v2/ansible/playbook/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@

class Base:

_tags = FieldAttribute(isa='list')
_when = FieldAttribute(isa='list')
def __init__(self):

def __init__(self, loader=DataLoader):

# the data loader class is used to parse data from strings and files
self._loader = loader()
# initialize the data loader, this will be provided later
# when the object is actually loaded
self._loader = None

# each class knows attributes set upon it, see Task.py for example
self._attributes = dict()
Expand All @@ -61,11 +59,17 @@ def munge(self, ds):

return ds

def load_data(self, ds):
def load_data(self, ds, loader=None):
''' walk the input datastructure and assign any values '''

assert ds is not None

# the data loader class is used to parse data from strings and files
if loader is not None:
self._loader = loader
else:
self._loader = DataLoader()

if isinstance(ds, string_types) or isinstance(ds, FileIO):
ds = self._loader.load(ds)

Expand All @@ -89,6 +93,8 @@ def load_data(self, ds):
self.validate()
return self

def get_loader(self):
return self._loader

def validate(self):
''' validation that is done at parse time, not load time '''
Expand Down
6 changes: 4 additions & 2 deletions v2/ansible/playbook/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Block(Base):
_block = FieldAttribute(isa='list')
_rescue = FieldAttribute(isa='list')
_always = FieldAttribute(isa='list')
_tags = FieldAttribute(isa='list', default=[])
_when = FieldAttribute(isa='list', default=[])

# for future consideration? this would be functionally
# similar to the 'else' clause for exceptions
Expand All @@ -43,9 +45,9 @@ def get_variables(self):
return dict()

@staticmethod
def load(data, role=None):
def load(data, role=None, loader=None):
b = Block(role=role)
return b.load_data(data)
return b.load_data(data, loader=loader)

def munge(self, ds):
'''
Expand Down
Loading

0 comments on commit 3b0e641

Please sign in to comment.