diff options
Diffstat (limited to 'lib/api')
| -rw-r--r-- | lib/api/__init__.py | 6 | ||||
| -rw-r--r-- | lib/api/lang.py | 11 | ||||
| -rw-r--r-- | lib/api/page_specs.py | 36 | ||||
| -rw-r--r-- | lib/api/server.py | 121 | ||||
| -rw-r--r-- | lib/api/simulator.py | 8 | ||||
| -rw-r--r-- | lib/api/stitch_plan.py | 29 |
6 files changed, 0 insertions, 211 deletions
diff --git a/lib/api/__init__.py b/lib/api/__init__.py deleted file mode 100644 index 35e411a7..00000000 --- a/lib/api/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# Authors: see git history -# -# Copyright (c) 2010 Authors -# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. - -from .server import APIServer
\ No newline at end of file diff --git a/lib/api/lang.py b/lib/api/lang.py deleted file mode 100644 index 73c190f4..00000000 --- a/lib/api/lang.py +++ /dev/null @@ -1,11 +0,0 @@ -import os - -from flask import Blueprint, jsonify - -languages = Blueprint('languages', __name__) - - -@languages.route('') -def get_lang(): - languages = dict(os.environ) - return jsonify(languages) diff --git a/lib/api/page_specs.py b/lib/api/page_specs.py deleted file mode 100644 index 8d3aee49..00000000 --- a/lib/api/page_specs.py +++ /dev/null @@ -1,36 +0,0 @@ -# Authors: see git history -# -# Copyright (c) 2010 Authors -# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. - -from flask import Blueprint, g, jsonify - -page_specs = Blueprint('page_specs', __name__) - - -@page_specs.route('') -def get_page_specs(): - svg = g.extension.document.getroot() - width = svg.get('width', 0) - height = svg.get('height', 0) - pagecolor = "white" - deskcolor = "white" - bordercolor = "black" - showpageshadow = True - - namedview = svg.namedview - if namedview is not None: - pagecolor = namedview.get('pagecolor', pagecolor) - deskcolor = namedview.get('inkscape:deskcolor', deskcolor) - bordercolor = namedview.get('bordercolor', bordercolor) - showpageshadow = namedview.get('inkscape:showpageshadow', showpageshadow) - - page_specs = { - "width": width, - "height": height, - "pagecolor": pagecolor, - "deskcolor": deskcolor, - "bordercolor": bordercolor, - "showpageshadow": showpageshadow - } - return jsonify(page_specs) diff --git a/lib/api/server.py b/lib/api/server.py deleted file mode 100644 index 5625d77d..00000000 --- a/lib/api/server.py +++ /dev/null @@ -1,121 +0,0 @@ -# Authors: see git history -# -# Copyright (c) 2010 Authors -# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. - -import errno -import logging -import socket -import sys -import time -from threading import Thread -from contextlib import closing - -import requests -from flask import Flask, g -from werkzeug.serving import make_server - -from ..utils.json import InkStitchJSONProvider -from .simulator import simulator -from .stitch_plan import stitch_plan -from .page_specs import page_specs -from .lang import languages -# this for electron axios -from flask_cors import CORS - - -class APIServer(Thread): - def __init__(self, *args, **kwargs): - self.extension = args[0] - Thread.__init__(self, *args[1:], **kwargs) - self.daemon = True - self.app = None - self.host = None - self.port = None - self.ready = False - - self.__setup_app() - self.flask_server = None - self.server_thread = None - - def __setup_app(self): # noqa: C901 - # Disable warning about using a development server in a production environment - cli = sys.modules['flask.cli'] - cli.show_server_banner = lambda *x: None - - self.app = Flask(__name__) - CORS(self.app) - self.app.json = InkStitchJSONProvider(self.app) - - self.app.register_blueprint(simulator, url_prefix="/simulator") - self.app.register_blueprint(stitch_plan, url_prefix="/stitch_plan") - self.app.register_blueprint(page_specs, url_prefix="/page_specs") - self.app.register_blueprint(languages, url_prefix="/languages") - - @self.app.before_request - def store_extension(): - # make the InkstitchExtension object available to the view handling - # this request - g.extension = self.extension - - @self.app.route('/ping') - def ping(): - return "pong" - - def stop(self): - self.flask_server.shutdown() - self.server_thread.join() - - def disable_logging(self): - logging.getLogger('werkzeug').setLevel(logging.ERROR) - - # https://github.com/aluo-x/Learning_Neural_Acoustic_Fields/blob/master/train.py - # https://github.com/pytorch/pytorch/issues/71029 - def find_free_port(self): - with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: - s.bind(('localhost', 0)) - return s.getsockname()[1] - - def run(self): - self.disable_logging() - - self.host = "127.0.0.1" - self.port = self.find_free_port() - self.flask_server = make_server(self.host, self.port, self.app) - self.server_thread = Thread(target=self.flask_server.serve_forever) - self.server_thread.start() - - def ready_checker(self): - """Wait until the server is started. - - Annoyingly, there's no way to get a callback to be run when the Flask - server starts. Instead, we'll have to poll. - """ - - while True: - if self.port: - try: - response = requests.get("http://%s:%s/ping" % (self.host, self.port)) - if response.status_code == 200: - break - except socket.error as e: - if e.errno == errno.ECONNREFUSED: - pass - else: - raise - - time.sleep(0.1) - - def start_server(self): - """Start the API server. - - returns: port (int) -- the port that the server is listening on - (on localhost) - """ - - checker = Thread(target=self.ready_checker) - checker.start() - self.start() - checker.join() - - return self.port diff --git a/lib/api/simulator.py b/lib/api/simulator.py deleted file mode 100644 index 26c0246c..00000000 --- a/lib/api/simulator.py +++ /dev/null @@ -1,8 +0,0 @@ -# Authors: see git history -# -# Copyright (c) 2010 Authors -# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. - -from flask import Blueprint - -simulator = Blueprint('simulator', __name__) diff --git a/lib/api/stitch_plan.py b/lib/api/stitch_plan.py deleted file mode 100644 index 0267a70a..00000000 --- a/lib/api/stitch_plan.py +++ /dev/null @@ -1,29 +0,0 @@ -# Authors: see git history -# -# Copyright (c) 2010 Authors -# Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. - -from flask import Blueprint, g, jsonify - -from ..exceptions import InkstitchException, format_uncaught_exception -from ..stitch_plan import stitch_groups_to_stitch_plan - -stitch_plan = Blueprint('stitch_plan', __name__) - - -@stitch_plan.route('') -def get_stitch_plan(): - if not g.extension.get_elements(): - return dict(colors=[], stitch_blocks=[], commands=[]) - - try: - metadata = g.extension.get_inkstitch_metadata() - collapse_len = metadata['collapse_len_mm'] - min_stitch_len = metadata['min_stitch_len_mm'] - stitch_groups = g.extension.elements_to_stitch_groups(g.extension.elements) - stitch_plan = stitch_groups_to_stitch_plan(stitch_groups, collapse_len=collapse_len, min_stitch_len=min_stitch_len) - return jsonify(stitch_plan) - except InkstitchException as exc: - return jsonify({"error_message": str(exc)}), 500 - except Exception: - return jsonify({"error_message": format_uncaught_exception()}), 500 |
