Skip to content

Replace io.open using pathlib #3839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions nikola/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
All filters defined in this module are registered in Nikola.__init__.
"""

import io
import json
import os
import re
from pathlib import Path
import shutil
import shlex
import subprocess
Expand Down Expand Up @@ -92,11 +92,10 @@ def apply_to_text_file(f):

@wraps(f)
def f_in_file(fname, *args, **kwargs):
with io.open(fname, 'r', encoding='utf-8-sig') as inf:
data = inf.read()
fpath = Path(fname)
data = fpath.read_text(encoding='utf-8-sig')
data = f(data, *args, **kwargs)
with io.open(fname, 'w+', encoding='utf-8') as outf:
outf.write(data)
fpath.write_text(data, encoding='utf-8')

return f_in_file

Expand Down Expand Up @@ -407,9 +406,8 @@ def php_template_injection(data):
r'<\!-- __NIKOLA_PHP_TEMPLATE_INJECTION source\:(.*) checksum\:(.*)__ -->', data
)
if template:
source = template.group(1)
with io.open(source, 'r', encoding='utf-8-sig') as in_file:
phpdata = in_file.read()
source = Path(template.group(1))
phpdata = source.read_text(encoding='utf-8-sig')
_META_SEPARATOR = (
'(' + os.linesep * 2 + '|' + ('\n' * 2) + '|' + ('\r\n' * 2) + ')'
)
Expand Down Expand Up @@ -498,8 +496,7 @@ def add_header_permalinks(fname, xpath_list=None, file_blacklist=None):
file_blacklist = file_blacklist or []
if fname in file_blacklist:
return
with io.open(fname, 'r', encoding='utf-8-sig') as inf:
data = inf.read()
data = Path(fname).read_text(encoding='utf-8-sig')
doc = lxml.html.document_fromstring(data)
# Get language for slugify
try:
Expand Down Expand Up @@ -538,8 +535,10 @@ def add_header_permalinks(fname, xpath_list=None, file_blacklist=None):
)
node.append(new_node)

with io.open(fname, 'w', encoding='utf-8') as outf:
outf.write('<!DOCTYPE html>\n' + lxml.html.tostring(doc, encoding='unicode'))
Path(fname).write_text(
'<!DOCTYPE html>\n' + lxml.html.tostring(doc, encoding='unicode'),
encoding='utf-8',
)


@_ConfigurableFilter(top_classes='DEDUPLICATE_IDS_TOP_CLASSES')
Expand Down
15 changes: 7 additions & 8 deletions nikola/nikola.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"""The main Nikola site object."""

import datetime
import io
import json
import functools
import logging
import operator
import os
import pathlib
from pathlib import Path
import sys
import typing
from typing import Any, Callable, Dict, Iterable, List, Optional, Set
Expand Down Expand Up @@ -1003,7 +1003,7 @@ def _filter_duplicate_plugins(self, plugin_list: Iterable[PluginCandidate]):
def plugin_position_in_places(plugin: PluginInfo):
# plugin here is a tuple:
# (path to the .plugin file, path to plugin module w/o .py, plugin metadata)
place: pathlib.Path
place: Path
for i, place in enumerate(self._plugin_places):
try:
# Path.is_relative_to backport
Expand Down Expand Up @@ -1036,7 +1036,7 @@ def init_plugins(self, commands_only=False, load_all=False) -> None:
os.path.expanduser(os.path.join('~', '.nikola', 'plugins')),
os.path.join(os.getcwd(), 'plugins'),
] + [path for path in extra_plugins_dirs if path]
self._plugin_places = [pathlib.Path(p) for p in self._plugin_places]
self._plugin_places = [Path(p) for p in self._plugin_places]

self.plugin_manager = PluginManager(plugin_places=self._plugin_places)

Expand Down Expand Up @@ -2571,11 +2571,10 @@ def atom_post_text(post, text):

dst_dir = os.path.dirname(output_path)
utils.makedirs(dst_dir)
with io.open(output_path, "w+", encoding="utf-8") as atom_file:
data = lxml.etree.tostring(feed_root.getroottree(), encoding="UTF-8", pretty_print=True, xml_declaration=True)
if isinstance(data, bytes):
data = data.decode('utf-8')
atom_file.write(data)
data = lxml.etree.tostring(feed_root.getroottree(), encoding="UTF-8", pretty_print=True, xml_declaration=True)
if isinstance(data, bytes):
data = data.decode('utf-8')
Path(output_path).write_text(data, encoding="utf-8")

def generic_index_renderer(self, lang, posts, indexes_title, template_name, context_source, kw, basename, page_link, page_path, additional_dependencies=None):
"""Create an index page.
Expand Down
Loading