# Path: website/models.py
# class NL(models.Model):
#     """
#     Natural language command.
#     """
#     str = models.TextField(primary_key=True)
# 
# class Command(models.Model):
#     """
#     Command line.
#     """
#     str = models.TextField(primary_key=True)
#     template = models.TextField(default='')
#     language = models.TextField(default='bash')
#     tags = models.ManyToManyField('Tag')
# 
# class Tag(models.Model):
#     """
#     Tag.
#     """
#     str = models.TextField(primary_key=True)
#     commands = models.ManyToManyField('Command')
#     annotations = models.ManyToManyField('Annotation')
# 
# class URL(models.Model):
#     """
#     URL.
# 
#     :member str: url address.
#     :member html_content: snapshot of the URL content at the time of annotation.
#     :member commands: commands in the URL (automatically extracted)
#     :member tags: tags of the URL (assigned based on user annotations)
#     """
#     str = models.TextField(primary_key=True)
#     html_content = models.TextField(default='')
#     commands = models.ManyToManyField('Command')
#     tags = models.ManyToManyField('Tag')


import os, sys
import socket
import ssl
import time
import urllib
from django.core.exceptions import ObjectDoesNotExist
from django.http import JsonResponse
from website.models import NL, Command, Tag, URL
from bashlint import data_tools
    except socket.timeout:
        print("Error: extract_text_from_url() socket.timeout")
        # return "", randomstr(180)
        return None, None
    except ssl.SSLError:
        print("Error: extract_text_from_url() ssl.SSLError")
        # return "", randomstr(180)
        return None, None
    return html.read()

def get_nl(nl_str):
    nl, _ = NL.objects.get_or_create(str=nl_str.strip())
    return nl

def get_command(command_str):
    command_str=command_str.strip()
    if Command.objects.filter(str=command_str).exists():
        cmd = Command.objects.get(str=command_str)
    else:
        cmd = Command.objects.create(str=command_str)
        ast = data_tools.bash_parser(command_str)
        for utility in data_tools.get_utilities(ast):
            cmd.tags.add(get_tag(utility))    
        template = data_tools.ast2template(
	        ast, loose_constraints=True)
        cmd.template = template
        cmd.save()
    return cmd

def get_tag(tag_str):

# Path: website/models.py
# class NL(models.Model):
#     """
#     Natural language command.
#     """
#     str = models.TextField(primary_key=True)
# 
# class Command(models.Model):
#     """
#     Command line.
#     """
#     str = models.TextField(primary_key=True)
#     template = models.TextField(default='')
#     language = models.TextField(default='bash')
#     tags = models.ManyToManyField('Tag')
# 
# class Tag(models.Model):
#     """
#     Tag.
#     """
#     str = models.TextField(primary_key=True)
#     commands = models.ManyToManyField('Command')
#     annotations = models.ManyToManyField('Annotation')
# 
# class URL(models.Model):
#     """
#     URL.
# 
#     :member str: url address.
#     :member html_content: snapshot of the URL content at the time of annotation.
#     :member commands: commands in the URL (automatically extracted)
#     :member tags: tags of the URL (assigned based on user annotations)
#     """
#     str = models.TextField(primary_key=True)
#     html_content = models.TextField(default='')
#     commands = models.ManyToManyField('Command')
#     tags = models.ManyToManyField('Tag')


# Path: website/utils.py
import os, sys
import socket
import ssl
import time
import urllib
from django.core.exceptions import ObjectDoesNotExist
from django.http import JsonResponse
from website.models import NL, Command, Tag, URL
from bashlint import data_tools

    except socket.timeout:
        print("Error: extract_text_from_url() socket.timeout")
        # return "", randomstr(180)
        return None, None
    except ssl.SSLError:
        print("Error: extract_text_from_url() ssl.SSLError")
        # return "", randomstr(180)
        return None, None
    return html.read()

def get_nl(nl_str):
    nl, _ = NL.objects.get_or_create(str=nl_str.strip())
    return nl

def get_command(command_str):
    command_str=command_str.strip()
    if Command.objects.filter(str=command_str).exists():
        cmd = Command.objects.get(str=command_str)
    else:
        cmd = Command.objects.create(str=command_str)
        ast = data_tools.bash_parser(command_str)
        for utility in data_tools.get_utilities(ast):
            cmd.tags.add(get_tag(utility))    
        template = data_tools.ast2template(
	        ast, loose_constraints=True)
        cmd.template = template
        cmd.save()
    return cmd

def get_tag(tag_str):

    tag, _ = Tag.objects.get_or_create(str=tag_str.strip())