Beispiel #1
0
def create_cron_job():
    try:
        my_cron = CronTab(user="******")
        job = my_cron.new(command="sudo python3 ~/Desktop/lamp/cronjob.py")
        job.minute.every(1)
        my_cron.write()
        print("cron job created")
    except:
        writeToLog(
            "!!!SEVERE!!!\n something went wrong creating the cron job \n !!!SEVERE!!!\n"
        )
        print("failed to create cronjob")
Beispiel #2
0
def predict(kicDir):
    kicId = os.path.basename(kicDir)
    files = glob.glob(os.path.join(kicDir, '*'))

    imgs = []
    for f in files:
        imgs.append(imread(f)[:, :, 0:1])

    probabilities = nn.predict(np.array(imgs)).flatten()
    threshold = 0.9
    surpassed = sum([p > threshold for p in probabilities])
    if surpassed != 0:
        utils.writeToLog(
            logfile, "%s,%s,%s,%s\n" % (kicId, surpassed, len(probabilities),
                                        surpassed / len(probabilities)))
Beispiel #3
0
def applySkin(path, application):
    """ Searches for the skin files, processes them and applies them to the
        forms """

    if not os.path.exists(path) or not os.path.isdir(path):
        raise Exception( "applySkin() expects a path. The '" + \
                         path + "' does not exist or is not a directory" )
    if not path.endswith('/'):
        path += '/'

    cssFiles = []
    buildCSSFilesList(path, cssFiles)

    # The Application.css file must be applied first
    for fileName in cssFiles:
        if os.path.basename(fileName) == 'Application.css':
            # Apply the application CSS
            content = getCSSContent(fileName).strip()
            if len(content) != 0:
                application.setStyleSheet(content)
                debugMsg("Setting APPLICATION level CSS")
                break

    # Apply all the other CSS files
    for fullFileName in cssFiles:
        fileName = os.path.basename(fullFileName)
        if fileName == 'Application.css':
            continue

        formName = fileName.replace(".css", "")
        if not kioskForms.has_key(formName):
            message = "WARNING. Style sheet file " + fullFileName + \
                      " is skipped because the '" + formName + "'" \
                      " form is not registered"
            debugMsg(message)
            writeToLog(message)
            continue

        content = getCSSContent(fullFileName).strip()
        if len(content) != 0:
            kioskForms[formName].setStyleSheet(content)
            debugMsg("Setting CSS for '" + formName + "' form")

    return
Beispiel #4
0
def exceptionHook( excType, excValue, tracebackObj ):
    """ Catches unhandled exceptions """

    globalData = utils.GlobalData()

    # Keyboard interrupt is a special case
    if issubclass( excType, KeyboardInterrupt ):
        if not globalData.application is None:
            globalData.application.quit()
        return

    filename, line, dummy, dummy = traceback.extract_tb( tracebackObj ).pop()
    filename = os.path.basename( filename )
    error    = "%s: %s" % ( excType.__name__, excValue )
    stackTraceString = "".join( traceback.format_exception( excType, excValue,
                                                            tracebackObj ) )

    # Write a message to a log file
    utils.writeToLog( "Unhandled exception is caught\n" + \
                      stackTraceString )

    print >> sys.stderr, "Unhandled exception is caught\n"
    print >> sys.stderr, stackTraceString

    # Display the message as a QT modal dialog box if the application
    # has started
    if not globalData.application is None:
        QtGui.QMessageBox.critical( None, "Error: " + error,
                                    "<html>Unhandled exception is caught." \
                                    "<pre>" + stackTraceString + "</pre>" \
                                    "</html>" )

    # Send an e-mail
    try:
        mail.sendErrorReport( "ttkiosk: unhandled exception",
                              "Unhandled exception is caught\n" + \
                              stackTraceString + "\n" + \
                              utils.collectHostInfo() )
    except:
        utils.writeToLog( "Cannot send e-mail error report: " + \
                          stackTraceString )
    sys.exit( 1 )
Beispiel #5
0
def main():
    if not os.path.exists(logsDir):
        os.makedirs(logsDir)

    print("Predicting\n-----------------")

    utils.writeToLog(logfile, "KIC ID,surpassed threshold,total,ratio\n")
    kicDirs = glob.glob(os.path.join(dataDir, "*"))

    print("Progress: [  0%] [{0}]".format('.' * 50), end='\r')

    for idx, kicDir in enumerate(kicDirs):
        predict(kicDir)
        percentage = math.ceil(100 * (idx + 1) / len(kicDirs))
        print("Progress: [{0}%] [{1}{2}]".format(
            "%3s" % percentage, '#' * math.ceil(percentage / 2),
            '.' * (50 - math.ceil(percentage / 2))),
              end='\r')

    print("\n")
    def storageFree(self):
        """ Checks availability of hard drive space. If there is space
        to proceed with the next batch returns True otherwise False.
        
        Usage:
        -----
        self.storageFree()
        
        Returns:
        -------
        bool (True/False)
        """

        free = False
        availableSpace = psutil.disk_usage(self.dropboxDir).free
        if (availableSpace >= 2 * self.batchSize):
            free = True
        else:
            utils.writeToLog(self.logFileName, message='Disk full')
            input(
                'Disk is full. Move data to online-only mode. Press enter to continue after more space is available.'
            )
        return free
    def uploadFiles(self):
        """ Uploads the batches using Dropbox App one by one.
        self.checkResource() is called repeatedly. If resources are
        available it a second verification is done if the files from
        current batch have uploaded successfully using
        self.checkFilesOnWebsite(). Only then, the next batch is
        submitted for upload.
        
        Usage:
        -----
        self.uploadFiles()
        
        Returns:
        -------
        NULL
        """

        for i in range(self.numBatches):
            if (self.storageFree() == True):
                print('Uploading batch %d/%d' % (i + 1, self.numBatches))
                utils.writeToLog(self.logFileName,
                                 message='Uploading batch %d/%d' %
                                 (i + 1, self.numBatches))
                tic = time.time()
                for fileName, fileSize, dropboxFile in zip(
                        self.fileNameBatch[i], self.fileSizeBatch[i],
                        self.dropboxFileBatch[i]):
                    print('%s\tMoving %s' % (utils.timestamp(), fileName))
                    utils.writeToLog(
                        self.logFileName,
                        message='%s\t%s\t%.6f' %
                        (fileName, dropboxFile, fileSize / 1024 / 1024 / 1024))
                    shutil.copy(fileName, dropboxFile)
                uploadStatus = False
                self.filesRemaining = self.dropboxWebFileBatch[i]
                while (uploadStatus == False):
                    time.sleep(self.sleepTime_min * 60)
                    uploadStatus = self.checkFilesOnWebsite()
                    toc = time.time()
                    timeElapsed = (toc - tic) / 60 / 60
                    if (timeElapsed > self.batchTimeLimit_hour):
                        utils.writeToLog(
                            self.logFileName,
                            message='Current batch upload incomplete')
                        input(
                            'Batch not uploaded! Make sure to finish batch sync and press enter to continue ...'
                        )
                        uploadStatus = True
                if (uploadStatus == True):
                    utils.deleteFiles(self.fileNameBatch[i])
    def splitFile(self, fileName):
        """ Split a large file into smaller pieces. The size of smaller
        pieces is defined by self.fileSizeLimit. Splits the file into
        chunks of self.fileSizeLimit and saves it in the same directory.
        fileName_split_0001, fileName_split_0002, ... is added at the
        end of each split. The original file is deleted after splitting.
        
        Usage:
        -----
        self.splitFile(file)
        
        Returns:
        -------
        NULL
        """

        fileSize = os.path.getsize(fileName)
        numFiles = int(fileSize / self.fileSizeLimit) + 1
        print('Splitting %s into %d parts' % (fileName, numFiles))
        utils.writeToLog(self.logFileName,
                         message='Split %s into %d parts' %
                         (fileName, numFiles))

        numChunksToRead, splitNum, chunkNum = int(
            numpy.ceil(fileSize / self.chunkSizeSplit)), 1, 0
        outputFileName = fileName
        infile = open(fileName, 'rb')
        outFile = open(outputFileName + '_split_' + str(splitNum).zfill(4),
                       'wb')
        for i in tqdm(range(numChunksToRead)):
            chunk = infile.read(self.chunkSizeSplit)
            chunkNum += 1
            if (chunkNum <= self.chunksInEachSplit):
                outFile.write(chunk)
            else:
                utils.writeToLog(self.logFileName,
                                 message='Split %d complete' % (splitNum))
                outFile.close()
                splitNum += 1
                outFile = open(
                    outputFileName + '_split_' + str(splitNum).zfill(4), 'wb')
                outFile.write(chunk)
                chunkNum = 1
        utils.writeToLog(self.logFileName,
                         message='Split %d complete' % (numFiles))
        outFile.close()
        infile.close()
        os.remove(fileName)
    def getFileList(self):
        """ Use self.df to generate the list of files that need to be
        uploaded to Dropbox.
        
        Usage:
        -----
        self.getFileList()
        
        Returns:
        -------
            NULL
            
        Creates:
        -------
        self.fileNameList : list
            List of all the files uploading to Dropbox.
        self.fileSizeList : list
            List of corresponding file size in bytes.
        """

        utils.writeToLog(self.logFileName, message='Filename\tFilesize (GB)')
        fileNameList, fileSizeList = [], []
        for inputFile in self.df.values[:, 0]:
            if (os.path.isfile(inputFile)):
                fileSize = os.path.getsize(inputFile)
                fileNameList.append(inputFile)
                fileSizeList.append(fileSize)
                utils.writeToLog(self.logFileName,
                                 message='%s\t%.6f' %
                                 (inputFile, fileSize / 1024 / 1024 / 1024))
            elif (os.path.isdir(inputFile)):
                inputDir = inputFile
                tempFileNameList, tempFileSizeList = self.getFilesinDir(
                    inputDir)
                for a, b in zip(tempFileNameList, tempFileSizeList):
                    fileNameList.append(a)
                    fileSizeList.append(b)
                    utils.writeToLog(self.logFileName,
                                     message='%s\t%.6f' %
                                     (a, b / 1024 / 1024 / 1024))
        self.fileNameList = fileNameList
        self.fileSizeList = fileSizeList
		
	# Stream Silence dialog
	elif parameter1 == 'audiooutput.streamsilence':
		utils.openDialogSelect(parameter1, parameter2, 32127)		
	
	# GUI Sound Mode dialog
	elif parameter1 == 'audiooutput.guisoundmode':
		utils.openDialogSelect(parameter1, parameter2, 32128)		
	
	# Passthrough Device dialog
	elif parameter1 == 'audiooutput.passthroughdevice':
		utils.openDialogSelect(parameter1, parameter2, 32130)	

	# Unknown dialog
	else:
		utils.writeToLog('Unknown arguments' + parameter1, xbmc.LOGERROR)

