Skip to content

Commit a13e75e

Browse files
use pythonanywhere core 0.1.9 for file sharing. by Giles and Filip
1 parent 28240fd commit a13e75e

File tree

3 files changed

+11
-33
lines changed

3 files changed

+11
-33
lines changed

pythonanywhere/files.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import getpass
66
import logging
7-
from urllib.parse import urljoin
87

98
from snakesay import snakesay
109

@@ -45,9 +44,6 @@ def __init__(self, path):
4544
def __repr__(self):
4645
return self.url
4746

48-
def _make_sharing_url(self, path):
49-
return urljoin(self.api.base_url.split("api")[0], path)
50-
5147
@staticmethod
5248
def _standarize_path(path):
5349
return path.replace("~", f"/home/{getpass.getuser()}") if path.startswith("~") else path
@@ -141,10 +137,9 @@ def get_sharing_url(self, quiet=False):
141137

142138
url = self.api.sharing_get(self.path)
143139
if url:
144-
sharing_url = self._make_sharing_url(url)
145140
if not quiet:
146-
logger.info(snakesay(f"{self.path} is shared at {sharing_url}"))
147-
return sharing_url
141+
logger.info(snakesay(f"{self.path} is shared at {url}"))
142+
return url
148143

149144
logger.info(snakesay(f"{self.path} has not been shared"))
150145

@@ -155,15 +150,13 @@ def share(self):
155150
empty string when share not successful."""
156151

157152
try:
158-
code, url = self.api.sharing_post(self.path)
153+
msg, url = self.api.sharing_post(self.path)
159154
except Exception as e:
160155
logger.warning(snakesay(str(e)))
161156
return ""
162157

163-
msg = {200: "was already", 201: "successfully"}[code]
164-
sharing_url = self._make_sharing_url(url)
165-
logger.info(snakesay(f"{self.path} {msg} shared at {sharing_url}"))
166-
return sharing_url
158+
logger.info(snakesay(f"{self.path} {msg} at {url}"))
159+
return url
167160

168161
def unshare(self):
169162
"""Returns `True` when file unshared or has not been shared,

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pytest==8.2.1
88
pytest-cov==5.0.0
99
pytest-mock==3.14.0
1010
pytest-mypy==0.10.3
11-
pythonanywhere_core==0.1.8
11+
pythonanywhere_core==0.1.9
1212
requests==2.32.3
1313
responses==0.25.0
1414
schema==0.7.2

tests/test_files.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ def test_repr_returns_url_property_value(self, mocker):
4949

5050
assert PAPath("path").__repr__() == mock_url
5151

52-
def test_make_sharing_url_contains_pa_site_address(self, mocker):
53-
mock_urljoin = mocker.patch("pythonanywhere.files.urljoin")
54-
pa_path = PAPath("path")
55-
56-
pa_path._make_sharing_url("rest")
57-
58-
assert mock_urljoin.call_args == call(pa_path.api.base_url.split("api")[0], "rest")
59-
6052
def test_sanitizes_path(self):
6153
pa_path = PAPath("~")
6254

@@ -205,15 +197,14 @@ class TestPAPathShare():
205197
def test_returns_full_url_for_shared_file(self, mocker):
206198
mock_sharing_get = mocker.patch("pythonanywhere_core.files.Files.sharing_get")
207199
mock_sharing_get.return_value = "url"
208-
mock_make_url = mocker.patch("pythonanywhere.files.PAPath._make_sharing_url")
209200
mock_snake = mocker.patch("pythonanywhere.files.snakesay")
210201
mock_info = mocker.patch("pythonanywhere.files.logger.info")
211202
query_path = "/pa/path/to/a/file"
212203

213204
result = PAPath(query_path).get_sharing_url()
214205

215206
assert mock_sharing_get.call_args == call(query_path)
216-
assert mock_snake.call_args == call(f"{query_path} is shared at {mock_make_url.return_value}")
207+
assert mock_snake.call_args == call(f"{query_path} is shared at {mock_sharing_get.return_value}")
217208
assert mock_info.call_args == call(mock_snake.return_value)
218209
assert result.endswith("url")
219210

@@ -233,35 +224,29 @@ def test_returns_empty_string_when_file_not_shared(self, mocker):
233224

234225
def test_path_already_shared(self, mocker):
235226
mock_sharing_post = mocker.patch("pythonanywhere_core.files.Files.sharing_post")
236-
mock_sharing_post.return_value = (200, "url")
237-
mock_make_url = mocker.patch("pythonanywhere.files.PAPath._make_sharing_url")
227+
mock_sharing_post.return_value = ("was already shared", "url")
238228
mock_snake = mocker.patch("pythonanywhere.files.snakesay")
239229
mock_info = mocker.patch("pythonanywhere.files.logger.info")
240230
path_to_share = "/pa/path/to/a/file"
241231

242232
result = PAPath(path_to_share).share()
243233

244234
assert mock_sharing_post.call_args == call(path_to_share)
245-
assert mock_snake.call_args == call(f"{path_to_share} was already shared at {mock_make_url.return_value}")
235+
assert mock_snake.call_args == call(f"{path_to_share} was already shared at {mock_sharing_post.return_value[1]}")
246236
assert mock_info.call_args == call(mock_snake.return_value)
247-
assert mock_make_url.call_args == call("url")
248-
assert result == mock_make_url.return_value
249237

250238
def test_path_successfully_shared(self, mocker):
251239
mock_sharing_post = mocker.patch("pythonanywhere_core.files.Files.sharing_post")
252-
mock_sharing_post.return_value = (201, "url")
253-
mock_make_url = mocker.patch("pythonanywhere.files.PAPath._make_sharing_url")
240+
mock_sharing_post.return_value = ("successfully shared", "url")
254241
mock_snake = mocker.patch("pythonanywhere.files.snakesay")
255242
mock_info = mocker.patch("pythonanywhere.files.logger.info")
256243
path_to_share = "/pa/path/to/a/file"
257244

258245
result = PAPath(path_to_share).share()
259246

260247
assert mock_sharing_post.call_args == call(path_to_share)
261-
assert mock_snake.call_args == call(f"{path_to_share} successfully shared at {mock_make_url.return_value}")
248+
assert mock_snake.call_args == call(f"{path_to_share} successfully shared at {mock_sharing_post.return_value[1]}")
262249
assert mock_info.call_args == call(mock_snake.return_value)
263-
assert mock_make_url.call_args == call("url")
264-
assert result == mock_make_url.return_value
265250

266251
def test_warns_if_share_fails(self, mocker):
267252
mock_sharing_post = mocker.patch("pythonanywhere_core.files.Files.sharing_post")

0 commit comments

Comments
 (0)