Skip to content

Commit

Permalink
[compat] Use compat_open()
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkf committed Jul 25, 2023
1 parent aac3315 commit a25e9f3
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 55 deletions.
4 changes: 3 additions & 1 deletion devscripts/make_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import sys
import re

from youtube_dl.compat import compat_open as open

README_FILE = 'README.md'
helptext = sys.stdin.read()

Expand All @@ -20,7 +22,7 @@
options = re.sub(r'(?m)^ (\w.+)$', r'## \1', options)
options = '# OPTIONS\n' + options + '\n'

with io.open(README_FILE, 'w', encoding='utf-8') as f:
with open(README_FILE, 'w', encoding='utf-8') as f:
f.write(header)
f.write(options)
f.write(footer)
6 changes: 3 additions & 3 deletions test/helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import unicode_literals

import errno
import io
import hashlib
import json
import os.path
Expand All @@ -14,6 +13,7 @@
import youtube_dl.extractor
from youtube_dl import YoutubeDL
from youtube_dl.compat import (
compat_open as open,
compat_os_name,
compat_str,
)
Expand All @@ -29,10 +29,10 @@ def get_params(override=None):
"parameters.json")
LOCAL_PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"local_parameters.json")
with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
with open(PARAMETERS_FILE, encoding='utf-8') as pf:
parameters = json.load(pf)
if os.path.exists(LOCAL_PARAMETERS_FILE):
with io.open(LOCAL_PARAMETERS_FILE, encoding='utf-8') as pf:
with open(LOCAL_PARAMETERS_FILE, encoding='utf-8') as pf:
parameters.update(json.load(pf))
if override:
parameters.update(override)
Expand Down
18 changes: 9 additions & 9 deletions test/test_InfoExtractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import unicode_literals

# Allow direct execution
import io
import os
import sys
import unittest
Expand All @@ -21,6 +20,7 @@
from youtube_dl.compat import (
compat_etree_fromstring,
compat_http_server,
compat_open as open,
)
from youtube_dl.extractor.common import InfoExtractor
from youtube_dl.extractor import (
Expand Down Expand Up @@ -902,8 +902,8 @@ def test_parse_m3u8_formats(self):
]

for m3u8_file, m3u8_url, expected_formats in _TEST_CASES:
with io.open('./test/testdata/m3u8/%s.m3u8' % m3u8_file,
mode='r', encoding='utf-8') as f:
with open('./test/testdata/m3u8/%s.m3u8' % m3u8_file,
mode='r', encoding='utf-8') as f:
formats = self.ie._parse_m3u8_formats(
f.read(), m3u8_url, ext='mp4')
self.ie._sort_formats(formats)
Expand Down Expand Up @@ -1127,8 +1127,8 @@ def test_parse_mpd_formats(self):
]

for mpd_file, mpd_url, mpd_base_url, expected_formats in _TEST_CASES:
with io.open('./test/testdata/mpd/%s.mpd' % mpd_file,
mode='r', encoding='utf-8') as f:
with open('./test/testdata/mpd/%s.mpd' % mpd_file,
mode='r', encoding='utf-8') as f:
formats = self.ie._parse_mpd_formats(
compat_etree_fromstring(f.read().encode('utf-8')),
mpd_base_url=mpd_base_url, mpd_url=mpd_url)
Expand All @@ -1154,8 +1154,8 @@ def test_parse_f4m_formats(self):
]

for f4m_file, f4m_url, expected_formats in _TEST_CASES:
with io.open('./test/testdata/f4m/%s.f4m' % f4m_file,
mode='r', encoding='utf-8') as f:
with open('./test/testdata/f4m/%s.f4m' % f4m_file,
mode='r', encoding='utf-8') as f:
formats = self.ie._parse_f4m_formats(
compat_etree_fromstring(f.read().encode('utf-8')),
f4m_url, None)
Expand Down Expand Up @@ -1202,8 +1202,8 @@ def test_parse_xspf(self):
]

for xspf_file, xspf_url, expected_entries in _TEST_CASES:
with io.open('./test/testdata/xspf/%s.xspf' % xspf_file,
mode='r', encoding='utf-8') as f:
with open('./test/testdata/xspf/%s.xspf' % xspf_file,
mode='r', encoding='utf-8') as f:
entries = self.ie._parse_xspf(
compat_etree_fromstring(f.read().encode('utf-8')),
xspf_file, xspf_url=xspf_url, xspf_base_url=xspf_url)
Expand Down
7 changes: 4 additions & 3 deletions test/test_YoutubeDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
compat_http_cookiejar_Cookie,
compat_http_cookies_SimpleCookie,
compat_kwargs,
compat_open as open,
compat_str,
compat_urllib_error,
)
Expand Down Expand Up @@ -701,12 +702,12 @@ def test_postprocessors(self):

