def publish_v2(topic, text, tid, path = 'prompts.json', head_info = None): import json from pprint import pprint if head_info is None: print(" -----1------ ") Pub = pb.ClientPub(id=tid, topic=topic, no_echo=True, content=json.dumps(text).encode('utf-8')) result = pb.ClientMsg(pub=Pub) else: with open(path) as f: data = json.load(f) print(" -----1------ ") pprint(data) print(" -----2------ ") Pub = pb.ClientPub(id=tid, topic=topic, no_echo=True, head={"mime":head_info.encode('utf-8')}, content=json.dumps(data).encode('utf-8')) result = pb.ClientMsg(pub=Pub) print(" -----3------ ") print(result) return result
def serialize_cmd(string, id): """Take string read from the command line, convert in into a protobuf message""" # Convert string into a dictionary cmd = parse_cmd(string) if cmd == None: return None # Process dictionary if cmd.cmd == "acc": return accMsg(id, cmd.user, cmd.scheme, cmd.secret, cmd.uname, cmd.password, cmd.do_login, cmd.fn, cmd.photo, cmd.private, cmd.auth, cmd.anon, cmd.tags, cmd.cred) elif cmd.cmd == "login": return loginMsg(id, cmd.scheme, cmd.secret, cmd.cred, cmd.uname, cmd.password) elif cmd.cmd == "sub": return subMsg(id, cmd.topic, cmd.fn, cmd.photo, cmd.private, cmd.auth, cmd.anon, cmd.mode, cmd.tags, cmd.get_query) elif cmd.cmd == "leave": return pb.ClientMsg(leave=pb.ClientLeave(id=str(id), topic=cmd.topic)) elif cmd.cmd == "pub": return pb.ClientMsg(pub=pb.ClientPub(id=str(id), topic=cmd.topic, no_echo=True, content=json.dumps(cmd.content))) elif cmd.cmd == "get": return getMsg(id, cmd.topic, cmd.desc, cmd.sub, cmd.tags, cmd.data) elif cmd.cmd == "set": return setMsg(id, cmd.topic, cmd.user, cmd.fn, cmd.photo, cmd.public, cmd.private, cmd.auth, cmd.anon, cmd.mode, cmd.tags) elif cmd.cmd == "del": return delMsg(id, cmd.topic, cmd.what, cmd.param, cmd.hard) elif cmd.cmd == "note": return noteMsg(id, cmd.topic, cmd.what, cmd.seq) else: print("Unrecognized: " + cmd.cmd) return None
def hello(): tid = next_id() return pb.ClientMsg(hi=pb.ClientHi(id=tid, user_agent=APP_NAME + "/" + VERSION + " gRPC-python", ver=VERSION, lang="EN"))
def loginMsg(id, scheme, secret, uname=None, password=None): if secret == None and uname != None: if password == None: password = '' secret = str(uname) + ":" + str(password) onCompletion[str(id)] = lambda params: save_cookie(params) return pb.ClientMsg(login=pb.ClientLogin(id=str(id), scheme=scheme, secret=secret))
def subscribe(topic): tid = next_id() onCompletion[tid] = { 'arg': topic, 'action': lambda topicName, unused: add_subscription(topicName), } return pb.ClientMsg(sub=pb.ClientSub(id=tid, topic=topic))
def leave(topic): tid = next_id() onCompletion[tid] = { 'arg': topic, 'action': lambda topicName, unused: del_subscription(topicName) } return pb.ClientMsg(leave=pb.ClientLeave(id=tid, topic=topic))
def hiMsg(id): onCompletion[str(id)] = lambda params: print_server_params(params) return pb.ClientMsg(hi=pb.ClientHi(id=str(id), user_agent=APP_NAME + "/" + VERSION + " gRPC-python", ver=VERSION, lang="EN"))
def leave(topic): tid = next_id() add_future(tid, { 'arg': topic, 'action': lambda topicName, unused: del_subscription(topicName) }) return pb.ClientMsg(leave=pb.ClientLeave(id=tid, topic=topic))
def subscribe(topic): tid = next_id() add_future(tid, { 'arg': topic, 'action': lambda topicName, unused: add_subscription(topicName), }) return pb.ClientMsg(sub=pb.ClientSub(id=tid, topic=topic))
def login(cookie_file_name, scheme, secret): tid = next_id() add_future(tid, { 'arg': cookie_file_name, 'action': lambda fname, params: on_login(fname, params), }) return pb.ClientMsg(login=pb.ClientLogin(id=tid, scheme=scheme, secret=secret))
def publish(topic, text): tid = next_id() return pb.ClientMsg( pub=pb.ClientPub(id=tid, topic=topic, no_echo=True, content=json.dumps(text).encode('utf-8')))
def subscribe_v2(topic, tid): add_future(tid, { 'arg': topic, 'action': lambda topicName, unused: add_subscription(topicName), }) A = pb.ClientMsg(sub=pb.ClientSub(id=tid, topic=topic)) print(A) return A
def hello(): tid = next_id() add_future(tid, { 'action': lambda unused, params: server_version(params), }) return pb.ClientMsg(hi=pb.ClientHi(id=tid, user_agent=APP_NAME + "/" + APP_VERSION + " (" + platform.system() + "/" + platform.release() + "); gRPC-python/" + LIB_VERSION, ver=LIB_VERSION, lang="EN"))
def login(cookie_file_name, scheme, secret): tid = next_id() onCompletion[tid] = { 'arg': cookie_file_name, 'action': lambda fname, params: save_auth_cookie(fname, params), } return pb.ClientMsg( login=pb.ClientLogin(id=tid, scheme=scheme, secret=secret))
def setMsg(id, topic, user, fn, photo, private, auth, anon, mode): return pb.ClientMsg(set=pb.ClientSet( id=str(id), topic=topic, query=pb.SetQuery(desc=pb.SetDesc(default_acs=pb.DefaultAcsMode( auth=auth, anon=anon), public=make_vcard(fn, photo), private=private), sub=pb.SetSub(user_id=user, mode=mode))))
def accMsg(id, user, scheme, secret, uname, password, do_login, tags, fn, photo, private, auth, anon): if secret == None and uname != None: if password == None: password = '' secret = str(uname) + ":" + str(password) return pb.ClientMsg(acc=pb.ClientAcc(id=str(id), user_id=user, scheme=scheme, secret=secret, login=do_login, tags=tags.split(",") if tags else None, desc=pb.SetDesc(default_acs=pb.DefaultAcsMode(auth=auth, anon=anon), public=make_vcard(fn, photo), private=private)))
def hello(): tid = next_id() return pb.ClientMsg(hi=pb.ClientHi(id=tid, user_agent=APP_NAME + "/" + APP_VERSION + " (" + platform.system() + "/" + platform.release() + "); gRPC-python/" + LIB_VERSION, ver=LIB_VERSION, lang="EN"))
def subMsg(id, topic, fn, photo, private, auth, anon, mode, tags, get_query): if get_query: get_query = pb.GetQuery(what=get_query.split(",").join(" ")) public = encode_to_bytes(make_vcard(fn, photo)) private = encode_to_bytes(private) return pb.ClientMsg(sub=pb.ClientSub(id=str(id), topic=topic, set_query=pb.SetQuery( desc=pb.SetDesc(public=public, private=private, default_acs=pb.DefaultAcsMode(auth=auth, anon=anon)), sub=pb.SetSub(mode=mode), tags=tags.split(",") if tags else None), get_query=get_query))
def getMsg(id, topic, desc, sub, data): what = [] if desc: what.append("desc") if sub: what.append("sub") if data: what.append("data") return pb.ClientMsg(get=pb.ClientGet( id=str(id), topic=topic, query=pb.GetQuery(what=" ".join(what))))
def loginMsg(id, scheme, secret, cred, uname, password): if secret == None and uname != None: if password == None: password = '' secret = str(uname) + ":" + str(password) if secret != None: secret=secret.encode('utf-8') onCompletion[str(id)] = lambda params: save_cookie(params) return pb.ClientMsg(login=pb.ClientLogin(id=str(id), scheme=scheme, secret=secret, cred=parse_cred(cred)))
def noteMsg(id, topic, what, seq): enum_what = None if what == 'kp': enum_what = pb.KP seq = None elif what == 'read': enum_what = pb.READ seq = int(seq) elif what == 'recv': enum_what = pb.READ seq = int(seq) return pb.ClientMsg(note=pb.ClientNote(topic=topic, what=enum_what, seq_id=seq))
def setMsg(id, topic, user, fn, photo, public, private, auth, anon, mode, tags): if public == None: public = encode_to_bytes(make_vcard(fn, photo)) else: public = encode_to_bytes(public) private = encode_to_bytes(private) return pb.ClientMsg(set=pb.ClientSet(id=str(id), topic=topic, query=pb.SetQuery( desc=pb.SetDesc(default_acs=pb.DefaultAcsMode(auth=auth, anon=anon), public=public, private=private), sub=pb.SetSub(user_id=user, mode=mode), tags=tags)))
def accMsg(id, user, scheme, secret, uname, password, do_login, fn, photo, private, auth, anon, tags, cred): if secret == None and uname != None: if password == None: password = '' secret = str(uname) + ":" + str(password) if secret != None: secret=secret.encode('utf-8') public = encode_to_bytes(make_vcard(fn, photo)) private = encode_to_bytes(private) return pb.ClientMsg(acc=pb.ClientAcc(id=str(id), user_id=user, scheme=scheme, secret=secret, login=do_login, tags=tags.split(",") if tags else None, desc=pb.SetDesc(default_acs=pb.DefaultAcsMode(auth=auth, anon=anon), public=public, private=private, cred=parse_cred(cred))))
def delMsg(id, topic, what, param, hard): if topic == None and param != None: topic = param param = None print(id, topic, what, param, hard) enum_what = None before = None seq_list = None user = None if what == 'msg': enum_what = pb.ClientDel.MSG if param == 'all': seq_list = [pb.DelQuery(range=pb.SeqRange(low=1, hi=0x8FFFFFF))] elif param != None: seq_list = [ pb.DelQuery(seq_id=int(x.strip())) for x in param.split(',') ] print(seq_list) elif what == 'sub': enum_what = pb.ClientDel.SUB user = param elif what == 'topic': enum_what = pb.ClientDel.TOPIC # Field named 'del' conflicts with the keyword 'del. This is a work around. msg = pb.ClientMsg() xdel = getattr(msg, 'del') """ setattr(msg, 'del', pb.ClientDel(id=str(id), topic=topic, what=enum_what, hard=hard, del_seq=seq_list, user_id=user)) """ xdel.id = str(id) xdel.topic = topic xdel.what = enum_what if hard != None: xdel.hard = hard if seq_list != None: xdel.del_seq.extend(seq_list) if user != None: xdel.user_id = user return msg
def delMsg(id, topic, what, param, del_before, hard): if topic == None and param != None: topic = param param = None print id, topic, what, param, del_before, hard enum_what = None before = None seq_list = None user = None if what == 'msg': enum_what = pb.ClientDel.MSG before = del_before if param != None: seq_list = [int(x.strip()) for x in param.split(',')] elif del_before != None: before = int(del_before) elif what == 'sub': enum_what = pb.ClientDel.SUB user = param elif what == 'topic': enum_what = pb.ClientDel.TOPIC # Field named 'del' conflicts with the keyword 'del. This is a work around. msg = pb.ClientMsg() xdel = getattr(msg, 'del') """ setattr(msg, 'del', pb.ClientDel(id=str(id), topic=topic, what=enum_what, hard=hard, seq_list=seq_list, before=before, user_id=user)) """ xdel.id = str(id) xdel.topic = topic xdel.what = enum_what if hard != None: xdel.hard = hard if seq_list != None: xdel.seq_list.extend(seq_list) if before != None: xdel.before = before if user != None: xdel.user_id = user return msg
def note_read(topic, seq): return pb.ClientMsg( note=pb.ClientNote(topic=topic, what=pb.READ, seq_id=seq))