def _custom_xml_dom_elements(self, doc):
   elements = []
   if self.lower_bound is not None:
     elements.append(_helpers.create_xml_dom_element(
         doc, 'lower_bound', self.lower_bound))
   if self.upper_bound is not None:
     elements.append(_helpers.create_xml_dom_element(
         doc, 'upper_bound', self.upper_bound))
   return elements
Ejemplo n.º 2
0
 def _custom_xml_dom_elements(self, doc):
   elements = []
   if self.lower_bound is not None:
     elements.append(_helpers.create_xml_dom_element(
         doc, 'lower_bound', self.lower_bound))
   if self.upper_bound is not None:
     elements.append(_helpers.create_xml_dom_element(
         doc, 'upper_bound', self.upper_bound))
   return elements
Ejemplo n.º 3
0
    def write_help_in_xml_format(self, outfile=None):
        """Outputs flag documentation in XML format.

    NOTE: We use element names that are consistent with those used by
    the C++ command-line flag library, from
    https://github.com/gflags/gflags.
    We also use a few new elements (e.g., <key>), but we do not
    interfere / overlap with existing XML elements used by the C++
    library.  Please maintain this consistency.

    Args:
      outfile: File object we write to.  Default None means sys.stdout.
    """
        doc = minidom.Document()
        all_flag = doc.createElement('AllFlags')
        doc.appendChild(all_flag)

        all_flag.appendChild(
            _helpers.create_xml_dom_element(doc, 'program',
                                            os.path.basename(sys.argv[0])))

        usage_doc = sys.modules['__main__'].__doc__
        if not usage_doc:
            usage_doc = '\nUSAGE: %s [flags]\n' % sys.argv[0]
        else:
            usage_doc = usage_doc.replace('%s', sys.argv[0])
        all_flag.appendChild(
            _helpers.create_xml_dom_element(doc, 'usage', usage_doc))

        # Get list of key flags for the main module.
        key_flags = self.get_key_flags_for_module(sys.argv[0])

        # Sort flags by declaring module name and next by flag name.
        flags_by_module = self.flags_by_module_dict()
        all_module_names = list(flags_by_module.keys())
        all_module_names.sort()
        for module_name in all_module_names:
            flag_list = [(f.name, f) for f in flags_by_module[module_name]]
            flag_list.sort()
            for unused_flag_name, flag in flag_list:
                is_key = flag in key_flags
                all_flag.appendChild(
                    flag._create_xml_dom_element(  # pylint: disable=protected-access
                        doc,
                        module_name,
                        is_key=is_key))

        outfile = outfile or sys.stdout
        if six.PY2:
            outfile.write(doc.toprettyxml(indent='  ', encoding='utf-8'))
        else:
            outfile.write(
                doc.toprettyxml(indent='  ', encoding='utf-8').decode('utf-8'))
        outfile.flush()
  def write_help_in_xml_format(self, outfile=None):
    """Outputs flag documentation in XML format.

    NOTE: We use element names that are consistent with those used by
    the C++ command-line flag library, from
    https://github.com/gflags/gflags.
    We also use a few new elements (e.g., <key>), but we do not
    interfere / overlap with existing XML elements used by the C++
    library.  Please maintain this consistency.

    Args:
      outfile: File object we write to.  Default None means sys.stdout.
    """
    doc = minidom.Document()
    all_flag = doc.createElement('AllFlags')
    doc.appendChild(all_flag)

    all_flag.appendChild(_helpers.create_xml_dom_element(
        doc, 'program', os.path.basename(sys.argv[0])))

    usage_doc = sys.modules['__main__'].__doc__
    if not usage_doc:
      usage_doc = '\nUSAGE: %s [flags]\n' % sys.argv[0]
    else:
      usage_doc = usage_doc.replace('%s', sys.argv[0])
    all_flag.appendChild(_helpers.create_xml_dom_element(
        doc, 'usage', usage_doc))

    # Get list of key flags for the main module.
    key_flags = self.get_key_flags_for_module(sys.argv[0])

    # Sort flags by declaring module name and next by flag name.
    flags_by_module = self.flags_by_module_dict()
    all_module_names = list(flags_by_module.keys())
    all_module_names.sort()
    for module_name in all_module_names:
      flag_list = [(f.name, f) for f in flags_by_module[module_name]]
      flag_list.sort()
      for unused_flag_name, flag in flag_list:
        is_key = flag in key_flags
        all_flag.appendChild(flag._create_xml_dom_element(  # pylint: disable=protected-access
            doc, module_name, is_key=is_key))

    outfile = outfile or sys.stdout
    if six.PY2:
      outfile.write(doc.toprettyxml(indent='  ', encoding='utf-8'))
    else:
      outfile.write(
          doc.toprettyxml(indent='  ', encoding='utf-8').decode('utf-8'))
    outfile.flush()
