def get_allocation(client, id_or_name):
    def _is_int(id_or_name):
        try:
            int(id_or_name)
            return True
        except ValueError:
            return False

    try:
        if _is_int(id_or_name):
            return client.allocations.get(id_or_name)
        else:
            filters = {
                'parent_request__isnull': True,
                'project_name': id_or_name
            }
            allocations = client.allocations.list(**filters)
            if len(allocations) == 1:
                return allocations[0]
            elif len(allocations) > 1:
                raise exceptions.CommandError(
                    "'%s' matches more than one allocation" % id_or_name)
            else:
                raise exceptions.NotFound()
    except exceptions.NotFound as ex:
        raise exceptions.CommandError(str(ex))
def format_parameters(params, parse_semicolon=True):
    '''Reformat parameters into dict of format expected by the API.'''

    if not params:
        return {}

    if parse_semicolon:
        # expect multiple invocations of --parameters but fall back
        # to ; delimited if only one --parameters is specified
        if len(params) == 1:
            params = params[0].split(';')

    parameters = {}
    for p in params:
        try:
            (n, v) = p.split(('='), 1)
        except ValueError:
            msg = 'Malformed parameter(%s). Use the key=value format.' % p
            raise exceptions.CommandError(msg)

        if n not in parameters:
            parameters[n] = v
        else:
            if not isinstance(parameters[n], list):
                parameters[n] = [parameters[n]]
            parameters[n].append(v)

    return parameters
    def take_action(self, parsed_args):
        self.log.debug('take_action(%s)', parsed_args)
        client = self.app.client_manager.allocation
        try:
            site = client.facilities.get(parsed_args.id)
        except exceptions.NotFound as ex:
            raise exceptions.CommandError(str(ex))

        return self.dict2columns(site.to_dict())