00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import os
00019 import rospy
00020 from copy import copy
00021 from copy import deepcopy
00022 from airbus_docgen import env
00023 from airbus_docgen.common import html
00024 from airbus_docgen.common.html import HtmlElement
00025
00026 class AbstractMenuItem(HtmlElement):
00027
00028 def __init__(self, name, href='#', subitem=True):
00029 HtmlElement.__init__(self, tag = html.Grouping.li)
00030
00031 root = HtmlElement(tag=html.Text.a)
00032 root.set(html.Attrib.href, href)
00033 root.text = name
00034 self.append(root)
00035
00036 self._container_menu = HtmlElement(tag=html.Grouping.ul)
00037
00038 if subitem:
00039 self.append(self._container_menu)
00040
00041 def getRoot(self):
00042 return self._container_menu
00043
00044 def addSubItem(self, parent, item_name):
00045 """
00046 Append element "<li><a>$menu_name</a><ul/></li>" into parent element
00047 """
00048 item = HtmlElement(tag=html.Grouping.li)
00049 name = HtmlElement(tag=html.Text.a)
00050 name.text = item_name
00051 item.append(name)
00052 subitem = HtmlElement(tag=html.Grouping.ul)
00053 item.append(subitem)
00054 parent.append(item)
00055
00056 return subitem
00057
00058 def addItem(self, parent, item_name, href="#"):
00059 """
00060 Append element "<li><a href="$href">$pkg_name</a></li>" into <ul> parent stack element
00061 """
00062 li = HtmlElement(tag=html.Grouping.li)
00063 a = HtmlElement(tag=html.Text.a)
00064 a.set(html.Attrib.href, href)
00065 a.text = item_name
00066 li.append(a)
00067 parent.append(li)
00068
00069 return li
00070
00071 def __str__(self):
00072 html.indent(self)
00073 return html.tostring(self)
00074
00075 class AbstractMenu(HtmlElement):
00076
00077 def __init__(self, menu_id):
00078 HtmlElement.__init__(self, tag=html.Grouping.div)
00079 self.set(html.Attrib.id, menu_id)
00080
00081 def addItem(self, item):
00082 self.append(item)
00083
00084 def __str__(self):
00085 html.indent(self)
00086 return html.tostring(self)
00087
00088 from airbus_docgen.docgen.home import HtmlHomeFileGenerator
00089
00090 class HomeMenuItem(AbstractMenuItem):
00091
00092 def __init__(self):
00093 AbstractMenuItem.__init__(self, "Home", "home.html", subitem=False)
00094
00095 def generate_htmls(self, index, packages_dir=[]):
00096 home_docgen = HtmlHomeFileGenerator(deepcopy(index), packages_dir)
00097 home_docgen.save()
00098
00099 from airbus_docgen.docgen.pkg import HtmlPkgFileGenerator
00100
00101 class PackagesMenuItem(AbstractMenuItem):
00102
00103 def __init__(self):
00104 AbstractMenuItem.__init__(self, "Packages")
00105 self._base_dir = ""
00106 self._stack_register = {}
00107 self._xml_pkg_dir = []
00108
00109 def get_packages_dir(self):
00110 return self._xml_pkg_dir
00111
00112 def parse(self, workspace_dir):
00113
00114 self._base_dir = copy(workspace_dir)
00115 self._parse(workspace_dir)
00116
00117 return self
00118
00119 def _parse(self, directory):
00120 ros_pkg = None
00121 stack = directory.split('/')[-1]
00122 if rospy.has_param('/airbus_docgen/pkg_dir'):
00123 ros_pkg = rospy.get_param('/airbus_docgen/pkg_dir')
00124 for filename in os.listdir(directory):
00125
00126 if filename == stack:
00127 continue
00128
00129 elif filename == "package.xml":
00130 self._xml_pkg_dir.append(directory)
00131 rpath = directory.replace(self._base_dir+"/","")
00132 spath = rpath.split("/")
00133 if ros_pkg in spath:
00134 self._gen_menu_(self.getRoot(), [], ros_pkg)
00135 else:
00136 self._gen_menu_(self.getRoot(), spath[:-1], spath[-1])
00137 continue
00138
00139 elif os.path.isdir(os.path.join(directory,filename)):
00140 self._parse(os.path.join(directory,filename))
00141
00142 else:
00143 continue
00144 def _gen_menu_(self, parent, stacks, package):
00145 if len(stacks) > 0:
00146 substack = None
00147 stack = stacks[0]
00148 if stack not in self._stack_register.keys():
00149 substack = self.addSubItem(parent, stack)
00150 self._stack_register.update({stack : substack})
00151 else:
00152 substack = self._stack_register[stack]
00153
00154 self._gen_menu_(parent=substack, stacks=stacks[1:], package=package)
00155 else:
00156 self.addItem(parent, package, "%s.html"%package)
00157
00158 def generate_htmls(self, index):
00159
00160 for pkg in self._xml_pkg_dir:
00161
00162 pkg_name = pkg.split('/')[-1].replace(".xml","")
00163 print "Generate html file from package '%s' ..."%pkg_name
00164 pkg_docgen = HtmlPkgFileGenerator(deepcopy(index), pkg, pkg_name)
00165 pkg_docgen.save()
00166
00167 from airbus_docgen.docgen.config import HtmlConfigFileGenerator
00168
00169 class ConfigMenuItem(AbstractMenuItem):
00170
00171 def __init__(self):
00172 AbstractMenuItem.__init__(self, "Configurations", subitem=True)
00173
00174 self._launch_file_dirs =[]
00175
00176 def read(self, config_xml):
00177
00178 for config in config_xml.iter("config"):
00179
00180 config_src = config.attrib["src"].replace("${rossrc}", env.ROS_WS+"/src")
00181 config_name = config.attrib["name"]
00182 parent = self.addSubItem(self.getRoot(),config_name)
00183
00184 for launch in config.iter("launch"):
00185
00186 self._launch_file_dirs.append(os.path.join(config_src, launch.attrib['name']))
00187
00188 launch_name = launch.attrib['name'].replace(".launch","")
00189 self.addItem(parent, launch_name, '%s.html'%launch_name)
00190
00191 def generate_htmls(self, index):
00192
00193 for ldir in self._launch_file_dirs:
00194 print "Generate html from launch '%s' ..."%ldir.split('/')[-1]
00195 launch_html = HtmlConfigFileGenerator(deepcopy(index), ldir)
00196 launch_html.save()
00197
00198
00199 class Menu(AbstractMenu):
00200
00201 def __init__(self):
00202 AbstractMenu.__init__(self, "mmenu")
00203
00204 self._home_item = HomeMenuItem()
00205 self._pkgs_item = PackagesMenuItem()
00206 self._configs_item = ConfigMenuItem()
00207
00208 def parse(self, wsdir):
00209
00210 docgen_conf = html.loadHtml(os.path.join(env.ROSDOC_RSC,'docgen.conf'))
00211 menu_xml_conf = docgen_conf.find("menu")
00212
00213 try:
00214 self._pkgs_item.parse(wsdir)
00215 except Exception as ex:
00216 raise ex
00217
00218 try:
00219 self._configs_item.read(menu_xml_conf.find("configs"))
00220 except Exception as ex:
00221 raise ex
00222
00223 self.addItem(self._home_item)
00224 self.addItem(self._pkgs_item)
00225 self.addItem(self._configs_item)
00226
00227 self.addItem(AbstractMenuItem("Futures", subitem=False))
00228 self.addItem(AbstractMenuItem("About", subitem=False))
00229 self.addItem(AbstractMenuItem("Contact", subitem=False))
00230
00231 return self
00232
00233 def generate(self, index):
00234 self._home_item.generate_htmls(index, self._pkgs_item.get_packages_dir())
00235 self._pkgs_item.generate_htmls(index)
00236 self._configs_item.generate_htmls(index)
00237
00238 if __name__ == '__main__':
00239 menu = Menu()
00240 print str(menu.parse(None, os.path.join(os.getenv("ROS_WORKSPACE"),"src")))
00241
00242