# Any other case, runs the script (try to switch audio device)		
else:
	utils.writeToLog("------ Script Started ------")
	utils.notify('Script Started')

	audiosettings = utils.getAudioSettings()
	print '-- Audio Settings Before --'
	print audiosettings

	utils.nextAudioProfile()

	audiosettings = utils.getAudioSettings()
	print '-- Audio Settings After --'
	print audiosettings
Beispiel #11
0
def talk_to_lamp():
    bulb_mac_address = config.mac
    writeToLog(f"Connecting to bulb at address {bulb_mac_address}\n")
    # version is 10 assuming all lamps deployed will be the same
    bulb = MagicBlue(bulb_mac_address, 10)
    try:
        bulb.connect()
    except:
        writeToLog("!!!SEVERE!!!\n something went wrong connecting to lamp \n !!!SEVERE!!!\n")
    try:
        with urllib.request.urlopen(config.url) as url:
            data = json.loads(url.read().decode())
            color = data['color']

        if color == "red":
            print("setting lamp to --RED--")
            writeToLog("setting lamp to --RED--\n")
            bulb.set_color([255, 0, 0])
        elif color == "green":
            bulb.set_color([0, 255, 0])
            print("setting lamp to --GREEN--")
            writeToLog("setting lamp to --GREEN--\n")
        elif color == "orange":
            bulb.set_color([255, 165, 0])
            print("setting lamp to --ORANGE--")
            writeToLog("setting lamp to --ORANGE--\n")
        else:
            bulb.set_color([0, 0, 255])
            print("INVALID color setting lamp to --BLUE--")
            writeToLog("INVALID color setting lamp to --BLUE--\n")
    except:
        print("couldn't get data from API setting lamp to --BLUE--")
        writeToLog("couldn't get data from API setting lamp to --BLUE--\n")
        bulb.set_color([0, 0, 255])
