# The MIT License (MIT)
#
# Copyright (c) 2017 Stefan Graupner
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import requests
import json
import redis

import gi
gi.require_version('OParl', '0.2')

from gi.repository import OParl

from src.cache import Cache;

class Validator:
    url = ""
    system = None
    cache = None

    def __init__(self, url, redis=True):
        self.url = url

        client = OParl.Client()
        client.connect("resolve_url", Validator.resolve_url)

        if redis:
            self.cache = Cache(url)

        self.system = client.open(url)
        self.validate()

    def resolve_url(_, url):
        try:
            r = requests.get(url)
            r.raise_for_status()

            return r.text
        except Exception as e:
            return None

    def validate(self):
        pass

@@ -1156,16 +1156,50 @@
  redis%0A%0A
+from colorama import Fore, Style%0A%0A
 import g
@@ -1299,16 +1299,79 @@
 Cache;%0A%0A
+VALID_OPARL_VERSIONS = %5B%0A    %22https://schema.oparl.org/1.0%22%0A%5D%0A%0A
 class Va
@@ -1396,22 +1396,22 @@
  %22%22%0A    
-system
+client
  = None%0A
@@ -1497,24 +1497,29 @@
 rl%0A%0A        
+self.
 client = OPa
@@ -1534,24 +1534,29 @@
 t()%0A        
+self.
 client.conne
@@ -1656,47 +1656,8 @@
 l)%0A%0A
-        self.system = client.open(url)%0A
     
@@ -1881,33 +1881,892 @@
 def 
-validate(self):%0A        pass
+info(self, message, *args):%0A        # TODO: make this output controllable%0A        print(message.format(*args))%0A%0A    def ok(self, message, *args):%0A        # TODO: make this output controllable%0A        message = message.format(*args)%0A        print(%22%7B%7D%5BOK%5D %7B%7D%7B%7D%22.format(Fore.GREEN, message, Style.RESET_ALL))%0A%0A    def error(self, message, *args):%0A        # TODO: make this output controllable%0A        message = message.format(*args)%0A        print(%22%7B%7D%5BERR%5D %7B%7D%7B%7D%22.format(Fore.RED, message, Style.RESET_ALL))%0A%0A    def validate(self):%0A        self.info(%22Validating %5C%22%7B%7D%5C%22%22, self.url)%0A        system = self.client.open(self.url)%0A        version = system.get_oparl_version()%0A%0A        msg = %22Detected OParl Version %7B%7D%22%0A        if (version in VALID_OPARL_VERSIONS):%0A            self.ok(msg, version)%0A        else:%0A            self.error(msg + %22%5CnExpected one of: %7B%7D%22, version, VALID_OPARL_VERSIONS)
 %0A

Check for valid oparl version