# Path: api/tool/tag2link.py
# class tag2link:
# 
#     def __init__(self, rulesFiles):
#         parser = xml.sax.make_parser()
#         handler = Exact()
#         parser.setContentHandler(handler)
#         parser.parse(open(rulesFiles, "r", encoding="utf-8"))
#         self.rules = handler.rules
#         self.all = re.compile('.*')
# 
#     def checkTags(self, tags):
#       try:
#         urls = {}
#         for rule in self.rules:
#             valid = True
#             id = {}
#             for condition in rule['conditions']:
#                 match = False
#                 for key in tags.keys():
#                     kmatch = condition['k'].match(key)
#                     if kmatch:
#                         tag = key
#                         value = tags[key]
#                         id[condition['id']] = {'k':kmatch}
#                         if condition['v'] == None:
#                             vmatch = self.all.match(tags[key])
#                             id[condition['id']]['v'] = vmatch
#                             match = True
#                             break
#                         else:
#                             vmatch = condition['v'].match(tags[key])
#                             if vmatch:
#                                 id[condition['id']]['v'] = vmatch
#                                 match = True
#                                 break
#                 if not match:
#                     valid = False
#                     break
#             if valid:
#                 replace = []
#                 for sub in rule['link']['subs']:
#                     for v in sub:
#                         if isinstance(v, str):
#                             replace.append(v)
#                             break
#                         else:
#                             val = id[v[0]][v[1]].group(v[2])
#                             if val:
#                                 replace.append(val)
#                                 break
#                 ret = rule['link']['url'] % tuple(replace)
#                 if not '://' in ret:
#                     ret = 'http://' + ret
#                 urls[id[rule['link']['subs'][0][0][0]]['k'].group(0)] = ret
#         return urls
#       except:
#         return {}


import os
from .tool import tag2link
from modules.query import fixes_default
## This program is free software: you can redistribute it and/or modify  ##
## it under the terms of the GNU General Public License as published by  ##
## the Free Software Foundation, either version 3 of the License, or     ##
## (at your option) any later version.                                   ##
##                                                                       ##
## This program is distributed in the hope that it will be useful,       ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of        ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ##
## GNU General Public License for more details.                          ##
##                                                                       ##
## You should have received a copy of the GNU General Public License     ##
## along with this program.  If not, see <http://www.gnu.org/licenses/>. ##
##                                                                       ##
###########################################################################


t2l = tag2link.tag2link(os.path.dirname(os.path.realpath(__file__)) + "/tool/tag2link_sources.xml")


def _get(db, err_id=None, uuid=None):
    columns_marker = [
        "markers.item",
        "markers.source_id", "markers.class",
        "elems", "fixes",
        "lat::float", "lon::float",
        "title", "subtitle", "updates_last.timestamp",
        "detail", "fix", "trap", "example", "source AS source_code", "resource",
    ]

    if err_id:

# Path: api/tool/tag2link.py
# class tag2link:
# 
#     def __init__(self, rulesFiles):
#         parser = xml.sax.make_parser()
#         handler = Exact()
#         parser.setContentHandler(handler)
#         parser.parse(open(rulesFiles, "r", encoding="utf-8"))
#         self.rules = handler.rules
#         self.all = re.compile('.*')
# 
#     def checkTags(self, tags):
#       try:
#         urls = {}
#         for rule in self.rules:
#             valid = True
#             id = {}
#             for condition in rule['conditions']:
#                 match = False
#                 for key in tags.keys():
#                     kmatch = condition['k'].match(key)
#                     if kmatch:
#                         tag = key
#                         value = tags[key]
#                         id[condition['id']] = {'k':kmatch}
#                         if condition['v'] == None:
#                             vmatch = self.all.match(tags[key])
#                             id[condition['id']]['v'] = vmatch
#                             match = True
#                             break
#                         else:
#                             vmatch = condition['v'].match(tags[key])
#                             if vmatch:
#                                 id[condition['id']]['v'] = vmatch
#                                 match = True
#                                 break
#                 if not match:
#                     valid = False
#                     break
#             if valid:
#                 replace = []
#                 for sub in rule['link']['subs']:
#                     for v in sub:
#                         if isinstance(v, str):
#                             replace.append(v)
#                             break
#                         else:
#                             val = id[v[0]][v[1]].group(v[2])
#                             if val:
#                                 replace.append(val)
#                                 break
#                 ret = rule['link']['url'] % tuple(replace)
#                 if not '://' in ret:
#                     ret = 'http://' + ret
#                 urls[id[rule['link']['subs'][0][0][0]]['k'].group(0)] = ret
#         return urls
#       except:
#         return {}


# Path: api/issue_utils.py
import os
from .tool import tag2link
from modules.query import fixes_default

## This program is free software: you can redistribute it and/or modify  ##
## it under the terms of the GNU General Public License as published by  ##
## the Free Software Foundation, either version 3 of the License, or     ##
## (at your option) any later version.                                   ##
##                                                                       ##
## This program is distributed in the hope that it will be useful,       ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of        ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ##
## GNU General Public License for more details.                          ##
##                                                                       ##
## You should have received a copy of the GNU General Public License     ##
## along with this program.  If not, see <http://www.gnu.org/licenses/>. ##
##                                                                       ##
###########################################################################


t2l = tag2link.tag2link(os.path.dirname(os.path.realpath(__file__)) + "/tool/tag2link_sources.xml")


def _get(db, err_id=None, uuid=None):
    columns_marker = [
        "markers.item",
        "markers.source_id", "markers.class",
        "elems", "fixes",
        "lat::float", "lon::float",
        "title", "subtitle", "updates_last.timestamp",
        "detail", "fix", "trap", "example", "source AS source_code", "resource",
    ]

    if err_id:

        sql = "SELECT uuid_to_bigint(markers.uuid) AS id, " + ",".join(columns_marker) + """