0
     #ˆ¾Îƒh¹¿Ð      #1
  #   %ˆl–ßi~”TÒ~êu@ŠãM\*bÑ¿Ï„"iÿÀÑ 3Á    %# -*- Mode: Python -*-
# pygobject - Python bindings for the GObject library
# Copyright (C) 2006  Johannes Hoelzl
#
#   glib/option.py: GOption command line parser
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, see <http://www.gnu.org/licenses/>.

"""GOption command line parser

Extends optparse to use the GOptionGroup, GOptionEntry and GOptionContext
objects. So it is possible to use the gtk, gnome_program and gstreamer command
line groups and contexts.

Use this interface instead of the raw wrappers of GOptionContext and
GOptionGroup in glib.
"""

import sys
import optparse
from optparse import OptParseError, OptionError, OptionValueError, \
    BadOptionError, OptionConflictError
from .module import get_introspection_module

if sys.version_info >= (3, 0):
    _basestring = str
    _bytes = lambda s: s.encode()
else:
    _basestring = basestring
    _bytes = str

from gi._gi import _glib
from gi._error import GError
GLib = get_introspection_module('GLib')

OPTION_CONTEXT_ERROR_QUARK = GLib.quark_to_string(GLib.option_error_quark())

__all__ = [
    "OptParseError",
    "OptionError",
    "OptionValueError",
    "BadOptionError",
    "OptionConflictError",
    "Option",
    "OptionGroup",
    "OptionParser",
    "make_option",
]


class Option(optparse.Option):
    """Represents a command line option

    To use the extended possibilities of the GOption API Option
    (and make_option) are extended with new types and attributes.

    Types:
        filename   The supplied arguments are read as filename, GOption
                   parses this type in with the GLib filename encoding.

    :ivar optional_arg:
        This does not need a arguement, but it can be supplied.
    :ivar hidden:
        The help list does not show this option
    :ivar in_main:
        This option apears in the main group, this should only
        be used for backwards compatibility.

    Use Option.REMAINING as option name to get all positional arguments.

    .. NOTE::
        Every argument to an option is passed as utf-8 coded string, the only
        exception are options which use the 'filename' type, its arguments
        are passed as strings in the GLib filename encoding.

    For further help, see optparse.Option.
    """
    TYPES = optparse.Option.TYPES + (
        'filename',
    )

    ATTRS = optparse.Option.ATTRS + [
        'hidden',
        'in_main',
        'optional_arg',
    ]

    REMAINING = '--' + GLib.OPTION_REMAINING

    def __init__(self, *args, **kwargs):
        optparse.Option.__init__(self, *args, **kwargs)
        if not self._long_opts:
            raise ValueError("%s at least one long option name.")

        if len(self._long_opts) < len(self._short_opts):
            raise ValueError(
                "%s at least more long option names than short option names.")

        if not self.help:
            raise ValueError("%s needs a help message.", self._long_opts[0])

    def _set_opt_string(self, opts):
        if self.REMAINING in opts:
            self._long_opts.append(self.REMAINING)
        optparse.Option._set_opt_string(self, opts)
        if len(self._short_opts) > len(self._long_opts):
            raise OptionError("goption.Option needs more long option names "
                              "than short option names")

    def _to_goptionentries(self):
        flags = 0

        if self.hidden:
            flags |= GLib.OptionFlags.HIDDEN

        if se