• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SpiNNakerManchester / SpiNNMan / 6574804013

19 Oct 2023 12:47PM UTC coverage: 51.937% (+1.2%) from 50.777%
6574804013

Pull #327

github

Christian-B
typing changes
Pull Request #327: Type Annotations and Checking

105 of 1288 branches covered (0.0%)

Branch coverage included in aggregate %.

2375 of 2375 new or added lines in 180 files covered. (100.0%)

4775 of 8108 relevant lines covered (58.89%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.56
/spinnman/spalloc/utils.py
1
# Copyright (c) 2021 The University of Manchester
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     https://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
"""
1✔
15
Miscellaneous utilities for working with URLs relating to the Spalloc Client.
16
"""
17

18
from typing import Iterable, Tuple, Optional
1✔
19
from urllib.parse import urlparse, urlsplit, urlunparse
1✔
20

21

22
def clean_url(url: str) -> str:
1✔
23
    """
24
    Add a ``/`` to the end of the path part of a URL if there isn't one.
25

26
    :param str url:
27
    :rtype: str
28
    """
29
    r = urlparse(url)
×
30
    parts = list(r)
×
31
    # Add a / to the end of the path if it isn't there
32
    if not parts[2].endswith("/"):
×
33
        parts[2] += "/"
×
34
    return urlunparse(parts)
×
35

36

37
def parse_service_url(url: str) -> Tuple[str, Optional[str], Optional[str]]:
1✔
38
    """
39
    Parses a combined service reference. Must include a hostname.
40

41
    :param str url:
42
    :return: URL, username (may be `None`), password (may be `None`)
43
    :rtype: tuple(str,str,str)
44
    """
45
    pieces = urlparse(url)
×
46
    user = pieces.username
×
47
    password = pieces.password
×
48
    netloc = pieces.hostname
×
49
    if netloc is None:
×
50
        raise ValueError("URL must have a hostname")
×
51
    if pieces.port is not None:
×
52
        netloc += f":{pieces.port}"
×
53
    url = urlunparse((
×
54
        pieces.scheme, netloc, pieces.path, None, None, None))
55
    return url, user, password
×
56

57

58
def get_hostname(url: str) -> str:
1✔
59
    """
60
    Parses a URL and extracts the hostname part.
61
    A hostname must be present.
62
    """
63
    netloc = urlsplit(url).hostname
×
64
    if netloc is None:
×
65
        raise ValueError("URL must have a hostname")
×
66
    return netloc
×
67

68

69
def is_server_address(
1✔
70
        address: str, additional_schemes: Iterable[str] = ()) -> bool:
71
    """
72
    Test if the given address is a likely Spalloc server URL.
73

74
    :param str address: The address to check
75
    :param ~collections.abc.Iterable(str) additional_schemes:
76
        Any additional URL schemes that should be considered to be successes;
77
        typically ``{"spalloc"}`` when looser matching is required.
78
    :rtype: bool
79
    """
80
    schemes = {"http", "https"}
×
81
    if additional_schemes:
×
82
        schemes.update(additional_schemes)
×
83
    try:
×
84
        pieces = urlparse(address)
×
85
        scheme = pieces.scheme.lower()
×
86
        return scheme in schemes and pieces.netloc is not None
×
87
    except Exception:  # pylint: disable=broad-except
×
88
        return False
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc