def update_credentials(self): self.server = False # False for an ics file, true for a caldav server -regardless of where the creds are stored self.no_creds = False self.server = True self.account = self.settings.get('username') self.password = self.settings.get('password') self.server = self.settings.get("server") self.folder = self.settings.get('folder') self.port = self.settings.get("port") if self.account is None or self.account == "" or self.server is None or self.server == "": # Using pw in config fs = FileSystemAccess(str(self.skill_id)) if fs.exists("email_conf.yml"): # Use yml file for config config = fs.open("email_conf.yml", "r").read() config = yaml.safe_load(config) self.account = config.get("username") self.password = config.get("password") self.server = config.get("server_address") self.folder = config.get("folder") self.port = config.get("port") else: self.no_creds = True if self.account is None or self.account == "": self.no_creds = True if self.no_creds is True: # Not set up in file/home self.speak_dialog("setup") return False return True
def update_credentials(self): self.server = False # False for an ics file, true for a caldav server -regardless of where the creds are stored self.no_creds = False server_type = self.settings.get("server_type") if server_type == "server": # On home self.server = True self.user = self.settings.get("username") self.server_address = self.settings.get("server_address") self.port = self.settings.get("port") self.password = self.settings.get("password") if self.user is None or self.user == "": # Using pw in config fs = FileSystemAccess(str(self.skill_id)) if fs.exists("calendar_conf.yml"): # Use yml file for config config = self.read_file("calendar_conf.yml") config = yaml.safe_load(config) self.user = config.get("username") self.server_address = config.get("server_address") self.port = config.get("port") self.password = config.get("password") else: self.no_creds = True if self.user is None or self.user == "": self.no_creds = True elif server_type == "local": # Use file pass if self.no_creds is True: # Not set up in file/home self.speak_dialog("setup") return False return True
def get_knowledge(self, filename): file_system = FileSystemAccess(str(self.skill_id)) file = file_system.open(filename, "r") data = [] for line in file: data.append(line.rstrip('\n')) knowledge = ".".join(data) return knowledge
def search_manual(self, situation): file_system = FileSystemAccess(str(self.skill_id)) file = file_system.open('troubleshooting.json', 'r') data = json.load(file) commonCauses = [li for li in data if li['name'] == situation] commonCauses = commonCauses[0]['cause'] return commonCauses
def save_data_json(self, filename, data, mode="w"): try: file_system = FileSystemAccess(str(self.skill_id)) file = file_system.open(filename, mode) json.dump(data, file) file.close() return True except Exception: self.log.info("ERROR: could not save skill file " + filename) return False
def get_events(self, date): """Get events on a date and return them as a list. date: Date object! Returns: list: {"datetime", "event"} """ if self.server is False: events = [] # The calendar is on the device # Check if it needs to be made... fs = FileSystemAccess(str(self.skill_id)) if fs.exists("calendar.ics"): # YAY! exists calendar = self.read_file("calendar.ics") c = ics.Calendar(imports=calendar) for event in c.timeline.on(day=arrow.get(date)): event_dict = { "datetime": event.begin.datetime, "event": event.name } events.append(event_dict) return events else: return [] elif self.server is True: url = "http://{}:{}@{}:{}/".format(self.user, self.password, self.server_address, self.port) try: client = caldav.DAVClient(url) principal = client.principal() # Select calendar events = [] for calendar in principal.calendars(): for event in calendar.events(): event_text = event.data cal = vobject.readOne(event_text) event_date = cal.vevent.dtstart.value.date() # If in date, append. if event_date == date: event_dict = { "datetime": cal.vevent.dtstart.value, "event": cal.vevent.summary.valueRepr() } events.append(event_dict) return events except: self.speak_dialog("error.logging.in") return None else: raise ("Wrong input")
def save_data_file(self, filename, data, mode="w"): try: file_system = FileSystemAccess(str(self.skill_id)) file = file_system.open(filename, mode) file.write(data) file.close() return True except Exception: #as e self.log.info("ERROR: could not save skill file " + filename) #LOG.error(e) return False
def save_knowledge(self, filename, knowledge, mode="w"): data = knowledge.split(".") data = "\n".join(data) try: file_system = FileSystemAccess(str(self.skill_id)) file = file_system.open(filename, mode) file.write(data) file.close() return True except Exception as e: LOG.warning("could not save skill file " + filename) LOG.error(e) return False
def load(): try: with FileSystemAccess('identity').open('identity.json', 'r') as f: IdentityManager.__identity = DeviceIdentity(**json.load(f)) except: IdentityManager.__identity = DeviceIdentity() return IdentityManager.__identity
def _load(): LOG.debug('Loading identity') try: with FileSystemAccess('identity').open('identity2.json', 'r') as f: IdentityManager.__identity = DeviceIdentity(**json.load(f)) except Exception: IdentityManager.__identity = DeviceIdentity()
def __init__(self, name, emitter=None): self.name = name self.bind(emitter) self.config_core = ConfigurationManager.get() self.config = self.config_core.get(name) self.dialog_renderer = None self.file_system = FileSystemAccess(join('skills', name)) self.registered_intents = [] self.log = getLogger(name)
def __init__(self, name=None, emitter=None): self.name = name or self.__class__.__name__ self.bind(emitter) self.config_core = ConfigurationManager.get() self.config = self.config_core.get(self.name) self.dialog_renderer = None self.file_system = FileSystemAccess(join('skills', self.name)) self.registered_intents = [] self.log = getLogger(self.name) self.reload_skill = True self.events = []
def serial(self): global sernum # save serial number try: self.log.info("READING SERIAL NUMBER") file_system = FileSystemAccess(str("skills/KukiSkill/")) file = file_system.open("kuki.serial", mode="r") data = file.read() file.close() sernum = data # save data to sernum self.log.info("SERIAL: " + sernum) return sernum except Exception as e: self.log.error("SERIAL NOT READ FROM FILE") self.log.error(e) #return False self.log.info("GENERATING NEW SERIAL NUMBER AND SAVE") generate_serial(StringLength=56) # generate new serial number and save self.log.info("SERIAL: " + sernum) try: file_system = FileSystemAccess(str("skills/KukiSkill/")) file = file_system.open("kuki.serial", mode="w") file.write(sernum) file.close() return True except Exception as e: self.log.error("SERIAL NOT SAVE TO FILE") self.log.error(e) return False
class IdentityManager(object): def __init__(self): self.filesystem = FileSystemAccess('identity') self.identity = None self.initialize() def initialize(self): if self.filesystem.exists('identity.json'): self.identity = DeviceIdentity.load(self.filesystem.open('identity.json', 'r')) else: identity = DeviceIdentity(device_id=str(uuid4())) self.update(identity) def update(self, identity): self.identity = identity with self.filesystem.open('identity.json', 'w') as f: self.identity.save(f) def is_paired(self): return self.identity is not None and self.identity.owner is not None def get(self): return self.identity
def save(login=None, lock=True): LOG.debug('Saving identity') if lock: identity_lock.acquire() try: if login: IdentityManager._update(login) with FileSystemAccess('identity').open('identity2.json', 'w') as f: json.dump(IdentityManager.__identity.__dict__, f) f.flush() os.fsync(f.fileno()) finally: if lock: identity_lock.release()
class IdentityManager(object): def __init__(self): self.filesystem = FileSystemAccess('identity') self.identity = None self.initialize() def initialize(self): if self.filesystem.exists('identity.json'): self.identity = DeviceIdentity.load( self.filesystem.open('identity.json', 'r')) else: identity = DeviceIdentity(device_id=str(uuid4())) self.update(identity) def update(self, identity): self.identity = identity with self.filesystem.open('identity.json', 'w') as f: self.identity.save(f) def is_paired(self): return self.identity is not None and self.identity.owner is not None def get(self): return self.identity
def __init__(self, name=None, emitter=None): self.name = name or self.__class__.__name__ # Get directory of skill self._dir = dirname(abspath(sys.modules[self.__module__].__file__)) self.bind(emitter) self.config_core = ConfigurationManager.get() self.config = self.config_core.get(self.name) self.dialog_renderer = None self.vocab_dir = None self.file_system = FileSystemAccess(join('skills', self.name)) self.registered_intents = [] self.log = LOG.create_logger(self.name) self.reload_skill = True self.events = [] self.skill_id = 0
def __init__(self, name=None, bus=None, use_settings=True): self.name = name or self.__class__.__name__ self.resting_name = None self.skill_id = '' # will be set from the path, so guaranteed unique self.settings_meta = None # set when skill is loaded in SkillLoader # Get directory of skill #: Member variable containing the absolute path of the skill's root #: directory. E.g. /opt/mycroft/skills/my-skill.me/ self.root_dir = dirname(abspath(sys.modules[self.__module__].__file__)) if use_settings: self.settings = Settings(self) self._initial_settings = deepcopy(self.settings.as_dict()) else: self.settings = None #: Set to register a callback method that will be called every time #: the skills settings are updated. The referenced method should #: include any logic needed to handle the updated settings. self.settings_change_callback = None self.gui = SkillGUI(self) self._bus = None self._enclosure = None self.bind(bus) #: Mycroft global configuration. (dict) self.config_core = Configuration.get() self.dialog_renderer = None #: Filesystem access to skill specific folder. #: See mycroft.filesystem for details. self.file_system = FileSystemAccess(join('skills', self.name)) self.log = LOG.create_logger(self.name) #: Skill logger instance self.reload_skill = True #: allow reloading (default True) self.events = EventContainer(bus) self.voc_match_cache = {} # Delegator classes self.event_scheduler = EventSchedulerInterface(self.name) self.intent_service = IntentServiceInterface()
def __init__(self, name=None, emitter=None): self.name = name or self.__class__.__name__ # Get directory of skill self._dir = dirname(abspath(sys.modules[self.__module__].__file__)) self.settings = SkillSettings(self._dir, self.name) self.bind(emitter) self.config_core = Configuration.get() self.config = self.config_core.get(self.name) or {} self.dialog_renderer = None self.vocab_dir = None self.root_dir = None self.file_system = FileSystemAccess(join('skills', self.name)) self.registered_intents = [] self.log = LOG.create_logger(self.name) self.reload_skill = True # allow reloading self.events = [] self.scheduled_repeats = [] self.skill_id = '' # will be set from the path, so guaranteed unique
def __init__(self, name=None, bus=None, use_settings=True): self.name = name or self.__class__.__name__ self.resting_name = None self.skill_id = '' # will be set from the path, so guaranteed unique self.settings_meta = None # set when skill is loaded in SkillLoader # Get directory of skill self.root_dir = dirname(abspath(sys.modules[self.__module__].__file__)) if use_settings: self._initial_settings = get_local_settings( self.root_dir, self.name) self.settings = Settings(self) else: self.settings = None self.settings_change_callback = None self.gui = SkillGUI(self) self._bus = None self._enclosure = None self.bind(bus) #: Mycroft global configuration. (dict) self.config_core = Configuration.get() self.dialog_renderer = None #: Filesystem access to skill specific folder. #: See mycroft.filesystem for details. self.file_system = FileSystemAccess(join('skills', self.name)) self.log = LOG.create_logger(self.name) #: Skill logger instance self.reload_skill = True #: allow reloading (default True) self.events = EventContainer(bus) self.voc_match_cache = {} # Delegator classes self.event_scheduler = EventSchedulerInterface(self.name) self.intent_service = IntentServiceInterface()
def handle_add_appoint(self, message): event = message.data.get("event") while not event: #We need to get the event event = self.get_response("new.event.name") utterance = message.data['utterance'] date, rest = extract_datetime(utterance, datetime.now(), self.lang) while rest == normalize(utterance): utterance = self.get_response("new.event.date") date, rest = extract_datetime(utterance) self.log.info(" Calendar skill new event: date: " + str(date) + " event: " + event) #ADD EVENT server_type = self.check_server() if server_type == "Handled": return elif server_type == True: #Home server_address = self.settings.get("server_address") port = self.settings.get("port") username = self.settings.get("username") password = self.settings.get("password") #start creating a vevent: cal = vobject.iCalendar() cal.add("vevent") #add name cal.vevent.add("summary").value = str(event) #add date cal.vevent.add('dtstart').value = date # add it to the calendar url = "http://{}:{}@{}:{}/".format(username, password, server_address, port) try: client = caldav.DAVClient(url) principal = client.principal() #Select calendar events = [] for calendar in principal.calendars(): calendar.add_event(str(cal.serialize())) self.speak_dialog("new.event.summary", data={"event": str(event)}) except: self.speak_dialog("error.logging.in") return None elif server_type is None: # Password local server_address = self.settings.get("server_address") calendar = self.settings.get("calendar") port = self.settings.get("port") account_config = self.config_core.get("calendar", {}) username = account_config.get("username") password = account_config.get("password") # start creating a vevent: cal = vobject.iCalendar() cal.add("vevent") # add name cal.vevent.add("summary").value = str(event) # add date cal.vevent.add('dtstart').value = date # add it to the calendar url = "http://{}:{}@{}:{}/".format(username, password, server_address, port) try: client = caldav.DAVClient(url) principal = client.principal() # Select calendar events = [] for calendar in principal.calendars(): calendar.add_event(str(cal.serialize())) self.speak_dialog("new.event.summary", data={"event": str(event)}) except: self.speak_dialog("error.logging.in") return None elif server_type == False: #Local #The calendar is on the device #Check if it needs to be made... fs = FileSystemAccess(str(self.skill_id)) if fs.exists("calendar.ics"): #YAY! exists calendar = self.read_file("calendar.ics") c = ics.Calendar(calendar) e = ics.Event() #add event e.name = str(event) e.begin = str(arrow.get(date)) c.events.apaddpend(e) self.write_file("calendar.ics", str(c)) self.speak_dialog("new.event.summary", data={"event": str(event)}) else: #create calendar c = ics.Calendar() e = ics.Event() #add event e.name = str(event) e.begin = str(arrow.get(date)) c.events.add(e) self.write_file("calendar.ics", str(c)) self.speak_dialog("new.event.summary", data={"event": str(event)})
def write_file(self, filename, data): fs = FileSystemAccess(str(self.skill_id)) data_file = fs.open(filename, "w") data_file.writelines(data) data_file.close() return True
def read_file(self, filename): fs = FileSystemAccess(str(self.skill_id)) data_file = fs.open(filename, "r") data = data_file.read() data_file.close() return data
def __init__(self): self.filesystem = FileSystemAccess('identity') self.identity = None self.initialize()
def handle_add_appoint(self, message): if self.update_credentials() is False: # No credentials return event = message.data.get("event") while not event: # We need to get the event event = self.get_response("new.event.name") utterance = message.data['utterance'] date, rest = extract_datetime(utterance, datetime.datetime.now(), self.lang) while rest == normalize(utterance): utterance = self.get_response("new.event.date") date, rest = extract_datetime(utterance, datetime.datetime.now(), self.lang) # Clean the date being in the event test_date, rest = extract_datetime(event, datetime.datetime.now(), self.lang) if test_date is not None: date_said = event.replace(rest, '') event = event.replace(date_said, '') # Check that there is a time - ask for one if there isn't if not self.check_for_time(date): time = None # No time- ask while self.check_for_time(time) is False: time = self.get_response("new.event.time", data={"event": event}) time, _ = extract_datetime(time) # user said date: add to date object date = datetime.datetime.combine(date.date(), time.time()) self.log.info("Calendar skill new event: date: " + str(date) + " event: " + event) # ADD EVENT if self.server is True: # start creating a vevent: cal = vobject.iCalendar() cal.add("vevent") # add name cal.vevent.add("summary").value = str(event) # add date cal.vevent.add('dtstart').value = date # add it to the calendar url = "http://{}:{}@{}:{}/".format(self.user, self.password, self.server_address, self.port) try: client = caldav.DAVClient(url) principal = client.principal() # Select calendar events = [] for calendar in principal.calendars(): calendar.add_event(str(cal.serialize())) self.speak_dialog("new.event.summary", data={"event": str(event)}) except: self.speak_dialog("error.logging.in") return None elif self.server is False: # Local # The calendar is on the device # Check if it needs to be made... fs = FileSystemAccess(str(self.skill_id)) if fs.exists("calendar.ics"): # YAY! exists calendar = self.read_file("calendar.ics") c = ics.Calendar(calendar) e = ics.Event() # add event e.name = str(event) e.begin = str(arrow.get(date)) c.events.append(e) self.write_file("calendar.ics", str(c)) self.speak_dialog("new.event.summary", data={"event": str(event)}) else: # create calendar c = ics.Calendar() e = ics.Event() # add event e.name = str(event) e.begin = str(arrow.get(date)) c.events.add(e) self.write_file("calendar.ics", str(c)) self.speak_dialog("new.event.summary", data={"event": str(event)})
def get_events(self, date): """Get events on a date and return them as a list. date: Date object! Returns: list: {"datetime", "event"} """ server_type = self.check_server() if server_type == "Handled": #Do nothing return "Handled" elif server_type == False: events = [] #The calendar is on the device #Check if it needs to be made... fs = FileSystemAccess(str(self.skill_id)) if fs.exists("calendar.ics"): #YAY! exists calendar = self.read_file("calendar.ics") c = ics.Calendar(calendar) for event in c.events: event_date = event.begin.datetime if event_date.date() == date: event_dict = { "datetime": event.begin.datetime, "event": event.name } events.append(event_dict) return events else: return [] elif server_type == True: #Get server and password info from home server_address = self.settings.get("server_address") port = self.settings.get("port") username = self.settings.get("username") password = self.settings.get("password") url = "http://{}:{}@{}:{}/".format(username, password, server_address, port) try: client = caldav.DAVClient(url) principal = client.principal() #Select calendar events = [] for calendar in principal.calendars(): for event in calendar.events(): event_text = event.data cal = vobject.readOne(event_text) event_date = cal.vevent.dtstart.value.date() #If in date, append. if event_date == date: event_dict = { "date": cal.vevent.dtstart.value.date(), "time": cal.vevent.dtstart.value.time(), "event": cal.vevent.summary.valueRepr() } events.append(event_dict) return events except: self.speak_dialog("error.logging.in") return None elif server_type == None: #Get server info on home. Password in config. server_address = self.settings.get("server_address") calendar = self.settings.get("calendar") port = self.settings.get("port") account_config = self.config_core.get("calendar", {}) username = account_config.get("username") password = account_config.get("password") url = "http://{}:{}@{}:{}/".format(username, password, server_address, port) try: client = caldav.DAVClient(url) principal = client.principal() #Select calendar events = [] for calendar in principal.calendars(): for event in calendar.events(): event_text = event.data cal = vobject.readOne(event_text) event_date = cal.vevent.dtstart.value.date() #If in date, append. if event_date == date: event_dict = { "date": cal.vevent.dtstart.value.date(), "time": cal.vevent.dtstart.value.time(), "event": cal.vevent.summary.valueRepr() } events.append(event_dict) return events except: self.speak_dialog("error.logging.in") return None else: raise ("Wrong input")
def save(login=None): if login: IdentityManager.update(login) with FileSystemAccess('identity').open('identity.json', 'w') as f: json.dump(IdentityManager.__identity.__dict__, f)
'---------------------------------------------------------------------------------------------------------' ) myRobot = str( input('Please type the name of the robot you want to configure (None): ') or 'None') if myRobot != 'None': for robot in account.robots: if robot.name == myRobot: n = base64.b64encode(robot.name.encode('ascii')) snr = base64.b64encode(robot.serial.encode('ascii')) sec = base64.b64encode(robot.secret.encode('ascii')) credentials = {'n': n, 'snr': snr, 'sec': sec} file_system = FileSystemAccess(join('skills', 'NeatoSkill')) with file_system.open('credentials.store', 'wb') as f: pickle.dump(credentials, f, pickle.HIGHEST_PROTOCOL) print('Name: {}'.format(robot.name)) print('Serial: {}'.format(robot.serial)) print('Secret: {}'.format(robot.secret)) print('Created credentials.store for {} in {}'.format( robot.name, file_system.path)) # Dump possible room configuration dummy = account.maps persMaps = account.persistent_maps if robot.serial in persMaps: maps = {}