def updateTextViewType(self, viewType, hashBefore, hashAfter): event = self.event text = "" if viewType == "After change (Text)": if hashAfter != "": text = event.getRevision(hashAfter).getInfo() elif viewType == "Before change (Text)": if hashBefore != "": text = event.getRevision(hashBefore).getInfo() elif viewType == "After change (JSON)": if hashAfter != "": text = dataToPrettyJson(event.getRevision(hashAfter).getData()) elif viewType == "After change (Plain JSON)": if hashAfter != "": text = dataToPrettyJson(self.getObjectData(hashAfter)) elif viewType == "Before change (JSON)": if hashBefore != "": text = dataToPrettyJson( event.getRevision(hashBefore).getData()) elif viewType == "Before change (Plain JSON)": if hashBefore != "": text = dataToPrettyJson(self.getObjectData(hashBefore)) elif viewType == "Change (JSON Diff)": if hashBefore != "" and hashAfter != "": diff = self.extractChangeDiff(hashBefore, hashAfter) text = dataToPrettyJson(diff) else: raise ValueError("Unexpected viewType=%r" % viewType) self.textbuff.set_text(text) self.setScrolledWinChild(self.textview)
def updateTextViewType(self, viewType, hashBefore, hashAfter): event = self.event text = "" if viewType == "After change (Text)": if hashAfter != "": text = event.getRevision(hashAfter).getInfo() elif viewType == "Before change (Text)": if hashBefore != "": text = event.getRevision(hashBefore).getInfo() elif viewType == "After change (JSON)": if hashAfter != "": text = dataToPrettyJson(event.getRevision(hashAfter).getData()) elif viewType == "After change (Plain JSON)": if hashAfter != "": text = dataToPrettyJson(self.getObjectData(hashAfter)) elif viewType == "Before change (JSON)": if hashBefore != "": text = dataToPrettyJson(event.getRevision(hashBefore).getData()) elif viewType == "Before change (Plain JSON)": if hashBefore != "": text = dataToPrettyJson(self.getObjectData(hashBefore)) elif viewType == "Change (JSON Diff)": if hashBefore != "" and hashAfter != "": diff = self.extractChangeDiff(hashBefore, hashAfter) text = dataToPrettyJson(diff) else: raise ValueError("Unexpected viewType=%r" % viewType) self.textbuff.set_text(text) self.setScrolledWinChild(self.textview)
def importTrashIter(): yield 1 yield 0 jsonPath = join(oldEventDir, 'trash.json') newJsonPath = join(newEventDir, 'trash.json') try: data = json.loads(open(jsonPath).read()) except Exception as e: print(e) return if 'history' in data: print('skipping "%s": history already exists' % jsonPath) return try: tm = data.pop('modified') except KeyError: tm = now() ### basicData = {} #basicData['modified'] = tm ### ## remove extra params from data and add to basicData for param in ('idList', ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData['history'] = [(tm, _hash)] open(newJsonPath, 'w').write(dataToPrettyJson(basicData, sort_keys=True))
def importTrashIter(): yield 1 yield 0 jsonPath = join(oldEventDir, 'trash.json') newJsonPath = join(newEventDir, 'trash.json') try: data = json.loads(open(jsonPath).read()) except Exception as e: print(e) return if 'history' in data: print('skipping "%s": history already exists'%jsonPath) return try: tm = data.pop('modified') except KeyError: tm = now() ### basicData = {} #basicData['modified'] = tm ### ## remove extra params from data and add to basicData for param in ( 'idList', ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData['history'] = [(tm, _hash)] open(newJsonPath, 'w').write(dataToPrettyJson(basicData, sort_keys=True))
def importTrashIter(): yield 1 yield 0 jsonPath = join(oldEventDir, "trash.json") newJsonPath = join(newEventDir, "trash.json") try: data = json.loads(open(jsonPath).read()) except Exception as e: print(e) return if "history" in data: print("skipping \"%s\": history already exists" % jsonPath) return try: tm = data.pop("modified") except KeyError: tm = now() ### basicData = {} #basicData["modified"] = tm ### ## remove extra params from data and add to basicData for param in ("idList", ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData["history"] = [(tm, _hash)] open(newJsonPath, "w").write(dataToPrettyJson(basicData, sort_keys=True))
def importTrashIter(): yield 1 yield 0 jsonPath = join(oldEventDir, "trash.json") newJsonPath = join(newEventDir, "trash.json") try: data = json.loads(open(jsonPath).read()) except Exception as e: print(e) return if "history" in data: print("skipping \"%s\": history already exists" % jsonPath) return try: tm = data.pop("modified") except KeyError: tm = now() ### basicData = {} #basicData["modified"] = tm ### ## remove extra params from data and add to basicData for param in ( "idList", ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData["history"] = [(tm, _hash)] open(newJsonPath, "w").write(dataToPrettyJson(basicData, sort_keys=True))
def writeJsonConf(name, data): if data is None: return fname = name + '.json' jsonPath = join(newConfDir, fname) text = dataToPrettyJson(data, sort_keys=True) try: open(jsonPath, 'w').write(text) except Exception as e: print('failed to write file %r: %s' % (jsonPath, e))
def writeJsonConf(name, data): if data is None: return fname = name + '.json' jsonPath = join(newConfDir, fname) text = dataToPrettyJson(data, sort_keys=True) try: open(jsonPath, 'w').write(text) except Exception as e: print('failed to write file %r: %s'%(jsonPath, e))
def checkAndSaveJsonLockFile(fpath): locked = False if isfile(fpath): try: text = open(fpath).read() except: myRaise() locked = True else: try: data = jsonToData(text) except: print('lock file %s is not valid' % fpath) else: try: pid = data['pid'] cmd = data['cmd'] except: print('lock file %s is not valid' % fpath) else: try: proc = psutil.Process(pid) except psutil.NoSuchProcess: print('lock file %s: pid %s does not exist' % (fpath, pid)) else: if get_cmdline(proc) == cmd: locked = True else: print('lock file %s: cmd does match: %s != %s' % (fpath, get_cmdline(proc), cmd)) elif exists(fpath): ## what to do? FIXME pass ###### if not locked: my_pid = os.getpid() my_proc = psutil.Process(my_pid) my_cmd = get_cmdline(my_proc) my_text = dataToPrettyJson( OrderedDict([ ('pid', my_pid), ('cmd', my_cmd), ('time', now()), ])) try: open(fpath, 'w').write(my_text) except Exception as e: print('failed to write lock file %s: %s' % (fpath, e)) else: atexit.register(os.remove, fpath) ###### return locked
def importAccountsIter(): makeDir(newAccountsDir) ### oldFiles = os.listdir(oldAccountsDir) yield len(oldFiles) index = 0 ### for fname in oldFiles: yield index; index += 1 jsonPath = join(oldAccountsDir, fname) newJsonPath = join(newAccountsDir, fname) if not isfile(jsonPath): print("\"%s\": not such file" % jsonPath) continue jsonPathNoX, ext = splitext(fname) if ext != ".json": continue try: _id = int(jsonPathNoX) except ValueError: continue try: data = json.loads(open(jsonPath).read()) except Exception as e: print("error while loading json file \"%s\"" % jsonPath) continue if "history" in data: print("skipping \"%s\": history already exists" % jsonPath) continue try: tm = data.pop("modified") except KeyError: tm = now() ### basicData = {} #basicData["modified"] = tm ### ## remove extra params from data and add to basicData for param in ( "enable", ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData["history"] = [(tm, _hash)] open(newJsonPath, "w").write( dataToPrettyJson(basicData, sort_keys=True) )
def checkAndSaveJsonLockFile(fpath): locked = False if isfile(fpath): try: text = open(fpath).read() except: myRaise() locked = True else: try: data = jsonToData(text) except: print('lock file %s is not valid'%fpath) else: try: pid = data['pid'] cmd = data['cmd'] except: print('lock file %s is not valid'%fpath) else: try: proc = psutil.Process(pid) except psutil.NoSuchProcess: print('lock file %s: pid %s does not exist'%(fpath, pid)) else: if get_cmdline(proc) == cmd: locked = True else: print('lock file %s: cmd does match: %s != %s'%(fpath, get_cmdline(proc), cmd)) elif exists(fpath): ## what to do? FIXME pass ###### if not locked: my_pid = os.getpid() my_proc = psutil.Process(my_pid) my_cmd = get_cmdline(my_proc) my_text = dataToPrettyJson(OrderedDict([ ('pid', my_pid), ('cmd', my_cmd), ('time', now()), ])) try: open(fpath, 'w').write(my_text) except Exception as e: print('failed to write lock file %s: %s'%(fpath, e)) else: atexit.register(os.remove, fpath) ###### return locked
def importEventsIter(): makeDir(newEventEventsDir) oldFiles = os.listdir(oldEventEventsDir) yield len(oldFiles) index = 0 for dname in oldFiles: yield index index += 1 #### try: _id = int(dname) except ValueError: continue dpath = join(oldEventEventsDir, dname) newDpath = join(newEventEventsDir, dname) if not isdir(dpath): print('"%s" must be a directory' % dpath) continue jsonPath = join(dpath, 'event.json') if not isfile(jsonPath): print('"%s": not such file' % jsonPath) continue try: data = json.loads(open(jsonPath).read()) except Exception as e: print('error while loading json file "%s"' % jsonPath) continue try: tm = data.pop('modified') except KeyError: tm = now() ### basicData = {} #basicData['modified'] = tm ### ## remove extra params from data and add to basicData for param in ( 'remoteIds', 'notifiers', ## FIXME ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData['history'] = [(tm, _hash)] open(newDpath + '.json', 'w').write(dataToPrettyJson(basicData, sort_keys=True))
def importEventsIter(): makeDir(newEventEventsDir) oldFiles = os.listdir(oldEventEventsDir) yield len(oldFiles) index = 0 for dname in oldFiles: yield index; index += 1 #### try: _id = int(dname) except ValueError: continue dpath = join(oldEventEventsDir, dname) newDpath = join(newEventEventsDir, dname) if not isdir(dpath): print("\"%s\" must be a directory" % dpath) continue jsonPath = join(dpath, "event.json") if not isfile(jsonPath): print("\"%s\": not such file" % jsonPath) continue try: data = json.loads(open(jsonPath).read()) except Exception as e: print("error while loading json file \"%s\"" % jsonPath) continue try: tm = data.pop("modified") except KeyError: tm = now() ### basicData = {} #basicData["modified"] = tm ### ## remove extra params from data and add to basicData for param in ( "remoteIds", "notifiers",## FIXME ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData["history"] = [(tm, _hash)] open(newDpath + ".json", "w").write( dataToPrettyJson(basicData, sort_keys=True) )
def importAccountsIter(): makeDir(newAccountsDir) ### oldFiles = os.listdir(oldAccountsDir) yield len(oldFiles) index = 0 ### for fname in oldFiles: yield index index += 1 jsonPath = join(oldAccountsDir, fname) newJsonPath = join(newAccountsDir, fname) if not isfile(jsonPath): print('"%s": not such file' % jsonPath) continue jsonPathNoX, ext = splitext(fname) if ext != '.json': continue try: _id = int(jsonPathNoX) except ValueError: continue try: data = json.loads(open(jsonPath).read()) except Exception as e: print('error while loading json file "%s"' % jsonPath) continue if 'history' in data: print('skipping "%s": history already exists' % jsonPath) continue try: tm = data.pop('modified') except KeyError: tm = now() ### basicData = {} #basicData['modified'] = tm ### ## remove extra params from data and add to basicData for param in ('enable', ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData['history'] = [(tm, _hash)] open(newJsonPath, 'w').write(dataToPrettyJson(basicData, sort_keys=True))
#!/usr/bin/env python3 import os import sys import json from scal3.s_object import loadBsonObject from scal3.json_utils import dataToPrettyJson from scal3 import event_lib if __name__ == "__main__": eventId = int(sys.argv[1]) with open(event_lib.Event.getFile(eventId)) as eventFile: eventJsonData = json.load(eventFile) lastHash = eventJsonData["history"][0][1] data = loadBsonObject(lastHash) print(dataToPrettyJson(data))
def importGroupsIter(): groupsEnableDict = {} ## {groupId -> enable} ### makeDir(newGroupsDir) ### oldFiles = os.listdir(oldGroupsDir) yield len(oldFiles) + 1 index = 0 ### for fname in oldFiles: yield index ; index += 1 jsonPath = join(oldGroupsDir, fname) newJsonPath = join(newGroupsDir, fname) if not isfile(jsonPath): print('"%s": not such file'%jsonPath) continue jsonPathNoX, ext = splitext(fname) if ext != '.json': continue try: _id = int(jsonPathNoX) except ValueError: continue try: data = json.loads(open(jsonPath).read()) except Exception as e: print('error while loading json file "%s"'%jsonPath) continue #### groupsEnableDict[_id] = data.pop('enable', True) #### if 'history' in data: print('skipping "%s": history already exists'%jsonPath) continue try: tm = data.pop('modified') except KeyError: tm = now() ### basicData = {} #basicData['modified'] = tm ### ## remove extra params from data and add to basicData for param in ( 'remoteIds', ): basicData[param] = data.pop(param, None) for param in ( 'enable', 'idList', 'remoteSyncData', 'deletedRemoteEvents', ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData['history'] = [(tm, _hash)] open(newJsonPath, 'w').write(dataToPrettyJson(basicData, sort_keys=True)) #### yield index ; index += 1 oldGroupListFile = join(oldEventDir, 'group_list.json') newGroupListFile = join(newEventDir, 'group_list.json') try: groupIds = json.loads(open(oldGroupListFile).read()) except Exception as e: print('error while loading %s: %s'%(oldGroupListFile, e)) else: if isinstance(groupIds, list): signedGroupIds = [ (1 if groupsEnableDict.get(gid, True) else -1) * gid \ for gid in groupIds ] try: open(newGroupListFile, 'w').write(dataToPrettyJson(signedGroupIds)) except Exception as e: print('error while writing %s: %s'%(newGroupListFile, e)) else: print('file "%s" contains invalid data, must contain a list'%oldGroupListFile)
def checkAndSaveJsonLockFile(fpath): locked = False my_pid = os.getpid() if isfile(fpath): try: text = open(fpath).read() except: myRaise() locked = True else: try: data = jsonToData(text) except: print("lock file %s is not valid" % fpath) else: try: pid = data["pid"] cmd = data["cmd"] except: print("lock file %s is not valid" % fpath) else: try: proc = psutil.Process(pid) except psutil.NoSuchProcess: print("lock file %s: pid %s does not exist" % (fpath, pid)) else: if pid == my_pid: print("lock file %s: pid == my_pid == %s" % ( fpath, pid, )) elif get_cmdline(proc) != cmd: print("lock file %s: cmd does match: %s != %s" % ( fpath, get_cmdline(proc), cmd, )) else: locked = True elif exists(fpath): ## what to do? FIXME pass ###### if not locked: my_proc = psutil.Process(my_pid) my_cmd = get_cmdline(my_proc) my_text = dataToPrettyJson( OrderedDict([ ("pid", my_pid), ("cmd", my_cmd), ("time", now()), ])) try: open(fpath, "w").write(my_text) except Exception as e: print("failed to write lock file %s: %s" % (fpath, e)) else: atexit.register(os.remove, fpath) ###### return locked
def importGroupsIter(): groupsEnableDict = {} ## {groupId -> enable} ### makeDir(newGroupsDir) ### oldFiles = os.listdir(oldGroupsDir) yield len(oldFiles) + 1 index = 0 ### for fname in oldFiles: yield index; index += 1 jsonPath = join(oldGroupsDir, fname) newJsonPath = join(newGroupsDir, fname) if not isfile(jsonPath): print("\"%s\": not such file" % jsonPath) continue jsonPathNoX, ext = splitext(fname) if ext != ".json": continue try: _id = int(jsonPathNoX) except ValueError: continue try: data = json.loads(open(jsonPath).read()) except Exception as e: print("error while loading json file \"%s\"" % jsonPath) continue #### groupsEnableDict[_id] = data.pop("enable", True) #### if "history" in data: print("skipping \"%s\": history already exists" % jsonPath) continue try: tm = data.pop("modified") except KeyError: tm = now() ### basicData = {} #basicData["modified"] = tm ### ## remove extra params from data and add to basicData for param in ( "remoteIds", ): basicData[param] = data.pop(param, None) for param in ( "enable", "idList", "remoteSyncData", "deletedRemoteEvents", ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData["history"] = [(tm, _hash)] open(newJsonPath, "w").write(dataToPrettyJson(basicData, sort_keys=True)) #### yield index; index += 1 oldGroupListFile = join(oldEventDir, "group_list.json") newGroupListFile = join(newEventDir, "group_list.json") try: groupIds = json.loads(open(oldGroupListFile).read()) except Exception as e: print("error while loading %s: %s" % (oldGroupListFile, e)) else: if isinstance(groupIds, list): signedGroupIds = [ (1 if groupsEnableDict.get(gid, True) else -1) * gid for gid in groupIds ] try: open(newGroupListFile, "w").write(dataToPrettyJson(signedGroupIds)) except Exception as e: print("error while writing %s: %s" % (newGroupListFile, e)) else: print( "file \"%s\" contains invalid data" % oldGroupListFile + ", must contain a list" )
def importGroupsIter(): groupsEnableDict = {} ## {groupId -> enable} ### makeDir(newGroupsDir) ### oldFiles = os.listdir(oldGroupsDir) yield len(oldFiles) + 1 index = 0 ### for fname in oldFiles: yield index index += 1 jsonPath = join(oldGroupsDir, fname) newJsonPath = join(newGroupsDir, fname) if not isfile(jsonPath): print('"%s": not such file' % jsonPath) continue jsonPathNoX, ext = splitext(fname) if ext != '.json': continue try: _id = int(jsonPathNoX) except ValueError: continue try: data = json.loads(open(jsonPath).read()) except Exception as e: print('error while loading json file "%s"' % jsonPath) continue #### groupsEnableDict[_id] = data.pop('enable', True) #### if 'history' in data: print('skipping "%s": history already exists' % jsonPath) continue try: tm = data.pop('modified') except KeyError: tm = now() ### basicData = {} #basicData['modified'] = tm ### ## remove extra params from data and add to basicData for param in ('remoteIds', ): basicData[param] = data.pop(param, None) for param in ( 'enable', 'idList', 'remoteSyncData', 'deletedRemoteEvents', ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData['history'] = [(tm, _hash)] open(newJsonPath, 'w').write(dataToPrettyJson(basicData, sort_keys=True)) #### yield index index += 1 oldGroupListFile = join(oldEventDir, 'group_list.json') newGroupListFile = join(newEventDir, 'group_list.json') try: groupIds = json.loads(open(oldGroupListFile).read()) except Exception as e: print('error while loading %s: %s' % (oldGroupListFile, e)) else: if isinstance(groupIds, list): signedGroupIds = [ (1 if groupsEnableDict.get(gid, True) else -1) * gid \ for gid in groupIds ] try: open(newGroupListFile, 'w').write(dataToPrettyJson(signedGroupIds)) except Exception as e: print('error while writing %s: %s' % (newGroupListFile, e)) else: print('file "%s" contains invalid data, must contain a list' % oldGroupListFile)
def importGroupsIter(): groupsEnableDict = {} ## {groupId -> enable} ### makeDir(newGroupsDir) ### oldFiles = os.listdir(oldGroupsDir) yield len(oldFiles) + 1 index = 0 ### for fname in oldFiles: yield index index += 1 jsonPath = join(oldGroupsDir, fname) newJsonPath = join(newGroupsDir, fname) if not isfile(jsonPath): print("\"%s\": not such file" % jsonPath) continue jsonPathNoX, ext = splitext(fname) if ext != ".json": continue try: _id = int(jsonPathNoX) except ValueError: continue try: data = json.loads(open(jsonPath).read()) except Exception as e: print("error while loading json file \"%s\"" % jsonPath) continue #### groupsEnableDict[_id] = data.pop("enable", True) #### if "history" in data: print("skipping \"%s\": history already exists" % jsonPath) continue try: tm = data.pop("modified") except KeyError: tm = now() ### basicData = {} #basicData["modified"] = tm ### ## remove extra params from data and add to basicData for param in ("remoteIds", ): basicData[param] = data.pop(param, None) for param in ( "enable", "idList", "remoteSyncData", "deletedRemoteEvents", ): try: basicData[param] = data.pop(param) except KeyError: pass ### _hash = saveBsonObject(data) basicData["history"] = [(tm, _hash)] open(newJsonPath, "w").write(dataToPrettyJson(basicData, sort_keys=True)) #### yield index index += 1 oldGroupListFile = join(oldEventDir, "group_list.json") newGroupListFile = join(newEventDir, "group_list.json") try: groupIds = json.loads(open(oldGroupListFile).read()) except Exception as e: print("error while loading %s: %s" % (oldGroupListFile, e)) else: if isinstance(groupIds, list): signedGroupIds = [ (1 if groupsEnableDict.get(gid, True) else -1) * gid for gid in groupIds ] try: open(newGroupListFile, "w").write(dataToPrettyJson(signedGroupIds)) except Exception as e: print("error while writing %s: %s" % (newGroupListFile, e)) else: print("file \"%s\" contains invalid data" % oldGroupListFile + ", must contain a list")