Changeset 10676 for main/waeup.kofa/trunk/src/waeup/kofa/utils
- Timestamp:
- 31 Oct 2013, 17:51:25 (11 years ago)
- Location:
- main/waeup.kofa/trunk/src/waeup/kofa/utils
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py
r10028 r10676 713 713 def emit(self, record): 714 714 pass 715 716 717 def check_csv_charset(iterable): 718 """Check contents of `iterable` regarding valid CSV encoding. 719 720 `iterable` is expected to be an iterable on _rows_ (not 721 chars). This is true for instance for 722 filehandlers. `zope.publisher.browser.FileUpload` instances are 723 _not_ iterable, unfortunately. 724 725 Returns line num of first illegal char or ``None``. Line nums 726 start counting with 1 (not zero). 727 """ 728 linenum = 1 729 reader = csv.DictReader(iterable) 730 try: 731 for row in reader: 732 linenum += 1 733 except UnicodeDecodeError: 734 return linenum 735 except: 736 return linenum + 1 737 return None -
main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py
r9689 r10676 492 492 493 493 494 class CheckCSVCharsetTestCase(unittest.TestCase): 495 496 def test_valid_data1(self): 497 csv = ( 498 'col1,col2,col3\n' 499 'val1,val2,val3\n' 500 ).splitlines() 501 self.assertEqual(helpers.check_csv_charset(csv), None) 502 503 def test_valid_data2(self): 504 csv = ( 505 "code,title,title_prefix\n" 506 "FAC1,Faculty 1,faculty\n" 507 "FAC2,Faculty 2,institute\n" 508 "FAC3,Fäcülty 3,school\n" 509 ).splitlines() 510 self.assertEqual(helpers.check_csv_charset(csv), None) 511 512 def test_invalid_data1(self): 513 csv = ( 514 'col1,col2,col3\n' + 515 chr(0x92) + 'val1,val2,val3\n' 516 ).splitlines() 517 self.assertEqual(helpers.check_csv_charset(csv), 1) 518 519 def test_invalid_data2(self): 520 csv = ( 521 'some text that \n' 522 '\n' # this empty line will break 523 'is not a csv file \n' + chr(0x92) + '\n' 524 ).splitlines() 525 self.assertEqual(helpers.check_csv_charset(csv), 2) 494 526 495 527 def test_suite(): … … 509 541 MergeCSVFileTestCase, 510 542 SimpleHelpersTestCase, 543 CheckCSVCharsetTestCase, 511 544 ]: 512 545 suite.addTests(
Note: See TracChangeset for help on using the changeset viewer.