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.

0002-Add-tool-options-from-JSON-file.patch 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. From c01128dbf60e7c88df46de54aef0873435d7ffe6 Mon Sep 17 00:00:00 2001
  2. From: Bernhard Ehlers <be@bernhard-ehlers.de>
  3. Date: Thu, 28 Dec 2017 17:18:39 +0100
  4. Subject: [PATCH 2/7] Add tool options from JSON file
  5. ---
  6. gns3/tool.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++-------
  7. 1 file changed, 46 insertions(+), 7 deletions(-)
  8. diff --git a/gns3/tool.py b/gns3/tool.py
  9. index 088867c..f2df147 100644
  10. --- a/gns3/tool.py
  11. +++ b/gns3/tool.py
  12. @@ -19,8 +19,10 @@ Manage Tools
  13. """
  14. import os
  15. +import json
  16. import logging
  17. import subprocess
  18. +from enum import Enum
  19. from .version import __version__
  20. from .items.node_item import NodeItem
  21. @@ -31,12 +33,32 @@ from .items.drawing_item import DrawingItem
  22. log = logging.getLogger(__name__)
  23. +class ContextState(Enum):
  24. + """ enum for context option """
  25. + disable = 0
  26. + enable = 1
  27. + node = 2
  28. +
  29. +
  30. class Tool:
  31. """ tool class """
  32. - def __init__(self, name, path):
  33. + def __init__(self, name, path, options):
  34. self._name = name
  35. self._path = path
  36. + self.menu = options.get('menu', True)
  37. + context = options.get('context', ContextState.enable)
  38. + try:
  39. + if isinstance(context, ContextState):
  40. + pass
  41. + elif isinstance(context, int):
  42. + context = ContextState(context)
  43. + else:
  44. + context = ContextState[context]
  45. + except (KeyError, TypeError, ValueError):
  46. + log.warning("Tool %s: unknown context option '%s'", name, context)
  47. + context = ContextState.enable
  48. + self.context = context
  49. def name(self):
  50. """ tool name """
  51. @@ -61,7 +83,7 @@ class Tool:
  52. try:
  53. subprocess.Popen(args, env=os.environ)
  54. except (OSError, ValueError, subprocess.SubprocessError) as err:
  55. - log.error('could not start tool %s: %s', self._name, err)
  56. + log.error('Could not start tool %s: %s', self._name, err)
  57. class ToolList:
  58. @@ -76,7 +98,17 @@ class ToolList:
  59. if not path.lower().endswith(".json") and \
  60. os.path.isfile(path) and os.access(path, os.X_OK):
  61. name = os.path.splitext(entry)[0]
  62. - tools.append(Tool(name, path))
  63. + json_file = os.path.join(tool_path, name) + ".json"
  64. + options = {}
  65. + try:
  66. + with open(json_file, "r") as json_fd:
  67. + options = json.load(json_fd)
  68. + except OSError:
  69. + pass
  70. + except ValueError as err:
  71. + log.warning('Tool %s: invalid JSON: %s', name, err)
  72. + name = options.pop('name', name)
  73. + tools.append(Tool(name, path, options))
  74. except OSError:
  75. pass
  76. tools.sort(key=lambda k: k.name().lower())
  77. @@ -84,10 +116,17 @@ class ToolList:
  78. def contextTools(self, items):
  79. """ return context tool list """
  80. - if True in list(map(lambda item: isinstance(item, (DrawingItem, NodeItem)), items)):
  81. - return self._tools
  82. - return []
  83. +
  84. + tool_list = []
  85. + has_node = True in list(map(lambda item: isinstance(item, NodeItem), items))
  86. + has_drawing = True in list(map(lambda item: isinstance(item, DrawingItem), items))
  87. + if has_node or has_drawing:
  88. + for tool in self._tools:
  89. + if tool.context == ContextState.enable or \
  90. + tool.context == ContextState.node and has_node:
  91. + tool_list.append(tool)
  92. + return tool_list
  93. def menuTools(self):
  94. """ return menu tool list """
  95. - return self._tools
  96. + return list(tool for tool in self._tools if tool.menu)
  97. --
  98. 2.10.1 (Apple Git-78)