Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/smart_open/local_file.py: 78%

18 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:57 +0000

1# -*- coding: utf-8 -*- 

2# 

3# Copyright (C) 2020 Radim Rehurek <me@radimrehurek.com> 

4# 

5# This code is distributed under the terms and conditions 

6# from the MIT License (MIT). 

7# 

8"""Implements the transport for the file:// schema.""" 

9import io 

10import os.path 

11 

12SCHEME = 'file' 

13 

14URI_EXAMPLES = ( 

15 './local/path/file', 

16 '~/local/path/file', 

17 'local/path/file', 

18 './local/path/file.gz', 

19 'file:///home/user/file', 

20 'file:///home/user/file.bz2', 

21) 

22 

23 

24open = io.open 

25 

26 

27def parse_uri(uri_as_string): 

28 local_path = extract_local_path(uri_as_string) 

29 return dict(scheme=SCHEME, uri_path=local_path) 

30 

31 

32def open_uri(uri_as_string, mode, transport_params): 

33 parsed_uri = parse_uri(uri_as_string) 

34 fobj = io.open(parsed_uri['uri_path'], mode) 

35 return fobj 

36 

37 

38def extract_local_path(uri_as_string): 

39 if uri_as_string.startswith('file://'): 

40 local_path = uri_as_string.replace('file://', '', 1) 

41 else: 

42 local_path = uri_as_string 

43 return os.path.expanduser(local_path)