1 ###################################################################### 2 # This file should be kept compatible with Python 2.3, see PEP 291. # 3 ###################################################################### 4 """ 5 Generic framework path manipulation 6 """ 7 8 import re 9 10 __all__ = ['framework_info'] 11 12 STRICT_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 22 def 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 47 def 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 67 if __name__ == '__main__': 68 test_framework_info() 69