From bf154049d2a38f393bacb2168c3326c594bcdd54 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Tue, 28 Jun 2016 19:58:57 +0200 Subject: [PATCH] Suppport ~/GNS3/symbols --- gns3server/controller/symbols.py | 22 +++++++++++++++++++++- tests/conftest.py | 12 ++++++++++++ tests/controller/test_symbols.py | 17 +++++++++++++++-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/gns3server/controller/symbols.py b/gns3server/controller/symbols.py index 9549507d..45ac4bfc 100644 --- a/gns3server/controller/symbols.py +++ b/gns3server/controller/symbols.py @@ -19,6 +19,7 @@ import os from ..utils.get_resource import get_resource +from ..config import Config class Symbols: @@ -42,10 +43,29 @@ class Symbols: 'builtin': True, }) self._symbols_path[symbol_id] = os.path.join(get_resource("symbols"), file) + directory = self._symbol_path() + if directory: + for file in os.listdir(directory): + if file.startswith('.'): + continue + symbol_id = file + symbols.append({ + 'symbol_id': symbol_id, + 'filename': file, + 'builtin': False, + }) + self._symbols_path[symbol_id] = os.path.join(get_resource("symbols"), file) + + symbols.sort(key=lambda x: x["filename"]) - #TODO: support ~/GNS3/symbols directory return symbols + def _symbol_path(self): + directory = os.path.expanduser(Config.instance().get_section_config("Server").get("symbols_path", "~/GNS3/symbols")) + if directory: + os.makedirs(directory, exist_ok=True) + return directory + def get_path(self, symbol_id): return self._symbols_path[symbol_id] diff --git a/tests/conftest.py b/tests/conftest.py index 16708fb3..e1427d5b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -204,6 +204,7 @@ def run_around_tests(monkeypatch, port_manager, controller, config): port_manager._instance = port_manager os.makedirs(os.path.join(tmppath, 'projects')) config.set("Server", "projects_path", os.path.join(tmppath, 'projects')) + config.set("Server", "symbols_path", os.path.join(tmppath, 'symbols')) config.set("Server", "images_path", os.path.join(tmppath, 'images')) config.set("Server", "ubridge_path", os.path.join(tmppath, 'bin', 'ubridge')) config.set("Server", "auth", False) @@ -243,6 +244,17 @@ def images_dir(config): return path +@pytest.fixture +def symbols_dir(config): + """ + Get the location of symbols + """ + path = config.get_section_config("Server").get("symbols_path") + os.makedirs(path, exist_ok=True) + print(path) + return path + + @pytest.fixture def projects_dir(config): """ diff --git a/tests/controller/test_symbols.py b/tests/controller/test_symbols.py index 563d0441..dfea1e74 100644 --- a/tests/controller/test_symbols.py +++ b/tests/controller/test_symbols.py @@ -16,22 +16,35 @@ # along with this program. If not, see . import os +from unittest.mock import patch from gns3server.controller.symbols import Symbols from gns3server.utils.get_resource import get_resource -def test_list(): +def test_list(symbols_dir): + + print(symbols_dir) + with open(os.path.join(symbols_dir, "linux.svg"), "w+") as f: + pass + symbols = Symbols() assert { 'symbol_id': ':/symbols/firewall.svg', 'filename': 'firewall.svg', 'builtin': True } in symbols.list() - assert symbols + assert { + 'symbol_id': 'linux.svg', + 'filename': 'linux.svg', + 'builtin': False + } in symbols.list() def test_get_path(): symbols = Symbols() assert symbols.get_path(':/symbols/firewall.svg') == get_resource("symbols/firewall.svg") + + +