def _teigha_convert(data, extension='dwg'):
    """
    Convert any DXF/DWG to R14 ASCII DXF using Teigha Converter.

    Parameters
    ---------------
    data : str or bytes
       The contents of a DXF or DWG file
    extension : str
       The format of data: 'dwg' or 'dxf'

    Returns
    --------------
    converted : str
       Result as R14 ASCII DXF
    """
    # temp directory for DWG file
    dir_dwg = tempfile.mkdtemp()
    # temp directory for DXF output
    dir_out = tempfile.mkdtemp()

    # put together the subprocess command
    cmd = [_xvfb_run,  # suppress the GUI QT status bar
           '-a',      # use an automatic screen
           _teigha,   # run the converter
           dir_dwg,   # the directory containing DWG files
           dir_out,   # the directory for output DXF files
           'ACAD14',  # the revision of DXF
           'DXF',     # the output format
           '1',       # recurse input folder
           '1']       # audit each file

    # if Xvfb is already running it probably
    # has a working configuration so use it
    running = b'Xvfb' in subprocess.check_output(['ps', '-eaf'])
    # chop off XVFB if it isn't installed or is running
    if running or _xvfb_run is None:
        cmd = cmd[2:]

    # create file in correct mode for data
    if hasattr(data, 'encode'):
        # data is a string which can be encoded to bytes
        mode = 'w'
    else:
        # data is already bytes
        mode = 'wb'

    # write the file_obj in the temp directory
    dwg_name = os.path.join(dir_dwg, 'drawing.' + extension)
    with open(dwg_name, mode) as f:
        f.write(data)

    # run the conversion
    output = subprocess.check_output(cmd)

    # load the ASCII DXF produced from the conversion
    name_result = os.path.join(dir_out, 'drawing.dxf')
    # if the conversion failed log things before failing
    if not os.path.exists(name_result):
        log.error('teigha convert failed!\nls {}: {}\n\n {}'.format(
            dir_out,
            os.listdir(dir_out),
            output))
        raise ValueError('conversion using Teigha failed!')

    # load converted file into a string
    with open(name_result, 'r') as f:
        converted = f.read()

    # remove the temporary directories
    shutil.rmtree(dir_out)
    shutil.rmtree(dir_dwg)

    return converted
Convert any DXF/DWG to R14 ASCII DXF using Teigha Converter.

    Parameters
    ---------------
    data : str or bytes
       The contents of a DXF or DWG file
    extension : str
       The format of data: 'dwg' or 'dxf'

    Returns
    --------------
    converted : str
       Result as R14 ASCII DXF