Example #1
0
def presentation_serializer(serializer):
    filing = serializer.filing
    date = filing.date
    company = filing.company
    nsmap = gen_nsmap(filing, 'Presentation')
    maker = ElementMaker(nsmap=nsmap)

    with xml_namespace(maker, None, auto_convert=True) as maker:
        linkbase = maker.linkbase(
            **{
                #find out about this
                'xsi:schemaLocation':
                'http://www.xbrl.org/2003/linkbase http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd',
                'xmlns': 'http://www.xbrl.org/2003/linkbase',
            })
        for chart in filing.charts:
            roleRef = maker.roleRef(
                **{
                    'roleURI':
                    convert_role_url(chart.role, filing),
                    'xlink:type':
                    'simple',
                    'xlink:href':
                    '{0}#{1}'.format(serializer.document_name('Schema'),
                                     chart.role)
                })

            linkbase.append(roleRef)
            chart.bind(serializer)
            linkbase.append(chart_serializer(chart, filing, maker))

    return linkbase
Example #2
0
def presentation_serializer(serializer):
	filing = serializer.filing
	date = filing.date
	company = filing.company
	nsmap = gen_nsmap(filing, 'Presentation')
	maker = ElementMaker(nsmap=nsmap)

	with xml_namespace(maker, None, auto_convert=True) as maker:
		linkbase = maker.linkbase(**{
			#find out about this
			'xsi:schemaLocation': 'http://www.xbrl.org/2003/linkbase http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd',
			'xmlns': 'http://www.xbrl.org/2003/linkbase',
		})
		for chart in filing.charts:
			roleRef = maker.roleRef(**{
				'roleURI': convert_role_url(chart.role, filing),
				'xlink:type': 'simple',
				'xlink:href': '{0}#{1}'.format(
						serializer.document_name('Schema'),
						chart.role
					)
			})

			linkbase.append(roleRef)
			chart.bind(serializer)
			linkbase.append(chart_serializer(chart, filing, maker))
	
	return linkbase
Example #3
0
def label_serializer(serializer):
    filing = serializer.filing
    date = filing.date
    company = filing.company
    nsmap = gen_nsmap(filing, 'Label')
    maker = ElementMaker(nsmap=nsmap)

    with xml_namespace(maker, 'link', auto_convert=True) as maker:
        linkbase = maker.linkbase(
            **{
                #find out about this
                'xsi:schemaLocation':
                'http://www.xbrl.org/2003/linkbase http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd'
            })
        labellink = maker.labelLink(
            **{
                'xlink:type': 'extended',
                'xlink:role': 'http://www.xbrl.org/2003/role/link',
            })

        locs = set([])
        for (fact, unit), context, data in filing.data_stream:
            if fact.href not in locs:
                labellink.append(make_loc(fact, maker, namespace='link'))
                locs.add(fact.href)

        facts = set([])
        for (fact, unit), context, data in filing.data_stream:
            if (fact, unit) in facts:
                continue

            facts.add((fact, unit))
            utitle = 'label_{0}_{1}'.format(fact.title, uid())
            labellink.append(make_label(fact, maker, utitle, namespace='link'))
            labellink.append(
                make_labelArc(fact, maker, utitle, namespace='link'))

        linkbase.append(labellink)

    return linkbase
Example #4
0
def instance_serializer(serializer):
    #looks like
    #<xbrli:xbrl [namespaces]>
    #<link:schemaRef to xsd document>
    #[<contexts>]
    #[<units>]
    #[<facts>]
    #</xbrli:xbrl>
    filing = serializer.filing
    date = filing.date
    company = filing.company
    nsmap = gen_nsmap(filing, 'Instance')
    maker = ElementMaker(namespace=nsmap['xbrli'], nsmap=nsmap)

    xbrl = maker.xbrl()
    with xml_namespace(maker, 'link'):
        schemaRef = maker.schemaRef(**convert_attribs(
            {
                'xlink:type': 'simple',
                'xlink:href': serializer.document_name('Schema'),
            }, nsmap))

    xbrl.append(schemaRef)

    #Loop over contexts, appending to xbrl
    for context in set(filing.contexts):
        xbrl.append(context.serialize(maker, company.cik))

    #Loop over units, appending to xbrl
    for unit in set(filing.units):
        xbrl.append(unit.serialize(maker))

    #Loop over facts appending to xbrl
    for (fact, unit), context, value in filing.data_stream:
        xbrl.append(fact.serialize(value, unit, context, maker))

    return xbrl
Example #5
0
def instance_serializer(serializer):
	#looks like
	#<xbrli:xbrl [namespaces]>
	#<link:schemaRef to xsd document>
	#[<contexts>]
	#[<units>]
	#[<facts>]
	#</xbrli:xbrl>
	filing = serializer.filing
	date = filing.date
	company = filing.company
	nsmap = gen_nsmap(filing, 'Instance')
	maker = ElementMaker(namespace=nsmap['xbrli'], nsmap=nsmap)

	xbrl = maker.xbrl()
	with xml_namespace(maker, 'link'):
		schemaRef = maker.schemaRef(**convert_attribs({
			'xlink:type': 'simple',
			'xlink:href': serializer.document_name('Schema'),
		}, nsmap))
	
	xbrl.append(schemaRef)

	#Loop over contexts, appending to xbrl
	for context in set(filing.contexts):
		xbrl.append(context.serialize(maker, company.cik))
	
	#Loop over units, appending to xbrl
	for unit in set(filing.units):
		xbrl.append(unit.serialize(maker))
	
	#Loop over facts appending to xbrl
	for (fact, unit), context, value in filing.data_stream:
		xbrl.append(fact.serialize(value, unit, context, maker))

	return xbrl