Beispiel #12
0
def InitialFunction():
    try:
        f = open("config.txt", "r")
        info = f.readlines()
        config.url = info[0].strip("\n")
        config.mac = info[1].strip("\n")
        config.logFile = info[2].strip("\n")
        f.close()

        print("Config found using previous data" + "\n")
        print(f"lamp mac address : {config.mac}----- URL : {config.url}\n")

        writeToLog("Config found using previous data" + "\n")
        writeToLog(
            f"lamp mac address : {config.mac}----- URL : {config.url}\n")
        talk_to_lamp()

    except FileNotFoundError:
        print("Starting up attempting to connect to server \n")
        writeToLog("Starting up attempting to connect to server \n")
        try:
            with urllib.request.urlopen(config.setupUrl) as url:

                data = json.loads(url.read().decode())
                config.url = data["url"]
                config.mac = data["mac"]

                print("Retrieved data from server \n")
                print(
                    f"lamp mac address : {config.mac} ----- URL : {config.url} \n"
                )
                writeToLog("Retrieved data from server \n")
                writeToLog(
                    f"lamp mac address : {config.mac} ----- URL : {config.url} \n"
                )

                print("saving to config")
                f = open("config.txt", "a+")
                f.write(config.url + "\n")
                f.write(config.mac + "\n")
                f.write(config.logFile + "\n")
                f.close()

                # we did it boys now lets create a cron job
                print("create cronjob")
                create_cron_job()
                talk_to_lamp()

        except Exception:
            print(
                "Something went wrong getting the data please validate your settings. attempting "
                "to do it again! in 60 seconds \n")
            writeToLog(
                "Something went wrong getting the data please validate your settings. attempting "
                "to do it again! \n")
            writeToLog("error type was " + str(sys.exc_info()[0]) + "\n")
            print(f"_________________\n Developer info\n {sys.exc_info()}")
            time.sleep(60)
            InitialFunction()
                       names=['inputDir'])  #,usecols=[0])
df = df.dropna(axis=0, how='all')

############################################################
# STEP 0 - GENERATE LOG FILE BASED ON TIME STAMP
logDirName = '../logs/dataProcess'
logFileName = logDirName + '/' + datetime.datetime.now().strftime(
    "%Y%m%d_%H%M%S") + '.log'
############################################################

############################################################
# STEP 1 - FIND OUT THE NUMBER OF FILES AND THEIR SIZE IN EACH
# DIRECTORY
outFile = open('directoryList.txt', 'w')
outFile.write('Directory\tNumber of files\tFile size (GB)\tFile types\n')
utils.writeToLog(logFileName,
                 message='STEP 1 - SCANNING THROUGH ALL THE FOLDERS')
utils.writeToLog(
    logFileName,
    message='Directory\tNumber of files\tFile size (GB)\tFile types')

for inputDir in df.values:
    for root, dirs, files in os.walk(inputDir[0]):
        print('Scanning %s' % (root))
        fileExtList = []
        fileSize = 0
        numFiles = 0
        for name in files:
            fileName = os.path.join(root, name)
            extension = name.split('.')[-1]
            if (extension not in exclusionList):
                fileExtList.append(extension)
    def uploadFiles(self):
        """ Uploads files to cloud using Dropbox API. If the upload
        fails, it is attempted again for a maximum of three times
        before moving on to the next file. A large file is read in
        smaller chunks and uploaded.
        
        Usage:
        -----
        self.uploadFiles()
        
        Returns:
        -------
        NULL
        """

        maxAttemptCounter = 5
        for fileName, fileSize, dropboxFile in zip(self.fileNameList,
                                                   self.fileSizeList,
                                                   self.dropboxFileList):
            if not (self.dropboxStorageFree(fileSize)):
                input(
                    'Dropbox cloud full. Make sure you have enough space and press enter.'
                )

            print('Uploading %s' % (fileName))
            numChunks = int(numpy.ceil(fileSize / self.chunkSize))
            attemptCounter, success = 1, False
            while (attemptCounter <= maxAttemptCounter and success == False):
                try:
                    f = open(fileName, 'rb')
                    if (numChunks == 1):
                        self.dbx.files_upload(f.read(), dropboxFile)
                    else:
                        for i in tqdm(range(numChunks)):
                            chunk = f.read(self.chunkSize)
                            if (i == 0):
                                upload_session_start_result = self.dbx.files_upload_session_start(
                                    chunk)
                                cursor = dropbox.files.UploadSessionCursor(
                                    session_id=upload_session_start_result.
                                    session_id,
                                    offset=f.tell())
                                commit = dropbox.files.CommitInfo(
                                    path=dropboxFile)
                            elif (i == numChunks - 1):
                                self.dbx.files_upload_session_finish(
                                    chunk, cursor, commit)
                            else:
                                self.dbx.files_upload_session_append(
                                    chunk, cursor.session_id, cursor.offset)
                                cursor.offset = f.tell()
                    f.close()
                    os.remove(fileName)
                    utils.writeToLog(
                        self.logFileName,
                        message='%s\t%s\t%.6f\tSuccessful' %
                        (fileName, dropboxFile, fileSize / 1024 / 1024 / 1024))
                    success = True
                except:
                    utils.writeToLog(
                        self.logFileName,
                        message='%s\t%s\t%.6f\tFailed' %
                        (fileName, dropboxFile, fileSize / 1024 / 1024 / 1024))
                    print("Error uploading %s. Trying again." % (fileName))
                    attemptCounter += 1
                    f.close()