-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_print.py
More file actions
471 lines (379 loc) · 14.5 KB
/
console_print.py
File metadata and controls
471 lines (379 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
from rich import print as rprint
from rich.pretty import pprint
from rich.pretty import Pretty
from rich.panel import Panel
from rich.console import Console, RenderableType
from rich.table import Column, Table
from rich.tree import Tree
from rich.layout import Layout
from rich import print_json
from rich.text import Text
from rich.containers import Lines, Renderables
import logging
from rich.logging import RichHandler
from rich.live import Live
from typing import List
from rich import box
import time
from rich.progress import track
from rich.progress import (
BarColumn,
Progress,
SpinnerColumn,
TextColumn,
TimeElapsedColumn,
)
# from rich import print_json
Max_String = 21
expand_all_flag = True
console = Console()
layout = Layout()
text = Text("foo")
renderables = Renderables([text])
log_messages = []
transactions = []
def header_grid(header) -> Table:
grid = Table.grid(expand=True)
grid.add_column()
grid.add_column(justify="right", style="bold")
grid.add_row("Index", f"{header.index}")
grid.add_row("Previous Hash", f"{hex(int(header.prev_hash, 16))}")
grid.add_row("Timestamp", f"{header.timestamp}")
grid.add_row("Merkle Root", f"{hex(int(header.merkle_root, 16))}")
grid.add_row("Nonce", f"{header.nonce}")
grid.add_row(
"Public Key", f"({hex(header.public_key.h)}, {hex(header.public_key.g)}, {hex(header.public_key.p)})")
grid.add_row("Public Key Length", f"{header.public_key_length} bits")
return grid
def body_grid(transactions) -> Table:
grid = Table.grid(expand=True)
grid.add_column()
grid.add_column(justify="right", style="bold", no_wrap=True)
grid.add_row("Transactions count", f"{len(transactions)}")
# grid.add_row(f"Transactions" if len(transactions) > 0 else "[bold][red]No Transactions", "")
# for tx in transactions:
# grid.add_row("", "")
# grid.add_row("TxID", f"{hex(int(tx.transaction_id, 16))}")
# grid.add_row("Sender", f"{tx.sender}")
# grid.add_row("Receiver", f"{tx.recipient}")
# grid.add_row("Timestamp", f"{tx.timestamp}")
# grid.add_row("tx.encrypted_message", f"{tx.encrypted_message}")
return grid
def block_grid(block) -> Table:
panel_header = Panel(header_grid(block.header),
title="[bold]Header", title_align="left")
panel_body = Panel(body_grid(block.transactions),
title="[bold]Body", title_align="left")
grid = Table.grid(expand=True)
grid.add_column()
grid.add_row(panel_header)
grid.add_row(panel_body)
return grid
def blockchain_grid(blockchain) -> Table:
grid = Table.grid(expand=True)
grid.add_column()
for block in blockchain:
if block.header.index == 0:
grid.add_row(Panel.fit(block_grid(
block), title=f"BLOCK [bold red]{block.header.index}", subtitle="Genesis Block"))
else:
grid.add_row(Panel.fit(block_grid(
block), title=f"BLOCK [bold red]{block.header.index}", subtitle=""))
return grid
def current_block(blockchain) -> Panel:
block = blockchain[-1]
if block.header.index == 0:
return Panel.fit(block_grid(block), title=f"BLOCK [bold red]{block.header.index}", subtitle="Genesis Block")
else:
return Panel.fit(block_grid(block), title=f"BLOCK [bold red]{block.header.index}", subtitle="")
def tx_data(tx, index) -> dict:
tx_data = {
index: [
f"ID: {tx.transaction_id[:8]}...{tx.transaction_id[-8:]}",
{
"Sender": f"{tx.sender}",
"Receiver": f"{tx.recipient}",
"Timestamp": f"{tx.timestamp}",
"tx.encrypted_message": f"{tx.encrypted_message[:8]}...{tx.encrypted_message[-8:]}"
}
]
}
return tx_data
def transactions_data(transactions) -> dict:
data = dict()
for i, tx in enumerate(transactions):
data.update(tx_data(tx, i))
return data
def tx_panel(blockchain) -> Panel:
return Panel(Pretty(transactions_data(blockchain[-1].transactions), indent_guides=True), title="Transactions", expand=True, border_style="none")
def print_blockchain(blockchain):
rprint(blockchain_grid(blockchain))
def comparison3(renderable1: RenderableType, renderable2: RenderableType, rendable3: RenderableType) -> Table:
table = Table(show_header=False, pad_edge=False, box=None, expand=True)
table.add_column("1", style="bold red")
table.add_column("2", ratio=1)
table.add_column("3", ratio=1)
table.add_row(renderable1, renderable2, rendable3)
return table
def comparison2(renderable1: RenderableType, renderable2: RenderableType) -> Table:
table = Table(show_header=False, pad_edge=False, box=None, expand=True)
table.add_column("1", ratio=1, style="bold red")
table.add_column("2", ratio=2)
table.add_row(renderable1, renderable2)
return table
table = Table(
show_edge=False,
show_header=True,
expand=True,
row_styles=["none", "dim"],
box=box.SIMPLE,
)
table.add_column("TxID", justify="left", style="bold red", no_wrap=True)
table.add_column("From", justify="left", style="bold red", no_wrap=True)
table.add_column("", justify="left", style="bold red", no_wrap=True)
table.add_column("To", justify="left", style="bold red", no_wrap=True)
table.add_column("Payload", justify="left", style="bold red", no_wrap=True)
def transactionsTable(transaction: dict = None, decrypted_message: str = None) -> Table:
global transactions
if transaction:
transaction.decrypted_message = decrypted_message
transactions.append(transaction)
if len(transactions) > 10:
transactions = transactions[-10:]
for tx in reversed(transactions):
table.add_row(
f"{hex(int(tx.transaction_id, 16))}"[:8],
f"{tx.sender}",
f"->",
f"{tx.recipient}",
f"{tx.decrypted_message}"
# f"{tx.encrypted_message[:8]}...{tx.encrypted_message[-8:]}"
)
return table
def log_transaction(tx, decrypted_message):
transactionsTable(tx, decrypted_message)
def keyPairs(blockchain) -> Table:
table = Table(
# title="Key Pairs",
show_edge=False,
show_header=True,
expand=False,
# row_styles=["none", "dim"],
box=box.SIMPLE,
)
table.add_column("Public", justify="left", style="bold red", no_wrap=True)
table.add_column("Private", justify="left", style="bold red", no_wrap=True)
if len(blockchain) > 1:
for i in range(len(blockchain) - 1):
style = None if i == 0 else "dim"
table.add_row(
f"{hex(blockchain[i].header.public_key.h)}",
f"{hex(blockchain[i+1].header.nonce)}",
style=style
)
# table.add_row(
# f"{blockchain[-2].header.public_key.h}",
# f"{blockchain[-1].header.nonce}"
# )
return table
def upper_layout_content():
return Panel(update_logs("logging"), expand=True)
def table_content() -> Table:
table = Table.grid(padding=1, pad_edge=True)
table.add_column("Feature", no_wrap=True,
justify="center", style="bold red")
table.add_column("Demonstration")
table.add_row(
"Syntax\nhighlighting\n&\npretty\nprinting",
)
return table
def layout_content() -> Layout:
layout.split_column(
Layout("", name="upper"),
Layout("", name="lower")
)
layout["lower"].split_row(
Layout("", name="current_block"),
Layout("", name="current_transactions", ratio=2),
)
layout["upper"].split_row(
Layout("", name="progress", ratio=1),
Layout("", name="logs", ratio=1),
)
return layout
def update_logs(new_message: str = None) -> Text:
"""
Update and retrieve log messages.
Args:
new_message: Optional new message to add to logs
Returns:
Rich Text object with formatted log messages
"""
global log_messages
# Add new message if provided
if new_message:
log_messages.append(new_message)
# Keep only the last 10 log messages
if len(log_messages) > 16:
log_messages = log_messages[-16:]
# Create a Text object with log messages
log_text = Text()
for i, msg in enumerate(log_messages):
if i == len(log_messages) - 1: # Last message
log_text.append(msg + "\n", style="bold red")
else:
log_text.append(msg + "\n", style="dim")
return log_text
def log_message(message: str):
"""
Log a message to be displayed in the upper layout.
Args:
message: Message to log
"""
update_logs(message)
def create_mining_progress(description: str = "Mining block...", total: float = 1.0) -> Progress:
"""
Create a mining progress bar for the upper layout.
Args:
description: Description of the mining task
total: Total work units for the progress bar
Returns:
Rich Progress object
"""
return Progress(
SpinnerColumn(),
# BarColumn(),
TextColumn("[progress.description]{task.description}"),
TimeElapsedColumn(),
console=console,
transient=True # Removes the progress bar after completion
)
def update_mining_progress(progress: Progress, task_id: int, advance: float = 0.5) -> None:
"""
Update the mining progress bar.
Args:
progress: Rich Progress object
task_id: ID of the task to update
advance: Amount to advance the progress bar
"""
progress.update(task_id, advance=advance)
def mining_layout(blockchain=None, mining_progress: Progress = None) -> Table:
"""
Create a layout with mining progress and logs.
Args:
blockchain: Optional blockchain for current block display
mining_progress: Optional Progress object for mining
Returns:
Rich Layout object
"""
grid = Table.grid(expand=True, padding=1, pad_edge=True)
grid.add_column(justify="left")
# Update progress layout if a progress bar is provided
grid.add_row(
Panel(mining_progress if mining_progress else "",
title="Mining Progress", border_style="green", expand=True)
)
# Update logs layout with update_logs()
grid.add_row(Panel(update_logs(), height=12, title="Logs",
border_style="blue", expand=True))
# Update current block if blockchain is provided
if blockchain and blockchain:
grid.add_row(
comparison3(
"Latest\nmined block",
current_block(blockchain),
keyPairs(blockchain),
# Pretty(transactions_data(blockchain[-1].transactions), indent_guides=True)
)
)
# grid.add_row(transactionsTable(blockchain[-1].transactions))
return grid
# print the blockchain from trb.py file in a pretty format
def print_layout(blockchain):
console.clear()
# rprint(blockchain_grid(blockchain))
def update_display(mining_progress: Progress = None):
return mining_layout(blockchain, mining_progress)
# Use Live to update the display dynamically
live = Live(update_display(), refresh_per_second=4,
vertical_overflow="crop")
live.start()
def update_callback(updated_blockchain: List, mining_progress: Progress = None):
"""
Update the live display with the new blockchain state
Args:
updated_blockchain: The current state of the blockchain
mining_progress: Optional Progress object for mining
"""
global blockchain # Use global to modify the reference
blockchain = updated_blockchain
live.update(update_logs())
live.update(update_display(mining_progress))
# wait until the live display is updated
wait = 1
time.sleep(wait)
return update_callback
def make_layout(blockchain=None, mining_progress: Progress = None) -> Layout:
"""Define the layout."""
layout = Layout(name="root")
layout.split(
Layout(name="header", size=3),
Layout(name="main", ratio=1),
Layout(name="footer", size=10),
)
layout["main"].split_row(
Layout(name="side"),
Layout(name="body", ratio=2, minimum_size=30),
)
layout["side"].split(Layout(name="box1", ratio=2), Layout(name="box2"))
layout["header"].update(Panel(
mining_progress, title="Time Release Blockchain", border_style="green", expand=True))
layout["body"].update(
Panel(update_logs(), title="Logs", border_style="blue", expand=True))
layout["box2"].update(Panel(comparison2("Generated\nkey pairs", keyPairs(
blockchain)), border_style="none", expand=True))
layout["box1"].update(current_block(blockchain))
# layout["box1"].update(Panel(current_block(blockchain), title="Latest Block", border_style="white", expand=True))
layout["footer"].update(Panel(
transactionsTable(), title="Transactions", border_style="red", expand=True))
return layout
def main():
# console.clear()
# layout = layout_content()
layout = make_layout()
layout["header"].update(
Panel("", title="Time Release Blockchain", border_style="green", expand=True))
layout["body"].update(
Panel("", title="Logs", border_style="blue", expand=True))
layout["box2"].update(
Panel("", title="Key Pairs", border_style="green", expand=True))
layout["box1"].update(Panel("", title="Latest Block",
border_style="white", expand=True))
layout["footer"].update(
Panel("", title="Transactions", border_style="red", expand=True))
rprint(layout)
# pass
def display(blockchain):
def update_display(mining_progress: Progress = None):
return make_layout(blockchain, mining_progress)
# Use Live to update the display dynamically
live = Live(update_display(), refresh_per_second=4,
vertical_overflow="crop")
live.start()
def update_callback(updated_blockchain: List, mining_progress: Progress = None):
"""
Update the live display with the new blockchain state
Args:
updated_blockchain: The current state of the blockchain
mining_progress: Optional Progress object for mining
"""
global blockchain # Use global to modify the reference
blockchain = updated_blockchain
# live.update(transactionsTable())
live.update(update_display(mining_progress))
# wait until the live display is updated
# wait = 1
# time.sleep(wait)
return update_callback
if __name__ == "__main__":
main()