def load(self): """ Reads config file to dictionary. Config file should contain key=value rows. Key can be quoted or not. Value can be one item or list of comma-separated items. Each value item can be quoted or not. When value is a single item then it creates key:value item in dictionary When value is a list of items it creates key:[value, value,...] dictionary's item. """ try: # Read configuration file into list of tuples ignoring blank # lines, lines without delimiter, and lines with comments. with open(self.fileName) as cf: res = [ reFindall(r'^\s*(.+?)\s*%s\s*(.*)$' % self.delimiter, l)[0] for l in cf if l and self.delimiter in l and l.lstrip()[0] != '#' ] self.readSuccess = True except: LOGGER.error('Config file read error: %s', self.fileName) self.readSuccess = False return False for kv, vv in res: # Parse each line # Check key key = reFindall(r'^"([^"]+)"$|^([\w-]+)$', kv) if key == []: LOGGER.warning('Wrong key in line \'%s %s %s\'', kv, self.delimiter, vv) else: # Key is OK key = key[0][0] + key[0][ 1] # Join two possible keys variants (with and without quotes) if vv.strip() == '': LOGGER.warning('No value specified in line \'%s %s %s\'', kv, self.delimiter, vv) else: # Value is not empty value = self.getValue(vv) # Parse values if value is None: LOGGER.warning('Wrong value(s) in line \'%s %s %s\'', kv, self.delimiter, vv) else: # Value is OK if key in self.keys(): # Check double values LOGGER.warning( 'Double values for one key:\n%s = %s\nand\n%s = %s\nLast one is stored.', key, self[key], key, value) self[key] = value # Store last value LOGGER.debug('Config value read as: %s = %s', key, str(value)) LOGGER.info('Config read: %s', self.fileName) return True
def checkAutoStart(path): """ Check that auto-start is enabled """ if pathExists(path): i = 1 if getenv('XDG_CURRENT_DESKTOP') in ('Unity', 'Pantheon') else 0 with open(path, 'rt') as f: attr = reFindall(r'\nHidden=(.+)|\nX-GNOME-Autostart-enabled=(.+)', f.read()) if attr: if attr[0][i] and attr[0][i] == ('true' if i else 'false'): return True else: return True return False
def activateActions(activate, appInstPath): """ Install/deinstall file extensions """ userHome = getenv("HOME") result = False try: # Catch all exceptions during FM action activation/deactivation # --- Actions for Nautilus --- if which("nautilus") is not None: LOGGER.info("Nautilus installed") ver = check_output([which("lsb_release"), "-rs"]) if ver != '' and float(ver) < 12.10: nautilusPath = ".gnome2/nautilus-scripts/" else: nautilusPath = ".local/share/nautilus/scripts" LOGGER.debug(nautilusPath) if activate: # Install actions for Nautilus copyFile( pathJoin(appInstPath, "fm-actions/Nautilus_Nemo/publish"), pathJoin(userHome, nautilusPath, _("Publish via Yandex.Disk"))) copyFile( pathJoin(appInstPath, "fm-actions/Nautilus_Nemo/unpublish"), pathJoin(userHome, nautilusPath, _("Unpublish from Yandex.disk"))) else: # Remove actions for Nautilus deleteFile( pathJoin(userHome, nautilusPath, _("Publish via Yandex.Disk"))) deleteFile( pathJoin(userHome, nautilusPath, _("Unpublish from Yandex.disk"))) result = True # --- Actions for Nemo --- if which("nemo") is not None: LOGGER.info("Nemo installed") if activate: # Install actions for Nemo copyFile( pathJoin(appInstPath, "fm-actions/Nautilus_Nemo/publish"), pathJoin(userHome, ".local/share/nemo/scripts", _("Publish via Yandex.Disk"))) copyFile( pathJoin(appInstPath, "fm-actions/Nautilus_Nemo/unpublish"), pathJoin(userHome, ".local/share/nemo/scripts", _("Unpublish from Yandex.disk"))) else: # Remove actions for Nemo deleteFile( pathJoin(userHome, ".gnome2/nemo-scripts", _("Publish via Yandex.Disk"))) deleteFile( pathJoin(userHome, ".gnome2/nemo-scripts", _("Unpublish from Yandex.disk"))) result = True # --- Actions for Thunar --- if which("thunar") is not None: LOGGER.info("Thunar installed") ucaPath = pathJoin(userHome, ".config/Thunar/uca.xml") # Read uca.xml with open(ucaPath) as ucaf: [(ust, actions, uen)] = reFindall(r'(^.*<actions>)(.*)(<\/actions>)', ucaf.read(), reS) acts = reFindall(r'(<action>.*?<\/action>)', actions, reS) nActs = { reFindall(r'<name>(.+?)<\/name>', u, reS)[0]: u for u in acts } if activate: # Install actions for Thunar if _("Publish via Yandex.Disk") not in nActs.keys(): nActs[_("Publish via Yandex.Disk")] = ( "<action><icon>folder-publicshare</icon>" + '<name>' + _("Publish via Yandex.Disk") + '</name><command>yandex-disk publish %f | xclip -filter -selection' + ' clipboard; zenity --info ' + '--window-icon=/usr/share/yd-tools/icons/yd-128.png ' + '--title="Yandex.Disk" --ok-label="' + _('Close') + '" --text="' + _('URL to file: %f was copied into clipboard.') + '"</command><description/><patterns>*</patterns>' + '<directories/><audio-files/><image-files/><other-files/>' + "<text-files/><video-files/></action>") if _("Unpublish from Yandex.disk") not in nActs.keys(): nActs[_("Unpublish from Yandex.disk")] = ( "<action><icon>folder</icon><name>" + _("Unpublish from Yandex.disk") + '</name><command>zenity --info ' + '--window-icon=/usr/share/yd-tools/icons/yd-128_g.png --ok-label="' + _('Close') + '" --title="Yandex.Disk" --text="' + _("Unpublish from Yandex.disk") + ': `yandex-disk unpublish %f`"</command>' + '<description/><patterns>*</patterns>' + '<directories/><audio-files/><image-files/><other-files/>' + "<text-files/><video-files/></action>") else: # Remove actions for Thunar if _("Publish via Yandex.Disk") in nActs.keys(): del nActs[_("Publish via Yandex.Disk")] if _("Unpublish from Yandex.disk") in nActs.keys(): del nActs[_("Unpublish from Yandex.disk")] # Save uca.xml with open(ucaPath, 'wt') as ucaf: ucaf.write(ust + ''.join(u for u in nActs.values()) + uen) result = True # --- Actions for Dolphin --- if which("dolphin") is not None: LOGGER.info("Dolphin installed") if activate: # Install actions for Dolphin makeDirs( pathJoin(userHome, '.local/share/kservices5/ServiceMenus')) copyFile( pathJoin(appInstPath, "fm-actions/Dolphin/ydpublish.desktop"), pathJoin( userHome, ".local/share/kservices5/ServiceMenus/ydpublish.desktop" )) else: # Remove actions for Dolphin deleteFile( pathJoin( userHome, ".local/share/kservices5/ServiceMenus/ydpublish.desktop" )) result = True # --- Actions for Pantheon-files --- if which("pantheon-files") is not None: LOGGER.info("Pantheon-files installed") ctrs_path = "/usr/share/contractor/" gksudo = which("gksudo") if activate: # Install actions for Pantheon-files src_path = pathJoin(appInstPath, "fm-actions", "pantheon-files") ctr_pub = pathJoin(src_path, "yandex-disk-indicator-publish.contract") ctr_unpub = pathJoin( src_path, "yandex-disk-indicator-unpublish.contract") res = call([ gksudo, "-D", "yd-tools", "cp", ctr_pub, ctr_unpub, ctrs_path ]) if res == 0: result = True else: LOGGER.error("Cannot enable actions for Pantheon-files") else: # Remove actions for Pantheon-files res = call([ gksudo, "-D", "yd-tools", "rm", pathJoin(ctrs_path, "yandex-disk-indicator-publish.contract"), pathJoin(ctrs_path, "yandex-disk-indicator-unpublish.contract") ]) if res == 0: result = True else: LOGGER.error("Cannot disable actions for Pantheon-files") # --- Actions for Caja --- if which("caja") is not None: LOGGER.info("Caja installed") if activate: # Install actions for Nemo copyFile( pathJoin(appInstPath, "fm-actions/Nautilus_Nemo/publish"), pathJoin(userHome, ".config/caja/scripts", _("Publish via Yandex.Disk"))) copyFile( pathJoin(appInstPath, "fm-actions/Nautilus_Nemo/unpublish"), pathJoin(userHome, ".config/caja/scripts", _("Unpublish from Yandex.disk"))) else: # Remove actions for Nemo deleteFile( pathJoin(userHome, ".config/caja/scripts", _("Publish via Yandex.Disk"))) deleteFile( pathJoin(userHome, ".config/caja/scripts", _("Unpublish from Yandex.disk"))) result = True except Exception as e: LOGGER.error( "The following error occurred during the FM actions activation:\n %s", str(e)) return result
def dataRecode(self, dat): """ recode from line dat to writing row dat; no remain time. """ step = self.curSheetRow - 1 if dat[-1].find('Temp') != -1 or \ dat[-1].find('TEMP') != -1 or \ dat[-1].find('temp') != -1: dat[-1] = reFindall(r"\d+\.?\d*", dat[-1])[0] if int(dat[3]) >= 60: stepTime = '1:' + str(int(dat[3]) - 60).zfill(2) + ':' + str( int(dat[4])).zfill(2) else: stepTime = '0:' + str(int(dat[3])).zfill(2) + ':' + str(int( dat[4])).zfill(2) if dat[1].find("Temp")!=-1 or \ dat[1].find("TEMP")!=-1 or \ dat[1].find("temp")!=-1: if dat[0] in list(workHeadDictH)[5:10]: if dat[-2] in list(heatRankDict): ctrlMess = heatRankDict[dat[-2]] + '档加热至' + dat[ -1] + '度' + '\n' + '(' + workHeadDictH[dat[0]] + ')' elif dat[0] == list(workHeadDictH)[2]: ctrlMess = heatRankDict[dat[-2]] + '档' + workHeadDictH[ dat[0]] + '至' + dat[-1] + '度' elif dat[0] == list(workHeadDictH)[1]: ctrlMess = heatRankDict[dat[-1]] + '档' + workHeadDictH[dat[0]] elif dat[0] == list(workHeadDictH)[0]: ctrlMess = workHeadDictH[dat[0]] elif dat[0] == list(workHeadDictH)[4]: return self.repeatStepDeal(stepTime, int(dat[-2]), int(dat[-1])) # ctrlMess = workHeadDictH[dat[0]]+'上'+dat[-2]+'到'+dat[-1]+'步' else: ctrlMess = dat[0] else: if dat[0] in list(workHeadDictH)[5:10]: if dat[-2] in list(heatRankDict): ctrlMess = heatRankDict[dat[ -2]] + '档加热' + '\n' + '(' + workHeadDictH[dat[0]] + ')' elif dat[0] == list(workHeadDictH)[2]: ctrlMess = heatRankDict[dat[-2]] + '档' + workHeadDictH[dat[0]] elif dat[0] == list(workHeadDictH)[1]: ctrlMess = heatRankDict[dat[-1]] + '档' + workHeadDictH[dat[0]] elif dat[0] == list(workHeadDictH)[0]: ctrlMess = workHeadDictH[dat[0]] elif dat[0] == list(workHeadDictH)[4]: # ctrlMess = workHeadDictH[dat[0]]+'上'+dat[-2]+'到'+dat[-1]+'步' return self.repeatStepDeal(stepTime, int(dat[-2]), int(dat[-1])) else: ctrlMess = workHeadDictH[dat[0]] remainTime = "0:00:00" endCondition = '以' + workHeadDictL[dat[1]] if dat[-1] == 'DISP_UPDATE': endCondition = endCondition + "(更新时间)" self.inputDatTmp.append( [step, ctrlMess, stepTime, remainTime, endCondition]) return [self.inputDatTmp[-1]]
def rowdatRecode(self, rowDat: list): """ recode the row dat to c51 code. """ self.recodeRowDat = [] headH, headL, stepTime = 1, 4, 2 valueTmpHeadH = None for value in workHeadDictH.values(): #work head high bit recode if value!=workHeadDictH[list(workHeadDictH)[1]] and \ value!=workHeadDictH[list(workHeadDictH)[2]]:#other if rowDat[headH].find(value) != -1: valueTmpHeadH = value break else: #motor or heater if rowDat[headH].find(value) != -1: valueTmpHeadH = value if valueTmpHeadH != None: #not menu code self.recodeRowDat.append( self.get_key(workHeadDictH, valueTmpHeadH)[0]) else: return for value in workHeadDictL.values(): #work head lower bit recode if rowDat[headL].find('(') == -1: #time display update if rowDat[headL] == "以{}".format(value): self.recodeRowDat.append( self.get_key(workHeadDictL, value)[0]) break else: #normal end condition if rowDat[headL][:rowDat[headL].find('(')] == "以{}".format( value): self.recodeRowDat.append( self.get_key(workHeadDictL, value)[0]) break timeFormat = "%H:%M:%S" stepDatetime = datetime.strptime(rowDat[stepTime], timeFormat) #step time self.recodeRowDat.append("TIME_x_M_y_S({}, {})".format( stepDatetime.hour * 60 + stepDatetime.minute, stepDatetime.second)) if self.recodeRowDat[0]==list(workHeadDictH)[2] or \ self.recodeRowDat[0] in list(workHeadDictH)[5:10]:#heater rank = float(reFindall(r"\d+\.?\d*", rowDat[headH])[0]) self.recodeRowDat.append("HEAT_L{}_{}_{}".format( int(rank / 10), int(rank % 10), int(rank * 10 % 10))) elif self.recodeRowDat[0]==list(workHeadDictH)[1] or \ self.recodeRowDat[0]==list(workHeadDictH)[-2]:#motor self.recodeRowDat.append("MOTOR_MODE") elif self.recodeRowDat[0] == list(workHeadDictH)[4]: #repeat rank = int(reFindall(r"\d+\.?\d*", rowDat[headH])[0]) self.recodeRowDat.append(str(rank)) else: self.recodeRowDat.append("0") if rowDat[headL].find( "更新时间") != -1: #ctrl target for time display display self.recodeRowDat.append('DISP_UPDATE') else: #normal ctrl target if self.recodeRowDat[0]==list(workHeadDictH)[1] or \ self.recodeRowDat[0]==list(workHeadDictH)[-2]: #motor rank = int(reFindall(r"\d+\.?\d*", rowDat[headH])[0]) self.recodeRowDat.append("L{}".format( str(rank).replace('.', '_'))) elif self.recodeRowDat[0] in list(workHeadDictH)[5:-1] or \ self.recodeRowDat[0]==list(workHeadDictH)[2]: #heater try: rank = int(reFindall(r"\d+\.?\d*", rowDat[headH])[1]) except: rank = 0 self.recodeRowDat.append(str(rank)) elif self.recodeRowDat[0] == list(workHeadDictH)[4]: #repeat rank = int(reFindall(r"\d+\.?\d*", rowDat[headH])[1]) self.recodeRowDat.append(str(rank)) else: self.recodeRowDat.append('0') #no target