def package(id): try: data = toDict(PYLOAD.getPackageData(id)) data["links"] = [toDict(x) for x in data["links"]] for pyfile in data["links"]: if pyfile["status"] == 0: pyfile["icon"] = "status_finished.png" elif pyfile["status"] in (2, 3): pyfile["icon"] = "status_queue.png" elif pyfile["status"] in (9, 1): pyfile["icon"] = "status_offline.png" elif pyfile["status"] == 5: pyfile["icon"] = "status_waiting.png" elif pyfile["status"] == 8: pyfile["icon"] = "status_failed.png" elif pyfile["status"] == 4: pyfile["icon"] = "arrow_right.png" elif pyfile["status"] in (11, 13): pyfile["icon"] = "status_proc.png" else: pyfile["icon"] = "status_downloading.png" tmp = data["links"] tmp.sort(key=get_sort_key) data["links"] = tmp return data except: print_exc() return HTTPError()
def parseEXTHHeader(self): headerfmt = '>III' headerlen = calcsize(headerfmt) fields = [ 'identifier', 'header length', 'record Count' ] # unpack header, zip up into list of tuples results = zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen])) # convert tuple array to dictionary resultsDict = utils.toDict(results); self.offset += headerlen; resultsDict['records'] = {}; for record in range(resultsDict['record Count']): recordType, recordLen = unpack(">II", self.contents[self.offset:self.offset+8]); recordData = self.contents[self.offset+8:self.offset+recordLen]; resultsDict['records'][recordType] = recordData; self.offset += recordLen; return resultsDict;
def admin(): # convert to dict user = dict([(name, toDict(y)) for name, y in PYLOAD.getAllUserData().iteritems()]) perms = permlist() for data in user.itervalues(): data["perms"] = {} get_permission(data["perms"], data["permission"]) data["perms"]["admin"] = True if data["role"] is 0 else False s = request.environ.get('beaker.session') if request.environ.get('REQUEST_METHOD', "GET") == "POST": for name in user: if request.POST.get("%s|admin" % name, False): user[name]["role"] = 0 user[name]["perms"]["admin"] = True elif name != s["name"]: user[name]["role"] = 1 user[name]["perms"]["admin"] = False # set all perms to false for perm in perms: user[name]["perms"][perm] = False for perm in request.POST.getall("%s|perms" % name): user[name]["perms"][perm] = True user[name]["permission"] = set_permission(user[name]["perms"]) PYLOAD.setUserPermission(name, user[name]["permission"], user[name]["role"]) return render_to_response("admin.html", {"users": user, "permlist": perms}, [pre_processor])
def parseHeader(self): headerfmt = '>32shhIIIIII4s4sIIH' headerlen = calcsize(headerfmt) fields = [ "name", "attributes", "version", "created", "modified", "backup", "modnum", "appInfoId", "sortInfoID", "type", "creator", "uniqueIDseed", "nextRecordListID", "number of records" ] # unpack header, zip up into list of tuples results = zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen])) # increment offset into file self.offset += headerlen # convert tuple array to dictionary resultsDict = utils.toDict(results); return resultsDict
def parseRecordInfoList(self): records = {}; # read in all records in info list for recordID in range(self.header['number of records']): headerfmt = '>II' headerlen = calcsize(headerfmt) fields = [ "record Data Offset", "UniqueID", ] # create tuple with info results = zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen])) # increment offset into file self.offset += headerlen # convert tuple to dictionary resultsDict = utils.toDict(results); # futz around with the unique ID record, as the uniqueID's top 8 bytes are # really the "record attributes": resultsDict['record Attributes'] = (resultsDict['UniqueID'] & 0xFF000000) >> 24; resultsDict['UniqueID'] = resultsDict['UniqueID'] & 0x00FFFFFF; # store into the records dict records[resultsDict['UniqueID']] = resultsDict; return records;
def status(): try: status = toDict(PYLOAD.statusServer()) status['captcha'] = PYLOAD.isCaptchaWaiting() return status except: return HTTPError()
def deleteUser(id): if UserModel.r_id.match(id) == None: raise gen.Return('wrong id') old_val = yield UserModel.getUser(id) yield UserModel.sql('UPDATE "user" SET is_deleted=True WHERE id=%s', id) yield LogModel.write('user', old_val[0]['description'], 'delete', utils.toDict(old_val[0]), None)
def __cmd(cmd): cmd_result = '' cmdStr = "/root/dstrad %s" % cmd # logging.info("cmd:%s" % cmdStr) cmd_result_cxt = Popen(cmdStr, stdout=PIPE, shell=True).stdout.read().decode("utf-8") try: data = json.loads(cmd_result_cxt) if isinstance(data, dict): cmd_result = toDict(data) elif isinstance(data, list): dictData = [] for d in data: dictData.append(toDict(d)) cmd_result = dictData except JSONDecodeError: raise Exception(cmd_result_cxt) return cmd_result
def addUser(login, password, password2, interface_access = False, description = '', key = '', nodeUserAccessList = [], nodeSudoAccessList = [], classUserAccessList = [], classSudoAccessList = []): if password != password2: raise gen.Return('password1 != password2') if UserModel.r_log.match(login) == None or \ UserModel.r_pass.match(password) == None or \ UserModel.r_desc.match(description) == None or \ not (interface_access in (True, False)): raise gen.Return('description, login or password are wrong') ids = [] ids.extend(nodeUserAccessList) ids.extend(nodeSudoAccessList) ids.extend(classUserAccessList) ids.extend(classSudoAccessList) for _id in ids: if UserModel.r_id.match(_id) == None: raise gen.Return('bad id') ret = yield UserModel.sql('INSERT INTO "user" (login, \ password,\ interface_access, \ description, \ key, \ node_user_access_list, \ node_sudo_access_list, \ class_user_access_list, \ class_sudo_access_list) VALUES (%s, MD5(%s), %s, %s, %s, %s, %s, %s, %s) RETURNING *', login.lower(), md5_salt + password, interface_access, description, key, ','.join(nodeUserAccessList), ','.join(nodeSudoAccessList), ','.join(classUserAccessList), ','.join(classSudoAccessList)) yield PromiseModel.createPromise(ret[0]['id']) yield LogModel.write('user', description, 'create', None, utils.toDict(ret[0])) raise gen.Return(False)
def home(): try: res = [toDict(x) for x in PYLOAD.statusDownloads()] except: s = request.environ.get('beaker.session') s.delete() return redirect("/login") for link in res: if link["status"] == 12: link["information"] = "%s kB @ %s kB/s" % (link["size"] - link["bleft"], link["speed"]) return render_to_response("home.html", {"res": res}, [pre_processor])
def home(): try: res = [toDict(x) for x in PYLOAD.statusDownloads()] except: s = request.environ.get('beaker.session') s.delete() return relative_redirect("login") for link in res: if link["status"] == 12: link["information"] = "%s kB @ %s kB/s" % (link["size"] - link["bleft"], link["speed"]) return render_to_response("home.html", {"res": res}, [pre_processor])
def _get(self, userDescription = '', userLogin = '', userPassword = '', userPassword2 = '', userKey = '', interfaceAccess = False, error = False, nodeUserAccessList = [], nodeSudoAccessList = [], classUserAccessList = [], classSudoAccessList = []): users = yield UserModel.getUsers() nodes = yield NodeModel.getNodes() nodesById = {} for node in nodes: nodesById.update({node['id'] : utils.toDict(node)}) classes = yield NodeModel.getClasses() classesById = {} for cl in classes: classesById.update({cl['id'] : utils.toDict(cl)}) self.render('manage-users.html', **{'error' : error, 'users' : users, 'userKey' : userKey, 'interfaceAccess' : interfaceAccess, 'userDescription' : userDescription, 'userLogin' : userLogin, 'userPassword' : userPassword, 'userPassword2' : userPassword2, 'nodesById' : nodesById, 'classesById' : classesById, 'nodeUserAccessList' : nodeUserAccessList, 'nodeSudoAccessList' : nodeSudoAccessList, 'classUserAccessList' : classUserAccessList, 'classSudoAccessList' : classSudoAccessList })
def createCard(connection, card): params = toDict(card) keys = params.keys() SQL = 'INSERT INTO cards ("cardID",{}) VALUES (DEFAULT,{}) RETURNING "cardID";'.format( ",".join(['"{}"'.format(attr) for attr in keys]), ",".join(['%({})s'.format(attr) for attr in keys]) ) print SQL cursor = connection.cursor() cursor.execute(SQL, params) newID = cursor.fetchone()[0] connection.commit() cursor.close() return newID
def parsePalmDOCHeader(self): headerfmt = '>HHIHHHH' headerlen = calcsize(headerfmt) fields = [ "Compression", "Unused", "text length", "record count", "record size", "Encryption Type", "Unknown" ] offset = self.records[0]['record Data Offset']; # create tuple with info results = zip(fields, unpack(headerfmt, self.contents[offset:offset+headerlen])) # convert tuple array to dictionary resultsDict = utils.toDict(results); self.offset = offset+headerlen; return resultsDict
def links(): try: links = [toDict(x) for x in PYLOAD.statusDownloads()] ids = [] for link in links: ids.append(link['fid']) if link['status'] == 12: link['info'] = "%s @ %s/s" % (link['format_eta'], formatSize(link['speed'])) elif link['status'] == 5: link['percent'] = 0 link['size'] = 0 link['bleft'] = 0 link['info'] = _("waiting %s") % link['format_wait'] else: link['info'] = "" data = {'links': links, 'ids': ids} return data except Exception, e: print_exc() return HTTPError()
def parseMobiHeader(self): headerfmt = '> IIII II 40s III IIIII IIII I 36s IIII 8s HHIIIII' headerlen = calcsize(headerfmt) fields = [ "identifier", "header length", "Mobi type", "text Encoding", "Unique-ID", "Generator version", "-Reserved", "First Non-book index", "Full Name Offset", "Full Name Length", "Language", "Input Language", "Output Language", "Format version", "First Image index", "First Huff Record", "Huff Record Count", "First DATP Record", "DATP Record Count", "EXTH flags", "-36 unknown bytes, if Mobi is long enough", "DRM Offset", "DRM Count", "DRM Size", "DRM Flags", "-Usually Zeros, unknown 8 bytes", "-Unknown", "Last Image Record", "-Unknown", "FCIS record", "-Unknown", "FLIS record", "Unknown" ] # unpack header, zip up into list of tuples results = zip(fields, unpack(headerfmt, self.contents[self.offset:self.offset+headerlen])) # convert tuple array to dictionary resultsDict = utils.toDict(results); resultsDict['Start Offset'] = self.offset; resultsDict['Full Name'] = (self.contents[ self.records[0]['record Data Offset'] + resultsDict['Full Name Offset'] : self.records[0]['record Data Offset'] + resultsDict['Full Name Offset'] + resultsDict['Full Name Length']]) resultsDict['Has DRM'] = resultsDict['DRM Offset'] != 0xFFFFFFFF; resultsDict['Has EXTH Header'] = (resultsDict['EXTH flags'] & 0x40) != 0; self.offset += resultsDict['header length']; def onebits(x, width=16): return len(filter(lambda x: x == "1", (str((x>>i)&1) for i in xrange(width-1,-1,-1)))); resultsDict['extra bytes'] = 2*onebits(unpack(">H", self.contents[self.offset-2:self.offset])[0] & 0xFFFE) return resultsDict;
def default(self, o): if isinstance(o, BaseObject): return toDict(o) return json.JSONEncoder.default(self, o)
Configuration ''' import config_default from utils import toDict def merge(defaults, override): r = {} for k, v in defaults.items(): if k in override: if isinstance(v, dict): r[k] = merge(v, override[k]) else: r[k] = override[k] else: r[k] = v return r configs = config_default.configs try: import config_override configs = merge(configs, config_override.configs) except ImportError: pass configs = toDict(configs)
def updateUser( id, login, password, password2, interface_access = False, description = '', key = '', nodeUserAccessList = [], nodeSudoAccessList = [], classUserAccessList = [], classSudoAccessList = []): ids = [] ids.extend(nodeUserAccessList) ids.extend(nodeSudoAccessList) ids.extend(classUserAccessList) ids.extend(classSudoAccessList) for _id in ids: if UserModel.r_id.match(_id) == None: raise gen.Return('bad id') old_val = yield UserModel.getUser(id) if password == 'leave old': password = old_val[0]['password'] password2 = password else: password = md5(md5_salt + password).hexdigest() password2 = password if password != password2: raise gen.Return('password1 != password2') elif UserModel.r_id.match(id) == None or \ (password != old_val[0]['password'] and UserModel.r_pass.match(password) == None) or \ UserModel.r_log.match(login) == None or \ UserModel.r_desc.match(description) == None or \ not (interface_access in (True, False)): raise gen.Return('description, password or login are wrong') else: new_val = yield UserModel.sql('UPDATE "user" SET login=%s, \ interface_access=%s, \ description=%s, \ password=MD5(%s), \ key=%s,\ node_user_access_list=%s, \ node_sudo_access_list=%s, \ class_user_access_list=%s, \ class_sudo_access_list=%s \ WHERE id=%s RETURNING *', login, interface_access, description, md5_salt + password, key, ','.join(nodeUserAccessList), ','.join(nodeSudoAccessList), ','.join(classUserAccessList), ','.join(classSudoAccessList), id) yield LogModel.write('user', description, 'edit', utils.toDict(old_val[0]), utils.toDict(new_val[0]))