我试图为你写一个解析器。
我同意批量是对 UTF-8 的测试,如果您已经在字符串中具有值,这是多余的(UTF-8 是文件系统上的编码,unicode 是有效UTF-8 的内部表示)。这确实极大地简化了事情。
据我了解,BNF 说:
- locale-folder(可选)是字符串 'locale/' 后跟lang-tag
- lang-tag的形式为 'en'、'en-us'、'en-123'、'en-us-1' 等等:
- 至少一个记号,用'-'字符隔开
- 每个标记为 1 到 8 个字符
- 第一个标记可能只有小写字母
- 以下标记是小写字母和数字的混合
- 在可选语言环境之后,您可以拥有:
- 单个文件名或
- 路径(一系列以“/”分隔的文件夹名称)或
- 后跟文件名的路径
- 文件夹名和文件名是一种 unicode。每个字符要么
- AZ、az、0-9 或
- “$%'-_@~()&+,=[]”中的任何一个。
- u007F以上的任何字符(UTF8 二、三和四字节字符)
也就是说,这是一个简单的实现(为了调试,它捕获解析的输出。我这样做是为了调试,但如果你不需要它,请随意删除它)。路径中的错误会导致 ZipRelPath 构造函数引发 ValueError:
import re
class ZipRelPath:
FILE_NAME_RE = re.compile(u"^[a-zA-Z0-9 \$\%\'\-_@~\(\)&+,=\[\]\.\u0080-\uFFFF]+$")
LANG_TAG_RE = re.compile("^[a-z]{1,8}(\-[a-z0-9]{1,8})*$")
LOCALES = "locales/"
def __init__(self, path):
self.path = path
self.lang_tag = None
self.folders = []
self.file_name = None
self._parse_locales()
self._parse_folders()
def _parse_locales(self):
"""Consumes any leading 'locales' and lang-tag"""
if self.path.startswith(ZipRelPath.LOCALES):
self.path = self.path[len(ZipRelPath.LOCALES):]
self._parse_lang_tag()
def _parse_lang_tag(self):
"""Parses, consumes and validates the lang-tag"""
self.lang_tag, _, self.path = self.path.partition("/")
if not self.path:
raise ValueError("lang-tag missing closing /")
if not ZipRelPath.LANG_TAG_RE.match(self.lang_tag):
raise ValueError(u"'%s' is not a valid language tag" % self.lang_tag)
def _parse_folders(self):
"""Handles the folders and file-name after the locale"""
while (self.path):
self._parse_folder_or_file()
if not self.folders and not self.file_name:
raise ValueError("Missing folder or file name")
def _parse_folder_or_file(self):
"""Each call consumes a single path entry, validating it"""
folder_or_file, _, self.path = self.path.partition("/")
if not ZipRelPath.FILE_NAME_RE.match(folder_or_file):
raise ValueError(u"'%s' is not a valid file or folder name" % folder_or_file)
if self.path:
self.folders.append(folder_or_file)
else:
self.file_name = folder_or_file
def __unicode__(self):
return u"ZipRelPath [lang-tag: %s, folders: %s, file_name: %s" % (self.lang_tag, self.folders, self.file_name)
还有一组简短的测试:
GOOD = [
"$%'-_@~()&+,=[].txt9",
"my/path/to/file.txt",
"locales/en/file.txt",
"locales/en-us/file.txt",
"locales/en-us-abc123-xyz/file.txt",
"locales/abcdefgh-12345678/file.txt",
"locales/en/my/path/to/file.txt",
u"my\u00A5\u0160\u039E\u04FE\u069E\u0BCC\uFFFD/path/to/file.txt"
]
BAD = [
"",
"/starts/with/slash",
"bad^file",
"locales//bad/locale",
"locales/en123/bad/locale",
"locales/EN/bad/locale",
"locales/en-US/bad/locale",
]
for path in GOOD:
print unicode(ZipRelPath(path))
for path in BAD:
try:
zip = ZipRelPath(path)
raise Exception("Illegal path {0} was accepted by {1}".format(path, zip))
except ValueError as exception:
print "Incorrect path '{0}' fails with: {1}".format(path, exception)
产生:
ZipRelPath [lang-tag: None, folders: [], file_name: $%'-_@~()&+,=[].txt9
ZipRelPath [lang-tag: None, folders: ['my', 'path', 'to'], file_name: file.txt
ZipRelPath [lang-tag: en, folders: [], file_name: file.txt
ZipRelPath [lang-tag: en-us, folders: [], file_name: file.txt
ZipRelPath [lang-tag: en-us-abc123-xyz, folders: [], file_name: file.txt
ZipRelPath [lang-tag: abcdefgh-12345678, folders: [], file_name: file.txt
ZipRelPath [lang-tag: en, folders: ['my', 'path', 'to'], file_name: file.txt
ZipRelPath [lang-tag: None, folders: [u'my\xa5\u0160\u039e\u04fe\u069e\u0bcc\ufffd', u'path', u'to'], file_name: file.txt
Incorrect path '' fails with: Missing folder or file name
Incorrect path '/starts/with/slash' fails with: '' is not a valid file or folder name
Incorrect path 'bad^file' fails with: 'bad^file' is not a valid file or folder name
Incorrect path 'locales//bad/locale' fails with: '' is not a valid language tag
Incorrect path 'locales/en123/bad/locale' fails with: 'en123' is not a valid language tag
Incorrect path 'locales/EN/bad/locale' fails with: 'EN' is not a valid language tag
Incorrect path 'locales/en-US/bad/locale' fails with: 'en-US' is not a valid language tag
如果您的任何测试用例失败,请告诉我,我会看看是否可以修复它。