'Import Roles from CSV'

def import_role(self, filename):

    try:
        openFile = open(filename, 'r')
    except IOError:
        return ('Unable    to    open    file    %s' % filename)
    auth = current.auth
    acl = auth.permission
    create_role = auth.s3_create_role
    def parseACL(_acl):
        permissions = _acl.split('|')
        aclValue = 0
        for permission in permissions:
            if (permission == 'READ'):
                aclValue = (aclValue | acl.READ)
            if (permission == 'CREATE'):
                aclValue = (aclValue | acl.CREATE)
            if (permission == 'UPDATE'):
                aclValue = (aclValue | acl.UPDATE)
            if (permission == 'DELETE'):
                aclValue = (aclValue | acl.DELETE)
            if (permission == 'REVIEW'):
                aclValue = (aclValue | acl.REVIEW)
            if (permission == 'APPROVE'):
                aclValue = (aclValue | acl.APPROVE)
            if (permission == 'PUBLISH'):
                aclValue = (aclValue | acl.PUBLISH)
            if (permission == 'ALL'):
                aclValue = (aclValue | acl.ALL)
        return aclValue
    reader = self.csv.DictReader(openFile)
    roles = {}
    acls = {}
    args = {}
    for row in reader:
        if (row != None):
            role = row['role']
            desc = row.get('description', '')
            rules = {}
            extra_param = {}
            controller = row.get('controller')
            if controller:
                rules['c'] = controller
            fn = row.get('function')
            if fn:
                rules['f'] = fn
            table = row.get('table')
            if table:
                rules['t'] = table
            oacl = row.get('oacl')
            if oacl:
                rules['oacl'] = parseACL(oacl)
            uacl = row.get('uacl')
            if uacl:
                rules['uacl'] = parseACL(uacl)
            entity = row.get('entity')
            if entity:
                if (entity == 'any'):
                    pass
                else:
                    try:
                        entity = int(entity)
                    except ValueError:
                        entity = self._lookup_pe(entity)
                rules['entity'] = entity
            hidden = row.get('hidden')
            if hidden:
                extra_param['hidden'] = hidden
            system = row.get('system')
            if system:
                extra_param['system'] = system
            protected = row.get('protected')
            if protected:
                extra_param['protected'] = protected
            uid = row.get('uid')
            if uid:
                extra_param['uid'] = uid
        if (role in roles):
            acls[role].append(rules)
        else:
            roles[role] = [role, desc]
            acls[role] = [rules]
        if ((len(extra_param) > 0) and (role not in args)):
            args[role] = extra_param
    for rulelist in roles.values():
        if (rulelist[0] in args):
            create_role(rulelist[0], rulelist[1], *acls[rulelist[0]], **args[rulelist[0]])
        else:
            create_role(rulelist[0], rulelist[1], *acls[rulelist[0]])
