def testStringDictIO(embed_data): # Create a regular dict dictionary = dict(foo='bar', key='value') # Save it to a file obtained_file = embed_data['obtained_file.txt'] StringDictIO.Save(dictionary, obtained_file) # Create a file by hand expected_file = embed_data['expected_file.txt'] CreateFile(expected_file, 'foo = bar\nkey = value\n') # Syntax should match embed_data.assert_equal_files(obtained_file, expected_file) # Load a dict from our file new_dictionary = StringDictIO.Load(obtained_file) # That dict should match the original assert new_dictionary == dictionary filename = embed_data['testStringDictIO.txt'] CreateFile(filename, 'first=alpha\nsecond = bravo\nthird = charlie') loaded_dictionary = StringDictIO.Load(filename) assert loaded_dictionary == dict(first='alpha', second='bravo', third='charlie') loaded_dictionary = StringDictIO.Load(filename, inverted=True) assert loaded_dictionary == dict(alpha='first', bravo='second', charlie='third')
def testSymbols(embed_data): """ Test tf symbols command. This command is a WIP and should grow as "tf" interprets more and more symbols from python modules. """ filename = embed_data['testSymbols.py_'] original = """ import alpha class Alpha(object): def Method(self): pass def Function(): pass """ assert CreateFile(filename, dedent(original), encoding='UTF-8') app.TestScript( dedent(""" >terraformer symbols %(filename)s 1: IMPORT alpha """ % locals()))
def testFixIsFrozen(embed_data): """ General test for tbe "tf is-frozen" command. """ filename = embed_data['testFixIsFrozen.py'] data_dir = embed_data.get_data_dir() original = """ import coilib50 from coilib50.basic import property if coilib50.IsFrozen(): print "Frozen" property.Create('') """ assert CreateFile(filename, dedent(original), encoding='UTF-8') app.TestScript( dedent(""" >terraformer fix-is-frozen %(filename)s - %(filename)s """ % locals())) assert GetFileContents(filename, encoding='UTF-8') == dedent(""" from ben10 import property_ from ben10.foundation.is_frozen import IsFrozen import coilib50 from coilib50.basic import property if IsFrozen(): print "Frozen" property_.Create('') """)
def test_fix_format_error(embed_data): """ Check _FixFormat error output (detailed traceback). """ filename = embed_data['test_fix_format_error.py'] data_dir = embed_data.get_data_dir() original = """ import zulu from bravo import Bravo import alpha class Alpha(object): def Method(self) pass def Function(): pass """ assert CreateFile(filename, dedent(original), encoding='UTF-8') app.TestScript(dedent(""" >>>terraformer fix-format --single-job --traceback-limit=0 %(data_dir)s - %(filename)s: ERROR: \b\b On TerraForming.ReorganizeImports with filename: %(filename)s Had problems parsing: > import zulu > from bravo import Bravo > import alpha >\b > class Alpha(object): >\b > def Method(self) > pass >\b > def Function(): > pass bad input: type=4, value=u'\\r\\n', context=('', (7, 20)) --- * --- """ % locals()).replace('\b', ' '), input_prefix='>>>') assert GetFileContents(filename, encoding='UTF-8') == dedent(""" import zulu from bravo import Bravo import alpha class Alpha(object): def Method(self) pass def Function(): pass """)
def testStringDictIOEmptyFile(embed_data): # Make sure this works with empty files. filename = embed_data['empty_dict'] CreateFile(filename, contents='') dictionary = StringDictIO.Load(filename) assert len(dictionary) == 0
def TestFixEncoding(original, expected, encoding, lineno=0): filename = embed_data['testFixEncoding.py_'] assert CreateFile(filename, dedent(original), encoding=encoding) app.TestScript( dedent(""" >terraformer fix-encoding %(filename)s - %(filename)s: %(encoding)s (line:%(lineno)s) """ % locals())) obtained = GetFileContents(filename, encoding='UTF-8') assert obtained == dedent(expected)
def FixEncoding(console_, *sources): """ Fix python module files encoding, converting all non-ascii encoded files to UTF-8. :param sources: List of directories or files to process. """ from zerotk.easyfs import CreateFile, GetFileContents import io def GetPythonEncoding(filename): import re # Read the file contents in a encoding agnostic way, after all we're # trying to find out the file encoding. with open(filename, mode='rb') as iss: for i, i_line in enumerate(iss.readlines()): if i > 10: # Only searches the first lines for encoding information. break r = re.match(b'#.*coding[:=] *([\w\-\d]*)', i_line) if r is not None: return i, r.group(1) return 0, None for i_filename in _GetFilenames(sources, [PYTHON_EXT]): try: # Try to open using ASCII. If it fails means that we have a # non-ascii file. with io.open(i_filename, encoding='ascii') as iss: iss.read() except UnicodeDecodeError: try: line_no, encoding = GetPythonEncoding(i_filename) if encoding is None: console_.Print( '%s: <red>UKNOWN</r> Please configure the file coding.' % i_filename) continue console_.Item('%s: %s (line:%s)' % (i_filename, encoding, line_no)) lines = GetFileContents( i_filename, encoding=encoding).split('\n') del lines[line_no] lines = ['# coding: UTF-8'] + lines contents = '\n'.join(lines) CreateFile(i_filename, contents, encoding='UTF-8', eol_style=EOL_STYLE_UNIX) except: console_.Print('<red>%s: ERROR</>' % i_filename) raise
def testFixFormat(embed_data): """ General test for tbe "tf fix-format" command. This is a smoke test for the command interaction since most of the real testing is done on pytest_terra_former.py """ filename = embed_data['testFixFormat.py'] data_dir = embed_data.get_data_dir() original = """ import zulu from bravo import Bravo import alpha class Alpha(object): def Method(self): pass def Function(): pass """ assert CreateFile(filename, dedent(original), encoding='UTF-8') app.TestScript( dedent(""" >terraformer fix-format --single-job %(data_dir)s - %(filename)s: FIXED """ % locals())) assert GetFileContents(filename, encoding='UTF-8') == dedent(""" from bravo import Bravo import alpha import zulu class Alpha(object): def Method(self): pass def Function(): pass """)
def Save(cls, dictionary, target_filename, sort_items=False): """ Writes a dictionary into a file. :param unicode filename: Path and filename for target file. :param bool sort_items: If True, saved dict will be sorted alphabetically """ items = dictionary.items() if sort_items: items = sorted(items) contents = '\n'.join(['%s = %s' % i for i in items]) + '\n' from zerotk.easyfs import CreateFile CreateFile(target_filename, contents)
def testAddImportSymbol(embed_data): """ General test for the "tf add-import-symbol" command. """ filename = embed_data['testFixFormat.py'] data_dir = embed_data.get_data_dir() original = """ import zulu from bravo import Bravo import alpha class Alpha(object): def Method(self): pass def Function(): pass """ assert CreateFile(filename, dedent(original), encoding='UTF-8') app.TestScript( dedent(""" >terraformer add-import-symbol --single-job "__future__.unicode_literals" %(filename)s - %(filename)s: FIXED """ % locals())) assert GetFileContents(filename, encoding='UTF-8') == dedent(""" from __future__ import unicode_literals from bravo import Bravo import alpha import zulu class Alpha(object): def Method(self): pass def Function(): pass """)
def Save(self): """ Saves the filename applying the changes made by previous method calls. """ from zerotk.easyfs import CreateFile, EOL_STYLE_UNIX assert self.filename is not None, "No filename set on TerraFormer." assert self.__original_source is not None, "No original content set on TerraFormer." new_source = self.GenerateSource() changed = new_source != self.__original_source if changed: self.__original_source = self.GenerateSource() CreateFile(self.filename, self.__original_source, eol_style=EOL_STYLE_UNIX, encoding='UTF-8') return changed
def create_project_file(target_filename, template_filename, config): from jinja2 import Template template = Template(GetFileContents(template_filename)) CreateFile(target_filename, template.render(**config) + '\n', eol_style=EOL_STYLE_UNIX)
def test_parser(embed_data, basename): input_filename = embed_data[basename + '.lang'] obtained_filename = embed_data[basename + '.obtained.html'] expected_filename = embed_data[basename + '.html'] CreateFile(obtained_filename, generate(input_filename)) embed_data.assert_equal_files(obtained_filename, expected_filename)
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 zerotk.easyfs import CreateFile, EOL_STYLE_UNIX, 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' )