'Return a prefix for record.
Evaluate record against self.prefix_rules. Return assigned prefix
if matched.
Args:
record (dict): book metadata
Return:
prefix (str): matched a prefix_rule
None: no match'

def discover_prefix(self, record):

    def _log_prefix_rule_match_info(rule, record, matched):
        self.opts.log.info(("        %s    '%s'    by    %s    (%s:    '%s'    contains    '%s')" % (rule['prefix'], record['title'], record['authors'][0], rule['name'], self.db.metadata_for_field(rule['field'])['name'], matched)))
    for rule in self.prefix_rules:
        if (rule['field'].lower() == 'tags'):
            if (rule['pattern'].lower() in map(unicode.lower, record['tags'])):
                if (self.DEBUG and self.opts.verbose):
                    self.opts.log.info(("        %s    '%s'    by    %s    (%s:    Tags    includes    '%s')" % (rule['prefix'], record['title'], record['authors'][0], rule['name'], rule['pattern'])))
                return rule['prefix']
        elif rule['field'].startswith('#'):
            field_contents = self.db.get_field(record['id'], rule['field'], index_is_id=True)
            if (field_contents == ''):
                field_contents = None
            if ((not self.db.prefs.get('bools_are_tristate')) and (self.db.metadata_for_field(rule['field'])['datatype'] == 'bool') and (field_contents is None)):
                field_contents = _('False')
            if (field_contents is not None):
                if (self.db.metadata_for_field(rule['field'])['datatype'] == 'bool'):
                    field_contents = _(repr(field_contents))
                try:
                    if (re.search(rule['pattern'], unicode(field_contents), re.IGNORECASE) is not None):
                        if self.DEBUG:
                            _log_prefix_rule_match_info(rule, record, field_contents)
                        return rule['prefix']
                except:
                    if self.opts.verbose:
                        self.opts.log.error(('pattern    failed    to    compile:    %s' % rule['pattern']))
                    pass
            elif ((field_contents is None) and (rule['pattern'] == 'None')):
                if self.DEBUG:
                    _log_prefix_rule_match_info(rule, record, field_contents)
                return rule['prefix']
    return None
