Skip to content

Commit 8669596

Browse files
committed
Move destinations function and give it token
1 parent cd843c4 commit 8669596

File tree

4 files changed

+97
-82
lines changed

4 files changed

+97
-82
lines changed

src/murfey/client/destinations.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from __future__ import annotations
2+
3+
import logging
4+
from pathlib import Path
5+
from typing import Dict
6+
7+
from murfey.client.analyser import Analyser
8+
from murfey.client.instance_environment import (
9+
MurfeyInstanceEnvironment,
10+
global_env_lock,
11+
)
12+
from murfey.util.client import capture_get, capture_post
13+
14+
logger = logging.getLogger("murfey.client.destinations")
15+
16+
17+
def determine_default_destination(
18+
visit: str,
19+
source: Path,
20+
destination: str,
21+
environment: MurfeyInstanceEnvironment,
22+
analysers: Dict[Path, Analyser],
23+
token: str,
24+
touch: bool = False,
25+
extra_directory: str = "",
26+
include_mid_path: bool = True,
27+
use_suggested_path: bool = True,
28+
) -> str:
29+
machine_data = capture_get(
30+
base_url=str(environment.url.geturl()),
31+
router_name="session_control.router",
32+
function_name="machine_info_by_instrument",
33+
token=token,
34+
instrument_name=environment.instrument_name,
35+
).json()
36+
_default = ""
37+
if environment.processing_only_mode and environment.sources:
38+
logger.info(f"Processing only mode with sources {environment.sources}")
39+
_default = str(environment.sources[0].absolute()) or str(Path.cwd())
40+
elif machine_data.get("data_directories"):
41+
for data_dir in machine_data["data_directories"]:
42+
if source.absolute() == Path(data_dir).absolute():
43+
_default = f"{destination}/{visit}"
44+
break
45+
else:
46+
try:
47+
mid_path = source.absolute().relative_to(Path(data_dir).absolute())
48+
if use_suggested_path:
49+
with global_env_lock:
50+
source_name = (
51+
source.name
52+
if source.name != "Images-Disc1"
53+
else source.parent.name
54+
)
55+
if environment.destination_registry.get(source_name):
56+
_default = environment.destination_registry[source_name]
57+
else:
58+
suggested_path_response = capture_post(
59+
base_url=str(environment.url.geturl()),
60+
router_name="file_io_instrument.router",
61+
function_name="suggest_path",
62+
token=token,
63+
visit_name=visit,
64+
session_id=environment.murfey_session,
65+
data={
66+
"base_path": f"{destination}/{visit}/{mid_path.parent if include_mid_path else ''}/raw",
67+
"touch": touch,
68+
"extra_directory": extra_directory,
69+
},
70+
)
71+
if suggested_path_response is None:
72+
raise RuntimeError("Murfey server is unreachable")
73+
_default = suggested_path_response.json().get(
74+
"suggested_path"
75+
)
76+
environment.destination_registry[source_name] = _default
77+
else:
78+
_default = f"{destination}/{visit}/{mid_path if include_mid_path else source.name}"
79+
break
80+
except (ValueError, KeyError):
81+
_default = ""
82+
else:
83+
_default = ""
84+
else:
85+
_default = f"{destination}/{visit}"
86+
return (
87+
_default + f"/{extra_directory}"
88+
if not _default.endswith("/")
89+
else _default + f"{extra_directory}"
90+
)

