Skip to content

Commit

Permalink
Merge pull request #735 from Arelle/copy_assets
Browse files Browse the repository at this point in the history
Copying asset files from reports directory
  • Loading branch information
derekgengenbacher-wf authored Sep 19, 2024
2 parents 25b0a0f + 9dc5153 commit 08ee2d6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
32 changes: 29 additions & 3 deletions iXBRLViewerPlugin/iXBRLViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .constants import DEFAULT_JS_FILENAME, DEFAULT_OUTPUT_NAME, ERROR_MESSAGE_CODE, FEATURE_CONFIGS, INFO_MESSAGE_CODE
from .xhtmlserialize import XHTMLSerializer

REPORT_TYPE_EXTENSIONS = ('.xbrl', '.xhtml', '.html', '.htm', '.json')
UNRECOGNIZED_LINKBASE_LOCAL_DOCUMENTS_TYPE = 'unrecognizedLinkbase'
LINK_QNAME_TO_LOCAL_DOCUMENTS_LINKBASE_TYPE = {
XbrlConst.qnLinkCalculationLink: 'calcLinkbase',
Expand Down Expand Up @@ -89,6 +90,7 @@ def __init__(self,
basenameSuffix: str = '',
useStubViewer: bool = False,
):
self.reportZip = None
self.nsmap = NamespaceMap()
self.roleMap = NamespaceMap()
self.taxonomyData = {
Expand Down Expand Up @@ -116,6 +118,7 @@ def __init__(self,

self.fromSingleZIP = None
self.reportCount = 0
self.assets = []

def enableFeature(self, featureName: str):
if featureName in self.taxonomyData["features"]:
Expand Down Expand Up @@ -439,7 +442,6 @@ def processModel(
docSetFiles = [ srcFilename ]
filename = srcFilename
self.iv.addFile(iXBRLViewerFile(filename, report.modelDocument.xmlDocument))

docSetKey = frozenset(docSetFiles)
sourceReport = self.sourceReportsByFiles.get(docSetKey)
if sourceReport is None:
Expand Down Expand Up @@ -479,6 +481,14 @@ def processModel(
self.filingDocZipPath = os.path.dirname(report.modelDocument.filepath)
else:
self.fromSingleZIP = False
if report.fileSource.isArchive:
filelist = report.fileSource.fs.filelist
for file in filelist:
directory, asset = os.path.split(file.filename)
if "reports" in directory and asset != '' and not asset.lower().endswith(REPORT_TYPE_EXTENSIONS):
self.assets.append(file.filename)
if self.assets:
self.reportZip = report.fileSource.fs.filename

def createViewer(
self,
Expand All @@ -495,7 +505,6 @@ def createViewer(

self.taxonomyData["prefixes"] = self.nsmap.prefixmap
self.taxonomyData["roles"] = self.roleMap.prefixmap

if showValidations:
self.taxonomyData["validation"] = self.validationErrors()

Expand All @@ -513,7 +522,10 @@ def createViewer(
# If there is only a single report, call the output file "xbrlviewer.html"
# We should probably preserve the source file extension here.
self.iv.files[0].filename = 'xbrlviewer.html'

if self.assets:
self.iv.addReportAssets(self.assets)
if self.reportZip:
self.iv.reportZip = self.reportZip
return self.iv


Expand All @@ -534,10 +546,15 @@ def __init__(self, filename, xmlDocument):
class iXBRLViewer:

def __init__(self, cntlr: Cntlr):
self.reportZip = None
self.files = []
self.filingDocuments = None
self.cntlr = cntlr
self.filenames = set()
self.assets = []

def addReportAssets(self, assets):
self.assets.extend(assets)

def addFile(self, ivf):
if ivf.filename in self.filenames:
Expand Down Expand Up @@ -606,6 +623,15 @@ def save(self, destination: io.BytesIO | str, zipOutput: bool = False, copyScrip
filename = os.path.basename(self.filingDocuments)
self.cntlr.addToLog("Writing %s" % filename, messageCode=INFO_MESSAGE_CODE)
shutil.copy2(self.filingDocuments, os.path.join(destination, filename))
if self.assets:
with zipfile.ZipFile(self.reportZip) as z:
for asset in self.assets:
fileName = os.path.basename(asset)
path = os.path.join(destination, fileName)
self.cntlr.addToLog("Writing %s" % asset, messageCode=INFO_MESSAGE_CODE)
with z.open(asset) as zf, open(path, 'wb') as f:
shutil.copyfileobj(zf, f)

if copyScriptPath is not None:
self._copyScript(Path(destination), copyScriptPath)
else:
Expand Down
19 changes: 19 additions & 0 deletions tests/unit_tests/iXBRLViewerPlugin/test_iXBRLViewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,22 @@ def isoformat_effect():
format='format'
)

file_1 = Mock(
filename='something/reports/001.jpg'
)

file_2 = Mock(
filename='something/reports/002.jpg'
)

fs = Mock(
filelist = [file_1, file_2]
)

file_source = Mock(
fs = fs
)

def creationSoftwareMatches_effect(text):
return ["Example Software Name"]

Expand Down Expand Up @@ -381,6 +397,7 @@ def urlDocEntry(path, docType, linkQName=None):
baseSets=baseSets,
roleTypes=roleTypes,
facts=[fact_1, fact_with_typed_dimension, fact_with_missing_member_on_dimension],
fileSource=file_source,
info=info_effect,
modelDocument=self.modelDocument,
ixdsTarget=None,
Expand Down Expand Up @@ -411,6 +428,7 @@ def urlDocEntry(path, docType, linkQName=None):
baseSets=baseSets,
roleTypes=roleTypes,
facts=[fact_2, fact_3],
fileSource=file_source,
info=info_effect,
modelDocument=self.modelDocument,
ixdsTarget=None,
Expand All @@ -422,6 +440,7 @@ def urlDocEntry(path, docType, linkQName=None):
baseSets=baseSets,
roleTypes=roleTypes,
facts=[fact_1, fact_with_typed_dimension, fact_with_missing_member_on_dimension],
fileSource=file_source,
info=info_effect,
modelDocument=self.modelDocumentInlineSet,
ixdsTarget=None,
Expand Down

0 comments on commit 08ee2d6

Please sign in to comment.