formatter = config.pop('<STR_LIT>', None)<EOL>if formatter:<EOL><INDENT>try:<EOL><INDENT>formatter = self.config['<STR_LIT>'][formatter]<EOL><DEDENT>except StandardError as e:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>' % (formatter, e))<EOL><DEDENT><DEDENT>level = config.pop('<STR_LIT>', None)<EOL>filters = config.pop('<STR_LIT>', None)<EOL>if '<STR_LIT>' in config:<EOL><INDENT>c = config.pop('<STR_LIT>')<EOL>if not hasattr(c, '<STR_LIT>') andhasattr(types, '<STR_LIT>') andisinstance(c, types.ClassType):<EOL><INDENT>c = self.resolve(c)<EOL><DEDENT>factory = c<EOL><DEDENT>else:<EOL><INDENT>klass = self.resolve(config.pop('<STR_LIT:class>'))<EOL>if issubclass(klass, logging.handlers.MemoryHandler) and'<STR_LIT:target>' in config:<EOL><INDENT>try:<EOL><INDENT>config['<STR_LIT:target>'] =self.config['<STR_LIT>'][config['<STR_LIT:target>']]<EOL><DEDENT>except StandardError as e:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>' % (config['<STR_LIT:target>'], e))<EOL><DEDENT><DEDENT>elif issubclass(klass, logging.handlers.SMTPHandler) and'<STR_LIT>' in config:<EOL><INDENT>config['<STR_LIT>'] = self.as_tuple(config['<STR_LIT>'])<EOL><DEDENT>elif issubclass(klass, logging.handlers.SysLogHandler) and'<STR_LIT:address>' in config:<EOL><INDENT>config['<STR_LIT:address>'] = self.as_tuple(config['<STR_LIT:address>'])<EOL><DEDENT>factory = klass<EOL><DEDENT>kwargs = dict((k, config[k]) for k in config if valid_ident(k))<EOL>try:<EOL><INDENT>result = factory(**kwargs)<EOL><DEDENT>except TypeError as te:<EOL><INDENT>if "<STR_LIT>" not in str(te):<EOL><INDENT>raise<EOL><DEDENT>kwargs['<STR_LIT>'] = kwargs.pop('<STR_LIT>')<EOL>result = factory(**kwargs)<EOL><DEDENT>if formatter:<EOL><INDENT>result.setFormatter(formatter)<EOL><DEDENT>if level is not None:<EOL><INDENT>result.setLevel(_checkLevel(level))<EOL><DEDENT>if filters:<EOL><INDENT>self.add_filters(result, filters)<EOL><DEDENT>return result<EOL>


def add_handlers(self, logger, handlers):

for h in handlers:<EOL><INDENT>try:<EOL><INDENT>logger.addHandler(self.config['<STR_LIT>'][h])<EOL><DEDENT>except StandardError as e:<EOL><INDENT>raise ValueError('<STR_LIT>' % (h, e))<EOL><DEDENT><DEDENT>

Add handlers to a logger from a list of names.


def common_logger_config(self, logger, config, incremental=False):

level = config.get('<STR_LIT>', None)<EOL>if level is not None:<EOL><INDENT>logger.setLevel(_checkLevel(level))<EOL><DEDENT>if not incremental:<EOL><INDENT>for h in logger.handlers[:]:<EOL><INDENT>logger.removeHandler(h)<EOL><DEDENT>handlers = config.get('<STR_LIT>', None)<EOL>if handlers:<EOL><INDENT>self.add_handlers(logger, handlers)<EOL><DEDENT>filters = config.get('<STR_LIT>', None)<EOL>if filters:<EOL><INDENT>self.add_filters(logger, filters)<EOL><DEDENT><DEDENT>

Perform configuration which is common to root and non-root loggers.


def configure_logger(self, name, config, incremental=False):

logger = logging.getLogger(name)<EOL>self.common_logger_config(logger, config, incremental)<EOL>propagate = config.get('<STR_LIT>', None)<EOL>if propagate is not None:<EOL><INDENT>logger.propagate = propagate<EOL><DEDENT>

Configure a non-root logger from a dictionary.


def configure_root(self, config, incremental=False):

root = logging.getLogger()<EOL>self.common_logger_config(root, config, incremental)<EOL>

Configure a root logger from a dictionary.


func.__doc__ = doc<EOL>


__import__(name)<EOL>return sys.modules[name]<EOL>

Import module, returning the module after the last dot.


setattr(_MovedItems, move.name, move)<EOL>


try:<EOL><INDENT>delattr(_MovedItems, name)<EOL><DEDENT>except AttributeError:<EOL><INDENT>try:<EOL><INDENT>del moves.__dict__[name]<EOL><DEDENT>except KeyError:<EOL><INDENT>raise AttributeError("<STR_LIT>" % (name,))<EOL><DEDENT><DEDENT>


return iter(getattr(d, _iterkeys)(**kw))<EOL>

Return an iterator over the keys of a dictionary.


return iter(getattr(d, _itervalues)(**kw))<EOL>

Return an iterator over the values of a dictionary.


return iter(getattr(d, _iteritems)(**kw))<EOL>

Return an iterator over the (key, value) pairs of a dictionary.


return iter(getattr(d, _iterlists)(**kw))<EOL>

Return an iterator over the (key, [values]) pairs of a dictionary.


<EOL>class metaclass(meta):<EOL><INDENT>__call__ = type.__call__<EOL>__init__ = type.__init__<EOL>def __new__(cls, name, this_bases, d):<EOL><INDENT>if this_bases is None:<EOL><INDENT>return type.__new__(cls, name, (), d)<EOL><DEDENT>return meta(name, bases, d)<EOL><DEDENT><DEDENT>return metaclass('<STR_LIT>', None, {})<EOL>


def wrapper(cls):<EOL><INDENT>orig_vars = cls.__dict__.copy()<EOL>orig_vars.pop('<STR_LIT>', None)<EOL>orig_vars.pop('<STR_LIT>', None)<EOL>slots = orig_vars.get('<STR_LIT>')<EOL>if slots is not None:<EOL><INDENT>if isinstance(slots, str):<EOL><INDENT>slots = [slots]<EOL><DEDENT>for slots_var in slots:<EOL><INDENT>orig_vars.pop(slots_var)<EOL><DEDENT><DEDENT>return metaclass(cls.__name__, cls.__bases__, orig_vars)<EOL><DEDENT>return wrapper<EOL>

Class decorator for creating a class with a metaclass.


global verbose_output<EOL>verbose_output = is_verbose_output<EOL>if verbose_output:<EOL><INDENT>jocker_lgr.setLevel(logging.DEBUG)<EOL><DEDENT>else:<EOL><INDENT>jocker_lgr.setLevel(logging.INFO)<EOL><DEDENT>

sets the global verbosity level for console and the jocker_lgr logger.

    :param bool is_verbose_output: should be output be verbose


<EOL>jocker_lgr.debug('<STR_LIT>'.format(config_file))<EOL>try:<EOL><INDENT>jocker_lgr.debug('<STR_LIT>')<EOL>with open(config_file, '<STR_LIT:r>') as c:<EOL><INDENT>return yaml.safe_load(c.read())<EOL><DEDENT><DEDENT>except IOError as ex:<EOL><INDENT>jocker_lgr.error(str(ex))<EOL>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>except yaml.parser.ParserError as ex:<EOL><INDENT>jocker_lgr.error('<STR_LIT>'.format(ex))<EOL>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>

returns a configuration object

    :param string config_file: path to config file


def execute(varsfile, templatefile, outputfile=None, configfile=None,<EOL>dryrun=False, build=False, push=False, verbose=False):

if dryrun and (build or push):<EOL><INDENT>jocker_lgr.error('<STR_LIT>')<EOL>sys.exit(<NUM_LIT:100>)<EOL><DEDENT>_set_global_verbosity_level(verbose)<EOL>j = Jocker(varsfile, templatefile, outputfile, configfile, dryrun,<EOL>build, push)<EOL>formatted_text = j.generate()<EOL>if dryrun:<EOL><INDENT>g = j.dryrun(formatted_text)<EOL><DEDENT>if build or push:<EOL><INDENT>j.build_image()<EOL><DEDENT>if push:<EOL><INDENT>j.push_image()<EOL><DEDENT>if dryrun:<EOL><INDENT>return g<EOL><DEDENT>

generates a Dockerfile, builds an image and pushes it to DockerHub

    A `Dockerfile` will be generated by Jinja2 according to the `varsfile`
    imported. If build is true, an image will be generated from the
    `outputfile` which is the generated Dockerfile and committed to the
    image:tag string supplied to `build`.
    If push is true, a build will be triggered and the produced image
    will be pushed to DockerHub upon completion.

    :param string varsfile: path to file with variables.
    :param string templatefile: path to template file to use.
    :param string outputfile: path to output Dockerfile.
    :param string configfile: path to yaml file with docker-py config.
    :param bool dryrun: mock run.
    :param build: False or the image:tag to build to.
    :param push: False or the image:tag to build to. (triggers build)
    :param bool verbose: verbose output.


stack = <NUM_LIT:0><EOL>json_list = []<EOL>tmp_json = '<STR_LIT>'<EOL>for char in string:<EOL><INDENT>if not char == '<STR_LIT:\r>' and not char == '<STR_LIT:\n>':<EOL><INDENT>tmp_json += char<EOL><DEDENT>if char == '<STR_LIT:{>':<EOL><INDENT>stack += <NUM_LIT:1><EOL><DEDENT>elif char == '<STR_LIT:}>':<EOL><INDENT>stack -= <NUM_LIT:1><EOL><DEDENT>if stack == <NUM_LIT:0>:<EOL><INDENT>if not len(tmp_json) == <NUM_LIT:0>:<EOL><INDENT>json_list.append(tmp_json)<EOL><DEDENT>tmp_json = '<STR_LIT>'<EOL><DEDENT><DEDENT>return json_list<EOL>

since the push process outputs a single unicode string consisting of
        multiple JSON formatted "status" lines, we need to parse it so that it
        can be read as multiple strings.

        This will receive the string as an input, count curly braces and ignore
        any newlines. When the curly braces stack is 0, it will append the
        entire string it has read up until then to a list and so forth.

        :param string: the string to parse
        :rtype: list of JSON's


if logging_config is None:<EOL><INDENT>logging_config = {}<EOL><DEDENT>logging_config = logging_config or LOGGER<EOL>log_file = LOGGER['<STR_LIT>']['<STR_LIT:file>']['<STR_LIT:filename>']<EOL>log_dir = os.path.dirname(os.path.expanduser(log_file))<EOL>if os.path.isfile(log_dir):<EOL><INDENT>sys.exit('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>.format(log_dir))<EOL><DEDENT>try:<EOL><INDENT>if not os.path.exists(log_dir) and not len(log_dir) == <NUM_LIT:0>:<EOL><INDENT>os.makedirs(log_dir)<EOL><DEDENT>dictconfig.dictConfig(logging_config)<EOL>lgr = logging.getLogger('<STR_LIT:user>')<EOL>lgr.setLevel(base_level)<EOL>return lgr<EOL><DEDENT>except ValueError as e:<EOL><INDENT>sys.exit('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>.format(log_file, e))<EOL><DEDENT>

initializes a base logger

    you can use this to init a logger in any of your files.
    this will use config.py's LOGGER param and logging.dictConfig to configure
    the logger for you.

    :param int|logging.LEVEL base_level: desired base logging level
    :param int|logging.LEVEL verbose_level: desired verbose logging level
    :param dict logging_dict: dictConfig based configuration.
     used to override the default configuration from config.py
    :rtype: `python logger`


print("<STR_LIT>" % path)<EOL>for attrname in sorted(dir(path)):<EOL><INDENT>if attrname.startswith("<STR_LIT>"):<EOL><INDENT>value = getattr(path, attrname)<EOL>print("<STR_LIT>" % (attrname, value))<EOL><DEDENT><DEDENT>print()<EOL>

print information of a pathlib / os.DirEntry() instance with all "is_*" functions.


os.utime(self.extended_path, *args, **kwargs)<EOL>

Set the access and modified times of the file specified by path.


@classmethod<EOL><INDENT>def _from_parts(cls, args, init=True):<DEDENT>

if args:<EOL><INDENT>args = list(args)<EOL>if isinstance(args[<NUM_LIT:0>], WindowsPath2):<EOL><INDENT>args[<NUM_LIT:0>] = args[<NUM_LIT:0>].path<EOL><DEDENT>elif args[<NUM_LIT:0>].startswith("<STR_LIT>"):<EOL><INDENT>args[<NUM_LIT:0>] = args[<NUM_LIT:0>][<NUM_LIT:4>:]<EOL><DEDENT>args = tuple(args)<EOL><DEDENT>return super(WindowsPath2, cls)._from_parts(args, init)<EOL>


if self.is_absolute() and not self.path.startswith("<STR_LIT>"):<EOL><INDENT>return "<STR_LIT>" % self.path<EOL><DEDENT>return self.path<EOL>

Add prefix \\?\ to every absolute path, so that it's a "extended-length"
path, that should be longer than 259 characters (called: "MAX_PATH")
see:
https://msdn.microsoft.com/en-us/library/aa365247.aspx#maxpath


path = super(WindowsPath2, self).path<EOL>if path.startswith("<STR_LIT>"):<EOL><INDENT>return path[<NUM_LIT:4>:]<EOL><DEDENT>return path<EOL>

Return the path always without the \\?\ prefix.


return super(WindowsPath2, Path2(self.path)).relative_to(Path2(other).path)<EOL>

Important here is, that both are always the same:
both with \\?\ prefix or both without it.


