def find_free_prefix(self, auth, vrf, args):
<EOL>if type(args) is not dict:<EOL><INDENT>raise NipapInputError("<STR_LIT>")<EOL><DEDENT>max_count = <NUM_LIT:1000><EOL>if '<STR_LIT:count>' in args:<EOL><INDENT>if int(args['<STR_LIT:count>']) > max_count:<EOL><INDENT>raise NipapValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>else:<EOL><INDENT>args['<STR_LIT:count>'] = <NUM_LIT:1><EOL><DEDENT>if '<STR_LIT>' in args:<EOL><INDENT>if '<STR_LIT>' in args:<EOL><INDENT>raise NipapInputError("<STR_LIT>")<EOL><DEDENT>if '<STR_LIT>' not in args:<EOL><INDENT>raise NipapMissingInputError("<STR_LIT>")<EOL><DEDENT>try:<EOL><INDENT>assert int(args['<STR_LIT>']) in [ <NUM_LIT:4>, <NUM_LIT:6> ]<EOL><DEDENT>except (TypeError, AssertionError):<EOL><INDENT>raise NipapValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>elif '<STR_LIT>' in args:<EOL><INDENT>if type(args['<STR_LIT>']) is not list:<EOL><INDENT>raise NipapInputError("<STR_LIT>")<EOL><DEDENT>if '<STR_LIT>' in args:<EOL><INDENT>raise NipapInputError("<STR_LIT>")<EOL><DEDENT>if '<STR_LIT>' not in args:<EOL><INDENT>raise NipapMissingInputError("<STR_LIT>")<EOL><DEDENT>if '<STR_LIT>' in args:<EOL><INDENT>raise NipapExtraneousInputError("<STR_LIT>")<EOL><DEDENT><DEDENT>prefixes = []<EOL>wpl = <NUM_LIT:0><EOL>if '<STR_LIT>' in args:<EOL><INDENT>pool_result = self.list_pool(auth, args['<STR_LIT>'])<EOL>self._logger.debug(args)<EOL>if pool_result == []:<EOL><INDENT>raise NipapNonExistentError("<STR_LIT>")<EOL><DEDENT>for p in pool_result[<NUM_LIT:0>]['<STR_LIT>']:<EOL><INDENT>if self._get_afi(p) == int(args['<STR_LIT>']):<EOL><INDENT>prefixes.append(p)<EOL><DEDENT><DEDENT>if len(prefixes) == <NUM_LIT:0>:<EOL><INDENT>raise NipapInputError('<STR_LIT>' % unicode(args['<STR_LIT>']))<EOL><DEDENT>if '<STR_LIT>' not in args:<EOL><INDENT>if int(args['<STR_LIT>']) == <NUM_LIT:4>:<EOL><INDENT>wpl = pool_result[<NUM_LIT:0>]['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>wpl = pool_result[<NUM_LIT:0>]['<STR_LIT>']<EOL><DEDENT><DEDENT><DEDENT>afi = None<EOL>if '<STR_LIT>' in args:<EOL><INDENT>for prefix in args['<STR_LIT>']:<EOL><INDENT>prefix_afi = self._get_afi(prefix)<EOL>if afi is None:<EOL><INDENT>afi = prefix_afi<EOL><DEDENT>elif afi != prefix_afi:<EOL><INDENT>raise NipapInputError("<STR_LIT>")<EOL><DEDENT>prefixes.append(prefix)<EOL><DEDENT><DEDENT>if '<STR_LIT>' in args:<EOL><INDENT>try:<EOL><INDENT>wpl = int(args['<STR_LIT>'])<EOL><DEDENT>except ValueError:<EOL><INDENT>raise NipapValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>if afi == <NUM_LIT:4>:<EOL><INDENT>if wpl < <NUM_LIT:0> or wpl > <NUM_LIT:32>:<EOL><INDENT>raise NipapValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>elif afi == <NUM_LIT:6>:<EOL><INDENT>if wpl < <NUM_LIT:0> or wpl > <NUM_LIT>:<EOL><INDENT>raise NipapValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>params = {}<EOL>sql_prefix = '<STR_LIT>'.join('<STR_LIT>' + unicode(prefixes.index(p)) + '<STR_LIT>' for p in prefixes)<EOL>for p in prefixes:<EOL><INDENT>params['<STR_LIT>' + unicode(prefixes.index(p))] = unicode(p)<EOL><DEDENT>damp = '<STR_LIT>' + sql_prefix + '<STR_LIT>'<EOL>sql = """<STR_LIT>""" + damp + """<STR_LIT>"""<EOL>v = self._get_vrf(auth, vrf or {}, '<STR_LIT>')<EOL>params['<STR_LIT>'] = v['<STR_LIT:id>']<EOL>params['<STR_LIT>'] = prefixes<EOL>params['<STR_LIT>'] = wpl<EOL>params['<STR_LIT>'] = args['<STR_LIT:count>']<EOL>self._execute(sql, params)<EOL>res = list()<EOL>for row in self._curs_pg:<EOL><INDENT>res.append(unicode(row['<STR_LIT>']))<EOL><DEDENT>return res<EOL>
Finds free prefixes in the sources given in `args`.

            * `auth` [BaseAuth]
                AAA options.
            * `vrf` [vrf]
                Full VRF-dict specifying in which VRF the prefix should be
                unique.
            * `args` [find_free_prefix_args]
                Arguments to the find free prefix function.

            Returns a list of dicts.

            Prefixes can be found in two ways: from a pool of from a prefix.

            From a pool
            The `args` argument is set to a dict with key :attr:`from-pool` set to a
            pool spec. This is the pool from which the prefix will be assigned.
            Also the key :attr:`family` needs to be set to the adress family (integer
            4 or 6) of the requested prefix.  Optionally, also the key
            :attr:`prefix_length` can be added to the `attr` argument, and will then
            override the default prefix length.

            Example::

                args = {
                    'from-pool': { 'name': 'CUSTOMER-' },
                    'family': 6,
                    'prefix_length': 64
                }

            From a prefix
                Instead of specifying a pool, a prefix which will be searched
                for new prefixes can be specified. In `args`, the key
                :attr:`from-prefix` is set to list of prefixes you want to
                allocate from and the key :attr:`prefix_length` is set to 
                the wanted prefix length.

            Example::

                args = {
                    'from-prefix': ['192.0.2.0/24'],
                    'prefix_length': 27
                }

            The key :attr:`count` can also be set in the `args` argument to specify
            how many prefixes that should be returned. If omitted, the default
            value is 1000.

            The internal backend function :func:`find_free_prefix` is used
            internally by the :func:`add_prefix` function to find available
            prefixes from the given sources. It's also exposed over XML-RPC,
            please see the XML-RPC documentation for
            :py:func:`nipap.xmlrpc.NipapXMLRPC.find_free_prefix` for full
            understanding.