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
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-06-27 16:33:42 +00:00
|
|
|
|
|
|
|
class Symbols:
|
|
|
|
"""
|
|
|
|
Manage GNS3 symbols
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2017-01-23 09:23:18 +00:00
|
|
|
try:
|
|
|
|
self.list()
|
|
|
|
except OSError: # The error will be raised and forward later
|
|
|
|
pass
|
2016-08-17 09:58:19 +00:00
|
|
|
# Keep a cache of symbols size
|
|
|
|
self._symbol_size_cache = {}
|
2016-06-27 16:33:42 +00:00
|
|
|
|
|
|
|
def list(self):
|
|
|
|
self._symbols_path = {}
|
|
|
|
symbols = []
|
2017-02-20 17:28:49 +00:00
|
|
|
if get_resource("symbols"):
|
|
|
|
for file in os.listdir(get_resource("symbols")):
|
|
|
|
if file.startswith('.'):
|
|
|
|
continue
|
|
|
|
symbol_id = ':/symbols/' + file
|
|
|
|
symbols.append({
|
|
|
|
'symbol_id': symbol_id,
|
|
|
|
'filename': file,
|
|
|
|
'builtin': True,
|
|
|
|
})
|
|
|
|
self._symbols_path[symbol_id] = os.path.join(get_resource("symbols"), file)
|
2016-06-28 19:15:22 +00:00
|
|
|
directory = self.symbols_path()
|
2016-06-28 17:58:57 +00:00
|
|
|
if directory:
|
|
|
|
for file in os.listdir(directory):
|
|
|
|
if file.startswith('.'):
|
|
|
|
continue
|
2017-05-10 11:40:49 +00:00
|
|
|
if not os.path.isfile(os.path.join(directory, file)):
|
|
|
|
continue
|
2016-06-28 17:58:57 +00:00
|
|
|
symbol_id = file
|
|
|
|
symbols.append({
|
|
|
|
'symbol_id': symbol_id,
|
|
|
|
'filename': file,
|
|
|
|
'builtin': False,
|
|
|
|
})
|
2016-06-28 20:23:34 +00:00
|
|
|
self._symbols_path[symbol_id] = os.path.join(directory, file)
|
2016-06-28 17:58:57 +00:00
|
|
|
|
2016-06-28 16:17:48 +00:00
|
|
|
symbols.sort(key=lambda x: x["filename"])
|
|
|
|
|
2016-06-27 16:33:42 +00:00
|
|
|
return symbols
|
|
|
|
|
2016-06-28 19:15:22 +00:00
|
|
|
def symbols_path(self):
|
2016-06-28 17:58:57 +00:00
|
|
|
directory = os.path.expanduser(Config.instance().get_section_config("Server").get("symbols_path", "~/GNS3/symbols"))
|
|
|
|
if directory:
|
2018-09-11 13:06:01 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(directory, exist_ok=True)
|
|
|
|
except OSError as e:
|
|
|
|
log.error("Could not create symbol directory '{}': {}".format(directory, e))
|
|
|
|
return None
|
2016-06-28 17:58:57 +00:00
|
|
|
return directory
|
|
|
|
|
2016-06-27 16:33:42 +00:00
|
|
|
def get_path(self, symbol_id):
|
2016-09-15 16:21:39 +00:00
|
|
|
try:
|
|
|
|
return self._symbols_path[symbol_id]
|
2018-09-11 13:06:01 +00:00
|
|
|
# Symbol not found, let's refresh the cache
|
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):
|
|
|
|
log.warning("Could not retrieve symbol '{}'".format(symbol_id))
|
|
|
|
symbols_path = self._symbols_path
|
|
|
|
return symbols_path[":/symbols/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
|