An error occurred while loading the file. Please try again.
-
Tomasz Bartosiak authored020cca6b
schema_packer.py 2.75 KiB
#-*- coding:utf-8 -*-
import datetime
from collections import defaultdict
XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace'
class SchemaPacker():
def __init__(self):
# packed_subentry_id -> packed_schema_id -> (Subentry, Schema) (any that matches)
self.unique_schemas = defaultdict(lambda: defaultdict(lambda: None))
# packed_subentry -> id
self.free_subentry_id = 0
self.unique_subentry_id = defaultdict(lambda: -1)
# packed_schema -> id
self.free_schema_id = 0
self.unique_schema_id = defaultdict(lambda: -1)
# subentry -> schema -> (packed_subentry, packed_schema)
self.packed_schemas = defaultdict(lambda: defaultdict(lambda: None))
def _pack_subentry(self, subentry):
sie = subentry.inherent_sie.name
aspect = ''
if subentry.aspect is not None:
aspect = subentry.aspect.name
negativity = ''
if subentry.aspect is not None:
negativity = subentry.negativity.name
predicativity = subentry.predicativity.name
return (sie, aspect, negativity, predicativity)
def _pack_schema(self, schema):
opinion = 'unk'
if schema.opinion.key is not None:
opinion = schema.opinion.key
positions = []
for position in schema.positions.all():
positions.append(position.id)
positions = tuple(sorted(positions))
return (opinion, positions)
def _pack(self, subentry, schema):
if self.packed_schemas[subentry][schema] is None:
self.packed_schemas[subentry][schema] = (self._pack_subentry(subentry), self._pack_schema(schema))
return self.packed_schemas[subentry][schema]
def add(self, subentry, schema):
packed_subentry, packed_schema = self._pack(subentry, schema)
if self.unique_subentry_id[packed_subentry] == -1:
self.unique_subentry_id[packed_subentry] = self.free_subentry_id
self.free_subentry_id += 1
packed_subentry_id = self.unique_subentry_id[packed_subentry]
if self.unique_schema_id[packed_schema] == -1:
self.unique_schema_id[packed_schema] = self.free_schema_id
self.free_schema_id += 1
packed_schema_id = self.unique_schema_id[packed_schema]
self.unique_schemas[packed_subentry_id][packed_schema_id] = (subentry, schema)
self.packed_schemas[subentry][schema] = (packed_subentry, packed_schema)
def get_ids(self, subentry, schema):
packed_subentry, packed_schema = self.packed_schemas[subentry][schema]
return (self.unique_subentry_id[packed_subentry], self.unique_schema_id[packed_schema])