Skip to content

Commit 830dc17

Browse files
committed
Rewrite function and rewrite print in f-string format
1 parent 72c3c64 commit 830dc17

4 files changed

Lines changed: 200 additions & 229 deletions

File tree

jtop/__main__.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ def warning_messages(jetson, no_warnings=False):
5555
hardware = jetson.board['hardware']
5656
version = get_var(VERSION_RE)
5757
# Check is well stored the default jetson_clocks configuration
58-
if jetson.jetson_clocks:
59-
if not jetson.jetson_clocks.is_config:
60-
print("[{status}] Please stop manually jetson_clocks or reboot this board".format(status=bcolors.warning()))
58+
if jetson.jetson_clocks and not jetson.jetson_clocks.is_config:
59+
print("[{status}] Please stop manually jetson_clocks or reboot this board".format(status=bcolors.warning()))
6160
# Check if an hardware value is missing
62-
if not all([data for name, data in hardware.items() if name not in ['Jetpack']]):
61+
if not all(
62+
data for name, data in hardware.items() if name not in ['Jetpack']
63+
):
6364
hardware_missing(REPOSITORY, hardware, version)
6465
# Check if jetpack is missing
6566
if not hardware['Jetpack'] and hardware['L4T']:
@@ -143,31 +144,34 @@ def main():
143144
# https://stackoverflow.com/questions/56373360/n-curses-within-python-how-to-catch-and-print-non-ascii-character
144145
# Commented for issues #466 #393
145146
# locale.setlocale(locale.LC_ALL, '')
146-
# Open jtop client
147147
with jtop(interval=interval) as jetson:
148-
# Call the curses wrapper
149-
color_filter = bool(os.getenv('JTOP_COLOR_FILTER', args.color_filter))
150-
# Build list pages available
151-
pages = [ALL]
152-
if jetson.gpu:
153-
pages += [GPU]
154-
pages += [CPU, MEM]
155-
if jetson.engine:
156-
pages += [ENGINE]
157-
if jetson.fan or jetson.jetson_clocks is not None or jetson.nvpmodel is not None:
158-
pages += [CTRL]
159-
pages += [INFO]
160-
curses.wrapper(JTOPGUI, jetson, pages, init_page=args.page,
161-
loop=args.loop, seconds=LOOP_SECONDS, color_filter=color_filter)
162-
# Write warnings
163-
if 'L4T' in jetson.board['hardware']:
164-
warning_messages(jetson, args.no_warnings)
148+
jtop_client(args, jetson)
165149
except (KeyboardInterrupt, SystemExit):
166150
pass
167151
except JtopException as e:
168152
print(e)
169153

170154

155+
def jtop_client(args, jetson):
156+
# Call the curses wrapper
157+
color_filter = bool(os.getenv('JTOP_COLOR_FILTER', args.color_filter))
158+
# Build list pages available
159+
pages = [ALL]
160+
if jetson.gpu:
161+
pages += [GPU]
162+
pages += [CPU, MEM]
163+
if jetson.engine:
164+
pages += [ENGINE]
165+
if jetson.fan or jetson.jetson_clocks is not None or jetson.nvpmodel is not None:
166+
pages += [CTRL]
167+
pages += [INFO]
168+
curses.wrapper(JTOPGUI, jetson, pages, init_page=args.page,
169+
loop=args.loop, seconds=LOOP_SECONDS, color_filter=color_filter)
170+
# Write warnings
171+
if 'L4T' in jetson.board['hardware']:
172+
warning_messages(jetson, args.no_warnings)
173+
174+
171175
if __name__ == "__main__":
172176
main()
173177
# EOF

jtop/github.py

Lines changed: 76 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -28,134 +28,119 @@
2828

2929

