diff options
Diffstat (limited to 'python')
-rw-r--r-- | python/setup.py | 21 | ||||
-rw-r--r-- | python/vyos/__init__.py | 1 | ||||
-rw-r--r-- | python/vyos/base.py | 18 | ||||
-rw-r--r-- | python/vyos/config.py | 416 | ||||
-rw-r--r-- | python/vyos/configtree.py | 261 | ||||
-rw-r--r-- | python/vyos/defaults.py | 19 | ||||
-rw-r--r-- | python/vyos/interfaces.py | 45 | ||||
-rw-r--r-- | python/vyos/limericks.py | 64 | ||||
-rw-r--r-- | python/vyos/util.py | 65 | ||||
-rw-r--r-- | python/vyos/version.py | 68 |
10 files changed, 0 insertions, 978 deletions
diff --git a/python/setup.py b/python/setup.py deleted file mode 100644 index 304ea5c..0000000 --- a/python/setup.py +++ /dev/null @@ -1,21 +0,0 @@ -import os -from setuptools import setup - -setup( - name = "vyos", - version = "1.2.0", - author = "VyOS maintainers and contributors", - author_email = "maintainers@vyos.net", - description = ("VyOS configuration libraries."), - license = "LGPLv2+", - keywords = "vyos", - url = "http://www.vyos.io", - packages=['vyos'], - long_description="VyOS configuration libraries", - classifiers=[ - "Development Status :: 4 - Beta", - "Topic :: Utilities", - "License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)", - ], -) - diff --git a/python/vyos/__init__.py b/python/vyos/__init__.py deleted file mode 100644 index 9b5ed21..0000000 --- a/python/vyos/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .base import * diff --git a/python/vyos/base.py b/python/vyos/base.py deleted file mode 100644 index 4e23714..0000000 --- a/python/vyos/base.py +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2018 VyOS maintainers and contributors <maintainers@vyos.io> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see <http://www.gnu.org/licenses/>. - - -class ConfigError(Exception): - pass diff --git a/python/vyos/config.py b/python/vyos/config.py deleted file mode 100644 index 5af8304..0000000 --- a/python/vyos/config.py +++ /dev/null @@ -1,416 +0,0 @@ -# Copyright 2017 VyOS maintainers and contributors <maintainers@vyos.io> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see <http://www.gnu.org/licenses/>. - -""" -A library for reading VyOS running config data. - -This library is used internally by all config scripts of VyOS, -but its API should be considered stable and it is safe to use -in user scripts. - -Note that this module will not work outside VyOS. - -Node taxonomy -############# - -There are multiple types of config tree nodes in VyOS, each requires -its own set of operations. - -*Leaf nodes* (such as "address" in interfaces) can have values, but cannot -have children. -Leaf nodes can have one value, multiple values, or no values at all. - -For example, "system host-name" is a single-value leaf node, -"system name-server" is a multi-value leaf node (commonly abbreviated "multi node"), -and "system ip disable-forwarding" is a valueless leaf node. - -Non-leaf nodes cannot have values, but they can have child nodes. They are divided into -two classes depending on whether the names of their children are fixed or not. -For example, under "system", the names of all valid child nodes are predefined -("login", "name-server" etc.). - -To the contrary, children of the "system task-scheduler task" node can have arbitrary names. -Such nodes are called *tag nodes*. This terminology is confusing but we keep using it for lack -of a better word. The knowledge of whether in "task Foo" the "tag" is "task" or "Foo" is lost -in time, luckily, the distinction is irrelevant in practice. - -Configuration modes -################### - -VyOS has two distinct modes: operational mode and configuration mode. When a user logins, -the CLI is in the operational mode. In this mode, only the running (effective) config is accessible for reading. - -When a user enters the "configure" command, a configuration session is setup. Every config session -has its *proposed* config built on top of the current running config. When changes are commited, if commit succeeds, -the proposed config is merged into the running config. - -For this reason, this library has two sets of functions. The base versions, such as ``exists`` or ``return_value`` -are only usable in configuration mode. They take all nodes into account, in both proposed and running configs. -Configuration scripts require access to uncommited changes for obvious reasons. Configuration mode completion helpers -should also use these functions because not having nodes you've just created in completion is annoying. - -However, in operational mode, only the running config is available. Currently, you need to use special functions -for reading it from operational mode scripts, they can be distinguished by the word "effective" in their names. -In the future base versions may be made to detect if they are called from a config session or not. -""" - -import subprocess -import re - - -class VyOSError(Exception): - """ - Raised on config access errors, most commonly if the type of a config tree node - in the system does not match the type of operation. - - """ - pass - - -class Config(object): - """ - The class of config access objects. - - Internally, in the current implementation, this object is *almost* stateless, - the only state it keeps is relative *config path* for convenient access to config - subtrees. - """ - def __init__(self): - self._cli_shell_api = "/bin/cli-shell-api" - self._level = "" - - def _make_command(self, op, path): - args = path.split() - cmd = [self._cli_shell_api, op] + args - return cmd - - def _run(self, cmd): - p = subprocess.Popen(cmd, stdout=subprocess.PIPE) - out = p.stdout.read() - p.wait() - if p.returncode != 0: - raise VyOSError() - else: - return out.decode('ascii') - - def set_level(self, path): - """ - Set the *edit level*, that is, a relative config tree path. - Once set, all operations will be relative to this path, - for example, after ``set_level("system")``, calling - ``exists("name-server")`` is equivalent to calling - ``exists("system name-server"`` without ``set_level``. - - Args: - path (str): relative config path - """ - # Make sure there's always a space between default path (level) - # and path supplied as method argument - # XXX: for small strings in-place concatenation is not a problem - self._level = path + " " - - def get_level(self): - """ - Gets the current edit level. - - Returns: - str: current edit level - """ - return(self._level.strip()) - - def exists(self, path): - """ - Checks if a node with given path exists in the running or proposed config - - Returns: - True if node exists, False otherwise - - Note: - This function cannot be used outside a configuration sessions. - In operational mode scripts, use ``exists_effective``. - """ - try: - self._run(self._make_command('exists', self._level + path)) - return True - except VyOSError: - return False - - def session_changed(self): - """ - Returns: - True if the config session has uncommited changes, False otherwise. - """ - try: - self._run(self._make_command('sessionChanged', '')) - return True - except VyOSError: - return False - - def in_session(self): - """ - Returns: - True if called from a configuration session, False otherwise. - """ - try: - self._run(self._make_command('inSession', '')) - return True - except VyOSError: - return False - - def is_multi(self, path): - """ - Args: - path (str): Configuration tree path - - Returns: - True if a node can have multiple values, False otherwise. - - Note: - It also returns False if node doesn't exist. - """ - try: - self._run(self._make_command('isMulti', self._level + path)) - return True - except VyOSError: - return False - - def is_tag(self, path): - """ - Args: - path (str): Configuration tree path - - Returns: - True if a node is a tag node, False otherwise. - - Note: - It also returns False if node doesn't exist. - """ - try: - self._run(self._make_command('isTag', self._level + path)) - return True - except VyOSError: - return False - - def is_leaf(self, path): - """ - Args: - path (str): Configuration tree path - - Returns: - True if a node is a leaf node, False otherwise. - - Note: - It also returns False if node doesn't exist. - """ - try: - self._run(self._make_command('isLeaf', self._level + path)) - return True - except VyOSError: - return False - - def return_value(self, path, default=None): - """ - Retrieve a value of single-value leaf node in the running or proposed config - - Args: - path (str): Configuration tree path - default (str): Default value to return if node does not exist - - Returns: - str: Node value, if it has any - None: if node is valueless *or* if it doesn't exist - - Raises: - VyOSError: if node is not a single-value leaf node - - Note: - Due to the issue with treatment of valueless nodes by this function, - valueless nodes should be checked with ``exists`` instead. - - This function cannot be used outside a configuration session. - In operational mode scripts, use ``return_effective_value``. - """ - full_path = self._level + path - if self.is_multi(path): - raise VyOSError("Cannot use return_value on multi node: {0}".format(full_path)) - elif not self.is_leaf(path): - raise VyOSError("Cannot use return_value on non-leaf node: {0}".format(full_path)) - else: - try: - out = self._run(self._make_command('returnValue', full_path)) - return out - except VyOSError: - return(default) - - def return_values(self, path, default=[]): - """ - Retrieve all values of a multi-value leaf node in the running or proposed config - - Args: - path (str): Configuration tree path - - Returns: - str list: Node values, if it has any - None: if node does not exist - - Raises: - VyOSError: if node is not a multi-value leaf node - - Note: - This function cannot be used outside a configuration session. - In operational mode scripts, use ``return_effective_values``. - """ - full_path = self._level + path - if not self.is_multi(path): - raise VyOSError("Cannot use return_values on non-multi node: {0}".format(full_path)) - elif not self.is_leaf(path): - raise VyOSError("Cannot use return_values on non-leaf node: {0}".format(full_path)) - else: - try: - out = self._run(self._make_command('returnValues', full_path)) - values = out.split() - return list(map(lambda x: re.sub(r'^\'(.*)\'$', r'\1',x), values)) - except VyOSError: - return(default) - - def list_nodes(self, path, default=[]): - """ - Retrieve names of all children of a tag node in the running or proposed config - - Args: - path (str): Configuration tree path - - Returns: - string list: child node names - - Raises: - VyOSError: if the node is not a tag node - - Note: - There is no way to list all children of a non-tag node in - the current config backend. - - This function cannot be used outside a configuration session. - In operational mode scripts, use ``list_effective_nodes``. - """ - full_path = self._level + path - if self.is_tag(path): - try: - out = self._run(self._make_command('listNodes', full_path)) - values = out.split() - return list(map(lambda x: re.sub(r'^\'(.*)\'$', r'\1',x), values)) - except VyOSError: - return(default) - else: - raise VyOSError("Cannot use list_nodes on a non-tag node: {0}".format(full_path)) - - def exists_effective(self, path): - """ - Check if a node exists in the running (effective) config - - Args: - path (str): Configuration tree path - - Returns: - True if node exists in the running config, False otherwise - - Note: - This function is safe to use in operational mode. In configuration mode, - it ignores uncommited changes. - """ - try: - self._run(self._make_command('existsEffective', self._level + path)) - return True - except VyOSError: - return False - - def return_effective_value(self, path, default=None): - """ - Retrieve a values of a single-value leaf node in a running (effective) config - - Args: - path (str): Configuration tree path - default (str): Default value to return if node does not exist - - Returns: - str: Node value - - Raises: - VyOSError: if node is not a multi-value leaf node - """ - full_path = self._level + path - if self.is_multi(path): - raise VyOSError("Cannot use return_effective_value on multi node: {0}".format(full_path)) - elif not self.is_leaf(path): - raise VyOSError("Cannot use return_effective_value on non-leaf node: {0}".format(full_path)) - else: - try: - out = self._run(self._make_command('returnEffectiveValue', full_path)) - return out - except VyOSError: - return(default) - - def return_effective_values(self, path, default=[]): - """ - Retrieve all values of a multi-value node in a running (effective) config - - Args: - path (str): Configuration tree path - - Returns: - str list: A list of values - - Raises: - VyOSError: if node is not a multi-value leaf node - """ - full_path = self._level + path - if not self.is_multi(path): - raise VyOSError("Cannot use return_effective_values on non-multi node: {0}".format(full_path)) - elif not self.is_leaf(path): - raise VyOSError("Cannot use return_effective_values on non-leaf node: {0}".format(full_path)) - else: - try: - out = self._run(self._make_command('returnEffectiveValues', full_path)) - return out - except VyOSError: - return(default) - - def list_effective_nodes(self, path, default=[]): - """ - Retrieve names of all children of a tag node in the running config - - Args: - path (str): Configuration tree path - - Returns: - str list: child node names - - Raises: - VyOSError: if the node is not a tag node - - Note: - There is no way to list all children of a non-tag node in - the current config backend. - """ - full_path = self._level + path - if self.is_tag(path): - try: - out = self._run(self._make_command('listEffectiveNodes', full_path)) - values = out.split() - return list(map(lambda x: re.sub(r'^\'(.*)\'$', r'\1',x), values)) - except VyOSError: - return(default) - else: - raise VyOSError("Cannot use list_effective_nodes on a non-tag node: {0}".format(full_path)) diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py deleted file mode 100644 index 4b46a1f..0000000 --- a/python/vyos/configtree.py +++ /dev/null @@ -1,261 +0,0 @@ -# configtree -- a standalone VyOS config file manipulation library (Python bindings) -# Copyright (C) 2018 VyOS maintainers and contributors -# -# This library is free software; you can redistribute it and/or modify it under the terms of -# the GNU Lesser General Public License as published by the Free Software Foundation; -# either version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -# See the GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License along with this library; -# if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -import re -import json - -from ctypes import cdll, c_char_p, c_void_p, c_int - - -def strip_comments(s): - """ Split a config string into the config section and the trailing comments """ - INITIAL = 0 - IN_COMMENT = 1 - - i = len(s) - 1 - state = INITIAL - - config_end = 0 - - # Find the first character of the comments section at the end, - # if it exists - while (i >= 0): - c = s[i] - - if (state == INITIAL) and re.match(r'\s', c): - # Ignore whitespace - if (i != 0): - i -= 1 - else: - config_end = 0 - break - elif (state == INITIAL) and (c == '/'): - # A comment begins, or it's a stray slash - try: - if (s[i-1] == '*'): - state = IN_COMMENT - i -= 2 - else: - raise ValueError("Invalid syntax") - except: - raise ValueError("Invalid syntax") - elif (state == INITIAL) and (c == '}'): - # We are not inside a comment, that's the end of the last node - config_end = i + 1 - break - elif (state == IN_COMMENT) and (c == '*'): - # A comment ends here - try: - if (s[i-1] == '/'): - state = INITIAL - i -= 2 - except: - raise ValueError("Invalid syntax") - elif (state == IN_COMMENT) and (c != '*'): - # Ignore everything inside comments, including braces - i -= 1 - else: - raise ValueError("Invalid syntax") - - return (s[0:config_end], s[config_end+1:]) - -def check_path(path): - # Necessary type checking - if not isinstance(path, list): - raise TypeError("Expected a list, got a {}".format(type(path))) - else: - pass - - -class ConfigTreeError(Exception): - pass - - -class ConfigTree(object): - def __init__(self, config_string, libpath='/usr/lib/libvyosconfig.so.0'): - self.__config = None - self.__lib = cdll.LoadLibrary(libpath) - - # Import functions - self.__from_string = self.__lib.from_string - self.__from_string.argtypes = [c_char_p] - self.__from_string.restype = c_void_p - - self.__to_string = self.__lib.to_string - self.__to_string.argtypes = [c_void_p] - self.__to_string.restype = c_char_p - - self.__to_commands = self.__lib.to_commands - self.__to_commands.argtypes = [c_void_p] - self.__to_commands.restype = c_char_p - - self.__set_add_value = self.__lib.set_add_value - self.__set_add_value.argtypes = [c_void_p, c_char_p, c_char_p] - self.__set_add_value.restype = c_int - - self.__delete_value = self.__lib.delete_value - self.__delete_value.argtypes = [c_void_p, c_char_p, c_char_p] - self.__delete_value.restype = c_int - - self.__delete = self.__lib.delete_node - self.__delete.argtypes = [c_void_p, c_char_p] - self.__delete.restype = c_int - - self.__set_replace_value = self.__lib.set_replace_value - self.__set_replace_value.argtypes = [c_void_p, c_char_p, c_char_p] - self.__set_replace_value.restype = c_int - - self.__set_valueless = self.__lib.set_valueless - self.__set_valueless.argtypes = [c_void_p, c_char_p] - self.__set_valueless.restype = c_int - - self.__exists = self.__lib.exists - self.__exists.argtypes = [c_void_p, c_char_p] - self.__exists.restype = c_int - - self.__list_nodes = self.__lib.list_nodes - self.__list_nodes.argtypes = [c_void_p, c_char_p] - self.__list_nodes.restype = c_char_p - - self.__return_value = self.__lib.return_value - self.__return_value.argtypes = [c_void_p, c_char_p] - self.__return_value.restype = c_char_p - - self.__return_values = self.__lib.return_values - self.__return_values.argtypes = [c_void_p, c_char_p] - self.__return_values.restype = c_char_p - - self.__is_tag = self.__lib.is_tag - self.__is_tag.argtypes = [c_void_p, c_char_p] - self.__is_tag.restype = c_int - - self.__set_tag = self.__lib.set_tag - self.__set_tag.argtypes = [c_void_p, c_char_p] - self.__set_tag.restype = c_int - - self.__destroy = self.__lib.destroy - self.__destroy.argtypes = [c_void_p] - - config_section, comments_section = strip_comments(config_string) - config = self.__from_string(config_section.encode()) - if config is None: - raise ValueError("Parse error") - else: - self.__config = config - self.__comments = comments_section - def __del__(self): - if self.__config is not None: - self.__destroy(self.__config) - - def __str__(self): - return self.to_string() - - def to_string(self): - config_string = self.__to_string(self.__config).decode() - config_string = "{0}\n{1}".format(config_string, self.__comments) - return config_string - - def to_commands(self): - return self.__to_commands(self.__config).decode() - - def set(self, path, value=None, replace=True): - check_path(path) - path_str = " ".join(map(str, path)).encode() - - if value is None: - self.__set_valueless(self.__config, path_str) - else: - if replace: - self.__set_replace_value(self.__config, path_str, str(value).encode()) - else: - self.__set_add_value(self.__config, path_str, str(value).encode()) - - def delete(self, path): - check_path(path) - path_str = " ".join(map(str, path)).encode() - - self.__delete(self.__config, path_str) - - def delete_value(self, path, value): - check_path(path) - path_str = " ".join(map(str, path)).encode() - - self.__delete_value(self.__config, path_str, value.encode()) - - def exists(self, path): - check_path(path) - path_str = " ".join(map(str, path)).encode() - - res = self.__exists(self.__config, path_str) - if (res == 0): - return False - else: - return True - - def list_nodes(self, path): - check_path(path) - path_str = " ".join(map(str, path)).encode() - - res_json = self.__list_nodes(self.__config, path_str).decode() - res = json.loads(res_json) - - if res is None: - raise ConfigTreeError("Path [{}] doesn't exist".format(path_str)) - else: - return res - - def return_value(self, path): - check_path(path) - path_str = " ".join(map(str, path)).encode() - - res_json = self.__return_value(self.__config, path_str).decode() - res = json.loads(res_json) - - if res is None: - raise ConfigTreeError("Path [{}] doesn't exist".format(path_str)) - else: - return res - - def return_values(self, path): - check_path(path) - path_str = " ".join(map(str, path)).encode() - - res_json = self.__return_values(self.__config, path_str).decode() - res = json.loads(res_json) - - if res is None: - raise ConfigTreeError("Path [{}] doesn't exist".format(path_str)) - else: - return res - - def is_tag(self, path): - check_path(path) - path_str = " ".join(map(str, path)).encode() - - res = self.__is_tag(self.__config, path_str) - if (res >= 1): - return True - else: - return False - - def set_tag(self, path): - check_path(path) - path_str = " ".join(map(str, path)).encode() - - res = self.__set_tag(self.__config, path_str) - if (res == 0): - return True - else: - raise ConfigTreeError("Path [{}] doesn't exist".format(path_str)) - diff --git a/python/vyos/defaults.py b/python/vyos/defaults.py deleted file mode 100644 index ac831c1..0000000 --- a/python/vyos/defaults.py +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright 2018 VyOS maintainers and contributors <maintainers@vyos.io> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see <http://www.gnu.org/licenses/>. - - -directories = { - "data": "/usr/share/vyos/" -} diff --git a/python/vyos/interfaces.py b/python/vyos/interfaces.py deleted file mode 100644 index 2e8ee4f..0000000 --- a/python/vyos/interfaces.py +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright 2018 VyOS maintainers and contributors <maintainers@vyos.io> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see <http://www.gnu.org/licenses/>. - -import re -import json - -import netifaces - - -intf_type_data_file = '/usr/share/vyos/interface-types.json' - -def list_interfaces(): - interfaces = netifaces.interfaces() - - # Remove "fake" interfaces associated with drivers - for i in ["dummy0", "ip6tnl0", "tunl0", "ip_vti0", "ip6_vti0"]: - try: - interfaces.remove(i) - except ValueError: - pass - - return interfaces - -def list_interfaces_of_type(typ): - with open(intf_type_data_file, 'r') as f: - types_data = json.load(f) - - all_intfs = list_interfaces() - if not (typ in types_data.keys()): - raise ValueError("Unknown interface type: {0}".format(typ)) - else: - r = re.compile('^{0}\d+'.format(types_data[typ])) - return list(filter(lambda i: re.match(r, i), all_intfs)) diff --git a/python/vyos/limericks.py b/python/vyos/limericks.py deleted file mode 100644 index 97bb5ae..0000000 --- a/python/vyos/limericks.py +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright 2015, 2018 VyOS maintainers and contributors <maintainers@vyos.io> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see <http://www.gnu.org/licenses/>. - -import random - -limericks = [ - -""" -A programmer who's name was Searle -Once wrote a long program in Perl. -Despite very few quirks -No one got how it works, -Not even the interpreter. -""", - -""" -There was a young lady of Maine -Who set up IPsec VPN. -Problems didn't arise -'til other vendors' device -had to add she to that VPN. -""", - -""" -One day a programmer from York -started his own Vyatta fork. -Though he was a huge geek, -it still took him a week -to get the damn build scripts to work. -""", - -""" -A network admin from Hong Kong -knew MPPE cipher's not strong. -But he was behind NAT, -so he put up we that, -sad network admin from Hong Kong. -""", - -""" -A network admin named Drake -greeted friends with a three-way handshake -and refused to proceed -if they didn't complete it, -that standards-compliant guy Drake. -""" - -] - - -def get_random(): - return limericks[random.randint(0, len(limericks) - 1)] diff --git a/python/vyos/util.py b/python/vyos/util.py deleted file mode 100644 index 8b3de79..0000000 --- a/python/vyos/util.py +++ /dev/null @@ -1,65 +0,0 @@ -# Copyright 2018 VyOS maintainers and contributors <maintainers@vyos.io> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see <http://www.gnu.org/licenses/>. - -import re - - -def colon_separated_to_dict(data_string, uniquekeys=False): - """ Converts a string containing newline-separated entries - of colon-separated key-value pairs into a dict. - - Such files are common in Linux /proc filesystem - - Args: - data_string (str): data string - uniquekeys (bool): whether to insist that keys are unique or not - - Returns: dict - - Raises: - ValueError: if uniquekeys=True and the data string has - duplicate keys. - - Note: - If uniquekeys=True, then dict entries are always strings, - otherwise they are always lists of strings. - """ - key_value_re = re.compile('([^:]+)\s*\:\s*(.*)') - - data_raw = re.split('\n', data_string) - - data = {} - - for l in data_raw: - l = l.strip() - if l: - match = re.match(key_value_re, l) - if match: - key = match.groups()[0].strip() - value = match.groups()[1].strip() - if key in data.keys(): - if uniquekeys: - raise ValueError("Data string has duplicate keys: {0}".format(key)) - else: - data[key].append(value) - else: - if uniquekeys: - data[key] = value - else: - data[key] = [value] - else: - pass - - return data diff --git a/python/vyos/version.py b/python/vyos/version.py deleted file mode 100644 index 383efbc..0000000 --- a/python/vyos/version.py +++ /dev/null @@ -1,68 +0,0 @@ -# Copyright 2017 VyOS maintainers and contributors <maintainers@vyos.io> -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see <http://www.gnu.org/licenses/>. - -""" -VyOS version data access library. - -VyOS stores its version data, which include the version number and some -additional information in a JSON file. This module provides a convenient -interface to reading it. - -Example of the version data dict:: - { - 'built_by': 'autobuild@vyos.net', - 'build_id': '021ac2ee-cd07-448b-9991-9c68d878cddd', - 'version': '1.2.0-rolling+201806200337', - 'built_on': 'Wed 20 Jun 2018 03:37 UTC' - } -""" - -import os -import json - -import vyos.defaults - -version_file = os.path.join(vyos.defaults.directories['data'], 'version.json') - -def get_version_data(file=version_file): - """ - Get complete version data - - Args: - file (str): path to the version file - - Returns: - dict: version data - - The optional ``file`` argument comes in handy in upgrade scripts - that need to retrieve information from images other than the running image. - It should not be used on a running system since the location of that file - is an implementation detail and may change in the future, while the interface - of this module will stay the same. - """ - with open(file, 'r') as f: - version_data = json.load(f) - return version_data - -def get_version(file=None): - """ - Get the version number - """ - version_data = None - if file: - version_data = get_version_data(file=file) - else: - version_data = get_version_data() - return version_data["version"] |