#!/usr/bin/env python3
"""
--documentation--
"""

__author__ = 'Peter Kleiweg'
__version__ = '0.1'
__date__ = '2007/07/12'

#| imports

import cgitb; cgitb.enable(format='text')

import re, sys

#| globals

#| functions

def unesc(m):
    s = m.group(1)
    if len(s) == 3:
        return '%c' % int(s, 8)
    return s

def unps(s):
    return re.sub(r'\\([0-7][0-7][0-7]|.)', unesc, s)

def Ord(r, g, b):
    a = [float(r), float(g), float(b), 0.0, 1.0]
    c1 = a[order[0] - 1]
    c2 = a[order[1] - 1]
    c3 = a[order[2] - 1]
    if c1rev: c1 = 1.0 - c1
    if c2rev: c2 = 1.0 - c2
    if c3rev: c3 = 1.0 - c3
    return (c1, c2, c3)

#| main

ccol = []
order = [1, 2, 3]
c1rev = False
c2rev = False
c3rev = False

state = 0
fp = open(sys.argv[1], encoding='iso-8859-1')
sys.stdout.buffer.write('3\n'.encode('iso-8859-1'))
for line in fp:
    if state == 0:
        if line.startswith('%%Creator: mapclust'):
            ccol = []
            state = 2
            mtype = 'clust'
        if line.startswith('%%Creator: maprgb'):
            state = 4
            mtype = 'rgb'
    elif state == 2 or state == 3:
        if line.startswith('/c'):
            try:
                a = line[2:].split()
                assert a[1] == '[' and a[5] == ']' and a[6] == 'def'
                i = int(a[0])
                r = a[2]
                g = a[3]
                b = a[4]
            except:
                pass
            else:
                while len(ccol) <= i:
                    ccol.append(None)
                ccol[i] = (r, g, b)
                state = 3
        elif state == 3:
            state = 4
    elif state == 4:
        if line.startswith('/PP '):
            state = 5
        elif line.startswith('/Order '):
            order = [int(x) for x in line.replace('[', ' ').replace(']', ' ').split()[1:4]]
        elif line.startswith('/C1reverse'):
            c1rev = (line.split()[1] == 'true')
        elif line.startswith('/C2reverse'):
            c2rev = (line.split()[1] == 'true')
        elif line.startswith('/C3reverse'):
            c3rev = (line.split()[1] == 'true')
    elif state == 5:
        if line.startswith('] def'):
            state = 6
            break
        if not line.strip().startswith('['):
            continue
        lbl = unps(line[line.index('(') + 1:line.rindex(')')])
        if not lbl:
            continue
        sys.stdout.buffer.write((lbl + '\n').encode('iso-8859-1'))
        a = line.strip()[:-1].split()
        if mtype == 'clust':
            sys.stdout.buffer.write(('%s\n%s\n%s\n' % ccol[int(a[-1][1:])]).encode('iso-8859-1'))
        else:
            sys.stdout.buffer.write(('%s\n%s\n%s\n' % Ord(a[-4], a[-3], a[-2])).encode('iso-8859-1'))

fp.close()

assert state == 6


#%%Creator: maprgb
#
#% In what order should the rgb values be used
#% 1: First input column
#% 2: Second input column
#% 3: Third input column
#% 4: This component always 0
#% 5: This component always 1
#/Order [ 1 2 3 ] def
#
#/C1reverse false def
#/C2reverse false def
#/C3reverse false def
#
#/PP [
#  [ -75.1449 39.9972 Map (Philadelphia Co.) 1 0 default 0.784353 0.569367 0.187577 Ord ]
#] def


#%%Creator: mapclust
#
#/c0 [ 0.00 0.00 1.00 ] def
#/c1 [ 0.00 1.00 0.00 ] def
#/c2 [ 0.00 1.00 1.00 ] def
#/c3 [ 1.00 0.00 0.00 ] def
#/c4 [ 1.00 0.00 1.00 ] def
#/c5 [ 1.00 1.00 0.00 ] def
#
#/PP [
#  [ -75.1449 39.9972 Map (Philadelphia Co.) 1 0 default c3 ]
#] def

