def saveDts(cntlr, modelXbrl, outputDtsDir):
    from arelle import ModelDocument
    import shutil
    excelFileDir = os.path.dirname(modelXbrl.fileSource.url)

    def saveToFile(url):
        if os.path.isabs(url):
            return url
        filepath = os.path.join(outputDtsDir, url)
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        return filepath
    for doc in modelXbrl.urlDocs.values():
        if getattr(doc, 'loadedFromExcel', False):
            doc.save(saveToFile(doc.uri), updateFileHistory=False)
            cntlr.showStatus(_('Saving XBRL DTS: {0}').format(os.path.basename(doc.uri)))
            for refDoc in doc.referencesDocument.keys():
                if refDoc.inDTS:
                    if (refDoc.type == ModelDocument.Type.LINKBASE):
                        cntlr.showStatus(_('Saving XBRL DTS: {0}').format(os.path.basename(refDoc.uri)))
                        refDoc.save(saveToFile(refDoc.uri), updateFileHistory=False)
        elif (not (UrlUtil.isAbsolute(doc.uri) or os.path.isabs(doc.uri) or (outputDtsDir == excelFileDir))):
            srcfile = os.path.join(excelFileDir, doc.uri)
            destfile = saveToFile(doc.uri)
            if os.path.exists(srcfile):
                if (not os.path.exists(destfile)):
                    shutil.copyfile(srcfile, destfile)
            else:
                modelXbrl.error('loadFromExcel:missingReference', 'Missing source file to copy to output DTS directory: %(missingFile)s', modelXbrl=modelXbrl, missingFile=doc.uri)
def saveDts(cntlr, modelXbrl, outputDtsDir):
    from arelle import ModelDocument
    import shutil
    excelFileDir = os.path.dirname(modelXbrl.fileSource.url)

    def saveToFile(url):
        if os.path.isabs(url):
            return url
        filepath = os.path.join(outputDtsDir, url)
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        return filepath
    for doc in modelXbrl.urlDocs.values():
        if getattr(doc, 'loadedFromExcel', False):
            doc.save(saveToFile(doc.uri), updateFileHistory=False)
            cntlr.showStatus(_('Saving XBRL DTS: {0}').format(os.path.basename(doc.uri)))
            for refDoc in doc.referencesDocument.keys():
                if refDoc.inDTS:
                    if (refDoc.type == ModelDocument.Type.LINKBASE):
                        cntlr.showStatus(_('Saving XBRL DTS: {0}').format(os.path.basename(refDoc.uri)))
                        refDoc.save(saveToFile(refDoc.uri), updateFileHistory=False)
        elif (not (UrlUtil.isAbsolute(doc.uri) or os.path.isabs(doc.uri) or (outputDtsDir == excelFileDir))):
            srcfile = os.path.join(excelFileDir, doc.uri)
            destfile = saveToFile(doc.uri)
            if os.path.exists(srcfile):
                if (not os.path.exists(destfile)):
                    shutil.copyfile(srcfile, destfile)
            else:
                modelXbrl.error('loadFromExcel:missingReference', 'Missing source file to copy to output DTS directory: %(missingFile)s', modelXbrl=modelXbrl, missingFile=doc.uri)