<EOL>return cls(os.path.expanduser("<STR_LIT>"))<EOL>

Return a new path pointing to the user's home directory (as
returned by os.path.expanduser('~'))


def __init__(self, dir_entry, onerror=print):

self.dir_entry = dir_entry <EOL>self.path = dir_entry.path <EOL>self.is_symlink = dir_entry.is_symlink()<EOL>self.is_file = dir_entry.is_file(follow_symlinks=False)<EOL>self.is_dir = dir_entry.is_dir(follow_symlinks=False)<EOL>self.stat = dir_entry.stat(follow_symlinks=False)<EOL>self.path_instance = Path2(self.path)<EOL>try:<EOL><INDENT>self.resolved_path = self.path_instance.resolve()<EOL><DEDENT>except (PermissionError, FileNotFoundError) as err:<EOL><INDENT>onerror("<STR_LIT>" % (self.path, err))<EOL>self.resolved_path = None<EOL>self.resolve_error = err<EOL><DEDENT>else:<EOL><INDENT>self.resolve_error = None<EOL><DEDENT>if self.resolved_path is None:<EOL><INDENT>self.different_path = True<EOL><DEDENT>else:<EOL><INDENT>self.different_path = self.path_instance.path != self.resolved_path.path<EOL><DEDENT>

:param dir_entry: os.DirEntry() instance


types = (int, float, str)<EOL>_type = [a for a in types if is_type(a, x)][<NUM_LIT:0>]<EOL>return _type(x)<EOL>


values = (fn(v) for k, v in list(record.items()))<EOL>return dict(zip(record, values))<EOL>


def apply_types(use_types, guess_type, line):

new_line = {}<EOL>for k, v in list(line.items()):<EOL><INDENT>if k in use_types:<EOL><INDENT>new_line[k] = force_type(use_types[k], v)<EOL><DEDENT>elif guess_type:<EOL><INDENT>new_line[k] = determine_type(v)<EOL><DEDENT>else:<EOL><INDENT>new_line[k] = v<EOL><DEDENT><DEDENT>return new_line<EOL>

Apply the types on the elements of the line


def read_csv(filename, delimiter="<STR_LIT:U+002C>", skip=<NUM_LIT:0>, guess_type=True, has_header=True, use_types={}):

with open(filename, '<STR_LIT:r>') as f:<EOL><INDENT>if has_header:<EOL><INDENT>header = f.readline().strip().split(delimiter)<EOL><DEDENT>else:<EOL><INDENT>header = None<EOL><DEDENT>for i in range(skip):<EOL><INDENT>f.readline()<EOL><DEDENT>for line in csv.DictReader(f, delimiter=delimiter, fieldnames=header):<EOL><INDENT>if use_types:<EOL><INDENT>yield apply_types(use_types, guess_type, line)<EOL><DEDENT>elif guess_type:<EOL><INDENT>yield dmap(determine_type, line)<EOL><DEDENT>else:<EOL><INDENT>yield line<EOL><DEDENT><DEDENT><DEDENT>

Read a CSV file

    Usage
    -----
    >>> data = read_csv(filename, delimiter=delimiter, skip=skip,
            guess_type=guess_type, has_header=True, use_types={})

    # Use specific types
    >>> types = {"sepal.length": int, "petal.width": float}
    >>> data = read_csv(filename, guess_type=guess_type, use_types=types)

    keywords
    :has_header:
        Determine whether the file has a header or not


def write_csv(filename, header, data=None, rows=None, mode="<STR_LIT:w>"):

if data == rows == None:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg)<EOL><DEDENT>elif data != None and rows != None:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg)<EOL><DEDENT>data_header = dict((x, x) for x in header)<EOL>with open(filename, mode) as f:<EOL><INDENT>if data:<EOL><INDENT>writer = csv.DictWriter(f, fieldnames=header)<EOL>if mode == "<STR_LIT:w>":<EOL><INDENT>writer.writerow(data_header)<EOL><DEDENT>writer.writerows(data)<EOL><DEDENT>elif rows:<EOL><INDENT>writer = csv.writer(f)<EOL>if mode == "<STR_LIT:w>":<EOL><INDENT>writer.writerow(header)<EOL><DEDENT>writer.writerows(rows)<EOL><DEDENT><DEDENT>print("<STR_LIT>" % filename)<EOL>

Write the data to the specified filename

    Usage
    -----
    >>> write_csv(filename, header, data, mode=mode)

    Parameters
    ----------
    filename : str
        The name of the file

    header : list of strings
        The names of the columns (or fields):
        (fieldname1, fieldname2, ...)

    data : list of dictionaries (optional)
        [
         {fieldname1: a1, fieldname2: a2},
         {fieldname1: b1, fieldname2: b2},
         ...
        ]

    rows : list of lists (optional)
        [
        (a1, a2),
        (b1, b2),
        ...
        ]

    mode : str (optional)
        "w": write the data to the file by overwriting it
        "a": write the data to the file by appending them

    Returns
    -------
    None. A CSV file is written.


def format_to_csv(filename, skiprows=<NUM_LIT:0>, delimiter="<STR_LIT>"):

if not delimiter:<EOL><INDENT>delimiter = "<STR_LIT:\t>"<EOL><DEDENT>input_file = open(filename, "<STR_LIT:r>")<EOL>if skiprows:<EOL><INDENT>[input_file.readline() for _ in range(skiprows)]<EOL><DEDENT>new_filename = os.path.splitext(filename)[<NUM_LIT:0>] + "<STR_LIT>"<EOL>output_file = open(new_filename, "<STR_LIT:w>")<EOL>header = input_file.readline().split()<EOL>reader = csv.DictReader(input_file, fieldnames=header, delimiter=delimiter)<EOL>writer = csv.DictWriter(output_file, fieldnames=header, delimiter="<STR_LIT:U+002C>")<EOL>writer.writerow(dict((x, x) for x in header))<EOL>for line in reader:<EOL><INDENT>if None in line: del line[None]<EOL>writer.writerow(line)<EOL><DEDENT>input_file.close()<EOL>output_file.close()<EOL>print("<STR_LIT>" % new_filename)<EOL>


def __init__(self, constant, *args, **kwargs):

super(ConstKDFChain, self).__init__(*args, **kwargs)<EOL>self.__constant = constant<EOL>

Initialize a ConstKDFChain, which uses constant input data instead of passed data
on chains steps.

:param constant: The constant data to pass to the next method on each step.


return super(ConstKDFChain, self).next(self.__constant)<EOL>

Use the key derivation function to derive new data. The kdf gets supplied with the
current key and the constant input data set using the constructor.

:returns: A bytes-like object encoding the output material.


self.__length = <NUM_LIT:0><EOL>self.__kdf = kdf<EOL>self.__key = key<EOL>

Initialize a KDFChain using the provided key derivation function and key.

:param kdf: An instance of the KDF interface.
:param key: A bytes-like object encoding the key to supply to the key derivation
    function.


self.__length += <NUM_LIT:1><EOL>result = self.__kdf.calculate(self.__key, data, <NUM_LIT:64>)<EOL>self.__key = result[:<NUM_LIT:32>]<EOL>return result[<NUM_LIT:32>:]<EOL>

Derive a new set of internal and output data from given input data and the data
stored internally.

Use the key derivation function to derive new data. The kdf gets supplied with the
current key and the data passed to this method.

:param data: A bytes-like object encoding the data to pass to the key derivation
    function.
:returns: A bytes-like object encoding the output material.


return self.__length<EOL>

:returns: The number of calls to the "next" method since initializing the chain.


def __init__(self, priv = None, pub = None):

raise NotImplementedError<EOL>

Initiate a KeyPair instance using the key information provided as parameters.

:param priv: The private key as a bytes-like object or None.
:param pub: The public key as a bytes-like object or None.


raise NotImplementedError<EOL>

:returns: A new key pair with private and public key set.


raise NotImplementedError<EOL>

:returns: A bytes-like object encoding the private key of this key pair instance.


raise NotImplementedError<EOL>

:returns: A bytes-like object encoding the public key of this key pair instance.


raise NotImplementedError<EOL>

Get a shared secret between the keys stored by this instance and the keys stored
by the other instance.

The shared secrets are generated, so that following equation is True: ::

    shared_secret(A.priv, B.pub) == shared_secret(B.priv, A.pub)

:param other: An instance of the KeyPair class.
:returns: The shared secret, as a bytes-like object.
:raises MissingKeyException: If any key is missing to complete this operation. The
    exception message will contain (human-readable) details.


raise NotImplementedError<EOL>

Use the key and the given input data to derive an output key of given length.

:param key: A bytes-like object encoding the key to use for derivation.
:param data: A bytes-like object encoding the data to use for derivation.
:param length: The length of the key to derive, as an integer.
:returns: A bytes-like object with the requested length encoding the derived key.


def __init__(self, hash_function, ck_constant = b"<STR_LIT>", mk_constant = b"<STR_LIT>"):

super(ChainKeyKDF, self).__init__()<EOL>if not hash_function in ChainKeyKDF.HASH_FUNCTIONS:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not isinstance(ck_constant, bytes):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if len(ck_constant) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not isinstance(mk_constant, bytes):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>if len(mk_constant) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>self.__hash_function = ChainKeyKDF.HASH_FUNCTIONS[hash_function]<EOL>self.__ck_constant = ck_constant<EOL>self.__mk_constant = mk_constant<EOL>

Prepare a ChainKeyKDF, following a recommendation by WhisperSystems.

:param hash_function: One of (the strings) "SHA-256" and "SHA-512".
:param ck_constant: A single byte used for derivation of the next chain key.
:param mk_constant: A single byte used for derivation of the next message key.


def __init__(self, hash_function, info_string):

super(RootKeyKDF, self).__init__()<EOL>if not hash_function in RootKeyKDF.HASH_FUNCTIONS:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not isinstance(info_string, bytes):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>self.__hash_function = RootKeyKDF.HASH_FUNCTIONS[hash_function]<EOL>self.__info_string   = info_string<EOL>

Prepare a RootKeyKDF, following a recommendation by WhisperSystems.

:param hash_function: One of (the strings) "SHA-256" and "SHA-512".
:param info_string: A bytes-like object encoding a string unique to this usage
    within the application.


super(CBCHMACAEAD, self).__init__()<EOL>if not hash_function in CBCHMACAEAD.HASH_FUNCTIONS:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not isinstance(info_string, bytes):<EOL><INDENT>raise TypeError("<STR_LIT>")<EOL><DEDENT>self.__hash_function = CBCHMACAEAD.HASH_FUNCTIONS[hash_function]<EOL>self.__digest_size = self.__hash_function().digest_size<EOL>self.__info_string = info_string<EOL>

Prepare a CBCHMACAEAD, following a recommendation by WhisperSystems.

:param hash_function: One of (the strings) "SHA-256" and "SHA-512".
:param info_string: A bytes-like object encoding a string unique to this usage
    within the application.


self.__dh_pub = dh_pub<EOL>self.__n  = n<EOL>self.__pn = pn<EOL>

Initiate a message header.

:param dh_pub: A bytes-like object encoding the new public key of the senders
    diffie-hellman ratchet.
:param n: The current length of the senders sending chain, as an integer.
:param pn: The length of the senders previous sending chain, as an integer. This
    enables the receiver to store keys for skipped messages of the previous chain.


return self.__dh_pub<EOL>

:returns: A bytes-like object encoding the new public key of the senders
    diffie-hellman ratchet.


return self.__n<EOL>

:returns: The current length of the senders sending chain, as an integer.


return self.__pn<EOL>

:returns: The length of the senders previous sending chain, as an integer. This
    enables the receiver to store keys for skipped messages of the previous chain.


raise NotImplementedError<EOL>

Encrypt given plaintext using given key and authenticating using given associated
data.

:param plaintext: A bytes-like object encoding the data to encrypt.
:param key: A bytes-like object encoding the key to encrypt with.
:param ad: A bytes-like object encoding the associated data to authenticate with.
:returns: A bytes-like object encoding the encrypted data (the ciphertext).


raise NotImplementedError<EOL>

Decrypt given ciphertext using given key and check validity of the authentication
using given associated data.

:param ciphertext: A bytes-like object encoding the data to decrypt.
:param key: A bytes-like object encoding the key to decrypt with.
:param ad: A bytes-like object encoding the associated data to authenticate with.
:returns: A bytes-like object encoding the decrypted data (the plaintext).
:raises AuthenticationFailedException: If the message could not be authenticated
    using the associated data.


def __init__(self, sending_chain_class, receiving_chain_class):

super(SymmetricKeyRatchet, self).__init__()<EOL>self.__SendingChain   = sending_chain_class<EOL>self.__ReceivingChain = receiving_chain_class<EOL>self.__sending_chain   = None<EOL>self.__receiving_chain = None<EOL>self.__previous_sending_chain_length = None<EOL>

Initialize a new SymmetricKeyRatchet.

:param sending_chain_class: An implementation of the Chain interface to be used
    for the sending chains.
:param receiving_chain_class: An implementations of the Chain interface to be used
    for the receiving chains.


if chain == "<STR_LIT>":<EOL><INDENT>self.__previous_sending_chain_length = self.sending_chain_length<EOL>self.__sending_chain = self.__SendingChain(key)<EOL><DEDENT>if chain == "<STR_LIT>":<EOL><INDENT>self.__receiving_chain = self.__ReceivingChain(key)<EOL><DEDENT>

Perform a rachted step, replacing one of the internally managed chains with a new
one.

:param key: A bytes-like object encoding the key to initialize the replacement
    chain with.
:param chain: The chain to replace. This parameter must be one of the two strings
    "sending" and "receiving".


return self.__previous_sending_chain_length<EOL>