Ejemplo n.º 5
0
 def _extra_xml_dom_elements(self, doc):
   elements = []
   if hasattr(self.parser, 'enum_values'):
     for enum_value in self.parser.enum_values:
       elements.append(_helpers.create_xml_dom_element(
           doc, 'enum_value', enum_value))
   return elements
Ejemplo n.º 6
0
 def _extra_xml_dom_elements(self, doc):
   elements = []
   if hasattr(self.parser, 'enum_values'):
     for enum_value in self.parser.enum_values:
       elements.append(_helpers.create_xml_dom_element(
           doc, 'enum_value', enum_value))
   return elements
Ejemplo n.º 7
0
    def _create_xml_dom_element(self, doc, module_name, is_key=False):
        """Returns an XML element that contains this flag's information.

    This is information that is relevant to all flags (e.g., name,
    meaning, etc.).  If you defined a flag that has some other pieces of
    info, then please override _ExtraXMLInfo.

    Please do NOT override this method.

    Args:
      doc: minidom.Document, the DOM document it should create nodes from.
      module_name: str,, the name of the module that defines this flag.
      is_key: boolean, True iff this flag is key for main module.

    Returns:
      A minidom.Element instance.
    """
        element = doc.createElement('flag')
        if is_key:
            element.appendChild(
                _helpers.create_xml_dom_element(doc, 'key', 'yes'))
        element.appendChild(
            _helpers.create_xml_dom_element(doc, 'file', module_name))
        # Adds flag features that are relevant for all flags.
        element.appendChild(
            _helpers.create_xml_dom_element(doc, 'name', self.name))
        if self.short_name:
            element.appendChild(
                _helpers.create_xml_dom_element(doc, 'short_name',
                                                self.short_name))
        if self.help:
            element.appendChild(
                _helpers.create_xml_dom_element(doc, 'meaning', self.help))
        # The default flag value can either be represented as a string like on the
        # command line, or as a Python object.  We serialize this value in the
        # latter case in order to remain consistent.
        if self.serializer and not isinstance(self.default, str):
            if self.default is not None:
                default_serialized = self.serializer.serialize(self.default)
            else:
                default_serialized = ''
        else:
            default_serialized = self.default
        element.appendChild(
            _helpers.create_xml_dom_element(doc, 'default',
                                            default_serialized))
        value_serialized = self._serialize_value_for_xml(self.value)
        element.appendChild(
            _helpers.create_xml_dom_element(doc, 'current', value_serialized))
        element.appendChild(
            _helpers.create_xml_dom_element(doc, 'type', self.flag_type()))
        # Adds extra flag features this flag may have.
        for e in self._extra_xml_dom_elements(doc):
            element.appendChild(e)
        return element
