Skip to content

Commit

Permalink
Added files in render() output #31
Browse files Browse the repository at this point in the history
Closes #31
  • Loading branch information
nemesifier committed Dec 9, 2015
1 parent 8ac9274 commit 69ad2f1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/source/backends/openwrt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ The ``files`` key of the *configuration dictionary* is a custom NetJSON extensio
present in the original NetJSON RFC.

.. warning::
The files won't be included in the output of the ``render`` method because
doing so would make the UCI output invalid.
The files are included in the output of the ``render`` method unless you pass
``files=False``, eg: ``openwrt.render(files=False)``

Plain file example
~~~~~~~~~~~~~~~~~~
Expand Down
46 changes: 42 additions & 4 deletions netjsonconfig/backends/openwrt/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
from ...utils import merge_config
from ...exceptions import ValidationError

DEFAULT_FILE_MODE = '644'
FILE_SECTION_DELIMITER = '# ------ files ------ #'


class OpenWrt(object):
""" OpenWrt Backend """
Expand Down Expand Up @@ -69,9 +72,17 @@ def _merge_config(self, config, templates):
return merge_config(base_config, config)
return config

def render(self):
def render(self, files=True):
"""
Converts the configuration dictionary to the native configuration
in text format.
:param files: boolean indicating if files must be included in the output
defaults to True
"""
self.validate()
output = ''
# render config
for renderer_class in self.renderers:
renderer = renderer_class(self)
additional_output = renderer.render()
Expand All @@ -80,6 +91,33 @@ def render(self):
if output and additional_output:
output += '\n'
output += additional_output
if files:
# render files
files_output = self._render_files()
if files_output:
output += files_output.replace('\n\n\n', '\n\n') # max 3 \n
return output

def _render_files(self):
""" renders files, used in main render method """
output = ''
# render files
files = self.config.get('files', [])
# add delimiter
if files:
output += '\n{0}\n\n'.format(FILE_SECTION_DELIMITER)
for f in files:
if isinstance(f['contents'], list):
contents = '\n'.join(f['contents'])
else:
contents = f['contents']
path = f['path']
mode = f.get('mode', DEFAULT_FILE_MODE)
# add file to output
file_output = '# path: {0}\n'\
'# mode: {1}\n\n'\
'{2}\n\n'.format(path, mode, contents)
output += file_output
return output

def validate(self):
Expand All @@ -101,7 +139,7 @@ def generate(self, name='openwrt-config'):
Generates tar.gz restorable in OpenWRT with:
sysupgrade -r <file>
"""
uci = self.render()
uci = self.render(files=False)
tar = tarfile.open('{0}.tar.gz'.format(name), 'w:gz')
# create a list with all the packages (and remove empty entries)
packages = re.split('package ', uci)
Expand Down Expand Up @@ -140,9 +178,9 @@ def _add_files(self, tar, timestamp):
name=path,
contents=contents,
timestamp=timestamp,
mode=file_item.get('mode', '644'))
mode=file_item.get('mode', DEFAULT_FILE_MODE))

def _add_file(self, tar, name, contents, timestamp, mode='644'):
def _add_file(self, tar, name, contents, timestamp, mode=DEFAULT_FILE_MODE):
"""
adds a single file in tar object
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/openwrt/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def test_file_inclusion(self):
})
output = o.render()
self.assertNotIn('package files', output)
self.assertNotIn('* * * * * echo', output)
self.assertIn('* * * * * echo', output)
# generate tar.gz archive and ensure the additional files are there
o.generate()
tar = tarfile.open('openwrt-config.tar.gz', 'r:gz')
Expand Down

0 comments on commit 69ad2f1

Please sign in to comment.