Get the length of the previous sending chain.

:returns: Either an integer representing the length of the previous sending chain
    or None, if the current one is the first sending chain.


return None if self.__sending_chain == None else self.__sending_chain.length<EOL>

Get the length of the current sending chain.

:returns: Either an integer representing the length of the current sending chain
    or None, if there is no sending chain.


return None if self.__receiving_chain == None else self.__receiving_chain.length<EOL>

Get the length of the receiving chain.

:returns: Either an integer representing the length of the receiving chain or
    None, if there is no receiving chain.


if self.__sending_chain == None:<EOL><INDENT>raise NotInitializedException(<EOL>"<STR_LIT>" +<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>return self.__sending_chain.next()<EOL>

Use the sending chain to derive the next encryption key.

:returns: A bytes-like object encoding the next encryption key.
:raises NotInitializedException: If there is no sending chain yet.


if self.__receiving_chain == None:<EOL><INDENT>raise NotInitializedException(<EOL>"<STR_LIT>" +<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>return self.__receiving_chain.next()<EOL>

Use the receiving chain to derive the next decryption key.

:returns: A bytes-like object encoding the next decryption key.
:raises NotInitializedException: If there is no receiving chain yet.


self.__aead    = aead<EOL>self.__mks_max = message_key_store_max<EOL>self.__skr     = symmetric_key_ratchet<EOL>self.__ad      = ad<EOL>self.__saved_message_keys = {}<EOL>super(DoubleRatchet, self).__init__(*args, **kwargs)<EOL>

Initialize a new DoubleRatchet.

:param aead: An instance of an implementation of the AEAD interface, which is used
    to provice authenticated message encryption and is fed with the message keys
    derived using the symmetric key ratchet.
:param message_key_store_max: An integer defining the maximum amount of message
    keys to store before raising an exception. This mechanism allows out-of-order
    messages, by storing message keys of out-of-order messages instead of
    discarding them.
:param symmetric_key_ratchet: An instance of the SymmetricKeyRatchet class, which
    is used to derive en- and decryption keys for message exchange.
:param ad: Some associated data to use for message authentication, encoded as a
    bytes-like object.


self.__skr.step(key, chain)<EOL>

Update the symmetric key ratchet with the new key.


def decryptMessage(self, ciphertext, header, ad = None):

if ad == None:<EOL><INDENT>ad = self.__ad<EOL><DEDENT>plaintext = self.__decryptSavedMessage(ciphertext, header, ad)<EOL>if plaintext:<EOL><INDENT>return plaintext<EOL><DEDENT>if self.triggersStep(header.dh_pub):<EOL><INDENT>self.__saveMessageKeys(header.pn)<EOL>self.step(header.dh_pub)<EOL><DEDENT>self.__saveMessageKeys(header.n)<EOL>return self.__decrypt(<EOL>ciphertext,<EOL>self.__skr.nextDecryptionKey(),<EOL>header,<EOL>ad<EOL>)<EOL>

Decrypt a message using this double ratchet session.

:param ciphertext: A bytes-like object encoding the message to decrypt.
:param header: An instance of the Header class. This should have been sent
    together with the ciphertext.
:param ad: A bytes-like object encoding the associated data to use for message
    authentication. Pass None to use the associated data set during construction.
:returns: The plaintext.

:raises AuthenticationFailedException: If checking the authentication for this
    message failed.
:raises NotInitializedException: If this double ratchet session is not yet
    initialized with a key pair, thus not prepared to decrypt an incoming message.
:raises TooManySavedMessageKeysException: If more than message_key_store_max have
    to be stored to decrypt this message.


def encryptMessage(self, message, ad = None):

if ad == None:<EOL><INDENT>ad = self.__ad<EOL><DEDENT>header = Header(<EOL>self.pub,<EOL>self.__skr.sending_chain_length,<EOL>self.__skr.previous_sending_chain_length<EOL>)<EOL>ciphertext = self.__aead.encrypt(<EOL>message,<EOL>self.__skr.nextEncryptionKey(),<EOL>self._makeAD(header, ad)<EOL>)<EOL>return {<EOL>"<STR_LIT>"     : header,<EOL>"<STR_LIT>" : ciphertext<EOL>}<EOL>

Encrypt a message using this double ratchet session.

:param message: A bytes-like object encoding the message to encrypt.
:param ad: A bytes-like object encoding the associated data to use for message
    authentication. Pass None to use the associated data set during construction.
:returns: A dictionary containing the message header and ciphertext. The header is
    required to synchronize the double ratchet of the receiving party. Send it
    along with the ciphertext.

The returned dictionary consists of two keys: "header", which includes an instance
of the Header class and "ciphertext", which includes the encrypted message encoded
as a bytes-like object.

:raises NotInitializedException: If this double ratchet session is not yet
    initialized with the other parties public key, thus not ready to encrypt a
    message to that party.


raise NotImplementedError<EOL>

Construct specific associated data for this message from the message header and
the general associated data.

:param header: An instance of the Header class.
:param ad: A bytes-like object encoding the general associated data.
:returns: A bytes-like object encoding the message-specific associated data.


def __init__(<EOL>self,<EOL>key_pair_class,<EOL>root_chain,<EOL>own_key   = None,<EOL>other_pub = None<EOL>):

super(DHRatchet, self).__init__()<EOL>self._KeyPair = key_pair_class<EOL>self.__root_chain = root_chain<EOL>if own_key:<EOL><INDENT>self.__key = own_key<EOL><DEDENT>else:<EOL><INDENT>self.__newRatchetKey()<EOL><DEDENT>self.__wrapOtherPub(other_pub)<EOL>if self.__other.pub != None:<EOL><INDENT>self.__newRootKey("<STR_LIT>")<EOL><DEDENT>

Initialize a new Diffie-Hellman ratchet.

:param key_pair_class: An implementations of the KeyPair interface which is used
    for the Diffie-Hellman key management and shared secret calculations.
:param root_chain: A KDFChain, which receives the Diffie-Hellman key exchange
    output to derive a new chain key.
:param own_key: An instance of key_pair_class holding the first key pair to
    initialize this ratchet with or None.
:param other_pub: A bytes-like object encoding the public key of the other
    Diffie-Hellman ratchet to synchronize with or None.


if self.triggersStep(other_pub):<EOL><INDENT>self.__wrapOtherPub(other_pub)<EOL>self.__newRootKey("<STR_LIT>")<EOL>self.__newRatchetKey()<EOL>self.__newRootKey("<STR_LIT>")<EOL><DEDENT>

Perform a rachted step, calculating a new shared secret from the public key and
deriving new chain keys from this secret.

New Diffie-Hellman calculations are only performed if the public key is different
from the previous one.

:param other_pub: A bytes-like object encoding the public key of the other
    Diffie-Hellman ratchet to synchronize with.


return other_pub != self.__other.pub<EOL>

:returns: A boolean indicating whether calling next with this public key would
    trigger a ratchet step.


return self.__key.pub<EOL>

:returns: A bytes-like object encoding the public key of the current internally
    managed key pair.


return self.__other.pub<EOL>

:returns: A bytes-like object encoding the public key of the other Diffie-Hellman
    ratchet to synchronize with.


raise NotImplementedError<EOL>

Perform a ratchet step using provided arguments.


return None<EOL>

:returns: A serializable Python structure, which contains all the state
    information of this object.

Use together with the fromSerialized method.
Here, "serializable" means, that the structure consists of any combination of the
following types:

* dictionaries
* lists
* strings
* integers
* floats
* booleans
* None


@classmethod<EOL><INDENT>def fromSerialized(cls, serialized, *args, **kwargs):<DEDENT>

return cls(*args, **kwargs)<EOL>

:param serialized: A serializable Python object.
:returns: Return a new instance that was set to the state that was saved into the
    serialized object.

Use together with the serialize method.
Notice: You have to pass all positional parameters required by the constructor of
the class you call fromSerialized on.


def _get_c_program(self, makefile_target_name, binary_name):

find_executable(MAKE_CMD)<EOL>if not find_executable(MAKE_CMD):<EOL><INDENT>print(<EOL>'<STR_LIT>'<EOL>% MAKE_CMD<EOL>)<EOL>exit(<NUM_LIT:1>)<EOL><DEDENT>SAMPLE_C_CODE_DIR = os.path.join(<EOL>os.path.dirname(os.path.realpath(__file__)), "<STR_LIT>"<EOL>)<EOL>binary_path = os.path.join(SAMPLE_C_CODE_DIR, binary_name)<EOL>subprocess.call(["<STR_LIT>", "<STR_LIT>"], cwd=SAMPLE_C_CODE_DIR)<EOL>subprocess.check_output(<EOL>[MAKE_CMD, makefile_target_name, "<STR_LIT>", SAMPLE_C_CODE_DIR, "<STR_LIT>"]<EOL>)<EOL>return binary_path<EOL>

build c program and return path to binary


new_index = self.index + count<EOL>if new_index > self.len:<EOL><INDENT>buf = self.raw_text[self.index :]  <EOL><DEDENT>else:<EOL><INDENT>buf = self.raw_text[self.index : new_index]<EOL><DEDENT>self.index = new_index<EOL>return buf<EOL>

Read count characters starting at self.index,
        and return those characters as a string


self.index = self.index + offset<EOL>

Advance the index of this StringStream by offset characters


start_index = self.index<EOL>while True:<EOL><INDENT>current_char = self.raw_text[self.index]<EOL>self.index += <NUM_LIT:1><EOL>if current_char in chars:<EOL><INDENT>break<EOL><DEDENT>elif self.index == self.len:<EOL><INDENT>break<EOL><DEDENT><DEDENT>return self.raw_text[start_index : self.index - <NUM_LIT:1>]<EOL>

Advance the index past specific chars
        Args chars (list): list of characters to advance past

        Return substring that was advanced past


if chars_to_remove_gdb_escape is None:<EOL><INDENT>chars_to_remove_gdb_escape = ['<STR_LIT:">']<EOL><DEDENT>buf = "<STR_LIT>"<EOL>while True:<EOL><INDENT>c = self.raw_text[self.index]<EOL>self.index += <NUM_LIT:1><EOL>logging.debug("<STR_LIT:%s>", fmt_cyan(c))<EOL>if c == "<STR_LIT:\\>":<EOL><INDENT>c2 = self.raw_text[self.index]<EOL>self.index += <NUM_LIT:1><EOL>buf += c2<EOL><DEDENT>elif c == '<STR_LIT:">':<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>buf += c<EOL><DEDENT><DEDENT>return buf<EOL>

characters that gdb escapes that should not be
        escaped by this parser


if raw_output:<EOL><INDENT>if buf:<EOL><INDENT>raw_output = b"<STR_LIT>".join([buf, raw_output])<EOL>buf = None<EOL><DEDENT>if b"<STR_LIT:\n>" not in raw_output:<EOL><INDENT>buf = raw_output<EOL>raw_output = None<EOL><DEDENT>elif not raw_output.endswith(b"<STR_LIT:\n>"):<EOL><INDENT>remainder_offset = raw_output.rindex(b"<STR_LIT:\n>") + <NUM_LIT:1><EOL>buf = raw_output[remainder_offset:]<EOL>raw_output = raw_output[:remainder_offset]<EOL><DEDENT><DEDENT>return (raw_output, buf)<EOL>

It is possible for some of gdb's output to be read before it completely finished its response.
    In that case, a partial mi response was read, which cannot be parsed into structured data.
    We want to ALWAYS parse complete mi records. To do this, we store a buffer of gdb's
    output if the output did not end in a newline.

    Args:
        raw_output: Contents of the gdb mi output
        buf (str): Buffered gdb response from the past. This is incomplete and needs to be prepended to
        gdb's next output.

    Returns:
        (raw_output, buf)


if USING_WINDOWS:<EOL><INDENT>LPDWORD = POINTER(DWORD)<EOL>PIPE_NOWAIT = wintypes.DWORD(<NUM_LIT>)<EOL>SetNamedPipeHandleState = windll.kernel32.SetNamedPipeHandleState<EOL>SetNamedPipeHandleState.argtypes = [HANDLE, LPDWORD, LPDWORD, LPDWORD]<EOL>SetNamedPipeHandleState.restype = BOOL<EOL>h = msvcrt.get_osfhandle(file_obj.fileno())<EOL>res = windll.kernel32.SetNamedPipeHandleState(h, byref(PIPE_NOWAIT), None, None)<EOL>if res == <NUM_LIT:0>:<EOL><INDENT>raise ValueError(WinError())<EOL><DEDENT><DEDENT>else:<EOL><INDENT>fcntl.fcntl(file_obj, fcntl.F_SETFL, os.O_NONBLOCK)<EOL><DEDENT>

make file object non-blocking
    Windows doesn't have the fcntl module, but someone on
    stack overflow supplied this code as an answer, and it works
    http://stackoverflow.com/a/34504971/2893090


return "<STR_LIT:U+0020>".join(quote(c) for c in self.cmd)<EOL>

Returns the shell-escaped string used to invoke the gdb subprocess.
        This is a string that can be executed directly in a shell.


if self.gdb_process:<EOL><INDENT>self.logger.debug(<EOL>"<STR_LIT>" % self.gdb_process.pid<EOL>)<EOL>self.exit()<EOL><DEDENT>self.logger.debug('<STR_LIT>' % "<STR_LIT:U+0020>".join(self.cmd))<EOL>self.gdb_process = subprocess.Popen(<EOL>self.cmd,<EOL>shell=False,<EOL>stdout=subprocess.PIPE,<EOL>stdin=subprocess.PIPE,<EOL>stderr=subprocess.PIPE,<EOL>bufsize=<NUM_LIT:0>,<EOL>)<EOL>_make_non_blocking(self.gdb_process.stdout)<EOL>_make_non_blocking(self.gdb_process.stderr)<EOL>self.stdout_fileno = self.gdb_process.stdout.fileno()<EOL>self.stderr_fileno = self.gdb_process.stderr.fileno()<EOL>self.stdin_fileno = self.gdb_process.stdin.fileno()<EOL>self.read_list = [self.stdout_fileno, self.stderr_fileno]<EOL>self.write_list = [self.stdin_fileno]<EOL>self._incomplete_output = {"<STR_LIT>": None, "<STR_LIT>": None}<EOL>return self.gdb_process.pid<EOL>

Spawn a new gdb subprocess with the arguments supplied to the object
        during initialization. If gdb subprocess already exists, terminate it before
        spanwing a new one.
        Return int: gdb process id


if not self.gdb_process:<EOL><INDENT>raise NoGdbProcessError("<STR_LIT>")<EOL><DEDENT>elif self.gdb_process.poll() is not None:<EOL><INDENT>raise NoGdbProcessError(<EOL>"<STR_LIT>"<EOL>% str(self.gdb_process.poll())<EOL>)<EOL><DEDENT>

Verify there is a process object, and that it is still running.
        Raise NoGdbProcessError if either of the above are not true.


self.verify_valid_gdb_subprocess()<EOL>if timeout_sec < <NUM_LIT:0>:<EOL><INDENT>self.logger.warning("<STR_LIT>")<EOL>timeout_sec = <NUM_LIT:0><EOL><DEDENT>if type(mi_cmd_to_write) in [str, unicode]:<EOL><INDENT>pass<EOL><DEDENT>elif type(mi_cmd_to_write) == list:<EOL><INDENT>mi_cmd_to_write = "<STR_LIT:\n>".join(mi_cmd_to_write)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError(<EOL>"<STR_LIT>"<EOL>+ str(type(mi_cmd_to_write))<EOL>)<EOL><DEDENT>self.logger.debug("<STR_LIT>", mi_cmd_to_write)<EOL>if not mi_cmd_to_write.endswith("<STR_LIT:\n>"):<EOL><INDENT>mi_cmd_to_write_nl = mi_cmd_to_write + "<STR_LIT:\n>"<EOL><DEDENT>else:<EOL><INDENT>mi_cmd_to_write_nl = mi_cmd_to_write<EOL><DEDENT>if USING_WINDOWS:<EOL><INDENT>outputready = [self.stdin_fileno]<EOL><DEDENT>else:<EOL><INDENT>_, outputready, _ = select.select([], self.write_list, [], timeout_sec)<EOL><DEDENT>for fileno in outputready:<EOL><INDENT>if fileno == self.stdin_fileno:<EOL><INDENT>self.gdb_process.stdin.write(mi_cmd_to_write_nl.encode())<EOL>self.gdb_process.stdin.flush()<EOL><DEDENT>else:<EOL><INDENT>self.logger.error("<STR_LIT>" % fileno)<EOL><DEDENT><DEDENT>if read_response is True:<EOL><INDENT>return self.get_gdb_response(<EOL>timeout_sec=timeout_sec, raise_error_on_timeout=raise_error_on_timeout<EOL>)<EOL><DEDENT>else:<EOL><INDENT>return []<EOL><DEDENT>

Write to gdb process. Block while parsing responses from gdb for a maximum of timeout_sec.

        Args:
            mi_cmd_to_write (str or list): String to write to gdb. If list, it is joined by newlines.
            timeout_sec (float): Maximum number of seconds to wait for response before exiting. Must be >= 0.
            raise_error_on_timeout (bool): If read_response is True, raise error if no response is received
            read_response (bool): Block and read response. If there is a separate thread running,
            this can be false, and the reading thread read the output.
        Returns:
            List of parsed gdb responses if read_response is True, otherwise []
        Raises:
            NoGdbProcessError if there is no gdb subprocess running
            TypeError if mi_cmd_to_write is not valid


def get_gdb_response(<EOL>self, timeout_sec=DEFAULT_GDB_TIMEOUT_SEC, raise_error_on_timeout=True<EOL>):

self.verify_valid_gdb_subprocess()<EOL>if timeout_sec < <NUM_LIT:0>:<EOL><INDENT>self.logger.warning("<STR_LIT>")<EOL>timeout_sec = <NUM_LIT:0><EOL><DEDENT>if USING_WINDOWS:<EOL><INDENT>retval = self._get_responses_windows(timeout_sec)<EOL><DEDENT>else:<EOL><INDENT>retval = self._get_responses_unix(timeout_sec)<EOL><DEDENT>if not retval and raise_error_on_timeout:<EOL><INDENT>raise GdbTimeoutError(<EOL>"<STR_LIT>" % timeout_sec<EOL>)<EOL><DEDENT>else:<EOL><INDENT>return retval<EOL><DEDENT>

Get response from GDB, and block while doing so. If GDB does not have any response ready to be read
        by timeout_sec, an exception is raised.

        Args:
            timeout_sec (float): Maximum time to wait for reponse. Must be >= 0. Will return after
            raise_error_on_timeout (bool): Whether an exception should be raised if no response was found
            after timeout_sec

        Returns:
            List of parsed GDB responses, returned from gdbmiparser.parse_response, with the
            additional key 'stream' which is either 'stdout' or 'stderr'

        Raises:
            GdbTimeoutError if response is not received within timeout_sec
            ValueError if select returned unexpected file number
            NoGdbProcessError if there is no gdb subprocess running


timeout_time_sec = time.time() + timeout_sec<EOL>responses = []<EOL>while True:<EOL><INDENT>try:<EOL><INDENT>self.gdb_process.stdout.flush()<EOL>if PYTHON3:<EOL><INDENT>raw_output = self.gdb_process.stdout.readline().replace(<EOL>b"<STR_LIT:\r>", b"<STR_LIT:\n>"<EOL>)<EOL><DEDENT>else:<EOL><INDENT>raw_output = self.gdb_process.stdout.read().replace(b"<STR_LIT:\r>", b"<STR_LIT:\n>")<EOL><DEDENT>responses += self._get_responses_list(raw_output, "<STR_LIT>")<EOL><DEDENT>except IOError:<EOL><INDENT>pass<EOL><DEDENT>try:<EOL><INDENT>self.gdb_process.stderr.flush()<EOL>if PYTHON3:<EOL><INDENT>raw_output = self.gdb_process.stderr.readline().replace(<EOL>b"<STR_LIT:\r>", b"<STR_LIT:\n>"<EOL>)<EOL><DEDENT>else:<EOL><INDENT>raw_output = self.gdb_process.stderr.read().replace(b"<STR_LIT:\r>", b"<STR_LIT:\n>")<EOL><DEDENT>responses += self._get_responses_list(raw_output, "<STR_LIT>")<EOL><DEDENT>except IOError:<EOL><INDENT>pass<EOL><DEDENT>if time.time() > timeout_time_sec:<EOL><INDENT>break<EOL><DEDENT><DEDENT>return responses<EOL>

Get responses on windows. Assume no support for select and use a while loop.


timeout_time_sec = time.time() + timeout_sec<EOL>responses = []<EOL>while True:<EOL><INDENT>select_timeout = timeout_time_sec - time.time()<EOL>if select_timeout <= <NUM_LIT:0>:<EOL><INDENT>select_timeout = <NUM_LIT:0><EOL><DEDENT>events, _, _ = select.select(self.read_list, [], [], select_timeout)<EOL>responses_list = None  <EOL>try:<EOL><INDENT>for fileno in events:<EOL><INDENT>if fileno == self.stdout_fileno:<EOL><INDENT>self.gdb_process.stdout.flush()<EOL>raw_output = self.gdb_process.stdout.read()<EOL>stream = "<STR_LIT>"<EOL><DEDENT>elif fileno == self.stderr_fileno:<EOL><INDENT>self.gdb_process.stderr.flush()<EOL>raw_output = self.gdb_process.stderr.read()<EOL>stream = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>" % fileno<EOL>)<EOL><DEDENT>responses_list = self._get_responses_list(raw_output, stream)<EOL>responses += responses_list<EOL><DEDENT><DEDENT>except IOError:  <EOL><INDENT>pass<EOL><DEDENT>if timeout_sec == <NUM_LIT:0>:  <EOL><INDENT>break<EOL><DEDENT>elif responses_list and self._allow_overwrite_timeout_times:<EOL><INDENT>timeout_time_sec = min(<EOL>time.time() + self.time_to_check_for_additional_output_sec,<EOL>timeout_time_sec,<EOL>)<EOL><DEDENT>elif time.time() > timeout_time_sec:<EOL><INDENT>break<EOL><DEDENT><DEDENT>return responses<EOL>

Get responses on unix-like system. Use select to wait for output.


def _get_responses_list(self, raw_output, stream):

responses = []<EOL>raw_output, self._incomplete_output[stream] = _buffer_incomplete_responses(<EOL>raw_output, self._incomplete_output.get(stream)<EOL>)<EOL>if not raw_output:<EOL><INDENT>return responses<EOL><DEDENT>response_list = list(<EOL>filter(lambda x: x, raw_output.decode(errors="<STR_LIT:replace>").split("<STR_LIT:\n>"))<EOL>)  <EOL>for response in response_list:<EOL><INDENT>if gdbmiparser.response_is_finished(response):<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>parsed_response = gdbmiparser.parse_response(response)<EOL>parsed_response["<STR_LIT>"] = stream<EOL>self.logger.debug("<STR_LIT:%s>", pformat(parsed_response))<EOL>responses.append(parsed_response)<EOL><DEDENT><DEDENT>return responses<EOL>

Get parsed response list from string output
        Args:
            raw_output (unicode): gdb output to parse
            stream (str): either stdout or stderr


try:<EOL><INDENT>signal = int(signal_input)<EOL><DEDENT>except Exception:<EOL><INDENT>signal = SIGNAL_NAME_TO_NUM.get(signal_input.upper())<EOL><DEDENT>if not signal:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>' % str(signal)<EOL>)<EOL><DEDENT>if self.gdb_process:<EOL><INDENT>os.kill(self.gdb_process.pid, signal)<EOL><DEDENT>else:<EOL><INDENT>raise NoGdbProcessError(<EOL>"<STR_LIT>"<EOL>)<EOL><DEDENT>

Send signal name (case insensitive) or number to gdb subprocess
        gdbmi.send_signal_to_gdb(2)  # valid
        gdbmi.send_signal_to_gdb('sigint')  # also valid
        gdbmi.send_signal_to_gdb('SIGINT')  # also valid

        raises ValueError if signal_input is invalie
        raises NoGdbProcessError if there is no gdb process to send a signal to


self.send_signal_to_gdb("<STR_LIT>")<EOL>

Send SIGINT (interrupt signal) to the gdb subprocess


if self.gdb_process:<EOL><INDENT>self.gdb_process.terminate()<EOL>self.gdb_process.communicate()<EOL><DEDENT>self.gdb_process = None<EOL>return None<EOL>

Terminate gdb process
        Returns: None


stream = StringStream(gdb_mi_text, debug=_DEBUG)<EOL>if _GDB_MI_NOTIFY_RE.match(gdb_mi_text):<EOL><INDENT>token, message, payload = _get_notify_msg_and_payload(gdb_mi_text, stream)<EOL>return {<EOL>"<STR_LIT:type>": "<STR_LIT>",<EOL>"<STR_LIT:message>": message,<EOL>"<STR_LIT>": payload,<EOL>"<STR_LIT>": token,<EOL>}<EOL><DEDENT>elif _GDB_MI_RESULT_RE.match(gdb_mi_text):<EOL><INDENT>token, message, payload = _get_result_msg_and_payload(gdb_mi_text, stream)<EOL>return {<EOL>"<STR_LIT:type>": "<STR_LIT:result>",<EOL>"<STR_LIT:message>": message,<EOL>"<STR_LIT>": payload,<EOL>"<STR_LIT>": token,<EOL>}<EOL><DEDENT>elif _GDB_MI_CONSOLE_RE.match(gdb_mi_text):<EOL><INDENT>return {<EOL>"<STR_LIT:type>": "<STR_LIT>",<EOL>"<STR_LIT:message>": None,<EOL>"<STR_LIT>": _GDB_MI_CONSOLE_RE.match(gdb_mi_text).groups()[<NUM_LIT:0>],<EOL>}<EOL><DEDENT>elif _GDB_MI_LOG_RE.match(gdb_mi_text):<EOL><INDENT>return {<EOL>"<STR_LIT:type>": "<STR_LIT>",<EOL>"<STR_LIT:message>": None,<EOL>"<STR_LIT>": _GDB_MI_LOG_RE.match(gdb_mi_text).groups()[<NUM_LIT:0>],<EOL>}<EOL><DEDENT>elif _GDB_MI_TARGET_OUTPUT_RE.match(gdb_mi_text):<EOL><INDENT>return {<EOL>"<STR_LIT:type>": "<STR_LIT:target>",<EOL>"<STR_LIT:message>": None,<EOL>"<STR_LIT>": _GDB_MI_TARGET_OUTPUT_RE.match(gdb_mi_text).groups()[<NUM_LIT:0>],<EOL>}<EOL><DEDENT>elif response_is_finished(gdb_mi_text):<EOL><INDENT>return {"<STR_LIT:type>": "<STR_LIT>", "<STR_LIT:message>": None, "<STR_LIT>": None}<EOL><DEDENT>else:<EOL><INDENT>return {"<STR_LIT:type>": "<STR_LIT>", "<STR_LIT:message>": None, "<STR_LIT>": gdb_mi_text}<EOL><DEDENT>

Parse gdb mi text and turn it into a dictionary.

    See https://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI-Stream-Records.html#GDB_002fMI-Stream-Records
    for details on types of gdb mi output.

    Args:
        gdb_mi_text (str): String output from gdb

    Returns:
        dict with the following keys:
        type (either 'notify', 'result', 'console', 'log', 'target', 'done'),
        message (str or None),
        payload (str, list, dict, or None)


if _GDB_MI_RESPONSE_FINISHED_RE.match(gdb_mi_text):<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>return False<EOL><DEDENT>

Return true if the gdb mi response is ending
    Returns: True if gdb response is finished


if expected_char_or_str != actual_char_or_str:<EOL><INDENT>print("<STR_LIT>")<EOL>pprint(expected_char_or_str)<EOL>print("<STR_LIT>")<EOL>print("<STR_LIT>")<EOL>pprint(actual_char_or_str)<EOL>raise ValueError()<EOL><DEDENT>

If values don't match, print them and raise a ValueError, otherwise,
    continue
    Raises: ValueError if argumetns do not match


token = stream.advance_past_chars(["<STR_LIT:=>", "<STR_LIT:*>"])<EOL>token = int(token) if token != "<STR_LIT>" else None<EOL>logger.debug("<STR_LIT:%s>", fmt_green("<STR_LIT>"))<EOL>message = stream.advance_past_chars(["<STR_LIT:U+002C>"])<EOL>logger.debug("<STR_LIT>")<EOL>logger.debug("<STR_LIT:%s>", fmt_green(message))<EOL>payload = _parse_dict(stream)<EOL>return token, message.strip(), payload<EOL>


groups = _GDB_MI_RESULT_RE.match(result).groups()<EOL>token = int(groups[<NUM_LIT:0>]) if groups[<NUM_LIT:0>] != "<STR_LIT>" else None<EOL>message = groups[<NUM_LIT:1>]<EOL>if groups[<NUM_LIT:2>] is None:<EOL><INDENT>payload = None<EOL><DEDENT>else:<EOL><INDENT>stream.advance_past_chars(["<STR_LIT:U+002C>"])<EOL>payload = _parse_dict(stream)<EOL><DEDENT>return token, message, payload<EOL>


obj = {}<EOL>logger.debug("<STR_LIT:%s>", fmt_green("<STR_LIT>"))<EOL>while True:<EOL><INDENT>c = stream.read(<NUM_LIT:1>)<EOL>if c in _WHITESPACE:<EOL><INDENT>pass<EOL><DEDENT>elif c in ["<STR_LIT:{>", "<STR_LIT:U+002C>"]:<EOL><INDENT>pass<EOL><DEDENT>elif c in ["<STR_LIT:}>", "<STR_LIT>"]:<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>stream.seek(-<NUM_LIT:1>)<EOL>key, val = _parse_key_val(stream)<EOL>if key in obj:<EOL><INDENT>if isinstance(obj[key], list):<EOL><INDENT>obj[key].append(val)<EOL><DEDENT>else:<EOL><INDENT>obj[key] = [obj[key], val]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>obj[key] = val<EOL><DEDENT>look_ahead_for_garbage = True<EOL>c = stream.read(<NUM_LIT:1>)<EOL>while look_ahead_for_garbage:<EOL><INDENT>if c in ["<STR_LIT:}>", "<STR_LIT:U+002C>", "<STR_LIT>"]:<EOL><INDENT>look_ahead_for_garbage = False<EOL><DEDENT>else:<EOL><INDENT>logger.debug("<STR_LIT>" + c)<EOL>c = stream.read(<NUM_LIT:1>)<EOL><DEDENT><DEDENT>stream.seek(-<NUM_LIT:1>)<EOL><DEDENT><DEDENT>logger.debug("<STR_LIT>")<EOL>logger.debug("<STR_LIT:%s>", fmt_green(obj))<EOL>return obj<EOL>

Parse dictionary, with optional starting character '{'
    return (tuple):
        Number of characters parsed from to_parse
        Parsed dictionary


logger.debug("<STR_LIT>")<EOL>key = _parse_key(stream)<EOL>val = _parse_val(stream)<EOL>logger.debug("<STR_LIT>")<EOL>logger.debug("<STR_LIT:%s>", fmt_green(key))<EOL>logger.debug("<STR_LIT:%s>", fmt_green(val))<EOL>return key, val<EOL>

Parse key, value combination
    return (tuple):
        Parsed key (string)
        Parsed value (either a string, array, or dict)


logger.debug("<STR_LIT>")<EOL>key = stream.advance_past_chars(["<STR_LIT:=>"])<EOL>logger.debug("<STR_LIT>")<EOL>logger.debug("<STR_LIT:%s>", fmt_green(key))<EOL>return key<EOL>

Parse key, value combination
    returns :
        Parsed key (string)


logger.debug("<STR_LIT>")<EOL>while True:<EOL><INDENT>c = stream.read(<NUM_LIT:1>)<EOL>if c == "<STR_LIT:{>":<EOL><INDENT>val = _parse_dict(stream)<EOL>break<EOL><DEDENT>elif c == "<STR_LIT:[>":<EOL><INDENT>val = _parse_array(stream)<EOL>break<EOL><DEDENT>elif c == '<STR_LIT:">':<EOL><INDENT>val = stream.advance_past_string_with_gdb_escapes()<EOL>break<EOL><DEDENT>elif _DEBUG:<EOL><INDENT>raise ValueError("<STR_LIT>" % c)<EOL><DEDENT>else:<EOL><INDENT>print(<EOL>'<STR_LIT>'<EOL>% c<EOL>)<EOL>val = "<STR_LIT>"  <EOL><DEDENT><DEDENT>logger.debug("<STR_LIT>")<EOL>logger.debug("<STR_LIT:%s>", fmt_green(val))<EOL>return val<EOL>

Parse value from string
    returns:
        Parsed value (either a string, array, or dict)


logger.debug("<STR_LIT>")<EOL>arr = []<EOL>while True:<EOL><INDENT>c = stream.read(<NUM_LIT:1>)<EOL>if c in _GDB_MI_VALUE_START_CHARS:<EOL><INDENT>stream.seek(-<NUM_LIT:1>)<EOL>val = _parse_val(stream)<EOL>arr.append(val)<EOL><DEDENT>elif c in _WHITESPACE:<EOL><INDENT>pass<EOL><DEDENT>elif c == "<STR_LIT:U+002C>":<EOL><INDENT>pass<EOL><DEDENT>elif c == "<STR_LIT:]>":<EOL><INDENT>break<EOL><DEDENT><DEDENT>logger.debug("<STR_LIT>")<EOL>logger.debug("<STR_LIT:%s>", fmt_green(arr))<EOL>return arr<EOL>

Parse an array, stream should be passed the initial [
    returns:
        Parsed array


<EOL>find_executable(MAKE_CMD)<EOL>if not find_executable(MAKE_CMD):<EOL><INDENT>print(<EOL>'<STR_LIT>'<EOL>% MAKE_CMD<EOL>)<EOL>exit(<NUM_LIT:1>)<EOL><DEDENT>subprocess.check_output([MAKE_CMD, "<STR_LIT>", SAMPLE_C_CODE_DIR, "<STR_LIT>"])<EOL>gdbmi = GdbController(verbose=verbose)<EOL>responses = gdbmi.write("<STR_LIT>" % SAMPLE_C_BINARY)<EOL>responses = gdbmi.write("<STR_LIT>")<EOL>responses = gdbmi.write("<STR_LIT>")<EOL>responses = gdbmi.write("<STR_LIT>")<EOL>responses = gdbmi.write("<STR_LIT>")<EOL>responses = gdbmi.write("<STR_LIT>")<EOL>responses = gdbmi.write("<STR_LIT>")  <EOL>gdbmi.exit()<EOL>

Build and debug an application programatically

    For a list of GDB MI commands, see https://www.sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI.html


def ioctl_GWINSZ(fd):<EOL><INDENT>"""<STR_LIT>"""<EOL>try:                               <EOL><INDENT>rows_cols = struct.unpack('<STR_LIT>', fcntl.ioctl(fd, termios.TIOCGWINSZ, '<STR_LIT>')) <EOL>return tuple(reversed(rows_cols))<EOL><DEDENT>except:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>cr = ioctl_GWINSZ(<NUM_LIT:0>) or ioctl_GWINSZ(<NUM_LIT:1>) or ioctl_GWINSZ(<NUM_LIT:2>)<EOL>if not cr:<EOL><INDENT>try:<EOL><INDENT>fd = os.open(os.ctermid(), os.O_RDONLY)<EOL>cr = ioctl_GWINSZ(fd)<EOL>os.close(fd)<EOL><DEDENT>except:<EOL><INDENT>cr = (int(os.environ.get('<STR_LIT>', default_cols)), <EOL>int(os.environ.get('<STR_LIT>', default_rows)))<EOL><DEDENT><DEDENT>return cr<EOL>

Return current terminal size (cols, rows) or a default if detect fails.

    This snippet comes from color ls by Chuck Blake:
    http://pdos.csail.mit.edu/~cblake/cls/cls.py


def get_metainfo(scriptfile,<EOL>keywords=['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:version>', '<STR_LIT>'],<EOL>special={},<EOL>first_line_pattern=r'<STR_LIT>',<EOL>keyword_pattern_template=r'<STR_LIT>',<EOL>prettify = lambda kw: kw.capitalize().replace('<STR_LIT:_>', '<STR_LIT:U+0020>')):

patterns = dict((kw, re.compile(keyword_pattern_template % dict(pretty=prettify(kw), keyword=kw))) for kw in keywords)<EOL>patterns.update(special)<EOL>metainfo = dict()<EOL>if scriptfile[-<NUM_LIT:4>:] in ['<STR_LIT>', '<STR_LIT>']: <EOL><INDENT>scriptfile = scriptfile[:-<NUM_LIT:1>]<EOL><DEDENT>script = open(scriptfile)<EOL>closer = '<STR_LIT>'<EOL>for line in script:<EOL><INDENT>line = line.strip()<EOL>if not line or line.startswith('<STR_LIT:#>'):<EOL><INDENT>continue<EOL><DEDENT>if line[:<NUM_LIT:3>] in ('<STR_LIT>', "<STR_LIT>"):<EOL><INDENT>closer = line[:<NUM_LIT:3>] <EOL>break<EOL><DEDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if not line:<EOL><INDENT>for line in script:<EOL><INDENT>line = line.strip()<EOL>if line:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>g = re.search(first_line_pattern, line[<NUM_LIT:3>:]).groupdict()<EOL>metainfo['<STR_LIT>'] = g['<STR_LIT>']<EOL>if g['<STR_LIT:version>']:<EOL><INDENT>metainfo['<STR_LIT:version>'] = g['<STR_LIT:version>']<EOL><DEDENT>for line in script:<EOL><INDENT>if line.strip().startswith(closer):<EOL><INDENT>break<EOL><DEDENT>for keyword, pattern in list(patterns.items()):<EOL><INDENT>m = pattern.search(line)<EOL>if m:<EOL><INDENT>metainfo[keyword] = m.group(keyword)<EOL>break<EOL><DEDENT><DEDENT>if line.strip() and not '<STR_LIT:description>' in metainfo:<EOL><INDENT>metainfo['<STR_LIT:description>'] = line.strip()<EOL><DEDENT><DEDENT>return metainfo<EOL>

Dumb helper for pulling metainfo from a script __doc__ string.

    Returns a metainfo dict with command, description, progname and the given
    keywords (if present).

    This function will only make minimal efforts to succeed. If you need
    anything else: roll your own.

    The docstring needs to be multiline and the closing quotes need to be first
    on a line, optionally preceeded by whitespace.

    The first non-whitespace line is re.search'ed using first_line_pattern,
    default e.g (version optional, contains no whitespace): PROGNAME [vVERSION]

    The next non-whitespace, non-keyword line is expected to be the program
    description.

    The following lines are re.search'ed against a keyword:pattern dict which
    is constructed using
    keyword_pattern % dict(pretty=prettify(keyword), keyword=keyword)
    Default prettify is keyword.capitalize().replace('_', ' '). Example,
    for the keyword "licence" will match the following line:
    License: The MIT license.
    and set the license metainfo to "The MIT license.".

    Any keyword:pattern pairs that need special treatment can be supplied with
    special.


super(DocParser, self).__init__(untagged=None, tabsize=tabsize)<EOL>self.addblock('<STR_LIT:title>', SingleParagraph)<EOL>self.addblock('<STR_LIT:description>', SingleParagraph)<EOL>self.addblock('<STR_LIT>', SingleParagraph)<EOL>self.addblock('<STR_LIT>', IndentedParagraphs)<EOL>self.addblock('<STR_LIT>', IndentedParagraphs)<EOL>self.addblock('<STR_LIT>', IndentedParagraphs)<EOL>self.addblock('<STR_LIT>', IndentedParagraphs)<EOL>self.addblock('<STR_LIT>', IndentedParagraphs)<EOL>self.addblock('<STR_LIT>', IndentedParagraphs)<EOL>self.addblock('<STR_LIT>', IndentedParagraphs)<EOL>self.addblock('<STR_LIT>', IndentedParagraphs)<EOL>self.addblock('<STR_LIT>', IndentedParagraphs)<EOL>self.addblock('<STR_LIT>', SingleParagraph, True, '<STR_LIT>')<EOL>self.addblock('<STR_LIT>', IndentedParagraphs, True, '<STR_LIT>')<EOL>

tabsize > 0 replaces tab characters with that many spaces. Set <= 0 to
disable.


return option.strip()<EOL>


unused = set([])<EOL>for section in _list(sections):<EOL><INDENT>if not self.has_section(section):<EOL><INDENT>continue<EOL><DEDENT>options = self.options(section)<EOL>raw_values = [self.get(section, option, raw=True) for option in options]<EOL>for option in options:<EOL><INDENT>formatter = "<STR_LIT>" + option + "<STR_LIT>"<EOL>for raw_value in raw_values:<EOL><INDENT>if formatter in raw_value:<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>unused.add(option) <EOL><DEDENT><DEDENT>return list(unused)<EOL><DEDENT>

Lists options that have not been used to format other values in
        their sections.

        Good for finding out if the user has misspelled any of the options.


Consume and process arguments and store the result.

        Override in subclasses to do actual work. Subclasses may take other
        params.


def parsestr(self, argsstr, usedname, location):

Parse a string lexically and store the result.

        Override in subclasses to do actual work. Subclasses may take other
        params.


def __init__(self, <EOL>name, <EOL>format, <EOL>abbreviation=None, <EOL>default=_UNSET,<EOL>recurring=False, <EOL>reserved=False,<EOL>docs=None):

super(Option, self).__init__(name, format, recurring, docs)<EOL>if default is _UNSET:<EOL><INDENT>if recurring:<EOL><INDENT>default = []<EOL><DEDENT>else:<EOL><INDENT>default = self.format.default<EOL><DEDENT><DEDENT>self.value = default<EOL>if abbreviation and len(abbreviation) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if abbreviation == '<STR_LIT:->':<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>self.abbreviation = abbreviation<EOL>self.reserved = reserved<EOL>self.location = "<STR_LIT>"<EOL>if docs is None:<EOL><INDENT>if recurring:<EOL><INDENT>self.docs += "<STR_LIT>"<EOL><DEDENT>if reserved:<EOL><INDENT>self.docs += "<STR_LIT>"<EOL><DEDENT><DEDENT>

name is the long option name. Used in settings files and with a '--'
prefix on the command line. This should be as brief and descriptive
as possible. Must be at least length 2, must start and end with a
letter or number and may contain letters, numbers, underscores and
hyphens in between.

format should be a formats.Format subclass or instance, and determines
how this option behaves and what arguments it may require. The name of
one of the formats in the tui.formats module is also accepted.

abbreviation (if given) is the one-character abbreviation of the option
name. Can be anything other than a literal '-' character. Used with a
'-' prefix on the command line.

default is the option's default value. Omit to use format.default, or
an empty list if the option is recurring.

If reserved is true the option will be reserved for command line use.

docs is user friendly help text for the option. None means use
format.docs and add note on recurrence or command line reservation.

If recurring is true, the option allowed to occur several times on the
command line. .value will then be a list of all parsed values, in
parsing order.


def parse(self, argv, usedname, location):

try:<EOL><INDENT>value = self.format.parse(argv)<EOL><DEDENT>except formats.BadNumberOfArguments as e:<EOL><INDENT>raise BadNumberOfArguments(usedname, e.required, e.supplied)<EOL><DEDENT>except formats.BadArgument as e:<EOL><INDENT>raise BadArgument(usedname, e.argument, e.message)<EOL><DEDENT>if self.recurring:<EOL><INDENT>self.value.append(value)<EOL><DEDENT>else:<EOL><INDENT>self.value = value<EOL><DEDENT>self.location = location<EOL>

Consume and process arguments and store the result.
        ARGS:
        argv <list str>:
            The argument list to parse.
        usedname <str>:
            The string used by the user to invoke the option.
        location <str>:
            A user friendly sring describing where the parser got this
            data from.


try:<EOL><INDENT>value = self.format.parsestr(argsstr)<EOL><DEDENT>except formats.BadNumberOfArguments as e:<EOL><INDENT>raise BadNumberOfArguments(usedname, e.required, e.supplied)<EOL><DEDENT>except formats.BadArgument as e:<EOL><INDENT>raise BadArgument(usedname, e.argument, e.message)<EOL><DEDENT>if self.recurring:<EOL><INDENT>self.value.append(value)<EOL><DEDENT>else:<EOL><INDENT>self.value = value<EOL><DEDENT>self.location = location<EOL>

Parse a string lexically and store the result.
        ARGS:
        argsstr <str>:
            The string to parse.
        usedname <str>:
            The string used by the user to invoke the option.
        location <str>:
            A user friendly sring describing where the parser got this
            data from.


def __init__(self, name, format, recurring=False, optional=False, docs='<STR_LIT>', displayname=None):

super(PositionalArgument, self).__init__(name, format, recurring, docs)<EOL>self.optional = optional<EOL>self._value = None<EOL>self.displayname = displayname or name.upper().replace('<STR_LIT:->', '<STR_LIT:_>')<EOL>if docs is None:<EOL><INDENT>if recurring:<EOL><INDENT>self.docs += "<STR_LIT>"<EOL><DEDENT>if optional:<EOL><INDENT>self.docs += "<STR_LIT>"<EOL><DEDENT><DEDENT>

name is the name of the positional argument. Must be at least length 2,
must start and end with a letter or number and may contain letters,
numbers, underscores and hyphens in between.

format should be a formats.Format subclass or instance, and determines
how this positional argument behaves and what arguments it may require.
The name of one of the formats in the tui.formats module is also
accepted.

docs is user friendly help text for the option. None means use
format.docs.

If recurring is true, this positional argument can be used multiple
times on the command line, and .parse() will consume all arguments fed
to it.

If optional is true, the user is permitted to omit this argument from
the command line?


if not argv and self.optional:<EOL><INDENT>self.value = [] if self.recurring else None<EOL>return<EOL><DEDENT>try:<EOL><INDENT>value = self.format.parse(argv)<EOL>if not self.recurring:<EOL><INDENT>self.value = value<EOL>return<EOL><DEDENT>self.value = [value]<EOL>while argv:<EOL><INDENT>self.value.append(self.format.parse(argv))<EOL><DEDENT><DEDENT>except formats.BadNumberOfArguments as e:<EOL><INDENT>raise BadNumberOfArguments(self.displayname, e.required, e.given)<EOL><DEDENT>except formats.BadArgument as e:<EOL><INDENT>raise BadArgument(self.displayname, e.argument, e.details)<EOL><DEDENT>

Consume and process arguments and store the result.

        argv is the list of arguments to parse (will be modified).

        Recurring PositionalArgumants get a list as .value.

        Optional PositionalArguments that do not get any arguments to parse get
        None as .value, or [] if recurring.


params = locals()<EOL>self.options = dict()<EOL>self.option_order = []<EOL>self.abbreviations = dict()<EOL>self.positional_args = []<EOL>for p in parameters:<EOL><INDENT>if isinstance(p, Option):<EOL><INDENT>self._add_option(p)<EOL><DEDENT>elif isinstance(p, PositionalArgument):<EOL><INDENT>self._add_positional_argument(p)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT><DEDENT>self.basic_option_names = dict()<EOL>for optiontype in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:version>']:<EOL><INDENT>option = params[optiontype + '<STR_LIT>']<EOL>if not option:<EOL><INDENT>continue<EOL><DEDENT>if not isinstance(option, Option):<EOL><INDENT>option = option()<EOL><DEDENT>self._add_option(option)<EOL>self.basic_option_names[optiontype] = option.name<EOL><DEDENT>if isinstance(install_dir, type(sys)):<EOL><INDENT>install_dir = install_dir.__file__<EOL><DEDENT>if isinstance(install_dir, str) and os.path.isfile(install_dir):<EOL><INDENT>install_dir = os.path.dirname(install_dir)<EOL><DEDENT>self.docvars = dict((s, params[s]) for s in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT:version>'])<EOL>if command is None:<EOL><INDENT>command = os.path.basename(sys.argv[<NUM_LIT:0>])<EOL><DEDENT>self.docvars['<STR_LIT>'] = command<EOL>command_base = command<EOL>if command.endswith('<STR_LIT>'):<EOL><INDENT>command_base = command[:-<NUM_LIT:3>]<EOL><DEDENT>self.docs = dict(title=_docs(title, self.docvars),<EOL>usage=_docs(usage, self.docvars),<EOL>files=_docs(filedocs, self.docvars))<EOL>if filedocs is None:<EOL><INDENT>filedocs = dict()<EOL><DEDENT>self.docs['<STR_LIT>'] = filedocs<EOL>self.docs.update((name, _list(params[name])) for name in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:description>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'])<EOL>self.ignore = _list(ignore)<EOL>if docsfiles is None:<EOL><INDENT>if install_dir:<EOL><INDENT>if docsfilenames is None:<EOL><INDENT>docsfilenames = [os.path.basename(install_dir) + '<STR_LIT>']<EOL>if command_base:<EOL><INDENT>docsfilenames.append(command_base + '<STR_LIT>')<EOL><DEDENT><DEDENT>docsfiles = [os.path.join(install_dir, f) for f in docsfilenames]<EOL><DEDENT><DEDENT>elif docsfilenames:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.read_docs(docsfiles)<EOL>self.sections = _list(sections, [command_base])<EOL>if configfiles is None:<EOL><INDENT>dirname = command_base<EOL>if isinstance(configdirs, str):<EOL><INDENT>dirname = configdirs<EOL>configdirs = None<EOL><DEDENT>if configdirs is None:<EOL><INDENT>configdirs = []<EOL>if install_dir is not None:<EOL><INDENT>configdirs.append(install_dir)<EOL><DEDENT>configdirs += [os.path.join('<STR_LIT>', dirname), <EOL>os.path.expanduser(os.path.join('<STR_LIT>', '<STR_LIT:.>' + dirname))]<EOL><DEDENT>configfilenames = _list(configfilenames, [command_base + '<STR_LIT>'])<EOL>configfiles = [os.path.join(d, f) for d in configdirs for f in configfilenames]<EOL><DEDENT>elif configdirs or configfilenames:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>elif isinstance(configfiles, str):<EOL><INDENT>configfiles = [configfiles]<EOL><DEDENT>self.configfiles = configfiles<EOL>if self.configfiles:<EOL><INDENT>self.addconfigfiledocs()<EOL><DEDENT>if not width:<EOL><INDENT>width = get_terminal_size()[<NUM_LIT:0>]<EOL><DEDENT>self.width = width<EOL>if launch:<EOL><INDENT>self.launch(argv)<EOL><DEDENT>

Many of the metainfo parameters (author, progname...) should already
be present in the program docstring if you're coding by the book. You
can avoid repeating them by doing something like:
ui = tui(..., **get_metainfo(__file__)).
The parameters that will be available using the above incantation are
marked with asterixes* below.

.readdocs() can read most user documentaion from external docsfiles,
so there is typically no need to supply that on instantiation. See the
docsfile parameter. Parameters that can be set using .readdocs() are
marked with percent% below.

All parameters that take a list of indented paragraphs can use docvars.
They also accept just a string which, is then taken as a list of one
paragraph. None means there is no such documentation.

All parameters that take an Option also accept factory functions for
Option objects (like a class, or a function like help_option). None
means don't add such an option. Note however that just adding the
helpoption, longhelpoption, versionoption and settingsoption at
instantiation will not activate their documented functionality. Use
e.g. .start() for this.

parameters is a list of Options and PositionalArguments to be added
to the user interface.

version* is the current program version string. Although you *can* set
this using get_metainfo, you typically want to pass __version__ here
instead. Available as a docvar.

install_dir (if given) will be searched for configfiles and docsfiles.
Use containing dir if given a python package or a path to a file (e.g.
mypackage or __file__).

argv is the list of command line arguments to use (see launch just
below). None means use a copy of sys.argv.

If launch is True (default) then .launch(argv) will be called after
initiallization.

*progname is the user friendly name of the program. Available as a
docvar.

*author is who made the program. Available as a docvar.

title is what to put in the title line on help pages. Can use docvars.

*%description is a oneliner describing the program. Can use docvars.

*%contact is contact information as a list of indented paragraphs.

*%website is the url to the website of the program. Can be given as a
list of indented paragraphs if needed.

*%download is where the program can be downloaded. Can be given as a
list of indented paragraphs if needed.

*%git is where the source can be accessed by git. Can be given as a
list of indented paragraphs if needed.

*%subversion is where the source can be accessed by svn. Can be given
as a list of indented paragraphs if needed.

*%copyright is copyright information as a list of indented paragraphs.

*%license is brief information on the licensing conditions of the
program. Can be given as a list of indented paragraphs if needed.

*command* is how to execute the program. Available as a docvar. None
means use the basename of sys.argv[0].

usage is user friendly help on how to execute the program. Can
use docvars. None means auto generate from options and positional
arguments.

%general is general program documentation as a list of indented
paragraphs. Typically displayed on the normal help page.

%additional is additional program documentation as a list of indented
paragraphs. Typically only displayed in verbose help (longhelp).

%note is the same as additional, only with a different label.

%filedocs is documentation on files used by the program, as a dict of
file names and indented paragraphs. Typically only displayed in
verbose help (longhelp). Keys and values may use docvars. None means
no such documentation.

docsfilenames is a list of basenames of docsfiles and is only useful
together with install_dir. A str means a list of one item. None means
[basename(install_dir) + 'docs' (if given), command + '.docs'].
Do not use together with the docsfiles parameter.

docsfiles is a list of paths to potential DocParser docsfiles; parse if
present, in the given order. None means look for each docsfilename in
install_dir (if given). A str means a list of one item. Note that no
docs will be read on instantation. Use .readdocs() (or .launch()) for
that.

width is the maximum allowed width for help text. 0 means try to guess
the terminal width, and use 79 if that fails.

configdirs is a list of paths to directories to search for configfiles.
None means [install_dir (if given), '/etc/' + command, '~/.' + command].
A str means the same, but with this string replacing command. Do not
use together with the configfiles parameter.

configfilenames is a list of basenames of configfiles. A str means a
list of one item. None means [command + '.conf']. Do not use together
with the configfiles parameter.

configfiles is a list of config files to parse. None means try each
configfilename in the given order, in each of the configdirs in the
given order. configfiles are parsed incrementally by .parsefiles() (or
.launch()). A str means a list of one item. [] means no configfiles.

sections is a list of sections to read in config files. A str means a
list of one item. None means [command]. The DEFAULT section (note
uppercase) is always read, and it is read as if its contents were
copied to the beginning of all other sections.

ignore is a list of option and positional argument names that should be
ignored if encountered in configfiles or docsfiles. Any other unknown
names will raise an error. This is primarily to help users catch
spelling mistakes. This feature is useful for example if you are using
the same files for several programs in a suite, where some parameters
are shared and you want to ignore the others. Set to None to ignore all
all unknown (not recommended).

options can be used to supply a preconfigured option dictionary, if you
for some reason prefer this to .makeoption(). tui will not check this
for you. See also option_order and abbreviations.

option_order determines the order in which the options will be shown in
help texts.

abbreviations is a abbreviation:option dict, similar to options.

positional_args can be used to supply a list of preconfigured positional
arguments, if you for some reason prefer this to .makeposarg(). tui will
not check this for you. See also positional_arg_names.

positional_arg_names are the display names for positional arguments in
help texts.

helpoption adds an option that lets the user request help in the usual
way. Default is '--help' or '-h', option reserved for command line use.
None means don't add such an option.

longhelpoption adds an option that lets the user request verbose help.
Default is '--HELP' or '-H', option reserved for command line use. None
means don't add such an option.

versionoption adds an option that lets the user print the version
string. Default is '--version' or '-V', option reserved for command
line use). None means don't add such an option.

settingsoption adds an option that lets the user print a brief summary
of program settings. Default is '--settings' or '-S', option reserved
for command line use). None means don't add such an option.


