Shaman: Moved URL parsing into a separate function

This allows other code to just call that function, rather than copying
the behaviour.
This commit is contained in:
Sybren A. Stüvel 2019-02-28 12:32:07 +01:00
parent f34bf7c66f
commit b22b9da5d0
3 changed files with 69 additions and 16 deletions

View File

@ -145,29 +145,15 @@ def create_shamanpacker(bpath: pathlib.Path, ppath: pathlib.Path, tpath: str) ->
This uses HTTPS to connect to the server. To connect using HTTP, use:
shaman+http://hostname/base-url#jobID
"""
import urllib.parse
from blender_asset_tracer.pack import shaman
urlparts = urllib.parse.urlparse(str(tpath))
if urlparts.scheme in {'shaman', 'shaman+https'}:
scheme = 'https'
elif urlparts.scheme == 'shaman+http':
scheme = 'http'
else:
raise SystemExit('Invalid scheme %r, choose shaman:// or shaman+http://', urlparts.scheme)
checkout_id = urlparts.fragment
endpoint, checkout_id = shaman.parse_endpoint(tpath)
if not checkout_id:
log.warning('No checkout ID given on the URL. Going to send BAT pack to Shaman, '
'but NOT creating a checkout')
new_urlparts = (scheme, *urlparts[1:-1], '')
endpoint = urllib.parse.urlunparse(new_urlparts)
log.info('Uploading to Shaman server %s with job %s', endpoint, checkout_id)
return shaman.ShamanPacker(bpath, ppath, tpath, endpoint=endpoint, checkout_id=checkout_id)
return shaman.ShamanPacker(bpath, ppath, '/', endpoint=endpoint, checkout_id=checkout_id)
def paths_from_cli(args) -> typing.Tuple[pathlib.Path, pathlib.Path, str]:

View File

@ -20,6 +20,7 @@
"""Shaman Client interface."""
import logging
import pathlib
import typing
import urllib.parse
import requests
@ -93,3 +94,24 @@ class ShamanPacker(bat_pack.Packer):
log.exception('Error communicating with Shaman')
self.abort(str(ex))
self._check_aborted()
def parse_endpoint(shaman_url: str) -> typing.Tuple[str, str]:
"""Convert shaman://hostname/path#checkoutID into endpoint URL + checkout ID."""
urlparts = urllib.parse.urlparse(str(shaman_url))
if urlparts.scheme in {'shaman', 'shaman+https'}:
scheme = 'https'
elif urlparts.scheme == 'shaman+http':
scheme = 'http'
else:
raise ValueError('Invalid scheme %r, choose shaman:// or shaman+http://', urlparts.scheme)
checkout_id = urllib.parse.unquote(urlparts.fragment)
path = urlparts.path or '/'
new_urlparts = (scheme, urlparts.netloc, path, *urlparts[3:-1], '')
endpoint = urllib.parse.urlunparse(new_urlparts)
return endpoint, checkout_id

45
tests/test_shaman.py Normal file
View File

@ -0,0 +1,45 @@
import unittest
from blender_asset_tracer.pack import shaman
class ParseEndpointTest(unittest.TestCase):
def test_path_slashyness(self):
self.assertEqual(
('https://endpoint/', '123'),
shaman.parse_endpoint('shaman://endpoint#123'),
)
self.assertEqual(
('https://endpoint/', '123'),
shaman.parse_endpoint('shaman://endpoint/#123'),
)
self.assertEqual(
('https://endpoint/root', '123'),
shaman.parse_endpoint('shaman://endpoint/root#123'),
)
self.assertEqual(
('https://endpoint/root/is/longer/', '123'),
shaman.parse_endpoint('shaman://endpoint/root/is/longer/#123'),
)
def test_schemes_with_plus(self):
self.assertEqual(
('https://endpoint/', '123'),
shaman.parse_endpoint('shaman+https://endpoint/#123'),
)
self.assertEqual(
('http://endpoint/', '123'),
shaman.parse_endpoint('shaman+http://endpoint/#123'),
)
def test_checkout_ids(self):
self.assertEqual(
('https://endpoint/', ''),
shaman.parse_endpoint('shaman+https://endpoint/'),
)
# Not a valid ID, but the parser should handle it gracefully anyway
self.assertEqual(
('http://endpoint/', 'ïđ'),
shaman.parse_endpoint('shaman+http://endpoint/#%C3%AF%C4%91'),
)