# Path: prometapi/compat.py
# def zlib_encode(data):
# 
# Path: prometapi/encoders.py
# def encrypt(e, t):
#     r = str_to_longs(e)
#     a = str_to_longs(t)
#     if len(r) <= 1:
#         r = [r[0], 0]
#     
#     l = len(r)
#     g = r[l - 1]
#     s = r[0]
#     i = 2654435769
#     c = math.floor(6 + 52 / l)
#     u = 0
#     
#     while c > 0:
#         u += i
#         n = rshift(u, 2) & 3
#         
#         p = 0
#         while l > p:
#             s = r[(p + 1) % l]
#             o = (rshift(g, 5) ^ ((s << 2) & 0xffffffff) ) + (rshift(s, 3) ^ ((g << 4) & 0xffffffff)) ^ (u ^ s) + (a[3 & p ^ n] ^ g)
#             g = r[p] = (r[p] + o) & 0xffffffff
#             
#             p += 1
#         c -= 1
#     f = longs_to_str(r)
#     return f
# 
# Path: prometapi/geoprocessing.py
# def get_coordtransform():
# 	from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform
# 	sl = SpatialReference('EPSG:3787')
# 	sl.import_wkt(sl_wkt)
# 	wgs = SpatialReference('EPSG:4326')
# 	trans = CoordTransform(sl, wgs)
# 	return trans
# 
# def find_coordtransform(srid):
# 	if srid == u'EPSG:3787':
# 		return get_coordtransform()
# 	elif srid == u'EPSG:3912':
# 		from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform
# 		sl = SpatialReference('EPSG:3912')
# 		wgs = SpatialReference('EPSG:4326')
# 		trans = CoordTransform(sl, wgs)
# 		return trans
# 	else:
# 		raise ValueError("Couldn't find transform: %r" % srid)


import datetime
import re
import os
import uuid
import simplejson
import time
import urllib
import requests
import subprocess
import xml.etree.ElementTree as ET
from prometapi.compat import urlopen, unicode_type, unichr_cast
from calendar import timegm
from django.db import models
from django.contrib.gis.geos import GEOSGeometry
from django.utils._os import safe_join
from django.utils.timezone import make_aware, utc
from django.conf import settings
from prometapi.encoders import encrypt
from prometapi.geoprocessing import get_coordtransform, find_coordtransform
    from django.db import connection
def fetch_parkirisca_lpt():
    return fetch(URL_LPT_PARKIRISCA), fetch(URL_LPT_OCCUPANCY)

def parse_parkirisca_lpt(parkirisca_data, occupancy_data):

    # silly xmlns
    parkirisca_data = parkirisca_data.replace(' xmlns="http://www.tempuri.org/dsP.xsd"', '')

    parkirisca = ET.fromstring(parkirisca_data)
    occupancy = ET.fromstring(occupancy_data)

    zattrs = ['ID_ParkiriscaNC', 'Cas', 'P_kratkotrajniki']
    zattrs.sort()
    zasedenost = {}

    for e in occupancy.findall('./ZASEDENOST'):
        zdict = dict([(i.tag, i.text) for i in e.getchildren()])
        assert list(sorted(zdict.keys())) == zattrs, 'occupancy.xml attributes changed!'
        zdict['Cas_timestamp'] = int(time.mktime(datetime.datetime.strptime(zdict['Cas'], '%Y-%m-%d %H:%M:%S').timetuple()))
        for k, v in zdict.items():
            if isinstance(v, str) and re.match('^\d+$', v):
                zdict[k] = int(v)
        zasedenost[zdict['ID_ParkiriscaNC']] = zdict

    assert len(zasedenost) > 1, 'Ni elementov v occupancy.xml?!'

    json = {'Parkirisca': [],}

    attrs = ['A_St_Mest', 'Cena_dan_Eur', 'Cena_mesecna_Eur', 'Cena_splosno', 'Cena_ura_Eur', 'ID_Parkirisca', 'ID_ParkiriscaNC', 'Ime', 'Invalidi_St_mest', 'KoordinataX', 'KoordinataY', 'Opis', 'St_mest', 'Tip_parkirisca', 'U_delovnik', 'U_sobota', 'U_splosno', 'Upravljalec']
    attrs.sort()

