Tools for GNS3 GUI
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.7 KiB

From c01128dbf60e7c88df46de54aef0873435d7ffe6 Mon Sep 17 00:00:00 2001
From: Bernhard Ehlers <none@b-ehlers.de>
Date: Thu, 28 Dec 2017 17:18:39 +0100
Subject: [PATCH 2/8] Add tool options from JSON file
---
gns3/tool.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 46 insertions(+), 7 deletions(-)
diff --git a/gns3/tool.py b/gns3/tool.py
index 088867c..f2df147 100644
--- a/gns3/tool.py
+++ b/gns3/tool.py
@@ -19,8 +19,10 @@ Manage Tools
"""
import os
+import json
import logging
import subprocess
+from enum import Enum
from .version import __version__
from .items.node_item import NodeItem
@@ -31,12 +33,32 @@ from .items.drawing_item import DrawingItem
log = logging.getLogger(__name__)
+class ContextState(Enum):
+ """ enum for context option """
+ disable = 0
+ enable = 1
+ node = 2
+
+
class Tool:
""" tool class """
- def __init__(self, name, path):
+ def __init__(self, name, path, options):
self._name = name
self._path = path
+ self.menu = options.get('menu', True)
+ context = options.get('context', ContextState.enable)
+ try:
+ if isinstance(context, ContextState):
+ pass
+ elif isinstance(context, int):
+ context = ContextState(context)
+ else:
+ context = ContextState[context]
+ except (KeyError, TypeError, ValueError):
+ log.warning("Tool %s: unknown context option '%s'", name, context)
+ context = ContextState.enable
+ self.context = context
def name(self):
""" tool name """
@@ -61,7 +83,7 @@ class Tool:
try:
subprocess.Popen(args, env=os.environ)
except (OSError, ValueError, subprocess.SubprocessError) as err:
- log.error('could not start tool %s: %s', self._name, err)
+ log.error('Could not start tool %s: %s', self._name, err)
class ToolList:
@@ -76,7 +98,17 @@ class ToolList:
if not path.lower().endswith(".json") and \
os.path.isfile(path) and os.access(path, os.X_OK):
name = os.path.splitext(entry)[0]
- tools.append(Tool(name, path))
+ json_file = os.path.join(tool_path, name) + ".json"
+ options = {}
+ try:
+ with open(json_file, "r") as json_fd:
+ options = json.load(json_fd)
+ except OSError:
+ pass
+ except ValueError as err:
+ log.warning('Tool %s: invalid JSON: %s', name, err)
+ name = options.pop('name', name)
+ tools.append(Tool(name, path, options))
except OSError:
pass
tools.sort(key=lambda k: k.name().lower())
@@ -84,10 +116,17 @@ class ToolList:
def contextTools(self, items):
""" return context tool list """
- if True in list(map(lambda item: isinstance(item, (DrawingItem, NodeItem)), items)):
- return self._tools
- return []
+
+ tool_list = []
+ has_node = True in list(map(lambda item: isinstance(item, NodeItem), items))
+ has_drawing = True in list(map(lambda item: isinstance(item, DrawingItem), items))
+ if has_node or has_drawing:
+ for tool in self._tools:
+ if tool.context == ContextState.enable or \
+ tool.context == ContextState.node and has_node:
+ tool_list.append(tool)
+ return tool_list
def menuTools(self):
""" return menu tool list """
- return self._tools
+ return list(tool for tool in self._tools if tool.menu)
--
2.10.1 (Apple Git-78)