File: local_file.py

package info (click to toggle)
smart-open 7.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 980 kB
  • sloc: python: 8,054; sh: 90; makefile: 14
file content (43 lines) | stat: -rw-r--r-- 1,004 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 Radim Rehurek <me@radimrehurek.com>
#
# This code is distributed under the terms and conditions
# from the MIT License (MIT).
#
"""Implements the transport for the file:// schema."""
import io
import os.path

SCHEME = 'file'

URI_EXAMPLES = (
    './local/path/file',
    '~/local/path/file',
    'local/path/file',
    './local/path/file.gz',
    'file:///home/user/file',
    'file:///home/user/file.bz2',
)


open = io.open


def parse_uri(uri_as_string):
    local_path = extract_local_path(uri_as_string)
    return dict(scheme=SCHEME, uri_path=local_path)


def open_uri(uri_as_string, mode, transport_params):
    parsed_uri = parse_uri(uri_as_string)
    fobj = io.open(parsed_uri['uri_path'], mode)
    return fobj


def extract_local_path(uri_as_string):
    if uri_as_string.startswith('file://'):
        local_path = uri_as_string.replace('file://', '', 1)
    else:
        local_path = uri_as_string
    return os.path.expanduser(local_path)