# Path: prometapi/compat.py
# def zlib_encode(data):
# 
# Path: prometapi/encoders.py
# def encrypt(e, t):
#     r = str_to_longs(e)
#     a = str_to_longs(t)
#     if len(r) <= 1:
#         r = [r[0], 0]
#     
#     l = len(r)
#     g = r[l - 1]
#     s = r[0]
#     i = 2654435769
#     c = math.floor(6 + 52 / l)
#     u = 0
#     
#     while c > 0:
#         u += i
#         n = rshift(u, 2) & 3
#         
#         p = 0
#         while l > p:
#             s = r[(p + 1) % l]
#             o = (rshift(g, 5) ^ ((s << 2) & 0xffffffff) ) + (rshift(s, 3) ^ ((g << 4) & 0xffffffff)) ^ (u ^ s) + (a[3 & p ^ n] ^ g)
#             g = r[p] = (r[p] + o) & 0xffffffff
#             
#             p += 1
#         c -= 1
#     f = longs_to_str(r)
#     return f
# 
# Path: prometapi/geoprocessing.py
# def get_coordtransform():
# 	from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform
# 	sl = SpatialReference('EPSG:3787')
# 	sl.import_wkt(sl_wkt)
# 	wgs = SpatialReference('EPSG:4326')
# 	trans = CoordTransform(sl, wgs)
# 	return trans
# 
# def find_coordtransform(srid):
# 	if srid == u'EPSG:3787':
# 		return get_coordtransform()
# 	elif srid == u'EPSG:3912':
# 		from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform
# 		sl = SpatialReference('EPSG:3912')
# 		wgs = SpatialReference('EPSG:4326')
# 		trans = CoordTransform(sl, wgs)
# 		return trans
# 	else:
# 		raise ValueError("Couldn't find transform: %r" % srid)


# Path: prometapi/models.py
import datetime
import re
import os
import uuid
import simplejson
import time
import urllib
import requests
import subprocess
import xml.etree.ElementTree as ET
from prometapi.compat import urlopen, unicode_type, unichr_cast
from calendar import timegm
from django.db import models
from django.contrib.gis.geos import GEOSGeometry
from django.utils._os import safe_join
from django.utils.timezone import make_aware, utc
from django.conf import settings
from prometapi.encoders import encrypt
from prometapi.geoprocessing import get_coordtransform, find_coordtransform
    from django.db import connection

def fetch_parkirisca_lpt():
    return fetch(URL_LPT_PARKIRISCA), fetch(URL_LPT_OCCUPANCY)

def parse_parkirisca_lpt(parkirisca_data, occupancy_data):

    # silly xmlns
    parkirisca_data = parkirisca_data.replace(' xmlns="http://www.tempuri.org/dsP.xsd"', '')

    parkirisca = ET.fromstring(parkirisca_data)
    occupancy = ET.fromstring(occupancy_data)

    zattrs = ['ID_ParkiriscaNC', 'Cas', 'P_kratkotrajniki']
    zattrs.sort()
    zasedenost = {}

    for e in occupancy.findall('./ZASEDENOST'):
        zdict = dict([(i.tag, i.text) for i in e.getchildren()])
        assert list(sorted(zdict.keys())) == zattrs, 'occupancy.xml attributes changed!'
        zdict['Cas_timestamp'] = int(time.mktime(datetime.datetime.strptime(zdict['Cas'], '%Y-%m-%d %H:%M:%S').timetuple()))
        for k, v in zdict.items():
            if isinstance(v, str) and re.match('^\d+$', v):
                zdict[k] = int(v)
        zasedenost[zdict['ID_ParkiriscaNC']] = zdict

    assert len(zasedenost) > 1, 'Ni elementov v occupancy.xml?!'

    json = {'Parkirisca': [],}

    attrs = ['A_St_Mest', 'Cena_dan_Eur', 'Cena_mesecna_Eur', 'Cena_splosno', 'Cena_ura_Eur', 'ID_Parkirisca', 'ID_ParkiriscaNC', 'Ime', 'Invalidi_St_mest', 'KoordinataX', 'KoordinataY', 'Opis', 'St_mest', 'Tip_parkirisca', 'U_delovnik', 'U_sobota', 'U_splosno', 'Upravljalec']
    attrs.sort()

    geotransform = get_coordtransform()