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
Example #2
0
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')))
Example #3
0
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