def test_parse_unix(self): parser = ListingParser(UNIX_LS) results = list(parser.parse_input()) date_factory = functools.partial(datetime.datetime, tzinfo=datetime.timezone.utc) datetime_now = datetime.datetime.utcnow() datetime_now = datetime_now.replace(tzinfo=datetime.timezone.utc) current_year = datetime_now.year datetime_1 = date_factory(current_year, 1, 29, 3, 26) datetime_2 = date_factory(current_year, 1, 25, 0, 17) if datetime_1 > datetime_now: datetime_1 = datetime_1.replace(year=current_year - 1) if datetime_2 > datetime_now: datetime_2 = datetime_2.replace(year=current_year - 1) self.assertEqual([ FileEntry('README', 'file', 531, datetime_1, perm=0o644), FileEntry('etc', 'dir', 512, date_factory(1994, 4, 8), perm=0o555), FileEntry('etc', 'dir', 512, date_factory(1994, 4, 8), perm=0o555), FileEntry('bin', 'symlink', 7, datetime_2, 'usr/bin', perm=0o777), FileEntry('blah', 'dir', 512, date_factory(2004, 4, 8), perm=0o555), ], results)
def test_parse_nlst(self): parser = ListingParser(NLST_LS) results = list(parser.parse_input()) self.assertEqual([ FileEntry('horse.txt'), FileEntry('fish'), FileEntry('dolphin.jpg'), FileEntry('delicious cake.wri'), FileEntry('egg'), ], results)
def test_parse_msdos(self): parser = ListingParser(MSDOS_LS) results = list(parser.parse_input()) date_factory = functools.partial(datetime.datetime, tzinfo=datetime.timezone.utc) self.assertEqual([ FileEntry('licensed', 'dir', None, date_factory( 2000, 4, 27, 21, 9)), FileEntry('pub', 'dir', None, date_factory(2000, 7, 18, 10, 16)), FileEntry('readme.htm', 'file', 589, date_factory(2000, 4, 14, 15, 47)), ], results)
def test_parse_unix_datelike_file_2(self): parser = ListingParser(UNIX_LS_DATELIKE_FILE_2) results = list(parser.parse_input()) date_factory = functools.partial(datetime.datetime, tzinfo=datetime.timezone.utc) self.assertEqual([ FileEntry('english_german.2010-03-24.tar.gz', 'file', 242408, date_factory(2010, 3, 24), perm=0o644), FileEntry( 'old', 'dir', 4096, date_factory(2010, 3, 24), perm=0o755), ], results)
def machine_listings_to_file_entries(listings: Iterable[dict]) -> \ Iterable[FileEntry]: '''Convert results from parsing machine listings to FileEntry list.''' for listing in listings: yield FileEntry(listing['name'], type=listing.get('type'), size=listing.get('size'), date=listing.get('modify'))
def test_parse_unix_datelike_file(self): parser = ListingParser(UNIX_LS_DATELIKE_FILE) results = list(parser.parse_input()) date_factory = functools.partial(datetime.datetime, tzinfo=datetime.timezone.utc) self.assertEqual([ FileEntry('2009-12', 'file', 1558532, date_factory(2009, 12, 30), perm=0o644), FileEntry('2010-01', 'file', 10564020, date_factory(2010, 1, 14), perm=0o644), ], results)
def test_convert_machine_listing_to_file_entry(self): results = tuple( machine_listings_to_file_entries( parse_machine_listing(SAMPLE_LISTING_1))) self.assertEqual( FileEntry('capmux.tar.z', type='file', size=25730, date=datetime.datetime(1994, 7, 28, 9, 58, 54, tzinfo=datetime.timezone.utc)), results[3])