def Load(cls, filename, inverted=False): ''' Loads a dictionary from a file. :param unicode filename: Path and filename for source file. :param bool inverted: If True inverts the key/value order. :returns dict: Dictionary that was loaded ''' from ben10.filesystem import GetFileContents contents = GetFileContents(filename) contents_dict = dict() import re for line in contents.split('\n'): search_result = re.search('(.*?)\s*=\s*(.*)', line) if search_result is not None: key, value = search_result.groups() if inverted: contents_dict[value] = key else: contents_dict[key] = value return contents_dict
def FixIsFrozen(console_, *sources): ''' Fix some pre-determinated set of symbols usage with the format: <module>.<symbol> Eg.: from coilib50.basic import property property.Create --- from ben10 import property_ property_.Create This was necessary for BEN-30. Today TerraFormer only acts on import-statements. We have plans to also act on imported symbols usage. :param sources: List of directories or files to process. ''' from ben10.filesystem import CreateFile, EOL_STYLE_UNIX, FindFiles, GetFileContents FIND_REPLACE = [ ('StringIO', 'StringIO', 'from io import StringIO'), ('cStringIO', 'StringIO', 'from io import StringIO'), ('coilib50.IsFrozen', 'IsFrozen', 'from ben10.foundation.is_frozen import IsFrozen'), ('coilib50.IsDevelopment', 'IsDevelopment', 'from ben10.foundation.is_frozen import IsDevelopment'), ('coilib50.SetIsFrozen', 'SetIsFrozen', 'from ben10.foundation.is_frozen import SetIsFrozen'), ('coilib50.SetIsDevelopment', 'SetIsDevelopment', 'from ben10.foundation.is_frozen import SetIsDevelopment'), ('coilib40.basic.IsInstance', 'IsInstance', 'from ben10.foundation.klass import IsInstance'), ] PROPERTY_MODULE_SYMBOLS = [ 'PropertiesDescriptor', 'Property', 'Create', 'CreateDeprecatedProperties', 'CreateForwardProperties', 'FromCamelCase', 'MakeGetName', 'MakeSetGetName', 'MakeSetName', 'ToCamelCase', 'Copy', 'DeepCopy', 'Eq', 'PropertiesStr', ] for i_symbol in PROPERTY_MODULE_SYMBOLS: FIND_REPLACE.append( ('property.%s' % i_symbol, 'property_.%s' % i_symbol, 'from ben10 import property_'), ) for i_filename in _GetFilenames(sources, [PYTHON_EXT]): contents = GetFileContents(i_filename) imports = set() for i_find, i_replace, i_import in FIND_REPLACE: if i_find in contents: contents = contents.replace(i_find, i_replace) imports.add(i_import) if imports: console_.Item(i_filename) lines = contents.split('\n') index = None top_doc = False for i, i_line in enumerate(lines): if i == 0: for i_top_doc in ("'''", '"""'): if i_top_doc in i_line: console_.Print('TOPDOC START: %d' % i, indent=1) top_doc = i_top_doc break continue elif top_doc: if i_top_doc in i_line: console_.Print('TOPDOC END: %d' % i, indent=1) index = i + 1 break continue elif i_line.startswith('import ') or i_line.startswith('from '): index = i - 1 break elif i_line.strip() == '': continue console_.Print('ELSE: %d: %s' % (i, i_line)) index = i break assert index is not None lines = lines[0:index] + list(imports) + lines[index:] contents = '\n'.join(lines) CreateFile(i_filename, contents, eol_style=EOL_STYLE_UNIX, encoding='UTF-8')