return iter(list(self.keys()))<EOL>


return key in list(self.keys())<EOL>


return self.getparam(key).value<EOL>


self.getparam(key).value = value<EOL>

Shorthand for .getparam(key).value = value.

        Useful when doing additional modifications after tui is done parsing.


return list(self.options.keys()) + [p.name for p in self.positional_args]<EOL>

List names of options and positional arguments.


return list(self.options.values()) + [p.value for p in self.positional_args]<EOL>

List values of options and positional arguments.


return [(p.name, p.value) for p in list(self.options.values()) + self.positional_args]<EOL>


return dict(list(self.items()))<EOL>

Return a name:value dict of all options and positional arguments.

        Useful when you absolutely need a dict, e.g: do_stuff(**ui.dict()).


try:<EOL><INDENT>return self.options[key]<EOL><DEDENT>except:<EOL><INDENT>pass<EOL><DEDENT>for posarg in self.positional_args:<EOL><INDENT>if posarg.name == key:<EOL><INDENT>return posarg<EOL><DEDENT><DEDENT>try:<EOL><INDENT>return self.abbreviations[key[<NUM_LIT:1>:]]<EOL><DEDENT>except:<EOL><INDENT>raise KeyError('<STR_LIT>')<EOL><DEDENT>

Get option or positional argument, by name, index or abbreviation.

        Abbreviations must be prefixed by a '-' character, like so: ui['-a']


