def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        # Print the request line
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, 'blue')

        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Parameters: ", arguments)

        context = {}
        if path_name == "/":
            context["list_genes"] = list(HUMAN_GENES.keys())
            contents = su.read_template_html_file("./html/index.html").render(
                context=context)
        elif path_name == "/listSpecies":
            contents = su.listSpecies(arguments)
            if "check" in arguments.keys():
                print('The check bottom was used')
            else:
                print('The checkbox was not used')

        elif path_name == "/karyotype":
            contents = su.karyotype(arguments, path_name)
        elif path_name == "/chromosomeLength":
            contents = su.karyotype(arguments, path_name)
        elif path_name == "/geneSeq":
            contents = su.gene_seq(arguments, path_name, HUMAN_GENES)
        elif path_name == "/geneInfo":
            contents = su.gene_seq(arguments, path_name, HUMAN_GENES)
        elif path_name == "/geneCalc":
            contents = su.gene_seq(arguments, path_name, HUMAN_GENES)

        else:
            contents = su.read_template_html_file(
                "html/errors/error.html").render()

        # Generating the response message
        self.send_response(200)  # -- Status line: OK!

        # Define the content-type header:
        self.send_header('Content-Type',
                         'text/html')  # Si no ponemos html saldrá texto!!
        self.send_header('Content-Length', len(contents.encode()))

        # The header is finished
        self.end_headers()

        # Send the response message
        self.wfile.write(contents.encode())

        return
