3 TODO: Support real help (from show_help()), not only help text
4 This module exports information about manage commands and options from django to PyCharm.
5 Information is provided in XML (to prevent encoding troubles and simplify deserialization on java side).
6 It does not have schema (yet!) but here is XML format it uses.
8 <commandInfo-array> -- root
9 <commandInfo args="args description" help="human readable text" name="command name"> -- info about command
10 <option help="option help" numberOfArgs="number of values (nargs)" type="option type: Option.TYPES"> -- one entry for each option
11 <longNames>--each-for-one-long-opt-name</longNames>
12 <shortNames>-each-for-one-short-name</shortNames>
20 from distutils.version import StrictVersion
21 from xml.dom import minidom
22 from xml.dom.minidom import Element
24 import _django_obtainer_optparse
26 __author__ = 'Ilya.Kazakevich'
29 class XmlDumper(object):
31 Creates an API to generate XML provided in this package.
33 * dumper.start_command(..)
34 * dumper.add_command_option(..) # optional
35 * dumper.close_command()
40 __command_info_tag = "commandInfo" # Name of main tag
43 self.__document = minidom.Document()
44 self.__root = self.__document.createElement("{0}-array".format(XmlDumper.__command_info_tag))
45 self.__document.appendChild(self.__root)
46 self.__command_element = None
48 def __create_text_array(self, parent, tag_name, values):
50 Creates array of text elements and adds them to parent
54 :type values list of str
56 :param parent destination to add new elements
57 :param tag_name name tag to create to hold text
58 :param values list of values to add
62 tag = self.__document.createElement(tag_name)
63 text = self.__document.createTextNode(value)
65 parent.appendChild(tag)
67 def start_command(self, command_name, command_help_text, command_args_text):
71 :param command_name: command name
72 :param command_help_text: command help
73 :param command_args_text: command text for args
77 assert not bool(self.__command_element), "Already in command"
78 self.__command_element = self.__document.createElement(XmlDumper.__command_info_tag)
79 self.__command_element.setAttribute("name", command_name)
80 self.__command_element.setAttribute("help", command_help_text)
81 self.__command_element.setAttribute("args", command_args_text)
82 self.__root.appendChild(self.__command_element)
84 def add_command_option(self, opt_type, choices, long_opt_names, short_opt_names, help_text, num_of_args):
88 :param opt_type: "string", "int", "long", "float", "complex", "choice"
89 :param choices: list of choices for "choice" type
90 :param long_opt_names: list of long opt names
91 :param short_opt_names: list of short opt names
92 :param help_text: help text
93 :param num_of_args: number of arguments
96 :type choices list of string
97 :type long_opt_names list of str
98 :type short_opt_names list of str
100 :type num_of_args int
102 assert isinstance(self.__command_element, Element), "Add option in command only"
103 option = self.__document.createElement("option")
104 option.setAttribute("type", opt_type)
107 self.__create_text_array(option, "choices", choices)
109 self.__create_text_array(option, "longNames", long_opt_names)
111 self.__create_text_array(option, "shortNames", short_opt_names)
113 option.setAttribute("help", help_text)
115 option.setAttribute("numberOfArgs", str(num_of_args))
116 self.__command_element.appendChild(option)
118 def close_command(self):
120 Closes currently opened command
122 assert bool(self.__command_element), "No command to close"
123 self.__command_element = None
130 :return: current commands as XML as described in package
133 return self.__document.toprettyxml()