def test_nice_date_time(self): for lang in self.test_config: set_default_lf_lang(lang) i = 1 while (self.test_config[lang].get('test_nice_date_time') and self.test_config[lang]['test_nice_date_time'].get(str(i))): p = self.test_config[lang]['test_nice_date_time'][str(i)] dp = ast.literal_eval(p['datetime_param']) np = ast.literal_eval(p['now']) dt = datetime.datetime(dp[0], dp[1], dp[2], dp[3], dp[4], dp[5]) now = None if not np else datetime.datetime( np[0], np[1], np[2], np[3], np[4], np[5]) print('Testing for ' + lang + ' that ' + str(dt) + ' is date time ' + p['assertEqual']) self.assertEqual( p['assertEqual'], nice_date_time(dt, lang=lang, now=now, use_24hour=ast.literal_eval( p['use_24hour']), use_ampm=ast.literal_eval(p['use_ampm']))) i = i + 1 set_default_lf_lang(default_lang)
def get_nice_event(event, is_on_date=False): """Transforms Events nicely spoken for Mycroft. nice_date() and nice_time() are functions from Mycroft.util.format that uses Lingua Franca to transform numbers and dates etc. to words. see mycroft.ai documentation at: https://mycroft-ai.gitbook.io/docs/mycroft-technologies/lingua-franca Args: event: Event extracted from Nextcloud. Returns: apmnt_date_time (str): The Date and Time of the next appointment nicely spoken String. apmnt_title (str): The Title of the Appointment. """ if type(event[0]) is date: apmnt_date_time = nice_date(event[0]) + ", all day " if is_on_date: apmnt_date_time = ", all day" else: apmnt_date_time = nice_date_time(event[0]) if is_on_date: apmnt_date_time = ", at " + apmnt_date_time apmnt_title = str(event[1]) return apmnt_date_time, apmnt_title
def _describe(self, alarm): if alarm["repeat_rule"]: # Describe repeating alarms if alarm["repeat_rule"].startswith( "FREQ=WEEKLY;INTERVAL=1;BYDAY="): days = alarm["repeat_rule"][29:] # e.g. "SU,WE" days = (days.replace("SU", "0").replace("MO", "1"). replace("TU", "2").replace("WE", "3"). replace("TH", "4").replace("FR", "5"). replace("SA", "6").replace(",", " ")) # now "0 3" recur = set() for day in days.split(): recur.add(day) desc = self._recur_desc(recur) else: desc = self.translate('repeats') dt = self.get_alarm_local(alarm) dialog = 'recurring.alarm' if alarm["name"] != "": dialog = dialog + '.named' return self.translate(dialog, data={'time': nice_time(dt, use_ampm=not self.use_24hour,speech=True, use_24hour = self.use_24hour, lang=self.lang), 'recurrence': desc, 'name': alarm["name"]}) else: dt = self.get_alarm_local(alarm) dt_string = nice_date_time(dt, now=now_local(), use_ampm= not self.use_24hour, use_24hour = False, lang=self.lang) if alarm["name"]: return self.translate('alarm.named', data={'datetime': dt_string, 'name': alarm["name"]}) else: return dt_string
def handle_last_pump_activation(self, message): self.speak_dialog('checking') with open('/home/pi/last_pump_activation.txt','r') as f: date_string = f.readline().strip("\n") date_time = (datetime.strptime(date_string,"%d/%m/%Y %H:%M:%S")) date_time_parsed_string = nice_date_time(date_time) self.speak(date_time_parsed_string)
def test_nice_date_time(self): for lang in self.test_config: i = 1 while (self.test_config[lang].get('test_nice_date_time') and self.test_config[lang]['test_nice_date_time'].get(str(i))): p = self.test_config[lang]['test_nice_date_time'][str(i)] dp = ast.literal_eval(p['datetime_param']) np = ast.literal_eval(p['now']) dt = datetime.datetime( dp[0], dp[1], dp[2], dp[3], dp[4], dp[5]) now = None if not np else datetime.datetime( np[0], np[1], np[2], np[3], np[4], np[5]) print('Testing for ' + lang + ' that ' + str(dt) + ' is date time ' + p['assertEqual']) self.assertEqual( p['assertEqual'], nice_date_time( dt, lang=lang, now=now, use_24hour=ast.literal_eval(p['use_24hour']), use_ampm=ast.literal_eval(p['use_ampm']))) i = i + 1
def handle_set_alarm(self, message): """Handler for "set an alarm for...""" utt = message.data.get('utterance').lower() recur = None if message.data.get('Recurring'): # Just ignoring the 'Recurrence' now, we support more complex stuff # recurrence = message.data.get('Recurrence') recur = self._create_day_set(utt) # TODO: remove days following an "except" in the utt while not recur: r = self.get_response('query.recurrence', num_retries=1) if not r: return recur = self._create_day_set(r) if self.voc_match(utt, "Except"): # TODO: Support exceptions self.speak_dialog("no.exceptions.yet") return # Get the time when, utt_no_datetime = extract_datetime(utt, lang=self.lang) or (None, utt) # Get name from leftover string from extract_datetime name = self._get_alarm_name(utt_no_datetime) # Will return dt of unmatched string today = extract_datetime(self.texts.get('today'), lang=self.lang) today = today[0] # Check the time if it's midnight. This is to check if the user # said a recurring alarm with only the Day or if the user did # specify to set an alarm on midnight. If it's confirmed that # it's for a day only, then get another response from the user # to clarify what time on that day the recurring alarm is. is_midnight = self._check_if_utt_has_midnight(utt, when, self.threshold) if (when is None or when.time() == today.time()) and not is_midnight: r = self.get_response('query.for.when', validator=extract_datetime) if not r: self.speak_dialog("alarm.schedule.cancelled") return when_temp = extract_datetime(r, lang=self.lang) if when_temp is not None: when_temp = when_temp[0] # TODO add check for midnight # is_midnight = self._check_if_utt_has_midnight(r, when_temp, # self.threshold) when = when_temp if when is None \ else datetime(tzinfo=when.tzinfo, year=when.year, month=when.month, day=when.day, hour=when_temp.hour, minute=when_temp.minute) else: when = None # Verify time alarm_time = when confirmed_time = False while (not when or when == today) and not confirmed_time: if recur: t = nice_time(alarm_time, use_ampm=not self.use_24hour, use_24hour = self.use_24hour) conf = self.ask_yesno('confirm.recurring.alarm', data={ 'time': t, 'recurrence': self._recur_desc(recur) }) else: t = nice_date_time(alarm_time, now=today, use_ampm= not self.use_24hour, use_24hour = self.use_24hour, lang=self.lang) conf = self.ask_yesno('confirm.alarm', data={'time': t}) if not conf: return if conf == 'yes': when = [alarm_time] confirmed_time = True else: # check if a new (corrected) time was given when = extract_datetime(conf, lang=self.lang) if when is not None: when = when[0] if not when or when == today: # Not a confirmation and no date/time in statement, quit return alarm_time = when when = None # reverify alarm = {} if not recur: alarm_time_ts = to_utc(alarm_time).timestamp() now_ts = now_utc().timestamp() if alarm_time_ts > now_ts: alarm = self.set_alarm(alarm_time, name) else: if (self.texts.get('today') in utt) or (self.texts.get('tonight') in utt): self.speak_dialog('alarm.past') return else: # Set the alarm to find the next 24 hour time slot while alarm_time_ts < now_ts: alarm_time_ts += 86400.0 alarm_time = datetime.utcfromtimestamp(alarm_time_ts) alarm = self.set_alarm(alarm_time, name) else: alarm = self.set_alarm(alarm_time, name, repeat=recur) if not alarm: # none set, it was a duplicate return # Don't want to hide the animation self.enclosure.deactivate_mouth_events() if confirmed_time: self.speak_dialog("alarm.scheduled") else: t = self._describe(alarm) reltime = nice_relative_time(self.get_alarm_local(alarm), lang=self.lang) if recur: self.speak_dialog("recurring.alarm.scheduled.for.time", data={"time": t, "rel": reltime}) else: self.speak_dialog("alarm.scheduled.for.time", data={"time": t, "rel": reltime}) self._show_alarm_anim(alarm_time) self.enclosure.activate_mouth_events()