Beispiel #2
0
    def do_GET(self):
        termcolor.cprint(self.requestline, 'green')
        termcolor.cprint(self.path, "blue")

        url = urlparse(self.path)
        endpoint = url.path
        parameters = parse_qs(url.query)
        print("Endpoint: ", endpoint)
        print("Parameters: ", parameters)

        has_error = False
        contents = ""
        status = 400  # BAD_REQUEST
        if endpoint in ENDPOINTS:
            if endpoint == "/":
                status = 200
                contents = Path("./html/index.html").read_text()
            elif endpoint == "/listSpecies":
                if len(parameters) == 0:
                    status, contents = su.list_species()
                elif len(parameters) == 1:
                    try:
                        limit = int(parameters['limit'][0])
                        status, contents = su.list_species(limit)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
            elif endpoint == "/karyotype":
                if len(parameters) == 1:
                    try:
                        specie = parameters['specie'][0]
                        status, contents = su.karyotype(specie)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
            elif endpoint == "/chromosomeLength":
                if len(parameters) == 2:
                    try:
                        specie = parameters['specie'][0]
                        chromo = parameters['chromo'][0]
                        status, contents = su.chromosome_length(specie, chromo)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
            elif endpoint == "/geneSeq":
                if len(parameters) == 1:
                    try:
                        gene = parameters['gene'][0]
                        status, contents = su.gene_seq(gene)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
            elif endpoint == "/geneInfo":
                if len(parameters) == 1:
                    try:
                        gene = parameters['gene'][0]
                        status, contents = su.gene_info(gene)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
            elif endpoint == "/geneCalc":
                if len(parameters) == 1:
                    try:
                        gene = parameters['gene'][0]
                        status, contents = su.gene_calc(gene)
                    except (KeyError, ValueError):
                        has_error = True
                else:
                    has_error = True
        else:
            has_error = True

        if has_error:
            contents = Path("./html/error.html").read_text()

        self.send_response(status)
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', str(len(contents.encode())))
        self.end_headers()
        self.wfile.write(contents.encode())
        print(contents)
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""

        o = urlparse(self.path)
        # get the endpoints
        path_name = o.path
        arguments = parse_qs(o.query)
        print("Resource requested: ", path_name)
        print("Parameters: ", arguments)
        server = "https://rest.ensembl.org"
        server_info = "https://grch37.rest.ensembl.org"

        # main page
        if path_name == "/":
            contents, content_type = su.read_html("./html/form.html", "")
            error_code = 200
        # list species
        elif path_name == "/listSpecies":
            ext = "/info/species?"
            html = "./html/list_of_species.html"
            contents, content_type, error_code = su.listSpecies(
                server, ext, arguments, html)

        # karyotype
        elif path_name == "/karyotype":
            ext = "/info/assembly/"
            html = "./html/karyotype.html"
            contents, content_type, error_code = su.karyotype(
                server, ext, arguments, html)

        # chromosomeLength
        elif path_name == "/chromosomeLength":

            ext = "/info/assembly/"
            html = "./html/len_chromo.html"
            contents, content_type, error_code = su.chromosomeLength(
                server, ext, arguments, html)

        # geneSeq
        elif path_name == "/geneSeq":
            ext = "/sequence/id/"
            html = "./html/sequence_gene.html"
            contents, content_type, error_code = su.geneSeq(
                server, ext, arguments, html)

        # geneInfo
        elif path_name == "/geneInfo":
            ext = "/lookup/id/"
            html = "./html/info_gene.html"
            contents, content_type, error_code = su.geneInfo(
                server_info, ext, arguments, html)

        # geneCalc
        elif path_name == "/geneCalc":

            ext = "/lookup/id/"
            html = "./html/calc_gene.html"
            contents, content_type, error_code = su.geneCalc(
                server_info, ext, arguments, html)

        else:
            contents, content_type = su.read_html("./html/Error.html",
                                                  "Endpoint not valid")
            error_code = 404

        # response message
        self.send_response(error_code)  # -- Status line: OK!

        # Define the content-type header:
        self.send_header('Content-Type', content_type)
        # print(contents) (used to print output in report-advanced)
        self.send_header('Content-Length', str(len(contents.encode())))

        # The header is finished
        self.end_headers()

        # response message
        self.wfile.write(contents.encode())

        return
    def do_GET(self):
        """This method is called whenever the client invokes the GET method
        in the HTTP protocol request"""
        o = urlparse(self.path)
        path_name = o.path
        arguments = parse_qs(o.query)
        print("RESOURCE REQUESTED: ", path_name)
        print("PARAMETERS: ", arguments)
        context = {}
        if path_name == "/":
            contents = server_utils.read_template_html_file(
                "./html/index.html").render(context=context)
        elif path_name == "/listSpecies":
            dict_s = server_utils.get_data("/info/species")
            if len(arguments) == 1:
                try:
                    limit = arguments["limit"][0]

                except KeyError:
                    limit = len(dict_s["species"])
                contents = server_utils.list_species(dict_s, limit)

            else:
                contents = server_utils.read_template_html_file(
                    "./html/ERROR.html").render()
        elif path_name == "/karyotype":
            if len(arguments) == 1:
                try:
                    specie_name = arguments["specie"][0]
                    dict_k = server_utils.get_data("info/assembly" + "/" +
                                                   specie_name)
                    contents = server_utils.karyotype(dict_k, specie_name)
                except KeyError:
                    contents = server_utils.read_template_html_file(
                        "./html/ERROR.html").render()
                except ValueError:
                    contents = server_utils.read_template_html_file(
                        "./html/ERROR.html").render()
            else:
                contents = server_utils.read_template_html_file(
                    "./html/ERROR.html").render()

        elif path_name == "/chromosomeLength":
            if len(arguments) == 2:
                try:
                    specie = arguments["specie"][0]
                    chromo = arguments["chromo"][0]
                    dict_c = server_utils.get_data("/info/assembly" + "/" +
                                                   specie + "/" + chromo)
                    contents = server_utils.length_chromosome(
                        dict_c, specie, chromo)
                except KeyError:
                    contents = server_utils.read_template_html_file(
                        "./html/ERROR.html").render()
                except ValueError:
                    contents = server_utils.read_template_html_file(
                        "./html/ERROR.html").render()
            else:
                contents = server_utils.read_template_html_file(
                    "./html/ERROR.html").render()

        elif path_name == "/geneSeq":
            if len(arguments) == 1:
                try:
                    gene = arguments["gene"][0]
                    dict_seq = server_utils.get_data(
                        "/sequence/id/" + server_utils.genes_dict[gene])
                    contents = server_utils.sequence(dict_seq, gene)
                except KeyError:
                    contents = server_utils.read_template_html_file(
                        "./html/ERROR.html").render()
                except ValueError:
                    contents = server_utils.read_template_html_file(
                        "./html/ERROR.html").render()
            else:
                contents = server_utils.read_template_html_file(
                    "./html/ERROR.html").render()
        elif path_name == "/geneInfo":
            if len(arguments) == 1:
                try:
                    gene = arguments["gene"][0]
                    dict_info = server_utils.get_data(
                        "/sequence/id/" + server_utils.genes_dict[gene])
                    contents = server_utils.infoseq(dict_info, gene)
                except KeyError:
                    contents = server_utils.read_template_html_file(
                        "./html/ERROR.html").render()
                except ValueError:
                    contents = server_utils.read_template_html_file(
                        "./html/ERROR.html").render()
            else:
                contents = server_utils.read_template_html_file(
                    "./html/ERROR.html").render()
        elif path_name == "/geneCalc":
            if len(arguments) == 1:
                try:
                    gene = arguments["gene"][0]
                    dict_calc = server_utils.get_data(
                        "/sequence/id/" + server_utils.genes_dict[gene])
                    contents = server_utils.calcseq(dict_calc, gene)
                except KeyError:
                    contents = server_utils.read_template_html_file(
                        "./html/ERROR.html").render()
                except ValueError:
                    contents = server_utils.read_template_html_file(
                        "./html/ERROR.html").render()
            else:
                contents = server_utils.read_template_html_file(
                    "./html/ERROR.html").render()

        else:
            contents = server_utils.read_template_html_file(
                "./html/ERROR.html").render()

        self.send_response(200)
        length = len(contents.encode())
        # Define the content-type header:
        self.send_header('Content-Type', 'text/html')
        self.send_header('Content-Length', str(length))
        # The header is finished
        self.end_headers()
        # Send the response message
        self.wfile.write(str.encode(contents))
        return