class SimplePP(PostProcessor):
def run(self, info):
with open(audiofile, 'wt') as f:
with open(audiofile, 'w') as f:
f.write('EXAMPLE')
return [info['filepath']], info

def run_pp(params, PP):
with open(filename, 'wt') as f:
with open(filename, 'w') as f:
f.write('EXAMPLE')
ydl = YoutubeDL(params)
ydl.add_post_processor(PP())
Expand All @@ -725,7 +726,7 @@ def run_pp(params, PP):

class ModifierPP(PostProcessor):
def run(self, info):
with open(info['filepath'], 'wt') as f:
with open(info['filepath'], 'w') as f:
f.write('MODIFIED')
return [], info

Expand Down
6 changes: 3 additions & 3 deletions test/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@


import hashlib
import io
import json
import socket

import youtube_dl.YoutubeDL
from youtube_dl.compat import (
compat_http_client,
compat_urllib_error,
compat_HTTPError,
compat_open as open,
compat_urllib_error,
)
from youtube_dl.utils import (
DownloadError,
Expand Down Expand Up @@ -245,7 +245,7 @@ def try_rm_tcs_files(tcs=None):
self.assertTrue(
os.path.exists(info_json_fn),
'Missing info file %s' % info_json_fn)
with io.open(info_json_fn, encoding='utf-8') as infof:
with open(info_json_fn, encoding='utf-8') as infof:
info_dict = json.load(infof)
expect_info_dict(self, info_dict, tc.get('info_dict', {}))
finally:
Expand Down
10 changes: 6 additions & 4 deletions test/test_swfinterp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import os
import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

dirn = os.path.dirname

sys.path.insert(0, dirn(dirn(os.path.abspath(__file__))))

import errno
import io
import json
import re
import subprocess

from youtube_dl.swfinterp import SWFInterpreter
from youtube_dl.compat import compat_open as open


TEST_DIR = os.path.join(
Expand Down Expand Up @@ -43,15 +45,15 @@ def test_func(self):
'-static-link-runtime-shared-libraries', as_file])
except OSError as ose:
if ose.errno == errno.ENOENT:
print('mxmlc not found! Skipping test.')
self.skipTest('mxmlc not found!')
return
raise

with open(swf_file, 'rb') as swf_f:
swf_content = swf_f.read()
swfi = SWFInterpreter(swf_content)

with io.open(as_file, 'r', encoding='utf-8') as as_f:
with open(as_file, 'r', encoding='utf-8') as as_f:
as_content = as_f.read()

def _find_spec(key):
Expand Down
12 changes: 7 additions & 5 deletions test/test_unicode_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

# Allow direct execution
import os
import re
import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import io
import re
dirn = os.path.dirname

rootDir = dirn(dirn(os.path.abspath(__file__)))

rootDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, rootDir)

IGNORED_FILES = [
'setup.py', # http://bugs.python.org/issue13943
Expand All @@ -24,6 +25,7 @@
]

from test.helper import assertRegexpMatches
from youtube_dl.compat import compat_open as open


class TestUnicodeLiterals(unittest.TestCase):
Expand All @@ -41,7 +43,7 @@ def test_all_files(self):
continue

fn = os.path.join(dirpath, basename)
with io.open(fn, encoding='utf-8') as inf:
with open(fn, encoding='utf-8') as inf:
code = inf.read()

if "'" not in code and '"' not in code:
Expand Down
5 changes: 2 additions & 3 deletions test/test_write_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
from test.helper import get_params, try_rm


import io

import xml.etree.ElementTree

import youtube_dl.YoutubeDL
import youtube_dl.extractor
from youtube_dl.compat import compat_open as open


class YoutubeDL(youtube_dl.YoutubeDL):
Expand Down Expand Up @@ -51,7 +50,7 @@ def test_info_json(self):
ydl.download([TEST_ID])
self.assertTrue(os.path.exists(ANNOTATIONS_FILE))
annoxml = None
with io.open(ANNOTATIONS_FILE, 'r', encoding='utf-8') as annof:
with open(ANNOTATIONS_FILE, 'r', encoding='utf-8') as annof:
annoxml = xml.etree.ElementTree.parse(annof)
self.assertTrue(annoxml is not None, 'Failed to parse annotations XML')
root = annoxml.getroot()
Expand Down
9 changes: 6 additions & 3 deletions test/test_youtube_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import io
import re
import string

