# -*- coding: iso-8859-1 -*-
"""
Sadly, the files from Tübingen come without headers.  This
core adds the uploaded metadata to them.  Yes, this is braindead.
"""

import gzip
import io
import os
import re
import urllib.parse

from gavo import base
from gavo import utils
from gavo.svcs import core

_ACCPATH_RE = re.compile("Access.Reference=([^\n]*)")

class Core(core.Core):
	inputTableXML = """<inputTable id="inFields">
			<inputKey name="accref" type="text" required="True"
				description="Accref of the data within the SSAP table."/>
			<inputKey name="dm" type="text" description="Data model to
				generate the table for (sdm or sed)">sdm</inputKey>
		</inputTable>"""
	
	def run(self, service, inputTable, queryMeta):
		# The accref is the inputsDir-relative path of the meta file
		# We get the URL of the actual (header-less) data from there.
		cacheDir = os.path.join(self.parent.rd.resdir, "txcache")
		metaPath = os.path.join(base.getConfig("inputsDir"),
			urllib.parse.unquote(inputTable.getParam("accref")))

		with open(metaPath, "r", encoding="utf-8") as f:
			metaTx = f.read().replace("\r", "")
		dataURL = _ACCPATH_RE.search(metaTx).group(1)
		# add "*" as surrogate comment char to all meta lines:
		metaTx = "\n".join(l and "* "+l for l in metaTx.split("\n"))

		dataTx = utils.getWithCache(dataURL, cacheDir)
		if dataTx.startswith(b"\x1f\x8b"):
			dataTx = gzip.GzipFile(fileobj=io.BytesIO(dataTx)).read()

		# Hm -- we shouldn't be serving text/plain with lf-only
		# But I'm not wild about changing that, either
		return "text/plain", metaTx.encode("utf-8")+dataTx.replace(b"\r", b"")