src/murfey/client/multigrid_control.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
from murfey.client.analyser import Analyser
1515
from murfey.client.contexts.spa import SPAModularContext
1616
from murfey.client.contexts.tomo import TomographyContext
17+
from murfey.client.destinations import determine_default_destination
1718
from murfey.client.instance_environment import MurfeyInstanceEnvironment
1819
from murfey.client.rsync import RSyncer, RSyncerUpdate, TransferResult
19-
from murfey.client.tui.screens import determine_default_destination
2020
from murfey.client.watchdir import DirWatcher
2121
from murfey.util import posix_path
2222
from murfey.util.client import (
@@ -267,6 +267,7 @@ def _start_rsyncer_multigrid(
267267
self._environment.default_destinations[source],
268268
self._environment,
269269
self.analysers or {},
270+
self.token,
270271
touch=True,
271272
extra_directory=extra_directory,
272273
include_mid_path=include_mid_path,

src/murfey/client/tui/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from murfey.client.analyser import Analyser
1717
from murfey.client.contexts.spa import SPAModularContext
1818
from murfey.client.contexts.tomo import TomographyContext
19+
from murfey.client.destinations import determine_default_destination
1920
from murfey.client.instance_environment import MurfeyInstanceEnvironment
2021
from murfey.client.rsync import RSyncer, RSyncerUpdate, TransferResult
2122
from murfey.client.tui.screens import (
@@ -27,7 +28,6 @@
2728
VisitCreation,
2829
VisitSelection,
2930
WaitingScreen,
30-
determine_default_destination,
3131
)
3232
from murfey.client.tui.status_bar import StatusBar
3333
from murfey.client.watchdir import DirWatcher
@@ -177,6 +177,7 @@ def _start_rsyncer_multigrid(
177177
self._default_destinations[source],
178178
self._environment,
179179
self.analysers,
180+
token,
180181
touch=True,
181182
extra_directory=extra_directory,
182183
include_mid_path=include_mid_path,

src/murfey/client/tui/screens.py

Lines changed: 3 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,10 @@
4545
)
4646
from werkzeug.utils import secure_filename
4747

48-
from murfey.client.analyser import Analyser
4948
from murfey.client.contexts.spa import SPAModularContext
5049
from murfey.client.contexts.tomo import TomographyContext
50+
from murfey.client.destinations import determine_default_destination
5151
from murfey.client.gain_ref import determine_gain_ref
52-
from murfey.client.instance_environment import (
53-
MurfeyInstanceEnvironment,
54-
global_env_lock,
55-
)
5652
from murfey.client.rsync import RSyncer
5753
from murfey.util import posix_path
5854
from murfey.util.client import (
@@ -72,81 +68,6 @@
7268
instrument_name = read_config()["Murfey"].get("instrument_name", "")
7369

7470

75-
def determine_default_destination(
76-
visit: str,
77-
source: Path,
78-
destination: str,
79-
environment: MurfeyInstanceEnvironment,
80-
analysers: Dict[Path, Analyser],
81-
touch: bool = False,
82-
extra_directory: str = "",
83-
include_mid_path: bool = True,
84-
use_suggested_path: bool = True,
85-
) -> str:
86-
machine_data = capture_get(
87-
base_url=str(environment.url.geturl()),
88-
router_name="session_control.router",
89-
function_name="machine_info_by_instrument",
90-
token=token,
91-
instrument_name=environment.instrument_name,
92-
).json()
93-
_default = ""
94-
if environment.processing_only_mode and environment.sources:
95-
log.info(f"Processing only mode with sources {environment.sources}")
96-
_default = str(environment.sources[0].absolute()) or str(Path.cwd())
97-
elif machine_data.get("data_directories"):
98-
for data_dir in machine_data["data_directories"]:
99-
if source.absolute() == Path(data_dir).absolute():
100-
_default = f"{destination}/{visit}"
101-
break
102-
else:
103-
try:
104-
mid_path = source.absolute().relative_to(Path(data_dir).absolute())
105-
if use_suggested_path:
106-
with global_env_lock:
107-
source_name = (
108-
source.name
109-
if source.name != "Images-Disc1"
110-
else source.parent.name
111-
)
112-
if environment.destination_registry.get(source_name):
113-
_default = environment.destination_registry[source_name]
114-
else:
115-
suggested_path_response = capture_post(
116-
base_url=str(environment.url.geturl()),
117-
router_name="file_io_instrument.router",
118-
function_name="suggest_path",
119-
token=token,
120-
visit_name=visit,
121-
session_id=environment.murfey_session,
122-
data={
123-
"base_path": f"{destination}/{visit}/{mid_path.parent if include_mid_path else ''}/raw",
124-
"touch": touch,
125-
"extra_directory": extra_directory,
126-
},
127-
)
128-
if suggested_path_response is None:
129-
raise RuntimeError("Murfey server is unreachable")
130-
_default = suggested_path_response.json().get(
131-
"suggested_path"
132-
)
133-
environment.destination_registry[source_name] = _default
134-
else:
135-
_default = f"{destination}/{visit}/{mid_path if include_mid_path else source.name}"
136-
break
137-
except (ValueError, KeyError):
138-
_default = ""
139-
else:
140-
_default = ""
141-
else:
142-
_default = f"{destination}/{visit}"
143-
return (
144-
_default + f"/{extra_directory}"
145-
if not _default.endswith("/")
146-
else _default + f"{extra_directory}"
147-
)
148-
149-
15071
class InputResponse(NamedTuple):
15172
question: str
15273
allowed_responses: List[str] | None = None
@@ -349,6 +270,7 @@ def on_button_pressed(self, event: Button.Pressed) -> None:
349270
defd,
350271
self.app._environment,
351272
self.app.analysers,
273+
token,
352274
touch=True,
353275
)
354276
visit_path = defd + f"/{text}"
@@ -1077,6 +999,7 @@ def compose(self):
1077999
f"{datetime.now().year}",
10781000
self.app._environment,
10791001
self.app.analysers,
1002+
token,
10801003
touch=True,
10811004
)
10821005
if dest and dest in destinations:

0 commit comments

Comments
 (0)