Skip to content

Commit

Permalink
activated new registry, fixed #75, fixed #69, fixed #59, fixed #53, f…
Browse files Browse the repository at this point in the history
…ixed #40
  • Loading branch information
manusimidt committed Feb 18, 2022
1 parent ca6a9cc commit 6348b22
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 198 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def read(filename):
packages=find_packages(exclude=('tests', 'cache', 'workdir')),

install_requires=[
"requests", # needed for fetching xml files from the internet
"urllib3" # needed for http retries
"requests", # needed for fetching xml files from the internet
"urllib3" # needed for http retries
],

classifiers=[
Expand Down
3 changes: 2 additions & 1 deletion tests/data/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
xmlns:link="http://www.xbrl.org/2003/linkbase"
xmlns:xbrli="http://www.xbrl.org/2003/instance"
xmlns:iso4217="http://www.xbrl.org/2003/iso4217"
xmlns:example="http://www.example.com/20210101">
xmlns:example="http://www.example.com/20210101"
xmlns:ixt="http://www.xbrl.org/inlineXBRL/transformation/2015-02-26">
<head>
<title>Sample iXBRL file</title>
<style>* {
Expand Down
19 changes: 12 additions & 7 deletions tests/test_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import sys
import unittest

from xbrl.helper.transformation import transform_ixt, transform_ixt_sec
from xbrl.transformations import normalize
from xbrl.transformations import normalize, TransformationException

logging.basicConfig(stream=sys.stdout, level=logging.INFO)

Expand All @@ -21,9 +20,8 @@

['booleantrue', 'yeah', 'true'],

['datedaymonth', '2.12', '--12-02'],
['datedaymonth', '1.1', '--01-01'],
['datedaymonth', '12.11', '--11-12'],
['datedaymonth', '11.12', '--12-11'],
['datedaymonth', '1.2', '--02-01'],

['datedaymonthen', '2. December', '--12-02'],
['datedaymonthen', '2 Sept.', '--09-02'],
Expand Down Expand Up @@ -177,8 +175,15 @@ def test_normalize(self):
for namespace in testTransforms:
for i, testCase in enumerate(testTransforms[namespace]):
formatCode, testInput, expected = testCase
testOutput = normalize(namespace, formatCode, testInput)
self.assertEqual(expected, testOutput, msg=f'Failed at test elem {i} of registry {namespace}')
if expected == 'exception':
try:
testOutput = normalize(namespace, formatCode, testInput)
self.fail('Expected Transformation Exception, received ' + testOutput)
except TransformationException:
pass
else:
testOutput = normalize(namespace, formatCode, testInput)
self.assertEqual(expected, testOutput, msg=f'Failed at test elem {i} of registry {namespace}')


if __name__ == '__main__':
Expand Down
174 changes: 0 additions & 174 deletions xbrl/helper/transformation.py

This file was deleted.

35 changes: 21 additions & 14 deletions xbrl/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

from xbrl import TaxonomyNotFound, InstanceParseException
from xbrl.cache import HttpCache
from xbrl.helper import transformation
from xbrl.taxonomy import Concept, TaxonomySchema, parse_taxonomy, parse_common_taxonomy, parse_taxonomy_url
from xbrl.helper.uri_helper import resolve_uri
from xbrl.helper.xml_parser import parse_file
from xbrl.transformations import normalize, TransformationException, TransformationNotImplemented

logger = logging.getLogger(__name__)
LINK_NS: str = "{http://www.xbrl.org/2003/linkbase}"
Expand Down Expand Up @@ -450,8 +450,6 @@ def _extract_non_numeric_value(fact_elem: ET.Element) -> str:
"""
This function parses a ix:nonNumeric fact as defined in:
https://www.xbrl.org/Specification/inlineXBRL-part1/PWD-2013-02-13/inlineXBRL-part1-PWD-2013-02-13.html#d1e6391
:param fact_elem:
:return:
"""
Expand All @@ -463,12 +461,16 @@ def _extract_non_numeric_value(fact_elem: ET.Element) -> str:

fact_format = fact_elem.attrib['format'] if 'format' in fact_elem.attrib else None
if fact_format:
# extract transformation registry namespace and transformation rule code
registryPrefix, formatCode = fact_format.split(':')
registryNS: str = fact_elem.attrib['ns_map'][registryPrefix]
try:
if fact_format.startswith('ixt:'):
fact_value = transformation.transform_ixt(fact_value, fact_format.split(':')[1])
elif fact_format.startswith('ixt-sec'):
fact_value = transformation.transform_ixt_sec(fact_value, fact_format.split(':')[1])
except Exception:
fact_value = normalize(registryNS, formatCode, fact_value)
except TransformationNotImplemented:
logging.info(f'Transformation rule {formatCode} of registry {registryPrefix} is not supported. '
f'The parser will just parse the value as it is and not transform it according to the rule.')
return fact_value
except TransformationException:
logging.warning(f'Could not transform value "{fact_value}" with format {fact_format}')
return fact_value
return fact_value
Expand All @@ -495,14 +497,18 @@ def _extract_non_fraction_value(fact_elem: ET.Element) -> float or None or str:
value_sign: str or None = fact_elem.attrib['sign'] if 'sign' in fact_elem.attrib else None

if fact_format:
# extract transformation registry namespace and transformation rule code
registryPrefix, formatCode = fact_format.split(':')
registryNS: str = fact_elem.attrib['ns_map'][registryPrefix]
try:
if fact_format.startswith('ixt:'):
fact_value = transformation.transform_ixt(fact_value, fact_format.split(':')[1])
elif fact_format.startswith('ixt-sec'):
fact_value = transformation.transform_ixt_sec(fact_value, fact_format.split(':')[1])
except Exception:
fact_value = normalize(registryNS, formatCode, fact_value)
except TransformationNotImplemented:
logging.info(f'Transformation rule {formatCode} of registry {registryPrefix} is not supported. '
f'The parser will just parse the value as it is and not transform it according to the rule.')
return fact_value
except TransformationException:
logging.warning(f'Could not transform value "{fact_value}" with format {fact_format}')
# return fact_value
return fact_value

scaled_value = float(fact_value) * pow(10, value_scale)
# Floating-point error mitigation
Expand All @@ -515,6 +521,7 @@ def _extract_non_fraction_value(fact_elem: ET.Element) -> float or None or str:

def _extract_text_value(element: ET.Element) -> str:
text = '' if element.text is None else element.text
if element.tail: text += element.tail
for children in element:
text += _extract_text_value(children)
return text
Expand Down

0 comments on commit 6348b22

Please sign in to comment.