if option.name in self.options:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if option.abbreviation in self.abbreviations:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if option.name in [arg.name for arg in self.positional_args]:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>self.options[option.name] = option<EOL>if option.abbreviation:<EOL><INDENT>self.abbreviations[option.abbreviation] = option<EOL><DEDENT>self.option_order.append(option.name)<EOL>

Add an Option object to the user interface.


if self.positional_args:<EOL><INDENT>if self.positional_args[-<NUM_LIT:1>].recurring:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if self.positional_args[-<NUM_LIT:1>].optional and not posarg.optional:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>self.positional_args.append(posarg)<EOL>

Append a positional argument to the user interface.

        Optional positional arguments must be added after the required ones.
        The user interface can have at most one recurring positional argument,
        and if present, that argument must be the last one.


updates = DocParser()<EOL>for docsfile in _list(docsfiles):<EOL><INDENT>if os.path.isfile(docsfile):<EOL><INDENT>updates.parse(docsfile)<EOL><DEDENT><DEDENT>self.docs.update((k, _docs(updates[k], self.docvars)) for k in self.docs if updates.blocks[k])<EOL>for name, text in list(updates['<STR_LIT>'].items()):<EOL><INDENT>if name in self:<EOL><INDENT>self.getparam(name).docs = text[<NUM_LIT:0>] % self.docvars<EOL><DEDENT>elif name not in self.ignore:<EOL><INDENT>raise ValueError("<STR_LIT>" % name)<EOL><DEDENT><DEDENT>

