2016-06-27 16:33:42 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
2018-11-23 11:26:04 +00:00
|
|
|
import posixpath
|
2016-06-27 16:33:42 +00:00
|
|
|
|
2018-11-23 10:27:10 +00:00
|
|
|
from .symbol_themes import BUILTIN_SYMBOL_THEMES
|
2020-10-02 06:37:50 +00:00
|
|
|
from .controller_error import ControllerNotFoundError
|
2016-06-27 16:33:42 +00:00
|
|
|
from ..utils.get_resource import get_resource
|
2016-08-17 09:58:19 +00:00
|
|
|
from ..utils.picture import get_size
|
2016-06-28 17:58:57 +00:00
|
|
|
from ..config import Config
|
2016-06-27 16:33:42 +00:00
|
|
|
|
2018-09-11 13:06:01 +00:00
|
|
|
import logging
|
2021-04-13 09:16:50 +00:00
|
|
|
|
2018-09-11 13:06:01 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-06-27 16:33:42 +00:00
|
|
|
|
|
|
|
class Symbols:
|
|
|
|
"""
|
|
|
|
Manage GNS3 symbols
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-11-23 10:27:10 +00:00
|
|
|
|
2017-01-23 09:23:18 +00:00
|
|
|
try:
|
|
|
|
self.list()
|
2018-11-23 10:27:10 +00:00
|
|
|
except OSError: # The error will be raised and forwarded later
|
2017-01-23 09:23:18 +00:00
|
|
|
pass
|
2018-11-23 10:27:10 +00:00
|
|
|
|
2016-08-17 09:58:19 +00:00
|
|
|
# Keep a cache of symbols size
|
|
|
|
self._symbol_size_cache = {}
|
2022-07-25 18:22:12 +00:00
|
|
|
|
|
|
|
self._server_config = Config.instance().settings.Server
|
|
|
|
self._current_theme = self._server_config.default_symbol_theme
|
2018-11-23 10:27:10 +00:00
|
|
|
self._themes = BUILTIN_SYMBOL_THEMES
|
|
|
|
|
|
|
|
@property
|
|
|
|
def theme(self):
|
|
|
|
|
|
|
|
return self._current_theme
|
|
|
|
|
|
|
|
@theme.setter
|
|
|
|
def theme(self, theme):
|
|
|
|
|
|
|
|
if not self._themes.get(theme):
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ControllerNotFoundError(f"Could not find symbol theme '{theme}'")
|
2018-11-23 10:27:10 +00:00
|
|
|
self._current_theme = theme
|
2016-06-27 16:33:42 +00:00
|
|
|
|
2019-03-12 11:13:33 +00:00
|
|
|
def default_symbols(self):
|
|
|
|
|
|
|
|
return BUILTIN_SYMBOL_THEMES
|
|
|
|
|
|
|
|
def get_default_symbol(self, symbol, symbol_theme):
|
|
|
|
|
|
|
|
theme = self._themes.get(symbol_theme, None)
|
|
|
|
if not theme:
|
2022-07-25 18:39:03 +00:00
|
|
|
log.warning(f"Could not find symbol theme '{symbol_theme}'")
|
2022-07-25 18:22:12 +00:00
|
|
|
return None
|
2019-03-12 11:13:33 +00:00
|
|
|
symbol_path = theme.get(symbol)
|
|
|
|
if symbol_path not in self._symbols_path:
|
2023-11-15 01:47:48 +00:00
|
|
|
log.debug(f"Default symbol {symbol} was not found")
|
2019-03-12 11:13:33 +00:00
|
|
|
return None
|
|
|
|
return symbol_path
|
|
|
|
|
2016-06-27 16:33:42 +00:00
|
|
|
def list(self):
|
2018-11-23 10:27:10 +00:00
|
|
|
|
2016-06-27 16:33:42 +00:00
|
|
|
self._symbols_path = {}
|
|
|
|
symbols = []
|
2017-02-20 17:28:49 +00:00
|
|
|
if get_resource("symbols"):
|
2018-08-12 15:08:48 +00:00
|
|
|
for root, _, files in os.walk(get_resource("symbols")):
|
|
|
|
for filename in files:
|
2021-04-13 09:16:50 +00:00
|
|
|
if filename.startswith("."):
|
2018-08-12 15:08:48 +00:00
|
|
|
continue
|
2021-04-13 09:16:50 +00:00
|
|
|
symbol_file = posixpath.normpath(
|
|
|
|
os.path.relpath(os.path.join(root, filename), get_resource("symbols"))
|
|
|
|
).replace("\\", "/")
|
|
|
|
theme = posixpath.dirname(symbol_file).replace("/", "-").capitalize()
|
2019-03-30 09:20:21 +00:00
|
|
|
if not theme:
|
|
|
|
continue
|
2021-04-13 09:16:50 +00:00
|
|
|
symbol_id = ":/symbols/" + symbol_file
|
|
|
|
symbols.append({"symbol_id": symbol_id, "filename": filename, "theme": theme, "builtin": True})
|
2018-08-12 15:08:48 +00:00
|
|
|
self._symbols_path[symbol_id] = os.path.join(root, filename)
|
|
|
|
|
2016-06-28 19:15:22 +00:00
|
|
|
directory = self.symbols_path()
|
2016-06-28 17:58:57 +00:00
|
|
|
if directory:
|
2018-08-12 17:16:02 +00:00
|
|
|
for root, _, files in os.walk(directory):
|
|
|
|
for filename in files:
|
2021-04-13 09:16:50 +00:00
|
|
|
if filename.startswith("."):
|
2018-08-12 17:16:02 +00:00
|
|
|
continue
|
2021-04-13 09:16:50 +00:00
|
|
|
symbol_file = posixpath.normpath(os.path.relpath(os.path.join(root, filename), directory)).replace(
|
|
|
|
"\\", "/"
|
|
|
|
)
|
|
|
|
theme = posixpath.dirname(symbol_file).replace("/", "-").capitalize()
|
2020-07-19 08:16:49 +00:00
|
|
|
if not theme:
|
|
|
|
theme = "Custom symbols"
|
2021-04-13 09:16:50 +00:00
|
|
|
symbols.append({"symbol_id": symbol_file, "filename": filename, "builtin": False, "theme": theme})
|
2018-08-12 17:16:02 +00:00
|
|
|
self._symbols_path[symbol_file] = os.path.join(root, filename)
|
2021-09-20 10:39:58 +00:00
|
|
|
symbols.sort(key=lambda x: x["theme"])
|
2016-06-27 16:33:42 +00:00
|
|
|
return symbols
|
|
|
|
|
2016-06-28 19:15:22 +00:00
|
|
|
def symbols_path(self):
|
2021-04-12 07:32:23 +00:00
|
|
|
|
|
|
|
server_config = Config.instance().settings.Server
|
|
|
|
directory = os.path.expanduser(server_config.symbols_path)
|
2016-06-28 17:58:57 +00:00
|
|
|
if directory:
|
2018-09-11 13:06:01 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(directory, exist_ok=True)
|
|
|
|
except OSError as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
log.error(f"Could not create symbol directory '{directory}': {e}")
|
2018-09-11 13:06:01 +00:00
|
|
|
return None
|
2016-06-28 17:58:57 +00:00
|
|
|
return directory
|
|
|
|
|
2021-10-10 07:05:11 +00:00
|
|
|
def has_symbol(self, symbol_id):
|
|
|
|
|
|
|
|
return self._symbols_path.get(symbol_id)
|
|
|
|
|
2022-07-25 10:33:40 +00:00
|
|
|
def resolve_symbol(self, symbol_name):
|
|
|
|
|
|
|
|
if not symbol_name.startswith(":/"):
|
|
|
|
symbol = self.get_default_symbol(symbol_name, self._current_theme)
|
|
|
|
if symbol:
|
|
|
|
return symbol
|
|
|
|
return symbol_name
|
|
|
|
|
2016-06-27 16:33:42 +00:00
|
|
|
def get_path(self, symbol_id):
|
2022-07-25 10:33:40 +00:00
|
|
|
|
|
|
|
symbol_id = self.resolve_symbol(symbol_id)
|
2016-09-15 16:21:39 +00:00
|
|
|
try:
|
2019-03-11 09:55:16 +00:00
|
|
|
return self._symbols_path[symbol_id]
|
2016-09-15 16:21:39 +00:00
|
|
|
except KeyError:
|
2016-11-16 12:17:58 +00:00
|
|
|
try:
|
2018-09-11 13:06:01 +00:00
|
|
|
self.list()
|
2016-11-16 12:17:58 +00:00
|
|
|
return self._symbols_path[symbol_id]
|
2018-09-11 13:06:01 +00:00
|
|
|
except (OSError, KeyError):
|
2019-03-11 09:55:16 +00:00
|
|
|
# try to return a symbol with the same name from the classic theme
|
2021-04-13 09:07:58 +00:00
|
|
|
symbol = self._symbols_path.get(f":/symbols/classic/{os.path.basename(symbol_id)}")
|
2019-03-11 09:55:16 +00:00
|
|
|
if symbol:
|
|
|
|
return symbol
|
|
|
|
else:
|
|
|
|
# return the default computer symbol
|
2021-04-13 09:07:58 +00:00
|
|
|
log.warning(f"Could not retrieve symbol '{symbol_id}', returning default symbol...")
|
2023-11-15 01:47:48 +00:00
|
|
|
symbol = self.get_default_symbol("computer", self._current_theme)
|
2023-11-15 01:57:14 +00:00
|
|
|
if symbol and symbol in self._symbols_path:
|
|
|
|
return self._symbols_path[symbol]
|
2019-03-11 09:55:16 +00:00
|
|
|
return self._symbols_path[":/symbols/classic/computer.svg"]
|
2017-01-23 09:23:18 +00:00
|
|
|
|
2016-08-17 09:58:19 +00:00
|
|
|
def get_size(self, symbol_id):
|
|
|
|
try:
|
|
|
|
return self._symbol_size_cache[symbol_id]
|
|
|
|
except KeyError:
|
|
|
|
with open(self.get_path(symbol_id), "rb") as f:
|
|
|
|
res = get_size(f.read())
|
|
|
|
self._symbol_size_cache[symbol_id] = res
|
|
|
|
return res
|