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.

app.py 1.6 KiB

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