|
| 1 | +import codecs |
| 2 | + |
1 | 3 | from .section import ScriptInfoSection, FieldSection, StylesSection, EventsSection, LineSection |
2 | 4 | from ._util import CaseInsensitiveOrderedDict |
3 | 5 |
|
@@ -39,6 +41,7 @@ class Document(object): |
39 | 41 | STYLE_ASS_HEADER = "V4+ Styles" |
40 | 42 | EVENTS_HEADER = "Events" |
41 | 43 | AEGISUB_PROJECT_HEADER = "Aegisub Project Garbage" |
| 44 | + PREFERRED_ENCODING = codecs.lookup('utf_8_sig') |
42 | 45 |
|
43 | 46 | SECTIONS = CaseInsensitiveOrderedDict({ |
44 | 47 | SCRIPT_INFO_HEADER: ScriptInfoSection, |
@@ -84,7 +87,7 @@ def parse_file(cls, f): |
84 | 87 | bom_seqeunces = ("\xef\xbb\xbf", "\xff\xfe", "\ufeff") |
85 | 88 | if any(line.startswith(seq) for seq in bom_seqeunces): |
86 | 89 | raise ValueError("BOM detected. Please open the file with the proper encoding," |
87 | | - " usually 'utf_8_sig'") |
| 90 | + " usually '%s'" % cls.PREFERRED_ENCODING.name) |
88 | 91 |
|
89 | 92 | line = line.strip() |
90 | 93 | if not line or line.startswith(';'): |
@@ -127,14 +130,22 @@ def parse_string(cls, string): |
127 | 130 | """ |
128 | 131 | return cls.parse_file(string.splitlines()) |
129 | 132 |
|
| 133 | + @classmethod |
| 134 | + def is_preferred_encoding(cls, encoding): |
| 135 | + try: |
| 136 | + enc_codec = codecs.lookup(encoding) |
| 137 | + except (LookupError, TypeError): |
| 138 | + return False |
| 139 | + return enc_codec == cls.PREFERRED_ENCODING |
| 140 | + |
130 | 141 | def dump_file(self, f): |
131 | 142 | """ Dump this ASS document to a file object. |
132 | 143 | """ |
133 | 144 | encoding = getattr(f, 'encoding') |
134 | | - if encoding and encoding != 'utf_8_sig': |
| 145 | + if encoding and not self.is_preferred_encoding(encoding): |
135 | 146 | import warnings |
136 | 147 | warnings.warn("It is recommended to write UTF-8 with BOM" |
137 | | - " using the 'utf_8_sig' encoding") |
| 148 | + " using the '%s' encoding" % self.PREFERRED_ENCODING.name) |
138 | 149 |
|
139 | 150 | for section in self.sections.values(): |
140 | 151 | f.write("\n".join(section.dump())) |
|
0 commit comments