mirror of
https://github.com/GNS3/gns3-server.git
synced 2024-11-17 01:04:51 +02:00
9de13c570c
# Conflicts: # gns3server/compute/builtin/nodes/cloud.py # gns3server/compute/docker/docker_vm.py # gns3server/compute/dynamips/nodes/ethernet_switch.py # gns3server/compute/iou/iou_vm.py # gns3server/compute/qemu/qemu_vm.py # gns3server/compute/virtualbox/virtualbox_vm.py # gns3server/compute/vmware/vmware_vm.py # gns3server/controller/__init__.py # gns3server/version.py # gns3server/web/web_server.py
107 lines
3.7 KiB
Python
107 lines
3.7 KiB
Python
#!/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
|
|
|
|
|
|
from ..utils.get_resource import get_resource
|
|
from ..utils.picture import get_size
|
|
from ..config import Config
|
|
|
|
import logging
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class Symbols:
|
|
"""
|
|
Manage GNS3 symbols
|
|
"""
|
|
|
|
def __init__(self):
|
|
try:
|
|
self.list()
|
|
except OSError: # The error will be raised and forward later
|
|
pass
|
|
# Keep a cache of symbols size
|
|
self._symbol_size_cache = {}
|
|
|
|
def list(self):
|
|
self._symbols_path = {}
|
|
symbols = []
|
|
if get_resource("symbols"):
|
|
for root, _, files in os.walk(get_resource("symbols")):
|
|
for filename in files:
|
|
if filename.startswith('.'):
|
|
continue
|
|
symbol_file = os.path.relpath(os.path.join(root, filename), get_resource("symbols"))
|
|
symbol_id = ':/symbols/' + symbol_file
|
|
symbols.append({
|
|
'symbol_id': symbol_id,
|
|
'filename': symbol_file,
|
|
'builtin': True,
|
|
})
|
|
self._symbols_path[symbol_id] = os.path.join(root, filename)
|
|
|
|
directory = self.symbols_path()
|
|
if directory:
|
|
for root, _, files in os.walk(directory):
|
|
for filename in files:
|
|
if filename.startswith('.'):
|
|
continue
|
|
symbol_file = os.path.relpath(os.path.join(root, filename), directory)
|
|
symbols.append({
|
|
'symbol_id': symbol_file,
|
|
'filename': symbol_file,
|
|
'builtin': False,
|
|
})
|
|
self._symbols_path[symbol_file] = os.path.join(root, filename)
|
|
|
|
symbols.sort(key=lambda x: x["filename"])
|
|
return symbols
|
|
|
|
def symbols_path(self):
|
|
directory = os.path.expanduser(Config.instance().get_section_config("Server").get("symbols_path", "~/GNS3/symbols"))
|
|
if directory:
|
|
try:
|
|
os.makedirs(directory, exist_ok=True)
|
|
except OSError as e:
|
|
log.error("Could not create symbol directory '{}': {}".format(directory, e))
|
|
return None
|
|
return directory
|
|
|
|
def get_path(self, symbol_id):
|
|
try:
|
|
return self._symbols_path[symbol_id]
|
|
# Symbol not found, let's refresh the cache
|
|
except KeyError:
|
|
try:
|
|
self.list()
|
|
return self._symbols_path[symbol_id]
|
|
except (OSError, KeyError):
|
|
log.warning("Could not retrieve symbol '{}'".format(symbol_id))
|
|
symbols_path = self._symbols_path
|
|
return symbols_path[":/symbols/computer.svg"]
|
|
|
|
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
|