def test_zotero_validation(self): """Test Zotero library validation.""" path = join('tests', 'data', 'zotero.csv') zc = ZoteroCollection() zc.load_csv(path) criteria = {'required': ['Title', 'ISSN']} invalid_records = zc.validate(criteria) assert_equal(len(invalid_records), 2) for k, v in invalid_records.items(): assert_equal(v['required'][0], 'ISSN') criteria = {'required': ['Title', 'Canary']} invalid_records = zc.validate(criteria) assert_equal(len(invalid_records), 2) for k, v in invalid_records.items(): assert_equal(v['unexpected'][0], 'Canary')
def main(**kwargs): """ main function """ # logger = logging.getLogger(sys._getframe().f_code.co_name) path = abspath(realpath(kwargs['zotero_csv'])) zc = ZoteroCollection() if kwargs['verbose']: print('Loading Zotero CSV file at {} ...'.format(path)) zc.load_csv(path) if kwargs['verbose']: print(' Loaded {} records.'.format(len(zc))) criteria = {'required': ['Title', 'Short Title', 'Date']} if kwargs['verbose']: print('Validating ...') invalid_records = zc.validate(criteria) if kwargs['verbose']: print(' Validation complete.') print('There are {} invalid records.'.format(len(invalid_records))) outpath = None if kwargs['output'] != 'NOTSET': outpath = abspath(realpath(kwargs['output'])) if not isdir(outpath): raise IOError('{} is not a directory'.format(path)) outf = open(join(outpath, 'zotero_errors.csv'), 'w') fieldnames = ['key', 'title', 'criterion', 'fields', 'suggestion'] writer = csv.DictWriter(outf, fieldnames=fieldnames, quoting=csv.QUOTE_NONNUMERIC) writer.writeheader() if kwargs['verbose'] or outpath is not None: for k, v in invalid_records.items(): if kwargs['verbose']: print('INVALID Zotero Record {}:'.format(k)) print(' {}'.format(zc.get_record(k)['Title'])) for criterion, fields in v.items(): if kwargs['verbose']: print(' 👾 {}: "{}"' ''.format(criterion.upper(), '", "'.join(fields))) if outpath is not None: if criterion == 'required' and fields == ['Short Title']: suggestion = zc.get_record(k).suggest_short_title() else: suggestion = '' writer.writerow({ 'key': k, 'title': zc.get_record(k)['Title'], 'criterion': criterion, 'fields': '|'.join(fields), 'suggestion': suggestion })