3030
def engine_gui(repository, hardware, version):
31-
hardware = deepcopy(hardware)
32-
del hardware['Serial Number']
31+
# Prepare hardware information, marking missing values
32+
hardware = {k: v or "**MISSING**" for k, v in deepcopy(hardware).items() if k != 'Serial Number'}
3333
module = hardware['Module']
34-
# Title
35-
title = "GUI compact engine page missing for [{module}]".format(module=module)
36-
# Template
34+
35+
# Create issue title and template
36+
title = f"GUI compact engine page missing for [{module}]"
3737
template = "engine-gui.md"
38-
# body
39-
body = "Please add a new jtop engine compact page for\n\n"
40-
body += "### Board\n\n"
41-
for name, value in hardware.items():
42-
if not value:
43-
value = "**MISSING**"
44-
body += " - {name}: {value}\n".format(name=name, value=value)
45-
body += "\n### jetson-stats\n\n"
46-
body += " - Version: " + version + "\n"
47-
# Print all raw output
48-
body += "\n<!-- Please attach a screenshot page from jtop Engine page -->\n"
49-
body += "### Screenshot engine page\n\n"
50-
body += "Screenshot page from jtop engine page attached"
51-
# Make url
38+
39+
# Create issue body
40+
body = (
41+
"Please add a new jtop engine compact page for\n\n### Board\n\n"
42+
+ "\n".join(
43+
[f" - {name}: {value}" for name, value in hardware.items()]
44+
)
45+
)
46+
body += f"\n\n### jetson-stats\n\n - Version: {version}\n"
47+
body += "\n<!-- Please attach a screenshot page from jtop Engine page -->\n### Screenshot engine page\n\nScreenshot page from jtop engine page attached"
48+
49+
# Generate issue URL
5250
url = make_issue(repository, title, body=body, labels="GUI,missing", template=template)
53-
# message shell
54-
message = "Module \"{module}\" missing in jtop GUI".format(module=module)
55-
hyperlink(message, url, "open a Github issue & {attach} a engine page screenshot".format(attach=bcolors.bold('attach')))
51+
52+
# Print message with hyperlink
53+
message = f"Module \"{module}\" missing in jtop GUI"
54+
hyperlink(message, url, f"open a Github issue & {bcolors.bold('attach')} a engine page screenshot")
5655

5756

5857
def jetpack_missing(repository, hardware, version):
58+
# Prepare L4T information
5959
l4t = hardware['L4T']
60-
# Title
61-
title = "jetson-stats not supported for [L4T {l4t}]".format(l4t=l4t)
62-
# Template
60+
61+
# Create issue title and template
62+
title = f"jetson-stats not supported for [L4T {l4t}]"
6363
template = "jetpack-missing.md"
64-
# Body
65-
body = "Please update jetson-stats with new jetpack\n\n"
66-
body += "### Linux for Tegra\n\n"
67-
body += " - L4T: " + l4t + "\n\n"
68-
body += "### Jetson-Stats\n\n"
69-
body += " - Version: " + version + "\n"
70-
# Make url
64+
65+
# Create issue body
66+
body = f"Please update jetson-stats with new jetpack\n\n### Linux for Tegra\n\n - L4T: {l4t}\n\n### Jetson-Stats\n\n - Version: {version}\n"
67+
68+
# Generate issue URL
7169
url = make_issue(repository, title, body=body, labels="Jetpack,missing", template=template)
72-
# message shell
73-
message = "jetson-stats not supported for [L4T {l4t}]".format(l4t=hardware['L4T'])
70+
71+
# Print message with hyperlink
72+
message = f"jetson-stats not supported for [L4T {l4t}]"
7473
hyperlink(message, url, "open a Github issue")
7574

7675

7776
def get_hardware_log():
78-
# Print platform
77+
# Collect platform and raw output information
7978
platform = get_platform_variables()
80-
body = "--------------------- PLATFORM -------------------------\n"
81-
for name, value in platform.items():
82-
body += "{name}: {value}\n".format(name=name, value=value)
83-
# Print all jetson raw output
8479
raw_output = get_jetson_raw_output()
85-
body += "-------------------- JETSON RAW OUTPUT -----------------\n"
86-
for name, value in raw_output.items():
87-
body += "------------------\n"
88-
body += "Path: {name}\n{value}\n".format(name=name, value=value)
89-
# Print device list
90-
raw_output = get_raw_igpu_devices()
80+
igpu_output = get_raw_igpu_devices()
81+
82+
# Create log body
83+
body = (
84+
"--------------------- PLATFORM -------------------------\n"
85+
+ "\n".join([f"{name}: {value}" for name, value in platform.items()])
86+
)
87+
body += "\n-------------------- JETSON RAW OUTPUT -----------------\n"
88+
body += "\n".join([f"------------------\nPath: {name}\n{value}" for name, value in raw_output.items()])
9189
body += "\n-------------------- IGPU OUTPUT ---------------------\n"
92-
for path, value in raw_output.items():
93-
body += "------------------\n"
94-
body += "Path: {path}\n{value}\n".format(path=path, value=value)
90+
body += "\n".join([f"------------------\nPath: {path}\n{value}" for path, value in igpu_output.items()])
91+
9592
return body
9693

9794

