parent
4ed043822e
commit
60d458f3cc
|
@ -233,7 +233,8 @@ class ExtensionContainer(object):
|
|||
if not filename or not os.path.exists(filename):
|
||||
return {}
|
||||
|
||||
extension_py = open(filename).read()
|
||||
encoding = util.guess_encoding(filename)
|
||||
extension_py = open(filename, "r", encoding=encoding).read()
|
||||
metadata = dict(re.findall("__([a-z_]+)__ = '([^']+)'", extension_py))
|
||||
|
||||
# Support for using gpodder.gettext() as _ to localize text
|
||||
|
|
|
@ -1887,3 +1887,34 @@ def delete_empty_folders(top):
|
|||
if not os.listdir(dirname):
|
||||
os.rmdir(dirname)
|
||||
|
||||
|
||||
def guess_encoding(filename):
|
||||
"""
|
||||
read filename encoding as defined in PEP 263
|
||||
- BOM marker => utf-8
|
||||
- coding: xxx comment in first 2 lines
|
||||
- else return None
|
||||
>>> guess_encoding("not.there")
|
||||
>>> guess_encoding("setup.py")
|
||||
>>> guess_encoding("share/gpodder/extensions/mpris-listener.py")
|
||||
'utf-8'
|
||||
"""
|
||||
def re_encoding(line):
|
||||
m = re.match(b"""^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)""", line)
|
||||
if m:
|
||||
return m.group(1).decode()
|
||||
else:
|
||||
return None
|
||||
|
||||
if not filename or not os.path.exists(filename):
|
||||
return None
|
||||
|
||||
with open(filename, "rb") as f:
|
||||
fst = f.readline()
|
||||
if fst[:3] == b"\xef\xbb\xbf":
|
||||
return "utf-8"
|
||||
encoding = re_encoding(fst)
|
||||
if not encoding:
|
||||
snd = f.readline()
|
||||
encoding = re_encoding(snd)
|
||||
return encoding
|
||||
|
|
Loading…
Reference in New Issue