PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/xtrcmd/tree/xtr_cls_fic_lst.py

https://gitlab.com/liuxin429go/toolbox
Python | 143 lines | 126 code | 5 blank | 12 comment | 2 complexity | 65a107fdc226a2565e16e6fd09e5fc28 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #************************************************************************
  4. # --- Copyright (c) 2007-2018 INRS
  5. # --- Copyright (c) Yves Secretan 2019
  6. # ---
  7. # --- Distributed under the GNU Lesser General Public License, Version 3.0.
  8. # --- See accompanying file LICENSE.txt.
  9. #************************************************************************
  10. """
  11. Extrait les commandes de H2D2
  12. """
  13. import enum
  14. import glob
  15. import os
  16. import xml.etree.ElementTree
  17. def if_else(p, n1, n2):
  18. if (p): return n1
  19. return n2
  20. class Info:
  21. type = enum.Enum('type', ('NIL', 'Module', 'Classe', 'Command', 'Method'))
  22. def __init__(self, name = ''):
  23. self.file = None
  24. self.dep = []
  25. self.name = name
  26. self.type = Info.type.NIL
  27. # if self.classe: self.grp = self.classe.split('_')[0]
  28. # if self.command: self.grp = self.command.split('_')[0]
  29. def __eq__(self, other):
  30. return (self.name == other.name)
  31. def isMdl(self):
  32. return self.type == Info.type.Module
  33. def isCls(self):
  34. return self.type == Info.type.Classe
  35. def isCmd(self):
  36. return self.type == Info.type.Command
  37. class Writer:
  38. def __init__(self, fname):
  39. self.fout = open(fname, 'w', encoding='utf-8')
  40. def __writeLine(self, l):
  41. self.fout.write('%s\n' % l)
  42. def __writeHdr(self):
  43. hdr = 'H2D2 - Commands menu'
  44. self.__writeLine(hdr)
  45. def __writeFtr(self):
  46. ftr = ''
  47. self.__writeLine(ftr)
  48. def __writeOneCmd(self, i):
  49. f = i.file
  50. n = i.name
  51. t = 'command'
  52. self.__writeLine( '\t\t\t<li><a target="main" href="%s#%s">%s</a></li>' % (f, n, n))
  53. def __writeOneCls(self, i):
  54. f = i.file
  55. n = i.name
  56. t = 'class'
  57. self.__writeLine( '\t\t\t<li><a target="main" href="%s#%s">%s</a></li>' % (f, n, n))
  58. def __writeMdl(self, info):
  59. self.__writeLine('\t<li class="liClosed">%s' % info.name)
  60. self.__writeLine('\t\t<ul>')
  61. for i in info.dep:
  62. if (i.isMdl()):
  63. self.__writeMdl(i)
  64. elif (i.isCls()):
  65. self.__writeOneCls(i)
  66. elif (i.isCmd()):
  67. self.__writeOneCmd(i)
  68. self.__writeLine('\t\t</ul>')
  69. self.__writeLine('\t</li>')
  70. def __writeMenu(self, infos):
  71. infos.sort()
  72. self.__writeLine('<!-- tree -->')
  73. self.__writeLine('<a href="#" onClick="expandTree(\'tree1\'); return false;"><img src="plus.gif" alt="Expand all"></a>')
  74. self.__writeLine('<a href="#" onClick="collapseTree(\'tree1\'); return false;"><img src="minus.gif" alt="Collapse all"></a>')
  75. # self.__writeLine('<A href="#" onClick="expandToItem(\'tree1\',\'login\'); return false;">Expand and find login.html</a>')
  76. self.__writeLine('')
  77. self.__writeLine('<ul class="mktree" id="tree1">')
  78. for k in keys:
  79. self.__writeLine( '\t<li class="liClosed">%s' % (k))
  80. cls_infos = dico[k]
  81. cls_infos.sort()
  82. self.__writeLine('\t\t<ul>')
  83. for i in cls_infos:
  84. f = i.file
  85. n = if_else(i.classe, i.classe, i.command)
  86. t = if_else(i.classe, 'class', 'command')
  87. self.__writeLine( '\t\t\t<li><a target="main" href="%s#%s">%s</a></li>' % (f, n, n))
  88. self.__writeLine('\t\t</ul>')
  89. self.__writeLine('\t</li>')
  90. self.__writeLine('</ul>')
  91. def write(self, cmds):
  92. self.__writeHdr()
  93. self.__writeMenu(cmds)
  94. self.__writeFtr()
  95. def readInfos(f, infos):
  96. tree = xml.etree.ElementTree.parse(f)
  97. root = tree.getroot()
  98. try:
  99. name = root.attrib['name']
  100. assert (name not in infos)
  101. file = root.attrib['file']
  102. file = os.path.basename(file)
  103. infos[name] = [ file ]
  104. except KeyError:
  105. pass
  106. for child in root:
  107. name = child.attrib['name']
  108. infos[name] = [ file ]
  109. return infos
  110. def main():
  111. infos = {}
  112. for f in glob.glob('*.tree.xml'):
  113. readInfos(f, infos)
  114. for i in infos: print(i)
  115. # w = Writer("_h2d2_cmd_tree.html")
  116. # w.write(infos)
  117. main()