
import re

from models import *



pattern_strip = r'"(.*)"'
prog_strip = re.compile(pattern_strip)
def strip(s):
    s1 = s.strip()
    m = prog_strip.match(s1)
    if m:
        return m[1]

    return s1

# Displays DICOM header information for an image


# , specialty ] = "CHEST" (
#   [ object, original_order ] = 1 ( 4, 4 )

#dupe key {'private_creator', 'referenced_sop_class_uid', 'Unknown element', 'referenced_sop_instance_uid', 'group_length'}

pattern_header = r'\[ (.+), (.+) \] = (.+) \('
prog_header = re.compile(pattern_header)


def dicom_header(d0, d1):
    dup_key = set()

    dict0 = {}
    for line in d0.split('\r\n'):
        cols = line.split('|')
        if len(cols) >= 5:
            key = cols[1].strip()
            if key in dict0:
                dup_key.add(key)
            dict0[key]=strip(cols[4])
        else:
            print(line)        


    dict1 = {}
    for line in d1.split('\r\n'):
        m = prog_header.search(line)
        if m:
            key1 = m[1].strip()
            key2 = m[2].strip()
            value = strip(m[3])

            if key1 not in dict1:
                dict1[key1] = {key2: value}
            else:
                if key2 in dict1[key1]:
                    dup_key.add(key2)
                dict1[key1][key2] = value
        else:
            print(line)


        
    for k in sorted(dict0.keys()):
        print(k, dict0[k])


        
    for k1 in sorted(dict1.keys()):
        for k2 in sorted(dict1[k1].keys()):
            print(k1, k2, dict1[k1][k2])
        # exit()


        # cols = line.split('|')
        # if len(cols) == 5:
        #     dict0[cols[1].strip()]=strip(cols[4])

    print(dup_key)
    # print(dict1)
    exit()

    for k in sorted(d.keys()):
        print(k, d[k])



    exit()


session = Session()

for series in session.query(Series):
    d0 = series.document0
    d1 = series.document1
    print(dicom_header(d0, d1))
    exit()
