Skip to content

Commit f00c482

Browse files
committed
Rearranged lines in 'run()' in logical blocks; added comments to describe purpose of code blocks
1 parent 59297f9 commit f00c482

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/murfey/client/__init__.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import webbrowser
1212
from datetime import datetime
1313
from pathlib import Path
14+
from pprint import pprint
1415
from queue import Queue
1516
from typing import List, Literal
1617
from urllib.parse import ParseResult, urlparse
@@ -91,6 +92,7 @@ def _check_for_updates(
9192

9293

9394
def run():
95+
# Load client config and server information
9496
config = read_config()
9597
instrument_name = config["Murfey"]["instrument_name"]
9698
try:
@@ -109,6 +111,7 @@ def run():
109111
else:
110112
known_server = config["Murfey"].get("server")
111113

114+
# Set up argument parser with dynamic defaults based on client config
112115
parser = argparse.ArgumentParser(description="Start the Murfey client")
113116
parser.add_argument(
114117
"--server",
@@ -194,23 +197,23 @@ def run():
194197
default=False,
195198
help="Do not trigger processing for any data directories currently on disk (you may have started processing for them in a previous murfey run)",
196199
)
197-
198200
args = parser.parse_args()
199201

202+
# Logic to exit early based on parsed args
200203
if not args.server:
201204
exit("Murfey server not set. Please run with --server host:port")
202205
if not args.server.startswith(("http://", "https://")):
203206
if "://" in args.server:
204207
exit("Unknown server protocol. Only http:// and https:// are allowed")
205208
args.server = f"http://{args.server}"
206-
207209
if args.remove_files:
208210
remove_prompt = Confirm.ask(
209211
f"Are you sure you want to remove files from {args.source or Path('.').absolute()}?"
210212
)
211213
if not remove_prompt:
212214
exit("Exiting")
213215

216+
# If a new server URL is provided, save info to config file
214217
murfey_url = urlparse(args.server, allow_fragments=False)
215218
if args.server != known_server:
216219
# New server specified. Verify that it is real
@@ -232,8 +235,7 @@ def run():
232235
if args.no_transfer:
233236
log.info("No files will be transferred as --no-transfer flag was specified")
234237

235-
from pprint import pprint
236-
238+
# Check ISPyB (if set up) for ongoing visits
237239
ongoing_visits = []
238240
if args.visit:
239241
ongoing_visits = [args.visit]
@@ -250,35 +252,38 @@ def run():
250252

251253
_enable_webbrowser_in_cygwin()
252254

255+
# Set up additional log handlers
253256
log.setLevel(logging.DEBUG)
254257
log_queue = Queue()
255258
input_queue = Queue()
256259

257-
# rich_handler = DirectableRichHandler(log_queue, enable_link_path=False)
260+
# Rich-based console handler
258261
rich_handler = DirectableRichHandler(enable_link_path=False)
259262
rich_handler.setLevel(logging.DEBUG if args.debug else logging.INFO)
260263

264+
# Set up websocket app and handler
261265
client_id = requests.get(f"{murfey_url.geturl()}/new_client_id/").json()
262266
ws = murfey.client.websocket.WSApp(
263267
server=args.server,
264268
id=client_id["new_id"],
265269
)
270+
ws_handler = CustomHandler(ws.send)
266271

272+
# Add additional handlers and set logging levels
267273
logging.getLogger().addHandler(rich_handler)
268-
handler = CustomHandler(ws.send)
269-
logging.getLogger().addHandler(handler)
274+
logging.getLogger().addHandler(ws_handler)
270275
logging.getLogger("murfey").setLevel(logging.INFO)
271276
logging.getLogger("websocket").setLevel(logging.WARNING)
272277

273278
log.info("Starting Websocket connection")
274279

275-
status_bar = StatusBar()
276-
280+
# Load machine data for subsequent sections
277281
machine_data = requests.get(
278282
f"{murfey_url.geturl()}/instruments/{instrument_name}/machine"
279283
).json()
280284
gain_ref: Path | None = None
281285

286+
# Set up Murfey environment instance and map it to websocket app
282287
instance_environment = MurfeyInstanceEnvironment(
283288
url=murfey_url,
284289
client_id=ws.id,
@@ -295,9 +300,10 @@ def run():
295300
else ""
296301
),
297302
)
298-
299303
ws.environment = instance_environment
300304

305+
# Set up and run Murfey TUI app
306+
status_bar = StatusBar()
301307
rich_handler.redirect = True
302308
app = MurfeyTUI(
303309
environment=instance_environment,

0 commit comments

Comments
 (0)