1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-29 02:08:10 +00:00

Early support for symbol themes.

This commit is contained in:
grossmj 2019-03-12 18:13:33 +07:00
parent bae3fb84b9
commit cf91e904f7
6 changed files with 62 additions and 16 deletions

View File

@ -45,6 +45,10 @@ class Appliance:
def symbol(self):
return self._data.get("symbol")
@symbol.setter
def symbol(self, new_symbol):
self._data["symbol"] = new_symbol
def __json__(self):
"""
Appliance data (a hash)

View File

@ -74,7 +74,7 @@ class ApplianceManager:
os.makedirs(appliances_path, exist_ok=True)
return appliances_path
def load_appliances(self):
def load_appliances(self, symbol_theme="Classic"):
"""
Loads appliance files from disk.
"""
@ -90,13 +90,33 @@ class ApplianceManager:
try:
with open(path, 'r', encoding='utf-8') as f:
appliance = Appliance(appliance_id, json.load(f), builtin=builtin)
appliance.__json__() # Check if loaded without error
json_data = appliance.__json__() # Check if loaded without error
if appliance.status != 'broken':
self._appliances[appliance.id] = appliance
if not appliance.symbol or appliance.symbol.startswith(":/symbols/"):
# apply a default symbol if the appliance has none or a default symbol
default_symbol = self._get_default_symbol(json_data, symbol_theme)
if default_symbol:
appliance.symbol = default_symbol
except (ValueError, OSError, KeyError) as e:
log.warning("Cannot load appliance file '%s': %s", path, str(e))
continue
def _get_default_symbol(self, appliance, symbol_theme):
"""
Returns the default symbol for a given appliance.
"""
from . import Controller
controller = Controller.instance()
category = appliance["category"]
if category == "guest":
if "docker" in appliance:
return controller.symbols.get_default_symbol("docker_guest", symbol_theme)
elif "qemu" in appliance:
return controller.symbols.get_default_symbol("qemu_guest", symbol_theme)
return controller.symbols.get_default_symbol(category, symbol_theme)
async def download_custom_symbols(self):
"""
Download custom appliance symbols from our GitHub registry repository.

View File

