Skip to content

Commit e36f90c

Browse files
committed
enh: let on startup create the py script and register protocol'
1 parent 8f7e038 commit e36f90c

File tree

2 files changed

+116
-8
lines changed

2 files changed

+116
-8
lines changed

IDCBrowser/IDCHandlerModule.py

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from WebServerLib.BaseRequestHandler import BaseRequestHandler, BaseRequestLoggingFunction
88

99
import os
10+
import platform
1011
import subprocess
1112
import time
1213
import logging
@@ -15,15 +16,8 @@
1516
import qt
1617
import slicer
1718
import ctk
18-
# IDCRequestHandler.py
19-
20-
from WebServerLib.BaseRequestHandler import BaseRequestHandler, BaseRequestLoggingFunction
21-
import urllib
22-
import os
23-
from typing import Optional
2419
from idc_index import index
2520

26-
2721
class IDCRequestHandler(BaseRequestHandler):
2822

2923
def __init__(self, logMessage: Optional[BaseRequestLoggingFunction] = None):
@@ -33,7 +27,7 @@ def __init__(self, logMessage: Optional[BaseRequestLoggingFunction] = None):
3327

3428
def canHandleRequest(self, uri: bytes, **_kwargs) -> float:
3529
parsedURL = urllib.parse.urlparse(uri)
36-
if parsedURL.path.startswith(b"/idc"):
30+
if (parsedURL.path.startswith(b"/idc")):
3731
return 0.5
3832
return 0.0
3933

@@ -126,4 +120,81 @@ def onStartupCompleted(self):
126120

127121
logic.start()
128122
print("IDC Request Handler has been registered and server started.")
123+
124+
self.writeResolverScript()
125+
self.registerCustomProtocol()
126+
127+
def writeResolverScript(self):
128+
current_dir = os.path.dirname(os.path.realpath(__file__))
129+
resolver_script_path = os.path.join(current_dir,'Resources', 'resolver.py')
130+
131+
resolver_script_content = '''import sys
132+
import urllib.parse
133+
import requests
134+
import webbrowser
135+
136+
def resolve_url(url):
137+
# Parse the URL
138+
parsed_url = urllib.parse.urlparse(url)
139+
140+
# Remove the scheme (idcbrowser://) from the URL and split the path
141+
path_parts = parsed_url.netloc.split('/') + parsed_url.path.split('/')[1:]
142+
143+
# Check the first part of the path to determine the endpoint
144+
if path_parts[0] == 'collections':
145+
new_url = "http://localhost:2042/idc/collections"
146+
# Open the new URL in a web browser
147+
webbrowser.open(new_url)
148+
elif path_parts[0] == 'series':
149+
new_url = f"http://localhost:2042/idc/download/seriesInstanceUID/{path_parts[1]}"
150+
elif path_parts[0] == 'studies':
151+
new_url = f"http://localhost:2042/idc/download/studyInstanceUID/{path_parts[1]}"
152+
else:
153+
print(f"Unhandled path: {path_parts[0]}")
154+
return
155+
156+
# Make the request to the new URL
157+
response = requests.get(new_url)
158+
159+
# Print the response
160+
print(response.text)
161+
162+
if __name__ == "__main__":
163+
# The URL is passed as the first argument
164+
url = sys.argv[1]
165+
166+
# Resolve the URL
167+
resolve_url(url)
168+
'''
169+
170+
with open(resolver_script_path, 'w') as f:
171+
f.write(resolver_script_content)
172+
print(f"Resolver script written to {resolver_script_path}")
173+
174+
def registerCustomProtocol(self):
175+
if platform.system() == "Linux":
176+
# Check if the protocol is already registered
177+
if os.path.exists(os.path.expanduser("~/.local/share/applications/idcbrowser.desktop")):
178+
print("IDC Browser URL protocol is already registered.")
179+
return
180+
181+
# Get the current directory
182+
current_dir = os.path.dirname(os.path.realpath(__file__))
183+
python_script_path = os.path.join(current_dir, 'resolver.py')
184+
185+
# Register IDC Browser URL protocol
186+
with open(os.path.expanduser("~/.local/share/applications/idcbrowser.desktop"), "w") as f:
187+
f.write(f"""[Desktop Entry]
188+
Name=IDC Browser
189+
Exec=python3 {python_script_path} %u
190+
Type=Application
191+
Terminal=false
192+
MimeType=x-scheme-handler/idcbrowser;
193+
""")
194+
195+
# Update MIME database
196+
os.system("update-desktop-database ~/.local/share/applications/")
197+
os.system("xdg-mime default idcbrowser.desktop x-scheme-handler/idcbrowser")
198+
else:
199+
print("IDC Browser URL protocol registration is not supported on this operating system.")
129200

IDCBrowser/Resources/resolver.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
import urllib.parse
3+
import requests
4+
import webbrowser
5+
6+
def resolve_url(url):
7+
# Parse the URL
8+
parsed_url = urllib.parse.urlparse(url)
9+
10+
# Remove the scheme (idcbrowser://) from the URL and split the path
11+
path_parts = parsed_url.netloc.split('/') + parsed_url.path.split('/')[1:]
12+
13+
# Check the first part of the path to determine the endpoint
14+
if path_parts[0] == 'collections':
15+
new_url = "http://localhost:2042/idc/collections"
16+
# Open the new URL in a web browser
17+
webbrowser.open(new_url)
18+
elif path_parts[0] == 'series':
19+
new_url = f"http://localhost:2042/idc/download/seriesInstanceUID/{path_parts[1]}"
20+
elif path_parts[0] == 'studies':
21+
new_url = f"http://localhost:2042/idc/download/studyInstanceUID/{path_parts[1]}"
22+
else:
23+
print(f"Unhandled path: {path_parts[0]}")
24+
return
25+
26+
# Make the request to the new URL
27+
response = requests.get(new_url)
28+
29+
# Print the response
30+
print(response.text)
31+
32+
if __name__ == "__main__":
33+
# The URL is passed as the first argument
34+
url = sys.argv[1]
35+
36+
# Resolve the URL
37+
resolve_url(url)

0 commit comments

Comments
 (0)