summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2020-12-28 20:07:52 +0100
committerGitHub <noreply@github.com>2020-12-28 20:07:52 +0100
commit829e76f7392e348ccc01c56e9680efb4eba80440 (patch)
tree282dac56ecfccf381fa92ad9027df6779d16ae2f /python
parentc1fcbba9cb45f981e5bd8decf3ebbc1e17d9fbd9 (diff)
parenteeb78e842423319169b036d16601e73227dbffdd (diff)
downloadvyos-1x-829e76f7392e348ccc01c56e9680efb4eba80440.tar.gz
vyos-1x-829e76f7392e348ccc01c56e9680efb4eba80440.zip
Merge pull request #643 from c-po/t563-webproxy
webproxy: T563: migrate from old Perl code to XML and get_config_dict()
Diffstat (limited to 'python')
-rw-r--r--python/vyos/template.py7
-rw-r--r--python/vyos/util.py24
2 files changed, 30 insertions, 1 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py
index 63d400642..bf087c223 100644
--- a/python/vyos/template.py
+++ b/python/vyos/template.py
@@ -248,7 +248,6 @@ def dec_ip(address, decrement):
from ipaddress import ip_interface
return str(ip_interface(address).ip - int(decrement))
-
@register_filter('isc_static_route')
def isc_static_route(subnet, router):
# https://ercpe.de/blog/pushing-static-routes-with-isc-dhcp-server
@@ -270,3 +269,9 @@ def isc_static_route(subnet, router):
string += ','.join(router.split('.'))
return string
+
+@register_filter('is_file')
+def is_file(filename):
+ if os.path.exists(filename):
+ return os.path.isfile(filename)
+ return False
diff --git a/python/vyos/util.py b/python/vyos/util.py
index fc6915687..494c8155e 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -215,6 +215,30 @@ def read_file(fname, defaultonfailure=None):
return defaultonfailure
raise e
+def write_file(fname, data, defaultonfailure=None, user=None, group=None):
+ """
+ Write content of data to given fname, should defaultonfailure be not None,
+ it is returned on failure to read.
+
+ If directory of file is not present, it is auto-created.
+ """
+ dirname = os.path.dirname(fname)
+ if not os.path.isdir(dirname):
+ os.makedirs(dirname, mode=0o755, exist_ok=False)
+ chown(dirname, user, group)
+
+ try:
+ """ Write a file to string """
+ bytes = 0
+ with open(fname, 'w') as f:
+ bytes = f.write(data)
+ chown(fname, user, group)
+ return bytes
+ except Exception as e:
+ if defaultonfailure is not None:
+ return defaultonfailure
+ raise e
+
def read_json(fname, defaultonfailure=None):
"""