<commit_before>#!/usr/bin/env python
from flask import Flask, Response, request, render_template
from flask.ext.cors import CORS
import rdflib
import argparse

# command line parameters
parser = argparse.ArgumentParser()
parser.add_argument('file')
parser.add_argument('-i', '--input', default='guess', choices=[
        'html',
        'hturtle',
        'mdata',
        'microdata',
        'n3',
        'nquads',
        'nt',
        'rdfa',
        'rdfa1.0',
        'rdfa1.1',
        'trix',
        'turtle',
        'xml'
    ], help='Optional input format')

args = parser.parse_args()

# new graph
g = rdflib.Graph()

# parse file into graph
with open(args.file, 'r') as fi:
    if args.input == 'guess':
        fo = rdflib.util.guess_format(args.file)
    else:
        fo = args.input

    g.parse(fi, format=fo)

# set up a micro service using flash
app = Flask(__name__, static_url_path='')
cors = CORS(app)

@app.route("/sparql", methods=['GET'])
def sparql():
    if 'query' in request.args:
        query = request.args['query']
        qres = g.query(query)
        return Response(qres.serialize(format='json').decode('utf-8'), content_type="application/json")
    else:
        return render_template('sparql.html', src=args.file, port=request.host)

if __name__ == "__main__":
    app.run()
<commit_msg>enable optional -p/--port argument<commit_after>#!/usr/bin/env python
from flask import Flask, Response, request, render_template
from flask.ext.cors import CORS
import rdflib
import argparse

# command line parameters
parser = argparse.ArgumentParser()
parser.add_argument('file')
parser.add_argument('-p', '--port', default=5000, type=int)
parser.add_argument('-i', '--input', default='guess', choices=[
        'html',
        'hturtle',
        'mdata',
        'microdata',
        'n3',
        'nquads',
        'nt',
        'rdfa',
        'rdfa1.0',
        'rdfa1.1',
        'trix',
        'turtle',
        'xml'
    ], help='Optional input format')

args = parser.parse_args()

# new graph
g = rdflib.Graph()

# parse file into graph
with open(args.file, 'r') as fi:
    if args.input == 'guess':
        fo = rdflib.util.guess_format(args.file)
    else:
        fo = args.input

    g.parse(fi, format=fo)

# set up a micro service using flash
app = Flask(__name__, static_url_path='')
cors = CORS(app)

@app.route("/sparql", methods=['GET'])
def sparql():
    if 'query' in request.args:
        query = request.args['query']
        qres = g.query(query)
        return Response(qres.serialize(format='json').decode('utf-8'), content_type="application/json")
    else:
        return render_template('sparql.html', src=args.file, port=request.host)

if __name__ == "__main__":
    app.run(port=args.port)

{'fraction_non_alphanumeric': 0.15510352342898656, 'fraction_numerical': 0.005811841627315656, 'mean_word_length': 2.9174964438122335, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3963846', 'n_tokens_mistral': 959, 'n_tokens_neox': 931, 'n_words': 215}