mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-19 23:20:57 +01:00
Ensure files written with AtomicFile are read in UTF8
With some locale configurations (not that uncommon on CentOS), open() may default to non-UTF8 encodings (eg. ANSI_X3.4-1968). This is usually not an issue, because we use open() both for writing and reading. However, AtomicFile implicitly enforces UTF8; which needs to be mirrored when reading.
This commit is contained in:
parent
b1cfb87e71
commit
a6aa5530dd
@ -209,7 +209,7 @@ class SqliteKarmaDB(object):
|
|||||||
|
|
||||||
def load(self, channel, filename):
|
def load(self, channel, filename):
|
||||||
filename = conf.supybot.directories.data.dirize(filename)
|
filename = conf.supybot.directories.data.dirize(filename)
|
||||||
fd = open(filename)
|
fd = open(filename, encoding='utf8')
|
||||||
reader = csv.reader(fd)
|
reader = csv.reader(fd)
|
||||||
db = self._getDb(channel)
|
db = self._getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
|
14
src/dbi.py
14
src/dbi.py
@ -150,7 +150,7 @@ class FlatfileMapping(MappingInterface):
|
|||||||
def __init__(self, filename, maxSize=10**6):
|
def __init__(self, filename, maxSize=10**6):
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
try:
|
try:
|
||||||
fd = open(self.filename)
|
fd = open(self.filename, encoding='utf8')
|
||||||
strId = fd.readline().rstrip()
|
strId = fd.readline().rstrip()
|
||||||
self.maxSize = len(strId)
|
self.maxSize = len(strId)
|
||||||
try:
|
try:
|
||||||
@ -175,7 +175,7 @@ class FlatfileMapping(MappingInterface):
|
|||||||
def _incrementCurrentId(self, fd=None):
|
def _incrementCurrentId(self, fd=None):
|
||||||
fdWasNone = fd is None
|
fdWasNone = fd is None
|
||||||
if fdWasNone:
|
if fdWasNone:
|
||||||
fd = open(self.filename, 'a')
|
fd = open(self.filename, 'a', encoding='utf8')
|
||||||
fd.seek(0)
|
fd.seek(0)
|
||||||
self.currentId += 1
|
self.currentId += 1
|
||||||
fd.write(self._canonicalId(self.currentId))
|
fd.write(self._canonicalId(self.currentId))
|
||||||
@ -193,7 +193,7 @@ class FlatfileMapping(MappingInterface):
|
|||||||
|
|
||||||
def add(self, s):
|
def add(self, s):
|
||||||
line = self._joinLine(self.currentId, s)
|
line = self._joinLine(self.currentId, s)
|
||||||
fd = open(self.filename, 'r+')
|
fd = open(self.filename, 'r+', encoding='utf8')
|
||||||
try:
|
try:
|
||||||
fd.seek(0, 2) # End.
|
fd.seek(0, 2) # End.
|
||||||
fd.write(line)
|
fd.write(line)
|
||||||
@ -205,7 +205,7 @@ class FlatfileMapping(MappingInterface):
|
|||||||
def get(self, id):
|
def get(self, id):
|
||||||
strId = self._canonicalId(id)
|
strId = self._canonicalId(id)
|
||||||
try:
|
try:
|
||||||
fd = open(self.filename)
|
fd = open(self.filename, encoding='utf8')
|
||||||
fd.readline() # First line, nextId.
|
fd.readline() # First line, nextId.
|
||||||
for line in fd:
|
for line in fd:
|
||||||
(lineId, s) = self._splitLine(line)
|
(lineId, s) = self._splitLine(line)
|
||||||
@ -221,7 +221,7 @@ class FlatfileMapping(MappingInterface):
|
|||||||
def set(self, id, s):
|
def set(self, id, s):
|
||||||
strLine = self._joinLine(id, s)
|
strLine = self._joinLine(id, s)
|
||||||
try:
|
try:
|
||||||
fd = open(self.filename, 'r+')
|
fd = open(self.filename, 'r+', encoding='utf8')
|
||||||
self.remove(id, fd)
|
self.remove(id, fd)
|
||||||
fd.seek(0, 2) # End.
|
fd.seek(0, 2) # End.
|
||||||
fd.write(strLine)
|
fd.write(strLine)
|
||||||
@ -233,7 +233,7 @@ class FlatfileMapping(MappingInterface):
|
|||||||
strId = self._canonicalId(id)
|
strId = self._canonicalId(id)
|
||||||
try:
|
try:
|
||||||
if fdWasNone:
|
if fdWasNone:
|
||||||
fd = open(self.filename, 'r+')
|
fd = open(self.filename, 'r+', encoding='utf8')
|
||||||
fd.seek(0)
|
fd.seek(0)
|
||||||
fd.readline() # First line, nextId
|
fd.readline() # First line, nextId
|
||||||
pos = fd.tell()
|
pos = fd.tell()
|
||||||
@ -262,7 +262,7 @@ class FlatfileMapping(MappingInterface):
|
|||||||
fd.close()
|
fd.close()
|
||||||
|
|
||||||
def vacuum(self):
|
def vacuum(self):
|
||||||
infd = open(self.filename)
|
infd = open(self.filename, encoding='utf8')
|
||||||
outfd = utils.file.AtomicFile(self.filename,makeBackupIfSmaller=False)
|
outfd = utils.file.AtomicFile(self.filename,makeBackupIfSmaller=False)
|
||||||
outfd.write(infd.readline()) # First line, nextId.
|
outfd.write(infd.readline()) # First line, nextId.
|
||||||
for line in infd:
|
for line in infd:
|
||||||
|
@ -84,7 +84,7 @@ def open_registry(filename, clear=False):
|
|||||||
global _lastModified
|
global _lastModified
|
||||||
if clear:
|
if clear:
|
||||||
_cache.clear()
|
_cache.clear()
|
||||||
_fd = open(filename)
|
_fd = open(filename, encoding='utf8')
|
||||||
fd = utils.file.nonCommentNonEmptyLines(_fd)
|
fd = utils.file.nonCommentNonEmptyLines(_fd)
|
||||||
acc = ''
|
acc = ''
|
||||||
slashEnd = re.compile(r'\\*$')
|
slashEnd = re.compile(r'\\*$')
|
||||||
|
@ -70,7 +70,7 @@ class Reader(object):
|
|||||||
return s.lower()
|
return s.lower()
|
||||||
|
|
||||||
def readFile(self, filename):
|
def readFile(self, filename):
|
||||||
self.read(open(filename))
|
self.read(open(filename, encoding='utf8'))
|
||||||
|
|
||||||
def read(self, fd):
|
def read(self, fd):
|
||||||
lineno = 0
|
lineno = 0
|
||||||
|
@ -130,7 +130,9 @@ def chunks(fd, size):
|
|||||||
|
|
||||||
class AtomicFile(object):
|
class AtomicFile(object):
|
||||||
"""Used for files that need to be atomically written -- i.e., if there's a
|
"""Used for files that need to be atomically written -- i.e., if there's a
|
||||||
failure, the original file remains, unmodified. mode must be 'w' or 'wb'"""
|
failure, the original file remains, unmodified. mode must be 'w' or 'wb'.
|
||||||
|
If ``encoding`` is None (or not provided), files are open in `utf8` regardless
|
||||||
|
of the system locale."""
|
||||||
class default(object): # Holder for values.
|
class default(object): # Holder for values.
|
||||||
# Callables?
|
# Callables?
|
||||||
tmpDir = None
|
tmpDir = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user