Coverage for sources/librovore/urls.py: 60%

19 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-03 21:59 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

4#============================================================================# 

5# # 

6# Licensed under the Apache License, Version 2.0 (the "License"); # 

7# you may not use this file except in compliance with the License. # 

8# You may obtain a copy of the License at # 

9# # 

10# http://www.apache.org/licenses/LICENSE-2.0 # 

11# # 

12# Unless required by applicable law or agreed to in writing, software # 

13# distributed under the License is distributed on an "AS IS" BASIS, # 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

15# See the License for the specific language governing permissions and # 

16# limitations under the License. # 

17# # 

18#============================================================================# 

19 

20 

21''' Common URL manipulation and normalization utilities. ''' 

22 

23 

24import urllib.parse as _urlparse 

25from urllib.parse import ParseResult as _Url 

26 

27from . import __ 

28from . import exceptions as _exceptions 

29 

30 

31def normalize_base_url( source: str ) -> _Url: 

32 ''' Extracts clean base documentation URL from any source. ''' 

33 try: url = _urlparse.urlparse( source ) 

34 except Exception as exc: 

35 raise _exceptions.InventoryUrlInvalidity( source ) from exc 

36 match url.scheme: 

37 case '': 37 ↛ 42line 37 didn't jump to line 42 because the pattern on line 37 always matched

38 path = __.Path( source ) 

39 if path.is_file( ) or ( not path.exists( ) and path.suffix ): 39 ↛ 40line 39 didn't jump to line 40 because the condition on line 39 was never true

40 path = path.parent 

41 url = _urlparse.urlparse( path.resolve( ).as_uri( ) ) 

42 case 'http' | 'https' | 'file': pass 

43 case _: 

44 raise _exceptions.InventoryUrlNoSupport( 

45 url, component = 'scheme', value = url.scheme ) 

46 # Remove trailing slash for consistency 

47 path = url.path.rstrip( '/' ) if url.path != '/' else '' 

48 return url._replace( path = path )