Read program documentation from a DocParser compatible file.

        docsfiles is a list of paths to potential docsfiles: parse if present.
        A string is taken as a list of one item.


def parse_files(self, files=None, sections=None):

files = _list(files, self.configfiles)<EOL>sections = _list(sections, self.sections)<EOL>for file in files:<EOL><INDENT>parser = StrictConfigParser()<EOL>parser.read(file)<EOL>for section in sections:<EOL><INDENT>if not parser.has_section(section):<EOL><INDENT>continue<EOL><DEDENT>for unused in parser.unusedoptions(section):<EOL><INDENT>if unused not in self.options and unused not in self.ignore: <EOL><INDENT>templ = "<STR_LIT>"<EOL>raise InvalidOption(unused, message=templ % (unused, section, file))<EOL><DEDENT><DEDENT>for name in parser.options(section):          <EOL><INDENT>if name in self.options:<EOL><INDENT>if self.options[name].reserved:<EOL><INDENT>templ = "<STR_LIT>"<EOL>raise ReservedOptionError(message=templ % (unused, section, file))<EOL><DEDENT>value = parser.get(section, name)<EOL>self.options[name].parsestr(value, name, '<STR_LIT>' % (file, section))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>

Parse configfiles.
        files <list str>, <str> or None:
            What files to parse. None means use self.configfiles. New
            values override old ones. A string value will be interpreted
            as a list of one item.
        sections <list str>, <str> or None:
            Which sections to parse from the files. None means use
            self.sections. A string value will be interpreted as a list
            of one item. The [DEFAULT]
            section is always read, and it is read as if its contents were
            copied to the beginning of all other sections.


def _parse_options(self, argv, location):

observed = []<EOL>while argv:<EOL><INDENT>if argv[<NUM_LIT:0>].startswith('<STR_LIT>'):<EOL><INDENT>name = argv.pop(<NUM_LIT:0>)[<NUM_LIT:2>:]<EOL>if not name:<EOL><INDENT>break<EOL><DEDENT>if name not in self.options:<EOL><INDENT>raise InvalidOption(name)<EOL><DEDENT>option = self.options[name]<EOL>if not option.recurring:<EOL><INDENT>if option in observed:<EOL><INDENT>raise OptionRecurrenceError(name)<EOL><DEDENT>observed.append(option)<EOL><DEDENT>option.parse(argv, name, location)<EOL><DEDENT>elif argv[<NUM_LIT:0>].startswith('<STR_LIT:->'):<EOL><INDENT>if argv[<NUM_LIT:0>] == '<STR_LIT:->':<EOL><INDENT>break<EOL><DEDENT>block = argv.pop(<NUM_LIT:0>)[<NUM_LIT:1>:]<EOL>for abbreviation in block[:-<NUM_LIT:1>]:<EOL><INDENT>if self.abbreviations[abbreviation].nargs != <NUM_LIT:0>:<EOL><INDENT>raise BadAbbreviationBlock(abbreviation, block, "<STR_LIT>")<EOL><DEDENT><DEDENT>for abbreviation in block:<EOL><INDENT>option = self.abbreviations[abbreviation]<EOL>if not option.recurring:<EOL><INDENT>if option in observed:<EOL><INDENT>raise OptionRecurrenceError(option.name)<EOL><DEDENT>observed.append(option)<EOL><DEDENT>option.parse(argv, '<STR_LIT:->' + abbreviation, location)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>break<EOL><DEDENT><DEDENT>

Parse the options part of an argument list.
        IN:
        lsArgs <list str>:
            List of arguments. Will be altered.
        location <str>:
            A user friendly string describing where this data came from.


for posarg in self.positional_args:<EOL><INDENT>posarg.parse(argv)<EOL><DEDENT>if argv:<EOL><INDENT>if None in [p.nargs for p in self.positional_args]:<EOL><INDENT>msg = '<STR_LIT>'<EOL>plural_s = len(argv) > <NUM_LIT:1> and '<STR_LIT:s>' or '<STR_LIT>'<EOL>raise BadNumberOfArguments(message=msg % (len(argv), plural_s))<EOL><DEDENT>msg = '<STR_LIT>'<EOL>required = len([p.nargs for p in self.positional_args])<EOL>raise BadNumberOfArguments(message=msg % (required, required + len(argv)))<EOL><DEDENT>

Parse the positional arguments part of an argument list.
        argv <list str>:
            List of arguments. Will be altered.


def parse_argv(self, argv=None, location='<STR_LIT>'):

if argv is None:<EOL><INDENT>argv = list(sys.argv)<EOL><DEDENT>argv.pop(<NUM_LIT:0>)<EOL>self._parse_options(argv, location)<EOL>self._parse_positional_arguments(argv)<EOL>

Parse command line arguments.

        args <list str> or None:
            The argument list to parse. None means use a copy of sys.argv. argv[0] is
            ignored.
        location = '' <str>:
            A user friendly string describing where the parser got this
            data from. '' means use "Command line." if args == None, and
            "Builtin default." otherwise.


def optionhelp(self, indent=<NUM_LIT:0>, maxindent=<NUM_LIT>, width=<NUM_LIT>):

def makelabels(option):<EOL><INDENT>labels = '<STR_LIT>' % (indent, '<STR_LIT:U+0020>', option.name)<EOL>if option.abbreviation:<EOL><INDENT>labels += '<STR_LIT>' + option.abbreviation<EOL><DEDENT>return labels + '<STR_LIT>'<EOL><DEDENT>docs = []<EOL>helpindent = _autoindent([makelabels(o) for o in list(self.options.values())], indent, maxindent)<EOL>for name in self.option_order:<EOL><INDENT>option = self.options[name]<EOL>labels = makelabels(option)<EOL>helpstring = "<STR_LIT>" % (option.formatname, option.strvalue, option.docs)<EOL>wrapped = self._wrap_labelled(labels, helpstring, helpindent, width)<EOL>docs.extend(wrapped)<EOL><DEDENT>return '<STR_LIT:\n>'.join(docs)<EOL>

Return user friendly help on program options.


def posarghelp(self, indent=<NUM_LIT:0>, maxindent=<NUM_LIT>, width=<NUM_LIT>):

docs = []<EOL>makelabel = lambda posarg: '<STR_LIT:U+0020>' * indent + posarg.displayname + '<STR_LIT>'<EOL>helpindent = _autoindent([makelabel(p) for p in self.positional_args], indent, maxindent)<EOL>for posarg in self.positional_args:<EOL><INDENT>label = makelabel(posarg)<EOL>text = posarg.formatname + '<STR_LIT>' + posarg.docs<EOL>wrapped = self._wrap_labelled(label, text, helpindent, width)<EOL>docs.extend(wrapped)<EOL><DEDENT>return '<STR_LIT:\n>'.join(docs)<EOL>

Return user friendly help on positional arguments in the program.


if usage is None:<EOL><INDENT>usage = self.docs['<STR_LIT>']<EOL><DEDENT>if usage is not None:<EOL><INDENT>return usage[<NUM_LIT:0>] % self.docvars<EOL><DEDENT>usage = self.docvars['<STR_LIT>']<EOL>if self.basic_option_names.get('<STR_LIT>'):<EOL><INDENT>usage += '<STR_LIT>' % self.basic_option_names.get('<STR_LIT>')<EOL><DEDENT>if self.options:<EOL><INDENT>usage += '<STR_LIT>'<EOL><DEDENT>optional = <NUM_LIT:0><EOL>for posarg in self.positional_args:<EOL><INDENT>usage += '<STR_LIT:U+0020>'<EOL>if posarg.optional:<EOL><INDENT>usage += "<STR_LIT:[>" <EOL>optional += <NUM_LIT:1><EOL><DEDENT>usage += posarg.displayname<EOL>if posarg.recurring:<EOL><INDENT>usage += '<STR_LIT>' % posarg.displayname<EOL><DEDENT><DEDENT>usage += '<STR_LIT:]>' * optional<EOL>return usage<EOL>

Return a formatted usage string.

        If usage is None, use self.docs['usage'], and if that is also None,
        generate one.


def _wrap(self, text, indent=<NUM_LIT:0>, width=<NUM_LIT:0>):

text = _list(text)<EOL>if not width:<EOL><INDENT>width = self.width<EOL><DEDENT>paragraph = text[<NUM_LIT:0>].lstrip()<EOL>s = '<STR_LIT:U+0020>' * (len(text[<NUM_LIT:0>]) - len(paragraph) + indent)<EOL>wrapped = textwrap.wrap(paragraph.strip(), width, initial_indent=s, subsequent_indent=s)<EOL>return '<STR_LIT:\n>'.join(wrapped)<EOL>

Textwrap an indented paragraph.
        ARGS:
        width = 0 <int>:
            Maximum allowed page width. 0 means use default from
            self.iMaxHelpWidth.


