A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

10 jaren geleden
10 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. Module to contain all the project's Flask server plumbing.
  3. """
  4. from json import dumps
  5. from flask import Flask, make_response, render_template, request
  6. from bitshift import assets
  7. from bitshift.database import Database
  8. from bitshift.languages import LANGS
  9. from bitshift.query import parse_query, QueryParseException
  10. app = Flask(__name__, static_folder="static", static_url_path="")
  11. app.config.from_object("bitshift.config")
  12. app_env = app.jinja_env
  13. app_env.line_statement_prefix = "="
  14. app_env.globals.update(assets=assets)
  15. database = Database()
  16. @app.route("/")
  17. def index():
  18. return render_template("index.html", autocomplete_languages=LANGS)
  19. @app.route("/search.json")
  20. def search():
  21. def reply(json):
  22. resp = make_response(dumps(json))
  23. resp.mimetype = "application/json"
  24. return resp
  25. query, page = request.args.get("q"), request.args.get("p", 1)
  26. if not query:
  27. return reply({"error": "No query given"})
  28. try:
  29. tree = parse_query(query)
  30. except QueryParseException as exc:
  31. return reply({"error": exc.args[0]})
  32. try:
  33. page = int(page)
  34. except ValueError:
  35. return reply({"error": u"Invalid page number: %s" % page})
  36. count, codelets = database.search(tree, page)
  37. results = [clt.serialize() for clt in codelets]
  38. return reply({"count": count, "results": results})
  39. @app.route("/about")
  40. def about():
  41. return render_template("about.html")
  42. @app.route("/developers")
  43. def developers():
  44. return render_template("developers.html")
  45. @app.route("/docs")
  46. def docs():
  47. return render_template("docs.html")
  48. @app.errorhandler(404)
  49. def error404(error):
  50. return render_template("error404.html"), 404
  51. if __name__ == "__main__":
  52. app.run(debug=True)