Example #6
0
def label_serializer(serializer):
	filing = serializer.filing
	date = filing.date
	company = filing.company
	nsmap = gen_nsmap(filing, 'Label')
	maker = ElementMaker(nsmap=nsmap)

	with xml_namespace(maker, 'link', auto_convert=True) as maker:
		linkbase = maker.linkbase(**{
			#find out about this
			'xsi:schemaLocation': 'http://www.xbrl.org/2003/linkbase http://www.xbrl.org/2003/xbrl-linkbase-2003-12-31.xsd'
		})
		labellink = maker.labelLink(**{
			'xlink:type': 'extended',
			'xlink:role': 'http://www.xbrl.org/2003/role/link',
		})
		
		locs = set([])
		for (fact, unit), context, data in filing.data_stream:
			if fact.href not in locs:
				labellink.append(make_loc(fact, maker, namespace='link'))
				locs.add(fact.href)

		facts = set([])
		for (fact, unit), context, data in filing.data_stream:
			if (fact, unit) in facts:
				continue
			
			facts.add((fact, unit))
			utitle = 'label_{0}_{1}'.format(fact.title, uid())
			labellink.append(make_label(fact, maker, utitle, namespace='link'))
			labellink.append(make_labelArc(fact, maker, utitle, namespace='link'))

		linkbase.append(labellink)

	return linkbase
Example #7
0
def schema_serializer(serializer):
	filing = serializer.filing
	date = filing.date
	company = filing.company
	nsmap = gen_nsmap(filing, 'Schema')
	maker = ElementMaker(namespace=nsmap['xsd'], nsmap=nsmap)
	targetNamespace = '{0}{1}'.format(filing.company.url, filing.date)
	schema = maker.schema({
		'targetNamespace': targetNamespace,
		'elementFormDefault': 'qualified',
	})

	#link bases
	annotation = maker.annotation()
	appinfo = maker.appinfo()
	bases = serializer.determine_files()
	bases.remove('Instance')
	bases.remove('Schema')
	roles = {
		'Presentation': "http://www.xbrl.org/2003/role/presentationLinkbaseRef",
		'Calculation': "http://www.xbrl.org/2003/role/calculationLinkbaseRef",
		'Label': "http://www.xbrl.org/2003/role/labelLinkbaseRef",
	}
	with xml_namespace(maker, 'link', auto_convert=True) as maker:
		for base in bases:
			appinfo.append(maker.linkbaseRef(**{
				'xlink:type': 'simple',
				'xlink:href': serializer.document_name(base),
				'xlink:role': roles[base],
				'xlink:arcrole': "http://www.w3.org/1999/xlink/properties/linkbase",
			}))

		for i, chart in enumerate(filing.charts):
			roleType = maker.roleType(**{
				'roleURI': convert_role_url(chart.role, filing),
				'id': chart.role
			})

			roleType.append(maker.definition('%04d - %s' % (10*(i+1), chart.role) ))
			roleType.append(maker.usedOn('link:presentationLink'))

			appinfo.append(roleType)
	
	annotation.append(appinfo)
	schema.append(annotation)

	#imports for the facts (probably have to derive this from facts)
	imports = {
		"http://www.xbrl.org/2003/instance": "http://www.xbrl.org/2003/xbrl-instance-2003-12-31.xsd",
		"http://xbrl.us/us-types/2009-01-31": "http://taxonomies.xbrl.us/us-gaap/2009/elts/us-types-2009-01-31.xsd",
	}
	for namespace, schemaLocation in imports.items():
		#Terrible hack to get around import being a keyword
		schema.append(maker.__getattr__('import')({
			'namespace': namespace,
			'schemaLocation': schemaLocation,
		}))
	
	#Create a custom element for everything
	fact_table = {}
	for (fact, _), _, _ in filing.data_stream:
		fact_table[fact.label] = fact
	
	facts = fact_table.values()


	with xml_namespace(maker, 'xsd', auto_convert=True) as maker:
		#append facts for the chart abstract facts
		for chart in filing.charts:
			fact = chart.loc_fact
			schema.append(maker.element(**{
				'id': fact.label,
				'name': fact.label,
				'type': 'xbrli:stringItemType',
				'substitutionGroup': 'xbrli:item',
				'nillable': 'true',
				'abstract': 'true',
				'xbrli:periodType': 'duration',
			}))
		
		#append the rest of the facts we need to define
		for fact in facts:
			schema.append(maker.element(**{
				'id': fact.label,
				'name': fact.label,
				'nillable': 'true',
				'substitutionGroup': 'xbrli:item',
				'type': 'xbrli:monetaryItemType',
				'xbrli:periodType': fact.period,
			}))
	
	return schema