def createRegistry(self): try: print "Creating device registry: {0}".format(config.getFullFileName("devices.ini") or 'Undefined') with open(config.getFullFileName("devices.ini"), 'w+') as f: print f return True except Exception as e: print e return False
def updateDeviceLastSeenTime(self, device): filesRead = self.parser.read(config.getFullFileName("devices.ini")) if len(filesRead) == 0: error = "The DeviceManager could not load it's configuration file: {0}".format(config.getFullFileName("devices.ini")) logging.error(error) raise Exception(error) else: sectionName = device.visibleName lastSeen = device.lastSeen self.parser.set(sectionName, 'lastSeen', str(lastSeen)) with open(config.getFullFileName("devices.ini"),'w') as f: self.parser.write(f)
def deleteRegistry(self): try: self.parser = SafeConfigParser() with open(config.getFullFileName("devices.ini"),'w') as f: self.parser.write(f) return True except Exception as e: print e return False
def viewExists(viewName): viewPath = config.getFullFileName(viewName + ".mc") print "Checking if view exists by looking at the file: ", viewPath try: with open(viewPath) as f: print "The view file was found. ", f return True except IOError as e: print "The view file could not be found: ", e return False
def addPlayedTrack(track): """ Adds an entry to the play history """ assert track != None fileName = config.getFullFileName('play_history.txt') entry = "{0} {1}".format(datetime.now(), track.filePath) try: with open(fileName, 'a') as f: f.write(entry + "\n") return True except IOError as e: print e return False
def registerDevice(self, device): """ Register or update the specified device. Devices are stored into the file devices.ini from the config folder. """ if not config.workspaceIsSet(): print "Cannot register a device when the workspace is not set." return False if not isinstance(device, Device): error = "The specified device argument must inherit from the type devices.Device." logging.info(error) raise TypeError(error) filesRead = self.parser.read(config.getFullFileName("devices.ini")) if len(filesRead) == 0: print "The DeviceManager is creating the registry..." if not self.createRegistry(): print "The DeviceManager could not create the registry." return False currentDevices = self.getDevices() if not currentDevices == None and device in currentDevices: self.updateDeviceLastSeenTime(device) return True sectionName = device.visibleName self.parser.add_section(sectionName) self.parser.set(sectionName, 'visibleName', device.visibleName) self.parser.set(sectionName, 'url', device.url) self.parser.set(sectionName, 'type', device.type) self.parser.set(sectionName, 'lastSeen', str(device.lastSeen)) with open(config.getFullFileName("devices.ini"),'w') as f: self.parser.write(f) print "Added device to the registry: {0} {1}".format(device.visibleName, device.url) return True
def loadView(viewName): if viewName in VIEWS: print "Loading view from memory: {0}".format(viewName) return VIEWS[viewName] print "Loading view from file: {0}".format(viewName) try: indexLocation = config.getFullFileName(viewName + ".mc") print indexLocation with open(indexLocation, 'rb') as _file: viewData = json.load(_file) VIEWS[viewName] = viewData return viewData except IOError as e: logging.debug(e) print e return None
def _saveResult(self, result): reportFolder = config.getFullFileName("reports/") from datetime import datetime now = datetime.now() resultFilename = reportFolder + "{0}.txt".format(datetime(now.year, now.month, now.day, now.hour, now.minute)) try: if not os.path.exists(reportFolder): os.makedirs(reportFolder) except IOError as e: print "Could not create directory to save results: {0}".format(reportFolder) return False print "Saving indexation result..." try: with open(resultFilename, 'wb') as f: json.dump(result, f) return True except IOError as e: logging.debug(e) print e return False
def getDevices(self): """ Read all configured devices from the registry. If the registry could not be read, return None If no devices were found in the registry, return an empty array otherwise return an array of Devices. """ devices = [] filesRead = self.parser.read(config.getFullFileName("devices.ini")) if len(filesRead) == 0: if not self.createRegistry(): return for device in self.parser.sections(): url = self.parser.get(device, 'url').encode("utf-8") lastSeen = self.parser.get(device, 'lastSeen') visibleName = self.parser.get(device, 'visibleName').encode("utf-8") type = self.parser.get(device, 'type').encode("utf-8") device = Device(type, visibleName, url, lastSeen) devices.append(device) return devices