@ -37,7 +37,7 @@ AFFINITY_SQUARE_BLUE_SYMBOL_THEME = {"cloud": ":/symbols/affinity/square/blue/cl
"frame_relay_switch.svg": ":/symbols/affinity/square/blue/isdn.svg",
"atm_switch": ":/symbols/affinity/square/blue/atm.svg",
"router": ":/symbols/affinity/square/blue/router.svg",
"multilayer_switch": ":/symbols/affinity/square/blue/multilayer_switch.svg",
"multilayer_switch": ":/symbols/affinity/square/blue/switch_multilayer.svg",
"firewall": ":/symbols/affinity/square/blue/firewall3.svg",
"computer": ":/symbols/affinity/square/blue/client.svg",
"vpcs_guest": ":/symbols/affinity/square/blue/client.svg",
@ -52,7 +52,7 @@ AFFINITY_SQUARE_RED_SYMBOL_THEME = {"cloud": ":/symbols/affinity/square/red/clou
"frame_relay_switch": ":/symbols/affinity/square/red/isdn.svg",
"atm_switch": ":/symbols/affinity/square/red/atm.svg",
"router": ":/symbols/affinity/square/red/router.svg",
"multilayer_switch": ":/symbols/affinity/square/red/multilayer_switch.svg",
"multilayer_switch": ":/symbols/affinity/square/red/switch_multilayer.svg",
"firewall": ":/symbols/affinity/square/red/firewall3.svg",
"computer": ":/symbols/affinity/square/red/client.svg",
"vpcs_guest": ":/symbols/affinity/square/red/client.svg",
@ -67,7 +67,7 @@ AFFINITY_SQUARE_GRAY_SYMBOL_THEME = {"cloud": ":/symbols/affinity/square/gray/cl
"frame_relay_switch": ":/symbols/affinity/square/gray/isdn.svg",
"atm_switch": ":/symbols/affinity/square/gray/atm.svg",
"router": ":/symbols/affinity/square/gray/router.svg",
"multilayer_switch": ":/symbols/affinity/square/gray/multilayer_switch.svg",
"multilayer_switch": ":/symbols/affinity/square/gray/switch_multilayer.svg",
"firewall": ":/symbols/affinity/square/gray/firewall3.svg",
"computer": ":/symbols/affinity/square/gray/client.svg",
"vpcs_guest": ":/symbols/affinity/square/gray/client.svg",
@ -82,7 +82,7 @@ AFFINITY_CIRCLE_BLUE_SYMBOL_THEME = {"cloud": ":/symbols/affinity/circle/blue/cl
"frame_relay_switch": ":/symbols/affinity/circle/blue/isdn.svg",
"atm_switch": ":/symbols/affinity/circle/blue/atm.svg",
"router": ":/symbols/affinity/circle/blue/router.svg",
"multilayer_switch": ":/symbols/affinity/circle/blue/multilayer_switch.svg",
"multilayer_switch": ":/symbols/affinity/circle/blue/switch_multilayer.svg",
"firewall": ":/symbols/affinity/circle/blue/firewall3.svg",
"computer": ":/symbols/affinity/circle/blue/client.svg",
"vpcs_guest": ":/symbols/affinity/circle/blue/client.svg",
@ -97,7 +97,7 @@ AFFINITY_CIRCLE_RED_SYMBOL_THEME = {"cloud": ":/symbols/affinity/circle/red/clou
"frame_relay_switch": ":/symbols/affinity/circle/red/isdn.svg",
"atm_switch": ":/symbols/affinity/circle/red/atm.svg",
"router": ":/symbols/affinity/circle/red/router.svg",
"multilayer_switch": ":/symbols/affinity/circle/red/multilayer_switch.svg",
"multilayer_switch": ":/symbols/affinity/circle/red/switch_multilayer.svg",
"firewall": ":/symbols/affinity/circle/red/firewall3.svg",
"computer": ":/symbols/affinity/circle/red/client.svg",
"vpcs_guest": ":/symbols/affinity/circle/red/client.svg",
@ -112,7 +112,7 @@ AFFINITY_CIRCLE_GRAY_SYMBOL_THEME = {"cloud": ":/symbols/affinity/circle/gray/cl
"frame_relay_switch": ":/symbols/affinity/circle/gray/isdn.svg",
"atm_switch": ":/symbols/affinity/circle/gray/atm.svg",
"router": ":/symbols/affinity/circle/gray/router.svg",
"multilayer_switch": ":/symbols/affinity/circle/gray/multilayer_switch.svg",
"multilayer_switch": ":/symbols/affinity/circle/gray/switch_multilayer.svg",
"firewall": ":/symbols/affinity/circle/gray/firewall3.svg",
"computer": ":/symbols/affinity/circle/gray/client.svg",
"vpcs_guest": ":/symbols/affinity/circle/gray/client.svg",

View File

@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import aiohttp
import posixpath
from .symbol_themes import BUILTIN_SYMBOL_THEMES
@ -53,10 +54,24 @@ class Symbols:
def theme(self, theme):
if not self._themes.get(theme):
log.error("Could not find symbol theme '{}'".format(theme))
return
raise aiohttp.web.HTTPNotFound(text="Could not find symbol theme '{}'".format(theme))
self._current_theme = theme
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:
raise aiohttp.web.HTTPNotFound(text="Could not find symbol theme '{}'".format(symbol_theme))
symbol_path = theme.get(symbol)
if symbol_path not in self._symbols_path:
log.warning("Default symbol {} was not found".format(symbol_path))
return None
return symbol_path
def list(self):
self._symbols_path = {}
@ -102,13 +117,8 @@ class Symbols:
return directory
def get_path(self, symbol_id):
#symbol_filename = os.path.splitext(os.path.basename(symbol_id))[0]
#theme = self._themes.get(self._current_theme, {})
#if not theme:
# log.error("Could not find symbol theme '{}'".format(self._current_theme))
try:
return self._symbols_path[symbol_id]
#return self._symbols_path[theme.get(symbol_filename, symbol_id)]
except KeyError:
try:
self.list()

View File

@ -38,5 +38,6 @@ class ApplianceHandler:
controller = Controller.instance()
if request.query.get("update", "no").lower() == "yes":
await controller.appliance_manager.download_appliances()
controller.appliance_manager.load_appliances()
symbol_theme = request.query.get("symbol_theme", "Classic")
controller.appliance_manager.load_appliances(symbol_theme=symbol_theme)
response.json([c for c in controller.appliance_manager.appliances.values()])

View File

@ -85,6 +85,17 @@ class SymbolHandler:
controller.symbols.list()
response.set_status(204)
@Route.get(
r"/default_symbols",
description="List of default symbols",
status_codes={
200: "Default symbols list returned"
})
def list_default_symbols(request, response):
controller = Controller.instance()
response.json(controller.symbols.default_symbols())
# @Route.post(
# r"/symbol_theme",
# description="Create a new symbol theme",