Skip to content

Commit

Permalink
feat(env): Add env module (#270)
Browse files Browse the repository at this point in the history
# What
- Add `env` module for environment-specific values
- Use in `request` module for local files

# Why
- To DRY up the code
- To fix #269
  • Loading branch information
tymokvo authored Feb 19, 2021
1 parent baf3997 commit f5b2641
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
9 changes: 4 additions & 5 deletions queenbee/base/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from urllib import request
from typing import Dict

from ..env.os import OS

USER_AGENT_STRING = 'Queenbee'


Expand All @@ -14,20 +16,17 @@ def get_uri(url):
elif url.startswith('http://') or url.startswith('https://'):
return url

# a local file but is not formatted as local url
pre = 'file:///' if platform.system() == 'Windows' else 'file://'
return resolve_local_source(pre + url)
return resolve_local_source(OS.file_uri_prefix + url)


def resolve_local_source(path, as_uri=True):
"""Get an absolute path for a file:// or file:/// path.
Apparently both of them are used: https://en.wikipedia.org/wiki/File_URI_scheme
"""
sep = 'file:///' if platform.system() == 'Windows' else 'file://'

try:
rel_path = path.split(sep)[1]
rel_path = path.split(OS.file_uri_prefix)[1]
except IndexError:
raise ValueError('Invalid local path: {path}')

Expand Down
18 changes: 18 additions & 0 deletions queenbee/env/os.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import platform


class OS:
"""A simple wrapper to handle OS-specific switches.
Attributes:
is_windows: A boolean of whether the current system is MS Windows.
is_nix: A boolean of whether the current system is *nix (any Linux).
file_uri_prefix: A string to be used as the default prefix for URIs
which point to local files on the system.
"""

is_windows: bool = platform.system() == 'Windows'
is_nix: bool = not is_windows

file_uri_prefix: str = 'file:///' if is_windows else 'file://'

0 comments on commit f5b2641

Please sign in to comment.