from youtube_dl.compat import compat_str, compat_urlretrieve
from youtube_dl.compat import (
compat_open as open,
compat_str,
compat_urlretrieve,
)

from test.helper import FakeYDL
from youtube_dl.extractor import YoutubeIE
Expand Down Expand Up @@ -208,7 +211,7 @@ def test_func(self):

if not os.path.exists(fn):
compat_urlretrieve(url, fn)
with io.open(fn, encoding='utf-8') as testf:
with open(fn, encoding='utf-8') as testf:
jscode = testf.read()
self.assertEqual(sig_func(jscode, sig_input), expected_sig)

Expand Down
19 changes: 7 additions & 12 deletions youtube_dl/YoutubeDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
from __future__ import absolute_import, unicode_literals

import collections
import contextlib
import copy
import datetime
import errno
import fileinput
import io
import itertools
import json
Expand Down Expand Up @@ -45,6 +43,7 @@
compat_kwargs,
compat_map as map,
compat_numeric_types,
compat_open as open,
compat_os_name,
compat_str,
compat_tokenize_tokenize,
Expand Down Expand Up @@ -1977,7 +1976,7 @@ def ensure_dir_exists(path):
else:
try:
self.to_screen('[info] Writing video description to: ' + descfn)
with io.open(encodeFilename(descfn), 'w', encoding='utf-8') as descfile:
with open(encodeFilename(descfn), 'w', encoding='utf-8') as descfile:
descfile.write(info_dict['description'])
except (OSError, IOError):
self.report_error('Cannot write description file ' + descfn)
Expand All @@ -1992,7 +1991,7 @@ def ensure_dir_exists(path):
else:
try:
self.to_screen('[info] Writing video annotations to: ' + annofn)
with io.open(encodeFilename(annofn), 'w', encoding='utf-8') as annofile:
with open(encodeFilename(annofn), 'w', encoding='utf-8') as annofile:
annofile.write(info_dict['annotations'])
except (KeyError, TypeError):
self.report_warning('There are no annotations to write.')
Expand All @@ -2019,7 +2018,7 @@ def ensure_dir_exists(path):
try:
# Use newline='' to prevent conversion of newline characters
# See https://github.com/ytdl-org/youtube-dl/issues/10268
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8', newline='') as subfile:
with open(encodeFilename(sub_filename), 'w', encoding='utf-8', newline='') as subfile:
subfile.write(sub_info['data'])
except (OSError, IOError):
self.report_error('Cannot write subtitles file ' + sub_filename)
Expand All @@ -2028,7 +2027,7 @@ def ensure_dir_exists(path):
try:
sub_data = ie._request_webpage(
sub_info['url'], info_dict['id'], note=False).read()
with io.open(encodeFilename(sub_filename), 'wb') as subfile:
with open(encodeFilename(sub_filename), 'wb') as subfile:
subfile.write(sub_data)
except (ExtractorError, IOError, OSError, ValueError) as err:
self.report_warning('Unable to download subtitle for "%s": %s' %
Expand Down Expand Up @@ -2232,12 +2231,8 @@ def download(self, url_list):
return self._download_retcode

def download_with_info_file(self, info_filename):
with contextlib.closing(fileinput.FileInput(
[info_filename], mode='r',
openhook=fileinput.hook_encoded('utf-8'))) as f:
# FileInput doesn't have a read method, we can't call json.load
# TODO: let's use io.open(), then
info = self.filter_requested_info(json.loads('\n'.join(f)))
with open(info_filename, encoding='utf-8') as f:
info = self.filter_requested_info(json.load(f))
try:
self.process_ie_result(info, download=True)
except DownloadError:
Expand Down
8 changes: 5 additions & 3 deletions youtube_dl/cache.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import unicode_literals

import errno
import io
import json
import os
import re
import shutil
import traceback

from .compat import compat_getenv
from .compat import (
compat_getenv,
compat_open as open,
)
from .utils import (
error_to_compat_str,
expand_path,
Expand Down Expand Up @@ -83,7 +85,7 @@ def load(self, section, key, dtype='json', default=None, min_ver=None):
cache_fn = self._get_cache_fn(section, key, dtype)
try:
try:
with io.open(cache_fn, 'r', encoding='utf-8') as cachef:
with open(cache_fn, 'r', encoding='utf-8') as cachef:
return self._validate(json.load(cachef), min_ver)
except ValueError:
try:
Expand Down
1 change: 1 addition & 0 deletions youtube_dl/extractor/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
compat_integer_types,
compat_http_client,
compat_map as map,
compat_open as open,
compat_os_name,
compat_str,
compat_urllib_error,
Expand Down
Loading

0 comments on commit a25e9f3

Please sign in to comment.