def _wraptext(self, text, indent=<NUM_LIT:0>, width=<NUM_LIT:0>):

return '<STR_LIT:\n>'.join(self._wrap(par, indent, width) for par in text)<EOL>

Shorthand for '\n'.join(self._wrap(par, indent, width) for par in text).


def _wrapusage(self, usage=None, width=<NUM_LIT:0>):

if not width:<EOL><INDENT>width = self.width<EOL><DEDENT>return textwrap.fill('<STR_LIT>' + self.format_usage(usage), width=width, subsequent_indent='<STR_LIT>')<EOL>

Textwrap usage instructions.
        ARGS:
        width = 0 <int>:
            Maximum allowed page width. 0 means use default from
            self.iMaxHelpWidth.


return self.docvars['<STR_LIT:version>']<EOL>

Return self.docvars['version'].

        width is ignored and is present only for symmetry with other help methods.


out = []<EOL>out.append(self._wrap(self.docs['<STR_LIT:title>'], width=width))<EOL>if self.docs['<STR_LIT:description>']:<EOL><INDENT>out.append(self._wrap(self.docs['<STR_LIT:description>'], indent=<NUM_LIT:2>, width=width))<EOL><DEDENT>out.append('<STR_LIT>')<EOL>out.append(self._wrapusage(width=width))<EOL>out.append('<STR_LIT>')<EOL>return '<STR_LIT:\n>'.join(out)<EOL>

Return brief help containing Title and usage instructions.
        ARGS:
        width = 0 <int>:
            Maximum allowed page width. 0 means use default from
            self.iMaxHelpWidth.


return self.customhelp(self.help_sections, width)<EOL>

Return the standard formatted command line help for the prog.

        width is maximum allowed page width, use self.width if 0.


return self.customhelp(self.longhelp_sections, width)<EOL>

Return the standard formatted help text for the prog.

        This should have approximately the amount of information as you'd
        expect in a man page.

        width is maximum allowed page width, use self.width if 0.


def strsettings(self, indent=<NUM_LIT:0>, maxindent=<NUM_LIT>, width=<NUM_LIT:0>):

out = []<EOL>makelabel = lambda name: '<STR_LIT:U+0020>' * indent + name + '<STR_LIT>'<EOL>settingsindent = _autoindent([makelabel(s) for s in self.options], indent, maxindent)<EOL>for name in self.option_order:<EOL><INDENT>option = self.options[name]<EOL>label = makelabel(name)<EOL>settingshelp = "<STR_LIT>" % (option.formatname, option.strvalue, option.location)<EOL>wrapped = self._wrap_labelled(label, settingshelp, settingsindent, width)<EOL>out.extend(wrapped)<EOL><DEDENT>return '<STR_LIT:\n>'.join(out)<EOL>

Return user friendly help on positional arguments.

        indent is the number of spaces preceeding the text on each line.

        The indent of the documentation is dependent on the length of the
        longest label that is shorter than maxindent. A label longer than
        maxindent will be printed on its own line.

        width is maximum allowed page width, use self.width if 0.


out = []<EOL>out.append(self._wrap(self.docs['<STR_LIT:title>'], width=width))<EOL>if self.docs['<STR_LIT:description>']:<EOL><INDENT>out.append(self._wrap(self.docs['<STR_LIT:description>'], indent=<NUM_LIT:2>, width=width))<EOL><DEDENT>out.append('<STR_LIT>')<EOL>out.append('<STR_LIT>')<EOL>out.append(self.strsettings(indent=<NUM_LIT:2>, width=width))<EOL>out.append('<STR_LIT>')<EOL>return '<STR_LIT:\n>'.join(out)<EOL>

Return a summary of program options, their values and origins.

        width is maximum allowed page width, use self.width if 0.


if showusageonnoargs and len(argv) == <NUM_LIT:1>:<EOL><INDENT>print(self.shorthelp(width=width))<EOL>if helphint:<EOL><INDENT>print(self._wrap(helphint, indent=<NUM_LIT:2>, width=width))<EOL><DEDENT>sys.exit(<NUM_LIT:0>)<EOL><DEDENT>parsing_error = None<EOL>try:<EOL><INDENT>self.parse_files()<EOL>self.parse_argv(argv)<EOL><DEDENT>except ParseError as parsing_error:<EOL><INDENT>if debug_parser:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>for optiontype in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:version>']:<EOL><INDENT>name = self.basic_option_names.get(optiontype)<EOL>if name and self[name]:<EOL><INDENT>methodname = optiontype.rstrip('<STR_LIT>') + '<STR_LIT>'<EOL>print(getattr(self, methodname)(width))<EOL>sys.exit()<EOL><DEDENT><DEDENT>if parsing_error:<EOL><INDENT>self.graceful_exit(parsing_error, width)<EOL><DEDENT>

Do the usual stuff to initiallize the program.

        Read config files and parse arguments, and if the user has used any
        of the help/version/settings options, display help and exit.

        If debug_parser is false, don't catch ParseErrors and exit with user
        friendly help. Crash with traceback instead.

        configfiles is a list of config files. None means use self.configfiles.

        sections is a list of configfile section names to read. None means use
        self.sections.

        argv is a list of arguments to parse. Will be modified. None means use
        copy of sys.argv. argv[0] is ignored.

        If showusageonnoargs is true, show usage and exit if the user didn't
        give any args. Should be False if there are no required PositionalArgs.

        width is the maximum allowed page width. 0 means use self.width.

        helphint is a string that hints on how to get more help which is
        displayed at the end of usage help messages.


self.lines = []<EOL>self.tabsize = tabsize<EOL>

tabsize > 0 replaces tab characters with that many spaces. Set <= 0 to
disable.


self.lines.append(line)<EOL>

Add a line (no trailing newlines) to the text block.


return self.lines<EOL>

Return the text in the block as a list of lines.


return ['<STR_LIT:U+0020>'.join(self.lines)]<EOL>


return self.lines<EOL>

Return the indented paragraphs as a list of strings.


Remove the comment parts from a line of text.

        Return None to indicate that the line is completely commented out and
        that it should not be fed to TextBlock storage.


if isinstance(untagged, type(TextBlock)):<EOL><INDENT>untagged = untagged()<EOL><DEDENT>self.untagged = untagged <EOL>if isinstance(decommenter, type(Decommenter)):<EOL><INDENT>decommenter = decommenter()<EOL><DEDENT>self.decommenter = decommenter<EOL>self.tabsize = tabsize<EOL>if blocks is None:<EOL><INDENT>blocks = {}<EOL><DEDENT>self.blocks = blocks<EOL>if labelled_classes is None:<EOL><INDENT>labelled_classes = dict()<EOL><DEDENT>self.labelled_classes = labelled_classes<EOL>if names is None:<EOL><INDENT>names = dict((name.upper, name) for name in blocks)<EOL><DEDENT>self.names = names<EOL>

untagged determines what to do with text before the first tagged text
block and should be a TextBlock subclass, or None to raise ParseError.

decommenter determines how to strip comments from the text. Set to None
to not decomment.

tabsize > 0 replaces tab characters with that many spaces. Set <= 0 to
disable.


if isinstance(file, basestring):<EOL><INDENT>file = open(file)<EOL><DEDENT>line_number = <NUM_LIT:0><EOL>label = None<EOL>block = self.untagged<EOL>for line in file:<EOL><INDENT>line_number += <NUM_LIT:1><EOL>line = line.rstrip('<STR_LIT:\n>')<EOL>if self.tabsize > <NUM_LIT:0>:<EOL><INDENT>line = line.replace('<STR_LIT:\t>', '<STR_LIT:U+0020>' * self.tabsize)<EOL><DEDENT>if self.decommenter:<EOL><INDENT>line = self.decommenter.decomment(line)<EOL>if line is None:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>tag = line.split('<STR_LIT::>', <NUM_LIT:1>)[<NUM_LIT:0>].strip()<EOL>if tag not in self.names:<EOL><INDENT>if block is None:<EOL><INDENT>if line and not line.isspace():<EOL><INDENT>raise ParseError(file.name, line, "<STR_LIT>" % line)<EOL><DEDENT>continue<EOL><DEDENT>block.addline(line)<EOL>continue<EOL><DEDENT>name = self.names[tag]<EOL>label = line.split('<STR_LIT::>',<NUM_LIT:1>)[<NUM_LIT:1>].strip()<EOL>if name in self.labelled_classes:<EOL><INDENT>if not label:<EOL><INDENT>raise ParseError(file.name, line, "<STR_LIT>" % name)<EOL><DEDENT>block = self.blocks[name].setdefault(label, self.labelled_classes[name]())<EOL><DEDENT>else:<EOL><INDENT>if label:<EOL><INDENT>msg = "<STR_LIT>" % (label, name)<EOL>raise ParseError(file.name, line_number, msg)<EOL><DEDENT>block = self.blocks[name]<EOL><DEDENT>block.startblock()<EOL><DEDENT>


if isinstance(format, BaseFormat):<EOL><INDENT>return format<EOL><DEDENT>if isinstance(format, str):<EOL><INDENT>for name, formatclass in list(globals().items()):<EOL><INDENT>if name.lower() == format.lower():<EOL><INDENT>if not issubclass(formatclass, BaseFormat):<EOL><INDENT>raise ValueError('<STR_LIT>' % format)<EOL><DEDENT>return formatclass()<EOL><DEDENT><DEDENT><DEDENT>try:<EOL><INDENT>return format()<EOL><DEDENT>except:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>

Get a format object.

    If format is a format object, return unchanged. If it is a string
    matching one of the BaseFormat subclasses in the tui.formats module
    (case insensitive), return an instance of that class. Otherwise assume
    it'a factory function for Formats (such as a class) so call and return,
    and raise ValueError on error.


self.argument = argument<EOL>self.message = message<EOL>

value is the bad value or string literal, or a tui instance to use
value[name].


Pop, parse and return the first self.nargs items from args.

        Subclasses that desire other behavior can override this (must be
        overridden if self.nargs is None).

        if self.nargs > 1 a list of parsed values will be returned.

        Raise BadNumberOfArguments or BadArgument on errors.

        NOTE: args may be modified in place by this method.


argv = shlex.split(argstr, comments=True)<EOL>if len(argv) != self.nargs:<EOL><INDENT>raise BadNumberOfArguments(self.nargs, len(argv))<EOL><DEDENT>return self.parse(argv)<EOL>

Parse arguments found in settings files.

        argstr is the string that should be parsed. Use e.g. '""' to pass an
        empty string.

        if self.nargs > 1 a list of parsed values will be returned.

        NOTE: formats with nargs == 0 or None probably want to override this
        method.


Return a user-friendly representation of a value.

        Lookup value in self.specials, or call .to_literal() if absent.


super(Format, self).__init__(**kwargs)<EOL>if args is None:<EOL><INDENT>args = tuple()<EOL><DEDENT>self.args = tuple(args)<EOL>if kw is None:<EOL><INDENT>kw = dict()<EOL><DEDENT>self.kw = dict(kw)<EOL>

special is a argument:value dict of arguments with special meaning,
where arguments (keys) must be strings and value can be any python
object. Can also be given as an iterable for argument:value pairs, or
as an iterable for arguments that should be passed through untouched,
or as a single string which is taken as a list of one item.

If case is False then all literals will be converted to lowercase for
lookup in self.specials.

If addspecialdocs is true then a notice of the accepted special
arguments will be appended to self.docs.

args and kw are additional positional and keyword parameters to be
passed to self.to_python and self.to_literal.


def to_python(self, literal, *args, **kw):

return literal<EOL>


def to_literal(self, value, *args, **kw):

return str(value)<EOL>

Convert a value to a user-friendly representation.


lookup = self.casesensitive and arg or arg.lower()<EOL>if lookup in self.special:<EOL><INDENT>return self.special[lookup]<EOL><DEDENT>try:<EOL><INDENT>return self.to_python(arg, *self.args, **self.kw)<EOL><DEDENT>except Exception as e:<EOL><INDENT>raise BadArgument(arg, str(e))<EOL><DEDENT>

Parse a single argument.

        Lookup arg in self.specials, or call .to_python() if absent. Raise
        BadArgument on errors.


if len(argv) < self.nargs:<EOL><INDENT>raise BadNumberOfArguments(self.nargs, len(argv))<EOL><DEDENT>if self.nargs == <NUM_LIT:1>:<EOL><INDENT>return self.parse_argument(argv.pop(<NUM_LIT:0>))<EOL><DEDENT>return [self.parse_argument(argv.pop(<NUM_LIT:0>)) for tmp in range(self.nargs)]<EOL>

Pop, parse and return the first self.nargs items from args.

        if self.nargs > 1 a list of parsed values will be returned.

        Raise BadNumberOfArguments or BadArgument on errors.

        NOTE: argv may be modified in place by this method.


for k, v in list(self.special.items()):<EOL><INDENT>if v == value:<EOL><INDENT>return k<EOL><DEDENT><DEDENT>return self.to_literal(value, *self.args, **self.kw)<EOL>


argv = shlex.split(argstr, comments=True)<EOL>if len(argv) != <NUM_LIT:1>:<EOL><INDENT>raise BadNumberOfArguments(<NUM_LIT:1>, len(argv))<EOL><DEDENT>arg = argv[<NUM_LIT:0>]<EOL>lower = arg.lower()<EOL>if lower in self.true:<EOL><INDENT>return True<EOL><DEDENT>if lower in self.false:<EOL><INDENT>return False<EOL><DEDENT>raise BadArgument(arg, "<STR_LIT>" + self.allowed + '<STR_LIT:.>')<EOL>

Parse arguments found in settings files.

        Use the values in self.true for True in settings files, or those in
        self.false for False, case insensitive.