open possibly-utf8 files with forced utf8

This commit is contained in:
jesopo 2020-02-24 15:24:39 +00:00
parent fc32a907a5
commit 175c0a285c
5 changed files with 12 additions and 7 deletions

View file

@ -28,7 +28,7 @@ class Module(ModuleManager.BaseModule):
channel.__log_file.write("%s\n" % line) channel.__log_file.write("%s\n" % line)
def _write(self, channel, filename, key, line): def _write(self, channel, filename, key, line):
if not hasattr(channel, "__log_file"): if not hasattr(channel, "__log_file"):
channel.__log_file = open(filename, "a") channel.__log_file = utils.io.open(filename, "a")
channel.__log_rsa = None channel.__log_rsa = None
channel.__log_aes = None channel.__log_aes = None

View file

@ -1,4 +1,5 @@
import collections, configparser, os, typing import collections, configparser, os, typing
from src import utils
class Config(object): class Config(object):
def __init__(self, name: str, location: str): def __init__(self, name: str, location: str):
@ -11,7 +12,7 @@ class Config(object):
def load(self): def load(self):
if os.path.isfile(self.location): if os.path.isfile(self.location):
with open(self.location) as config_file: with utils.io.open(self.location, "r") as config_file:
parser = self._parser() parser = self._parser()
parser.read_string(config_file.read()) parser.read_string(config_file.read())
self._config.clear() self._config.clear()
@ -20,7 +21,7 @@ class Config(object):
self._config[k] = v self._config[k] = v
def save(self): def save(self):
with open(self.location, "w") as config_file: with utils.io.open(self.location, "w") as config_file:
parser = self._parser() parser = self._parser()
parser[self._name] = self._config.copy() parser[self._name] = self._config.copy()
parser.write(config_file) parser.write(config_file)

View file

@ -1,6 +1,6 @@
import contextlib, enum, ipaddress, multiprocessing, os.path, queue, signal import contextlib, enum, ipaddress, multiprocessing, os.path, queue, signal
import threading, typing import threading, typing
from . import cli, consts, datetime, decorators, irc, http, parse, security from . import cli, consts, datetime, decorators, io, irc, http, parse, security
from .decorators import export, hook, kwarg, spec from .decorators import export, hook, kwarg, spec
from .settings import (BoolSetting, FunctionSetting, IntRangeSetting, from .settings import (BoolSetting, FunctionSetting, IntRangeSetting,

4
src/utils/io.py Normal file
View file

@ -0,0 +1,4 @@
import io
def open(path: str, mode: str):
return io.open(path, mode=mode, encoding="utf8")

View file

@ -1,5 +1,5 @@
import decimal, io, re, typing import decimal, re, typing
from src.utils import datetime, errors from src.utils import datetime, errors, io
from .spec import * from .spec import *
from .time import duration from .time import duration
@ -10,7 +10,7 @@ COMMENT_TYPES = ["#", "//"]
def hashflags(filename: str def hashflags(filename: str
) -> typing.List[typing.Tuple[str, typing.Optional[str]]]: ) -> typing.List[typing.Tuple[str, typing.Optional[str]]]:
hashflags = [] # type: typing.List[typing.Tuple[str, typing.Optional[str]]] hashflags = [] # type: typing.List[typing.Tuple[str, typing.Optional[str]]]
with io.open(filename, mode="r", encoding="utf8") as f: with io.open(filename, "r") as f:
for line in f: for line in f:
line = line.strip("\n") line = line.strip("\n")
found = False found = False