Development of an internal social media platform with personalised dashboards for students
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.

diagrams.py 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # Copyright (c) 2006, 2008-2010, 2012-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2014-2017 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2014 Brett Cannon <brett@python.org>
  4. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  5. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  6. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  7. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  8. """diagram objects
  9. """
  10. import astroid
  11. from pylint.pyreverse.utils import is_interface, FilterMixIn
  12. from pylint.checkers.utils import decorated_with_property
  13. class Figure(object):
  14. """base class for counter handling"""
  15. class Relationship(Figure):
  16. """a relation ship from an object in the diagram to another
  17. """
  18. def __init__(self, from_object, to_object, relation_type, name=None):
  19. Figure.__init__(self)
  20. self.from_object = from_object
  21. self.to_object = to_object
  22. self.type = relation_type
  23. self.name = name
  24. class DiagramEntity(Figure):
  25. """a diagram object, i.e. a label associated to an astroid node
  26. """
  27. def __init__(self, title='No name', node=None):
  28. Figure.__init__(self)
  29. self.title = title
  30. self.node = node
  31. class ClassDiagram(Figure, FilterMixIn):
  32. """main class diagram handling
  33. """
  34. TYPE = 'class'
  35. def __init__(self, title, mode):
  36. FilterMixIn.__init__(self, mode)
  37. Figure.__init__(self)
  38. self.title = title
  39. self.objects = []
  40. self.relationships = {}
  41. self._nodes = {}
  42. self.depends = []
  43. def get_relationships(self, role):
  44. # sorted to get predictable (hence testable) results
  45. return sorted(self.relationships.get(role, ()),
  46. key=lambda x: (x.from_object.fig_id, x.to_object.fig_id))
  47. def add_relationship(self, from_object, to_object,
  48. relation_type, name=None):
  49. """create a relation ship
  50. """
  51. rel = Relationship(from_object, to_object, relation_type, name)
  52. self.relationships.setdefault(relation_type, []).append(rel)
  53. def get_relationship(self, from_object, relation_type):
  54. """return a relation ship or None
  55. """
  56. for rel in self.relationships.get(relation_type, ()):
  57. if rel.from_object is from_object:
  58. return rel
  59. raise KeyError(relation_type)
  60. def get_attrs(self, node):
  61. """return visible attributes, possibly with class name"""
  62. attrs = []
  63. properties = [
  64. (n, m) for n, m in node.items()
  65. if isinstance(m, astroid.FunctionDef)
  66. and decorated_with_property(m)
  67. ]
  68. for node_name, ass_nodes in list(node.instance_attrs_type.items()) + \
  69. list(node.locals_type.items()) + properties:
  70. if not self.show_attr(node_name):
  71. continue
  72. names = self.class_names(ass_nodes)
  73. if names:
  74. node_name = "%s : %s" % (node_name, ", ".join(names))
  75. attrs.append(node_name)
  76. return sorted(attrs)
  77. def get_methods(self, node):
  78. """return visible methods"""
  79. methods = [
  80. m for m in node.values()
  81. if isinstance(m, astroid.FunctionDef)
  82. and not decorated_with_property(m)
  83. and self.show_attr(m.name)
  84. ]
  85. return sorted(methods, key=lambda n: n.name)
  86. def add_object(self, title, node):
  87. """create a diagram object
  88. """
  89. assert node not in self._nodes
  90. ent = DiagramEntity(title, node)
  91. self._nodes[node] = ent
  92. self.objects.append(ent)
  93. def class_names(self, nodes):
  94. """return class names if needed in diagram"""
  95. names = []
  96. for ass_node in nodes:
  97. if isinstance(ass_node, astroid.Instance):
  98. ass_node = ass_node._proxied
  99. if isinstance(ass_node, astroid.ClassDef) \
  100. and hasattr(ass_node, "name") and not self.has_node(ass_node):
  101. if ass_node.name not in names:
  102. ass_name = ass_node.name
  103. names.append(ass_name)
  104. return names
  105. def nodes(self):
  106. """return the list of underlying nodes
  107. """
  108. return self._nodes.keys()
  109. def has_node(self, node):
  110. """return true if the given node is included in the diagram
  111. """
  112. return node in self._nodes
  113. def object_from_node(self, node):
  114. """return the diagram object mapped to node
  115. """
  116. return self._nodes[node]
  117. def classes(self):
  118. """return all class nodes in the diagram"""
  119. return [o for o in self.objects if isinstance(o.node, astroid.ClassDef)]
  120. def classe(self, name):
  121. """return a class by its name, raise KeyError if not found
  122. """
  123. for klass in self.classes():
  124. if klass.node.name == name:
  125. return klass
  126. raise KeyError(name)
  127. def extract_relationships(self):
  128. """extract relation ships between nodes in the diagram
  129. """
  130. for obj in self.classes():
  131. node = obj.node
  132. obj.attrs = self.get_attrs(node)
  133. obj.methods = self.get_methods(node)
  134. # shape
  135. if is_interface(node):
  136. obj.shape = 'interface'
  137. else:
  138. obj.shape = 'class'
  139. # inheritance link
  140. for par_node in node.ancestors(recurs=False):
  141. try:
  142. par_obj = self.object_from_node(par_node)
  143. self.add_relationship(obj, par_obj, 'specialization')
  144. except KeyError:
  145. continue
  146. # implements link
  147. for impl_node in node.implements:
  148. try:
  149. impl_obj = self.object_from_node(impl_node)
  150. self.add_relationship(obj, impl_obj, 'implements')
  151. except KeyError:
  152. continue
  153. # associations link
  154. for name, values in list(node.instance_attrs_type.items()) + \
  155. list(node.locals_type.items()):
  156. for value in values:
  157. if value is astroid.YES:
  158. continue
  159. if isinstance(value, astroid.Instance):
  160. value = value._proxied
  161. try:
  162. ass_obj = self.object_from_node(value)
  163. self.add_relationship(ass_obj, obj, 'association', name)
  164. except KeyError:
  165. continue
  166. class PackageDiagram(ClassDiagram):
  167. """package diagram handling
  168. """
  169. TYPE = 'package'
  170. def modules(self):
  171. """return all module nodes in the diagram"""
  172. return [o for o in self.objects if isinstance(o.node, astroid.Module)]
  173. def module(self, name):
  174. """return a module by its name, raise KeyError if not found
  175. """
  176. for mod in self.modules():
  177. if mod.node.name == name:
  178. return mod
  179. raise KeyError(name)
  180. def get_module(self, name, node):
  181. """return a module by its name, looking also for relative imports;
  182. raise KeyError if not found
  183. """
  184. for mod in self.modules():
  185. mod_name = mod.node.name
  186. if mod_name == name:
  187. return mod
  188. #search for fullname of relative import modules
  189. package = node.root().name
  190. if mod_name == "%s.%s" % (package, name):
  191. return mod
  192. if mod_name == "%s.%s" % (package.rsplit('.', 1)[0], name):
  193. return mod
  194. raise KeyError(name)
  195. def add_from_depend(self, node, from_module):
  196. """add dependencies created by from-imports
  197. """
  198. mod_name = node.root().name
  199. obj = self.module(mod_name)
  200. if from_module not in obj.node.depends:
  201. obj.node.depends.append(from_module)
  202. def extract_relationships(self):
  203. """extract relation ships between nodes in the diagram
  204. """
  205. ClassDiagram.extract_relationships(self)
  206. for obj in self.classes():
  207. # ownership
  208. try:
  209. mod = self.object_from_node(obj.node.root())
  210. self.add_relationship(obj, mod, 'ownership')
  211. except KeyError:
  212. continue
  213. for obj in self.modules():
  214. obj.shape = 'package'
  215. # dependencies
  216. for dep_name in obj.node.depends:
  217. try:
  218. dep = self.get_module(dep_name, obj.node)
  219. except KeyError:
  220. continue
  221. self.add_relationship(obj, dep, 'depends')