PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/ctypes/macholib/framework.py

http://unladen-swallow.googlecode.com/
Python | 68 lines | 59 code | 3 blank | 6 comment | 0 complexity | ca48e38e802aa6db8b4b03002b6f3173 MD5 | raw file
 1######################################################################
 2#  This file should be kept compatible with Python 2.3, see PEP 291. #
 3######################################################################
 4"""
 5Generic framework path manipulation
 6"""
 7
 8import re
 9
10__all__ = ['framework_info']
11
12STRICT_FRAMEWORK_RE = re.compile(r"""(?x)
13(?P<location>^.*)(?:^|/)
14(?P<name>
15    (?P<shortname>\w+).framework/
16    (?:Versions/(?P<version>[^/]+)/)?
17    (?P=shortname)
18    (?:_(?P<suffix>[^_]+))?
19)$
20""")
21
22def framework_info(filename):
23    """
24    A framework name can take one of the following four forms:
25        Location/Name.framework/Versions/SomeVersion/Name_Suffix
26        Location/Name.framework/Versions/SomeVersion/Name
27        Location/Name.framework/Name_Suffix
28        Location/Name.framework/Name
29
30    returns None if not found, or a mapping equivalent to:
31        dict(
32            location='Location',
33            name='Name.framework/Versions/SomeVersion/Name_Suffix',
34            shortname='Name',
35            version='SomeVersion',
36            suffix='Suffix',
37        )
38
39    Note that SomeVersion and Suffix are optional and may be None
40    if not present
41    """
42    is_framework = STRICT_FRAMEWORK_RE.match(filename)
43    if not is_framework:
44        return None
45    return is_framework.groupdict()
46
47def test_framework_info():
48    def d(location=None, name=None, shortname=None, version=None, suffix=None):
49        return dict(
50            location=location,
51            name=name,
52            shortname=shortname,
53            version=version,
54            suffix=suffix
55        )
56    assert framework_info('completely/invalid') is None
57    assert framework_info('completely/invalid/_debug') is None
58    assert framework_info('P/F.framework') is None
59    assert framework_info('P/F.framework/_debug') is None
60    assert framework_info('P/F.framework/F') == d('P', 'F.framework/F', 'F')
61    assert framework_info('P/F.framework/F_debug') == d('P', 'F.framework/F_debug', 'F', suffix='debug')
62    assert framework_info('P/F.framework/Versions') is None
63    assert framework_info('P/F.framework/Versions/A') is None
64    assert framework_info('P/F.framework/Versions/A/F') == d('P', 'F.framework/Versions/A/F', 'F', 'A')
65    assert framework_info('P/F.framework/Versions/A/F_debug') == d('P', 'F.framework/Versions/A/F_debug', 'F', 'A', 'debug')
66
67if __name__ == '__main__':
68    test_framework_info()