Back to index

python3.2  3.2.2
framework.py
Go to the documentation of this file.
00001 """
00002 Generic framework path manipulation
00003 """
00004 
00005 import re
00006 
00007 __all__ = ['framework_info']
00008 
00009 STRICT_FRAMEWORK_RE = re.compile(r"""(?x)
00010 (?P<location>^.*)(?:^|/)
00011 (?P<name>
00012     (?P<shortname>\w+).framework/
00013     (?:Versions/(?P<version>[^/]+)/)?
00014     (?P=shortname)
00015     (?:_(?P<suffix>[^_]+))?
00016 )$
00017 """)
00018 
00019 def framework_info(filename):
00020     """
00021     A framework name can take one of the following four forms:
00022         Location/Name.framework/Versions/SomeVersion/Name_Suffix
00023         Location/Name.framework/Versions/SomeVersion/Name
00024         Location/Name.framework/Name_Suffix
00025         Location/Name.framework/Name
00026 
00027     returns None if not found, or a mapping equivalent to:
00028         dict(
00029             location='Location',
00030             name='Name.framework/Versions/SomeVersion/Name_Suffix',
00031             shortname='Name',
00032             version='SomeVersion',
00033             suffix='Suffix',
00034         )
00035 
00036     Note that SomeVersion and Suffix are optional and may be None
00037     if not present
00038     """
00039     is_framework = STRICT_FRAMEWORK_RE.match(filename)
00040     if not is_framework:
00041         return None
00042     return is_framework.groupdict()
00043 
00044 def test_framework_info():
00045     def d(location=None, name=None, shortname=None, version=None, suffix=None):
00046         return dict(
00047             location=location,
00048             name=name,
00049             shortname=shortname,
00050             version=version,
00051             suffix=suffix
00052         )
00053     assert framework_info('completely/invalid') is None
00054     assert framework_info('completely/invalid/_debug') is None
00055     assert framework_info('P/F.framework') is None
00056     assert framework_info('P/F.framework/_debug') is None
00057     assert framework_info('P/F.framework/F') == d('P', 'F.framework/F', 'F')
00058     assert framework_info('P/F.framework/F_debug') == d('P', 'F.framework/F_debug', 'F', suffix='debug')
00059     assert framework_info('P/F.framework/Versions') is None
00060     assert framework_info('P/F.framework/Versions/A') is None
00061     assert framework_info('P/F.framework/Versions/A/F') == d('P', 'F.framework/Versions/A/F', 'F', 'A')
00062     assert framework_info('P/F.framework/Versions/A/F_debug') == d('P', 'F.framework/Versions/A/F_debug', 'F', 'A', 'debug')
00063 
00064 if __name__ == '__main__':
00065     test_framework_info()