MCPcopy
hub / github.com/tinode/chat / gen_message

Function gen_message

tn-cli/client.py:93–194  ·  view source on GitHub ↗

Client message generator: reads user input as string, converts to pb.ClientMsg, and yields

(scheme, secret, args)

Source from the content-addressed store, hash-verified

91
92# Generator of protobuf messages.
93def gen_message(scheme, secret, args):
94 """Client message generator: reads user input as string,
95 converts to pb.ClientMsg, and yields"""
96 import random
97 import threading
98 from input_handler import stdin
99 from commands import hiMsg, loginMsg, serialize_cmd
100
101 random.seed()
102 id = random.randint(10000,60000)
103
104 # Asynchronous input-output
105 tn_globals.InputThread = threading.Thread(target=stdin, args=(tn_globals.InputQueue,))
106 tn_globals.InputThread.daemon = True
107 tn_globals.InputThread.start()
108
109 try:
110 from importlib.metadata import version
111 except ImportError:
112 from importlib_metadata import version
113 import platform
114
115 APP_NAME = "tn-cli"
116 APP_VERSION = "3.0.1"
117 LIB_VERSION = version("tinode_grpc")
118 GRPC_VERSION = version("grpcio")
119
120 user_agent = APP_NAME + "/" + APP_VERSION + " (" + \
121 platform.system() + "/" + platform.release() + "); gRPC-python/" + LIB_VERSION + "+" + GRPC_VERSION
122
123 msg = hiMsg(id, args.background, user_agent, LIB_VERSION)
124 if tn_globals.Verbose:
125 stdoutln("\r=> " + to_json(msg))
126 yield msg
127
128 if scheme != None:
129 id += 1
130 login = lambda:None
131 setattr(login, 'scheme', scheme)
132 setattr(login, 'secret', secret)
133 setattr(login, 'cred', None)
134 msg = loginMsg(id, login, args)
135 if tn_globals.Verbose:
136 stdoutln("\r=> " + to_json(msg))
137 yield msg
138
139 print_prompt = True
140
141 while True:
142 try:
143 if not tn_globals.WaitingFor and tn_globals.InputQueue:
144 id += 1
145 inp = tn_globals.InputQueue.popleft()
146
147 if inp == 'exit' or inp == 'quit' or inp == '.exit' or inp == '.quit':
148 # Drain the output queue.
149 while pop_from_output_queue():
150 pass

Callers 1

runFunction · 0.85

Calls 7

hiMsgFunction · 0.90
stdoutlnFunction · 0.90
to_jsonFunction · 0.90
loginMsgFunction · 0.90
serialize_cmdFunction · 0.90
pop_from_output_queueFunction · 0.85
startMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…