File size: 2,649 Bytes
34097e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from typing import Union

def ensure_install(module_name: str, lib_name: Union[str,None] = None):
    from importlib.util import find_spec
    
    if lib_name is None:
        lib_name = module_name
    
    if find_spec(module_name) is None:
        import subprocess
        try:
            print('-' * 80, file=sys.stderr)
            print(f'| installing {lib_name} ...', file=sys.stderr)
            print('-' * 80, file=sys.stderr)
            subprocess.check_call(
                [sys.executable, "-m", "pip", "install", lib_name],
                stdout=sys.stdout,
                stderr=sys.stderr
            )
        except Exception as e:
            msg = ''.join(traceback.TracebackException.from_exception(e).format())
            print(msg, file=sys.stderr)
            print('-' * 80, file=sys.stderr)
            print(f'| failed to install {lib_name}. exit...', file=sys.stderr)
            print('-' * 80, file=sys.stderr)
            sys.exit(1)

if __name__ == '__main__':
    import mimetypes
    mimetypes.add_type('application/javascript', '.js')

    import os, sys, traceback
    from functools import wraps
    
    ensure_install('flask')
    ensure_install('PIL', 'pillow')
    
    from flask import Flask, jsonify, request
    from common import posex_utils as posex
    
    app = Flask(__name__, static_folder='.', static_url_path='')
    
    posex.set_save_dir(os.path.join(app.static_folder, 'saved_poses'))
    
    @app.route('/')
    def index():
        return app.send_static_file('index.html')
    
    def api_try(fn):
        @wraps(fn)
        def f(*args, **kwargs):
            try:
                return fn(*args, **kwargs)
            except Exception as e:
                msg = ''.join(traceback.TracebackException.from_exception(e).format())
                print(msg, file=sys.stderr)
                return jsonify(result=str(e), ok=False)
        return f
    
    @app.route('/pose/all')
    @api_try
    def all_poses():
        return jsonify(list(posex.all_poses()))
    
    @app.route('/pose/save', methods=['POST'])
    @api_try
    def save_pose():
        data = request.json
        posex.save_pose(data)
        return jsonify(result='pose saved', ok=True)
    
    @app.route('/pose/delete', methods=['POST'])
    @api_try
    def delete_pose():
        data = request.json
        posex.delete_pose(data['name'])
        return jsonify(result='pose deleted', ok=True)
    
    @app.route('/pose/load', methods=['POST'])
    @api_try
    def load_pose():
        data = request.json
        return jsonify(posex.load_pose(data['name']))

    app.run(port=55502, debug=True)