9895
def hardware_missing(repository, hardware, version):
99-
hardware = deepcopy(hardware)
100-
del hardware['Serial Number']
101-
# Title
102-
if 'P-Number' in hardware:
103-
title = "Hardware Missing: {pnumber}".format(pnumber=hardware.get('P-Number', ''))
104-
else:
105-
title = "Hardware Missing".format()
106-
# Template
96+
# Prepare hardware information, marking missing values
97+
hardware = {k: v or "**MISSING**" for k, v in deepcopy(hardware).items() if k != 'Serial Number'}
98+
99+
# Create issue title and template
100+
title = f"Hardware Missing: {hardware.get('P-Number', '')}" if 'P-Number' in hardware else "Hardware Missing"
107101
template = "hardware-missing.md"
108-
# Body
109-
body = "Please update jetson-stats with this board\n\n"
110-
body += "### Board\n\n"
111-
for name, value in hardware.items():
112-
if not value:
113-
value = "**MISSING**"
114-
body += " - {name}: {value}\n".format(name=name, value=value)
115-
body += "\n### Jetson-Stats\n\n"
116-
body += " - Version: " + version + "\n"
117-
# Print all raw output
118-
body += "\n<!-- Please attach the output from: jtop --error-log -->\n"
119-
body += "### RAW Data\n\n"
120-
body += "File from `--error-log` attached"
121-
# Make url
102+
103+
# Create issue body
104+
body = (
105+
"Please update jetson-stats with this board\n\n### Board\n\n"
106+
+ "\n".join(
107+
[f" - {name}: {value}" for name, value in hardware.items()]
108+
)
109+
)
110+
body += f"\n\n### Jetson-Stats\n\n - Version: {version}\n"
111+
body += "\n<!-- Please attach the output from: jtop --error-log -->\n### RAW Data\n\nFile from `--error-log` attached"
112+
113+
# Generate issue URL
122114
url = make_issue(repository, title, body=body, labels="Hardware,missing", template=template)
123-
# message shell
115+
116+
# Print message with hyperlink
124117
message = "jtop not support this hardware"
125-
hyperlink(message, url, "open a Github issue & {attach} file from: jtop --error-log".format(attach=bcolors.bold('attach')))
118+
hyperlink(message, url, f"open a Github issue & {bcolors.bold('attach')} file from: jtop --error-log")
126119

127120

128121
def hyperlink(message, url, text):
129-
# Reference:
130-
# 1. http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
131-
# 2. https://stackoverflow.com/questions/40419276/python-how-to-print-text-to-console-as-hyperlink
132-
# 3. https://purpleidea.com/blog/2018/06/29/hyperlinks-in-gnome-terminal/
133-
# 4. https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
134-
# 5. https://stackoverflow.com/questions/44078888/clickable-html-links-in-python-3-6-shell
135122
# Print starting message
136-
print("[{status}] {message}".format(status=bcolors.warning(), message=message))
137-
print(" Please, try: {bold}sudo pip3 install -U jetson-stats{reset} or".format(bold=bcolors.BOLD, reset=bcolors.ENDC))
123+
print(f"[{bcolors.warning()}] {message}")
124+
print(f" Please, try: {bcolors.BOLD}sudo pip3 install -U jetson-stats{bcolors.ENDC} or")
125+
138126
# Generate hyperlink for shell
139-
# Check type of shell
140127
try:
141-
link = u'\u001b]8;;{url}\u001b\\{text}\u001b]8;;\u001b\\ (press CTRL + Click)'.format(url=url, text=text)
128+
link = f'\u001b]8;;{url}\u001b\\{text}\u001b]8;;\u001b\\ (press CTRL + Click)'
142129
except UnicodeDecodeError:
143-
link = "{text}".format(text=text)
144-
print(" {link}".format(link=link))
130+
link = f"{text}"
131+
print(f" {link}")
145132

146133

147134
def make_issue(repository, title, body="", labels="", template=""):
148-
# Reference
149-
# https://help.github.com/en/enterprise/2.17/user/github/managing-your-work-on-github/about-automation-for-issues-and-pull-requests-with-query-parameters
150-
# https://krypted.com/utilities/html-encoding-reference/
135+
# Encode title and body for URL
151136
title = title.replace(" ", "+").replace("[", "%5B").replace("]", "%5D")
152-
url = "{repository}/new?title={title}".format(repository=repository, title=title)
137+
url = f"{repository}/new?title={title}"
153138
if labels:
154-
url += "&labels=" + labels
139+
url += f"&labels={labels}"
155140
if template:
156-
url += "&template=" + template
141+
url += f"&template={template}"
157142
if body:
158143
body = body.replace(" ", "+").replace("#", "%23").replace("\n", "%0A").replace("*", "%2A")
159-
url += "&body=" + body
144+
url += f"&body={body}"
160145
return url
161146
# EOF

0 commit comments

Comments
 (0)