migrate env handling to dotenv

This commit is contained in:
Matt Behrens 2018-03-17 20:04:04 -04:00
parent 950a933621
commit e0a8c58476
5 changed files with 36 additions and 11 deletions

View file

@ -9,8 +9,9 @@ name = "pypi"
cssselect = "*" cssselect = "*"
lxml = "*" lxml = "*"
"mastodon.py" = "*" "Mastodon.py" = "*"
youtube-dl = "*" youtube_dl = "*"
python-dotenv = "*"
[dev-packages] [dev-packages]

10
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "f0d0df34026363f209c1bda09fac7757ccdd8e90d348cba74e5aa278850abe5d" "sha256": "f73fe1eabc45c7e9153c6512b0a96453287660e8a6c78539eb32a539211d71eb"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -101,6 +101,14 @@
], ],
"version": "==2.7.0" "version": "==2.7.0"
}, },
"python-dotenv": {
"hashes": [
"sha256:4965ed170bf51c347a89820e8050655e9c25db3837db6602e906b6d850fad85c",
"sha256:509736185257111613009974e666568a1b031b028b61b500ef1ab4ee780089d5"
],
"index": "pypi",
"version": "==0.8.2"
},
"pytz": { "pytz": {
"hashes": [ "hashes": [
"sha256:07edfc3d4d2705a20a6e99d97f0c4b61c800b8232dc1c04d87e8554f130148dd", "sha256:07edfc3d4d2705a20a6e99d97f0c4b61c800b8232dc1c04d87e8554f130148dd",

17
fediplay/env.py Normal file
View file

@ -0,0 +1,17 @@
'''Environment variable management.'''
from os import getenv
from dotenv import load_dotenv, find_dotenv
def api_base_url():
return getenv('FEDIPLAY_API_BASE_URL')
def no_check_certificate():
return bool(getenv('FEDIPLAY_NO_CHECK_CERTIFICATE'))
def play_command():
return (getenv('FEDIPLAY_PLAY_COMMAND') or
'ffplay -v 0 -nostats -hide_banner -autoexit -nodisp {filename}')
load_dotenv(find_dotenv())

View file

@ -1,16 +1,17 @@
'''Entry point for command-line interface.''' '''Entry point for command-line interface.'''
from os import path, environ from os import path
import sys import sys
from mastodon import Mastodon from mastodon import Mastodon
import fediplay.env as env
from fediplay.mastodon import stream, register, login from fediplay.mastodon import stream, register, login
def main(): def main():
'''Run fediplay command-line interface.''' '''Run fediplay command-line interface.'''
api_base_url = environ.get('FEDIPLAY_API_BASE_URL') api_base_url = env.api_base_url()
if not api_base_url: if not api_base_url:
print('FEDIPLAY_API_BASE_URL environment variable not set') print('FEDIPLAY_API_BASE_URL environment variable not set')
sys.exit(1) sys.exit(1)

View file

@ -1,12 +1,13 @@
'''The play queue.''' '''The play queue.'''
from os import environ
import shlex import shlex
from subprocess import run from subprocess import run
from threading import Thread, Lock from threading import Thread, Lock
from youtube_dl import YoutubeDL from youtube_dl import YoutubeDL
import fediplay.env as env
class Queue(object): class Queue(object):
'''The play queue.''' '''The play queue.'''
@ -64,7 +65,7 @@ class Getter(object):
options = { options = {
'format': 'mp3/mp4', 'format': 'mp3/mp4',
'nocheckcertificate': 'FEDIPLAY_NO_CHECK_CERTIFICATE' in environ, 'nocheckcertificate': env.no_check_certificate(),
'progress_hooks': [self._progress_hook] 'progress_hooks': [self._progress_hook]
} }
with YoutubeDL(options) as downloader: with YoutubeDL(options) as downloader:
@ -76,8 +77,5 @@ def build_play_command(filename):
'''Builds a play command for the given filename.''' '''Builds a play command for the given filename.'''
escaped_filename = shlex.quote(filename) escaped_filename = shlex.quote(filename)
template = environ.get( template = env.play_command()
'FEDIPLAY_PLAY_COMMAND',
'ffplay -v 0 -nostats -hide_banner -autoexit -nodisp {filename}'
)
return template.format(filename=escaped_filename) return template.format(filename=escaped_filename)