Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    def testStash(self, git):
        assert not git.IsDirty(git.cloned_remote)

        # Modify alpha.txt
        alpha_txt = git.cloned_remote + '/alpha.txt'
        CreateFile(alpha_txt, 'Changing alpha.txt\n')
        assert git.IsDirty(git.cloned_remote)

        # Stash changes so we have alpha.txt with its original content.
        git.Stash(git.cloned_remote)
        assert not git.IsDirty(git.cloned_remote)
        assert GetFileContents(alpha_txt) == 'alpha\n'

        # Pop changes so we have alpha.txt with our modification
        git.StashPop(git.cloned_remote)
        assert git.IsDirty(git.cloned_remote)
        assert GetFileContents(alpha_txt) == 'Changing alpha.txt\n'
Ejemplo n.º 3
0
def SendSample(console_, room=DEFAULT_ROOM, host=DEFAULT_HOST, port=DEFAULT_PORT, *config_ids):
    '''
    DEVELOPMENT
    '''
    import os
    from ben10.filesystem import GetFileContents

    for i_config_id in config_ids:
        sample_filename = os.path.dirname(__file__) + '/samples/' + i_config_id + '.json'
        if not os.path.isfile(sample_filename):
            console_.Print("Can't find sample file: %s" % sample_filename)
            return 1

        data = GetFileContents(sample_filename)

        r = _SlackData(console_, '/webhook/%s' % i_config_id, data, room=room, host=host, port=port)

        console_.Print(r)
Ejemplo n.º 4
0
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')