Skip to content

Commit fd3fc3f

Browse files
committed
Allow overriding any option from env
- Allow any configuration option to be overriden via the environment - Merge env in a single place at config.py instead of multiple places in multiple files
1 parent 03b4d04 commit fd3fc3f

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

ydl_server/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@
1616
'ignoreerrors': True
1717
},
1818
}
19+
20+
import os
21+
from collections import ChainMap
22+
app_config = ChainMap(os.environ, app_defaults)

ydl_server/logdb.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import os
21
import sqlite3
32
import re
43
from queue import Queue
54
from threading import Thread
6-
from ydl_server.config import app_defaults
5+
from ydl_server.config import app_config
76

87
STATUS_NAME =["Running",
98
"Completed",
@@ -55,7 +54,7 @@ class JobsDB:
5554

5655
@staticmethod
5756
def check_db_latest():
58-
conn = sqlite3.connect("file://%s" % app_defaults['YDL_DB_PATH'], uri=True)
57+
conn = sqlite3.connect("file://%s" % app_config['YDL_DB_PATH'], uri=True)
5958
cursor = conn.cursor()
6059
cursor.execute("PRAGMA table_info('jobs')")
6160
columns = [row[1] for row in cursor.fetchall()]
@@ -67,7 +66,7 @@ def check_db_latest():
6766

6867
@staticmethod
6968
def init_db():
70-
conn = sqlite3.connect("file://%s" % app_defaults['YDL_DB_PATH'], uri=True)
69+
conn = sqlite3.connect("file://%s" % app_config['YDL_DB_PATH'], uri=True)
7170
cursor = conn.cursor()
7271
cursor.execute("CREATE TABLE if not exists jobs \
7372
(id INTEGER PRIMARY KEY AUTOINCREMENT, \
@@ -82,7 +81,7 @@ def init_db():
8281
conn.close()
8382

8483
def __init__(self, readonly=True):
85-
self.conn = sqlite3.connect("file://%s%s" % (app_defaults['YDL_DB_PATH'],
84+
self.conn = sqlite3.connect("file://%s%s" % (app_config['YDL_DB_PATH'],
8685
"?mode=ro" if readonly else ""),
8786
uri=True)
8887

ydl_server/ydlhandler.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
from queue import Queue
32
from threading import Thread
43
import subprocess
@@ -12,7 +11,7 @@
1211

1312
from ydl_server.logdb import JobsDB, Job, Actions, JobType
1413
from ydl_server import jobshandler
15-
from ydl_server.config import app_defaults
14+
from ydl_server.config import app_config
1615

1716
queue = Queue()
1817
thread = None
@@ -59,8 +58,8 @@ def reload_youtube_dl():
5958
importlib.reload(sys.modules[module])
6059

6160
def update():
62-
if os.environ.get('YDL_PYTHONPATH'):
63-
command = ["pip", "install", "--no-cache-dir", "-t", os.environ.get('YDL_PYTHONPATH'), "--upgrade", "youtube-dlc"]
61+
if app_config['YDL_PYTHONPATH']:
62+
command = ["pip", "install", "--no-cache-dir", "-t", app_config['YDL_PYTHONPATH'], "--upgrade", "youtube-dlc"]
6463
else:
6564
command = ["pip", "install", "--no-cache-dir", "--upgrade", "youtube-dlc"]
6665
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
@@ -84,7 +83,7 @@ def get_ydl_options(request_options):
8483
elif requested_format in ['mp4', 'flv', 'webm', 'ogg', 'mkv', 'avi']:
8584
request_vars['YDL_RECODE_VIDEO_FORMAT'] = requested_format
8685

87-
ydl_vars = ChainMap(request_vars, os.environ, app_defaults)
86+
ydl_vars = ChainMap(request_vars, app_config)
8887

8988
postprocessors = []
9089

@@ -138,7 +137,7 @@ def fetch_metadata(url):
138137
def download(url, request_options, output, job_id):
139138
with youtube_dlc.YoutubeDL(get_ydl_options(request_options)) as ydl:
140139
ydl.params['extract_flat'] = 'in_playlist'
141-
ydl_opts = ChainMap(os.environ, app_defaults)
140+
ydl_opts = app_config
142141
info = ydl.extract_info(url, download=False)
143142
if 'title' in info and info['title']:
144143
jobshandler.put((Actions.SET_NAME, (job_id, info['title'])))

youtube-dl-server.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import unicode_literals
22
import json
3-
import os
4-
from collections import ChainMap
53
from itertools import chain
64
from operator import itemgetter
75
from queue import Queue
@@ -10,7 +8,7 @@
108
from pathlib import Path
119
from ydl_server.logdb import JobsDB, Job, Actions, JobType
1210
from ydl_server import jobshandler, ydlhandler
13-
from ydl_server.config import app_defaults
11+
from ydl_server.config import app_config
1412

1513
app = Bottle()
1614

@@ -125,11 +123,10 @@ def ydl_update():
125123

126124
ydlhandler.resume_pending()
127125

128-
app_vars = ChainMap(os.environ, app_defaults)
129126

130-
app.run(host=app_vars['YDL_SERVER_HOST'],
131-
port=app_vars['YDL_SERVER_PORT'],
132-
debug=app_vars['YDL_DEBUG'])
127+
app.run(host=app_config['YDL_SERVER_HOST'],
128+
port=app_config['YDL_SERVER_PORT'],
129+
debug=app_config['YDL_DEBUG'])
133130
ydlhandler.finish()
134131
jobshandler.finish()
135132
ydlhandler.join()

0 commit comments

Comments
 (0)