Ejemplo n.º 8
0
 def _custom_xml_dom_elements(self, doc):
   elements = super(WhitespaceSeparatedListParser, self
                   )._custom_xml_dom_elements(doc)
   separators = list(string.whitespace)
   if self._comma_compat:
     separators.append(',')
   separators.sort()
   for sep_char in separators:
     elements.append(_helpers.create_xml_dom_element(
         doc, 'list_separator', repr(sep_char)))
   return elements
 def _custom_xml_dom_elements(self, doc):
   elements = super(WhitespaceSeparatedListParser, self
                   )._custom_xml_dom_elements(doc)
   separators = list(string.whitespace)
   if self._comma_compat:
     separators.append(',')
   separators.sort()
   for sep_char in separators:
     elements.append(_helpers.create_xml_dom_element(
         doc, 'list_separator', repr(sep_char)))
   return elements
Ejemplo n.º 10
0
  def _create_xml_dom_element(self, doc, module_name, is_key=False):
    """Returns an XML element that contains this flag's information.

    This is information that is relevant to all flags (e.g., name,
    meaning, etc.).  If you defined a flag that has some other pieces of
    info, then please override _ExtraXMLInfo.

    Please do NOT override this method.

    Args:
      doc: minidom.Document, the DOM document it should create nodes from.
      module_name: str,, the name of the module that defines this flag.
      is_key: boolean, True iff this flag is key for main module.

    Returns:
      A minidom.Element instance.
    """
    element = doc.createElement('flag')
    if is_key:
      element.appendChild(_helpers.create_xml_dom_element(doc, 'key', 'yes'))
    element.appendChild(_helpers.create_xml_dom_element(
        doc, 'file', module_name))
    # Adds flag features that are relevant for all flags.
    element.appendChild(_helpers.create_xml_dom_element(doc, 'name', self.name))
    if self.short_name:
      element.appendChild(_helpers.create_xml_dom_element(
          doc, 'short_name', self.short_name))
    if self.help:
      element.appendChild(_helpers.create_xml_dom_element(
          doc, 'meaning', self.help))
    # The default flag value can either be represented as a string like on the
    # command line, or as a Python object.  We serialize this value in the
    # latter case in order to remain consistent.
    if self.serializer and not isinstance(self.default, str):
      if self.default is not None:
        default_serialized = self.serializer.serialize(self.default)
      else:
        default_serialized = ''
    else:
      default_serialized = self.default
    element.appendChild(_helpers.create_xml_dom_element(
        doc, 'default', default_serialized))
    element.appendChild(_helpers.create_xml_dom_element(
        doc, 'current', self.value))
    element.appendChild(_helpers.create_xml_dom_element(
        doc, 'type', self.flag_type()))
    # Adds extra flag features this flag may have.
    for e in self._extra_xml_dom_elements(doc):
      element.appendChild(e)
    return element
Ejemplo n.º 11
0
 def _extra_xml_dom_elements(self, doc):
     elements = []
     for enum_value in self.parser.enum_class.__members__.keys():
         elements.append(
             _helpers.create_xml_dom_element(doc, 'enum_value', enum_value))
     return elements
Ejemplo n.º 12
0
 def _custom_xml_dom_elements(self, doc):
     elements = super(ListParser, self)._custom_xml_dom_elements(doc)
     elements.append(
         _helpers.create_xml_dom_element(doc, 'list_separator', repr(',')))
     return elements
 def _check(self, name, value, expected_output):
     doc = xml.dom.minidom.Document()
     node = _helpers.create_xml_dom_element(doc, name, value)
     output = node.toprettyxml('  ', encoding='utf-8')
     self.assertEqual(expected_output, output)
Ejemplo n.º 14
0
 def _extra_xml_dom_elements(self, doc):
   elements = []
   for enum_value in self.parser.enum_class.__members__.keys():
     elements.append(_helpers.create_xml_dom_element(
         doc, 'enum_value', enum_value))
   return elements
 def _custom_xml_dom_elements(self, doc):
   elements = super(ListParser, self)._custom_xml_dom_elements(doc)
   elements.append(_helpers.create_xml_dom_element(
       doc, 'list_separator', repr(',')))
   return elements