Exemplo n.º 1
0
def testinfrep_inputmethod_recompiledfunc_outputmethod_func():
    testinfrep_setup()

    # define functions
    def inputfunc(filename):
        pattern = str(os.path.basename(filename)) + ':' + '([0-9]*)'
        regex = re.compile(pattern)
        return (regex)

    def outputfunc(match, filename):
        return (str(os.path.basename(filename)) + '!' + match.group(1))

    # do replace
    infrep_main([{
        'inputterm':
        inputfunc,
        'outputterm':
        outputfunc,
        'filenames': [__projectdir__ / Path('testinfrep/test_funcboth.txt')],
        'inputmethod':
        'recompiledfunc',
        'outputmethod':
        'func'
    }])

    # verify worked
    with open(__projectdir__ / Path('testinfrep/test_funcboth.txt')) as f:
        text = f.read()
    if text != 'test_funcboth.txt!123\ntest_funcboth2.txt:124\n':
        raise ValueError('No match')
def replacecommonsections(codelist,
                          folder_commonsections,
                          allextension='.all'):
    """
        

    allextension needs to begin with a "." for it to work.
    """

    if not os.path.isdir(folder_commonsections):
        raise ValueError(
            'The folder containing the common sections does not exist.')

    infreplist = []
    for filename_commonsection in os.listdir(folder_commonsections):
        fullfilename_commonsection = os.path.join(folder_commonsections,
                                                  filename_commonsection)

        if not os.path.isfile(fullfilename_commonsection):
            raise ValueError(
                'All the elements of the common section folder should be files.'
            )

        # get text in filename
        with open(fullfilename_commonsection) as f:
            commonsection = f.read()
        if commonsection[-1] != '\n':
            commonsection = commonsection + '\n'
        commonsectionsplit = commonsection.split('\n')
        firstline = commonsectionsplit[0]
        lastline = commonsectionsplit[-2]

        extension = os.path.splitext(fullfilename_commonsection)[1]
        if extension == '.all':
            codelist_extension = codelist
        else:
            codelist_extension = [
                codefile for codefile in codelist
                if str(codefile).endswith(extension)
            ]

        inputterm = re.compile(
            re.escape(firstline) + "(.*?)" + re.escape(lastline) + '\n',
            re.DOTALL)

        infreplist.append({
            'inputterm': inputterm,
            'outputterm': commonsection,
            'filenames': codelist_extension,
            'inputmethod': 'recompiled'
        })

    infrep_main(infreplist)
Exemplo n.º 3
0
def testinfrep_inputmethod_recompiled():
    testinfrep_setup()

    # do replace
    infrep_main([{
        'inputterm':
        re.compile('\\\\[0-9][a-z][a-z][a-z]\.'),
        'outputterm':
        '\\1dog.',
        'filenames': [__projectdir__ / Path('testinfrep/test_simple.txt')],
        'inputmethod':
        'recompiled'
    }])

    # verify worked
    with open(__projectdir__ / Path('testinfrep/test_simple.txt')) as f:
        text = f.read()
    if text != '1\n\\1dog.\n2\n':
        raise ValueError('No match')
Exemplo n.º 4
0
def testinfrep_basic():
    """
    Verifies the case where I just enter text strings works
    """
    testinfrep_setup()

    # do replace
    infrep_main([{
        'inputterm':
        '\\1cat.',
        'outputterm':
        '\\1dog.',
        'filenames': [__projectdir__ / Path('testinfrep/test_simple.txt')]
    }])

    # verify worked
    with open(__projectdir__ / Path('testinfrep/test_simple.txt')) as f:
        text = f.read()
    if text != '1\n\\1dog.\n2\n':
        raise ValueError('No match')
Exemplo n.º 5
0
def testinfrep_inputmethod_re_outputmethod_eval():
    testinfrep_setup()

    # do replace
    # note that I could also write r'\\' which would be the same
    infrep_main([{
        'inputterm':
        '\\\\[0-9]([a-z]*)\.',
        'outputterm':
        '"\\\\2" + match.group(1) + "."',
        'filenames': [__projectdir__ / Path('testinfrep/test_simple.txt')],
        'inputmethod':
        're',
        'outputmethod':
        'eval'
    }])

    # verify worked
    with open(__projectdir__ / Path('testinfrep/test_simple.txt')) as f:
        text = f.read()
    if text != '1\n\\2cat.\n2\n':
        raise ValueError('No match')