def test_strings(self): # testing strings self.assertEqual("<2011-11-03 Thu>", OrgFormat.strdate("2011-11-3"), "date string error") self.assertEqual("<2011-11-03 Thu 11:52>", OrgFormat.strdatetime("2011-11-3 11:52"), "datetime string error")
def test_strdatetime(self): self.assertEqual(OrgFormat.strdatetime('1980-12-31 23:59'), '<1980-12-31 Wed 23:59>') self.assertEqual(OrgFormat.strdatetime('1981-01-15 15:10'), '<1981-01-15 Thu 15:10>') with self.assertRaises(TimestampParseException): OrgFormat.strdatetime('1981-01-15 15.10') with self.assertRaises(TimestampParseException): OrgFormat.strdatetime('1981-01-15T15:10')
def test_strdatetime(self): self.assertEqual( OrgFormat.strdatetime('1980-12-31 23:59'), u'<1980-12-31 Wed 23:59>' ) self.assertEqual( OrgFormat.strdatetime('1981-01-15 15:10'), u'<1981-01-15 Thu 15:10>' ) with self.assertRaises(TimestampParseException): OrgFormat.strdatetime('1981-01-15 15.10') with self.assertRaises(TimestampParseException): OrgFormat.strdatetime('1981-01-15T15:10')
def __handle_file(self, file, rootdir): """ handles a file (except ending with a tilde) """ # don't handle emacs tmp files (file~) if file[-1:] == '~': return link = os.path.join(rootdir, file) logging.debug('__handle_file: ' + '#' * 50) logging.debug('__handle_file: ' + link) orgdate = False # set to default value if self._args.force_filedate_extraction: # in this case, skip any clever # extraction mechanism to extract # date/time from file name and use # the mtime instead: logging.debug('__handle_file: force_filedate_extraction: using datetime from mtime of file') file_datetime = time.localtime(os.path.getmtime(link)) orgdate = OrgFormat.datetime(file_datetime) self.__write_file(file, link, orgdate) return # very basic checks for correctness (e.g., month=20, hour=70) # are part of these RegEx (and do not have to be checked # below) filename_timestamp_match = DATETIME_REGEX.match(file) logging.debug('__handle_file: filename_timestamp_match? ' + str(filename_timestamp_match is True)) if filename_timestamp_match: # day1/2 are like 'YYYY-MM-DD' time1/2 like 'HH:MM': has_1ymd, has_1ymdhm, has_2ymd, has_2ymdhm, \ day1, time1, day2, time2 = self.__extract_days_and_times(filename_timestamp_match) # Note: following things are available for formatting: # self._args.inactive_timestamps -> Bool # OrgFormat.strdate('YYYY-MM-DD', inactive=False) -> <YYYY-MM-DD Sun> # OrgFormat.strdatetime('YYYY-MM-DD HH:MM', inactive=False) -> <YYYY-MM-DD Sun HH:MM> assert(has_1ymd) try: if has_1ymdhm: if self.__check_datestamp_correctness(day1): if self.__check_timestamp_correctness(time1): orgdate = OrgFormat.strdatetime(day1 + ' ' + time1, inactive=self._args.inactive_timestamps) else: logging.warn('File "' + file + '" has an invalid timestamp (' + str(time1) + '). Skipping this faulty time-stamp.') orgdate = OrgFormat.strdate(day1, inactive=self._args.inactive_timestamps) else: logging.warn('File "' + file + '" has an invalid datestamp (' + str(day1) + '). Skipping this faulty date.') # omit optional second day if first has an issue: has_2ymd = False has_2ymdhm = False elif has_1ymd: # missing time-stamp for day1 if self.__check_datestamp_correctness(day1): if not self._args.skip_filetime_extraction: # we've got only a day but we're able to determine # time from file mtime, if same as ISO day in file # name: logging.debug('__handle_file: try to get file time from mtime if days match between mtime and filename ISO ...') file_datetime = time.localtime(os.path.getmtime(link)) if self.__check_if_days_in_timestamps_are_same(file_datetime, day1): orgdate = OrgFormat.datetime(file_datetime, inactive=self._args.inactive_timestamps) else: logging.debug('__handle_file: day of mtime and filename ISO differs, using filename ISO day') orgdate = OrgFormat.strdate(day1, inactive=self._args.inactive_timestamps) else: # we've got only a day and determining mtime # is not planned, so use the day as date-stamp orgdate = OrgFormat.strdate(day1, inactive=self._args.inactive_timestamps) else: logging.warn('File "' + file + '" has an invalid datestamp (' + str(day1) + '). Skipping this faulty date.') # omit optional second day if first has an issue: has_2ymd = False has_2ymdhm = False # there is a time range: if has_2ymdhm: assert(day2) if self.__check_datestamp_correctness(day2): if self.__check_timestamp_correctness(time2): orgdate += '--' + OrgFormat.strdatetime(day2 + ' ' + time2, inactive=self._args.inactive_timestamps) else: logging.warn('File "' + file + '" has an invalid timestamp (' + str(time2) + '). Skipping this faulty time-stamp.') orgdate += '--' + OrgFormat.strdate(day2, inactive=self._args.inactive_timestamps) else: logging.warn('File "' + file + '" has an invalid datestamp (' + str(day2) + '). Skipping this faulty date.') elif has_2ymd: assert(day2) if self.__check_datestamp_correctness(day2): orgdate += '--' + OrgFormat.strdate(day2, inactive=self._args.inactive_timestamps) else: logging.warn('File "' + file + '" has an invalid datestamp (' + str(day2) + '). Skipping this faulty date.') except TimestampParseException: logging.error('File "' + str(file) + '" has in invalid date- or timestamp. OrgFormat of one of day1: "' + str(day1) + '" time1: "' + str(time1) + '" day2: "' + str(day2) + '" time2: "' + str(time2) + '" ' + 'failed with TimestampParseException. Skipping this faulty date.') orgdate = False else: logging.debug('__handle_file: no date- nor timestamp') orgdate = False if not orgdate and self._args.skip_notimestamp_files: logging.debug('__handle_file: file had no or wrong time-stamp and you decided to skip them.') return self.__write_file(file, link, orgdate) logging.debug('__handle_file: using orgdate: ' + str(orgdate)) return