def METHOD_NAME(filename, layer, geometry):
    with open(filename, 'r') as ifile:
        magtext = ifile.read().splitlines() 
    layerrex = re.compile('<< ' + layer + ' >>')
    sectionrex = re.compile('<< ')
    labelsrex = re.compile('<< labels >>')
    propsrex = re.compile('<< properties >>')
    endrex = re.compile('<< end >>')
    in_layer = False
    done = False
    with open(filename, 'w') as ofile:
        for line in magtext:
            if not done and not in_layer:
                # Handle case in which layer did not already exist in file
                lmatch = labelsrex.match(line)
                pmatch = propsrex.match(line)
                ematch = endrex.match(line)
                if lmatch or pmatch or ematch:
                    print('<< ' + layer + ' >>', file=ofile)
                    for coords in zip(*[iter(geometry.split())]*4):
                        print('rect ' + ' '.join(coords), file=ofile)
                    done = True
            lmatch = layerrex.match(line)
            if lmatch or pmatch:
                in_layer = True
            elif in_layer:
                smatch = sectionrex.match(line)
                if smatch:
                    for coords in zip(*[iter(geometry.split())]*4):
                        print('rect ' + ' '.join(coords), file=ofile)
                    in_layer = False
                    done = True
            print(line, file=ofile)