Completed
Push — dev-4.1 ( dbd54d...c25dff )
by Felipe A.
01:42
created

ArgParse._plugins()   A

Complexity

Conditions 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import sys
5
import os
6
import os.path
7
import argparse
8
import flask
9
10
from . import app, compat
11
from .compat import PY_LEGACY
12
13
14
class ArgParse(argparse.ArgumentParser):
15
    default_directory = os.path.abspath(compat.getcwd())
16
    default_host = os.getenv('BROWSEPY_HOST', '127.0.0.1')
17
    default_port = os.getenv('BROWSEPY_PORT', '8080')
18
19
    description = 'extendable web file browser'
20
21
    def __init__(self):
22
        super(ArgParse, self).__init__(description=self.description)
23
24
        self.add_argument(
25
            'host', nargs='?',
26
            default=self.default_host,
27
            help='address to listen (default: %s)' % self.default_host)
28
        self.add_argument(
29
            'port', nargs='?', type=int,
30
            default=self.default_port,
31
            help='port to listen (default: %s)' % self.default_port)
32
        self.add_argument(
33
            '--directory', metavar='PATH', type=self._directory,
34
            default=self.default_directory,
35
            help='base serving directory (default: current path)')
36
        self.add_argument(
37
            '--initial', metavar='PATH', type=self._directory,
38
            help='initial directory (default: same as --directory)')
39
        self.add_argument(
40
            '--removable', metavar='PATH', type=self._directory,
41
            default=None,
42
            help='base directory for remove (default: none)')
43
        self.add_argument(
44
            '--upload', metavar='PATH', type=self._directory,
45
            default=None,
46
            help='base directory for upload (default: none)')
47
        self.add_argument(
48
            '--plugin', metavar='PLUGIN_LIST', type=self._plugin,
49
            default=[],
50
            help='comma-separated list of plugins')
51
        self.add_argument('--debug', action='store_true', help='debug mode')
52
53
    def _plugin(self, arg):
54
        if not arg:
55
            return []
56
        return arg.split(',')
57
58
    def _directory(self, arg):
59
        if not arg:
60
            return None
61
        if PY_LEGACY and hasattr(sys.stdin, 'encoding'):
62
            encoding = sys.stdin.encoding
63
            if encoding is None:
64
                # if we are running without a terminal
65
                # assume default encoding
66
                encoding = sys.getdefaultencoding()
67
            arg = arg.decode(encoding)
68
        if os.path.isdir(arg):
69
            return os.path.abspath(arg)
70
        self.error('%s is not a valid directory' % arg)
71
72
73
def main(argv=sys.argv[1:], app=app, parser=ArgParse, run_fnc=flask.Flask.run):
74
    plugin_manager = app.extensions['plugin_manager']
75
    args = plugin_manager.load_arguments(argv, parser())
76
    app.config.update(
77
        directory_base=args.directory,
78
        directory_start=args.initial or args.directory,
79
        directory_remove=args.removable,
80
        directory_upload=args.upload,
81
        plugin_modules=args.plugin
82
        )
83
    plugin_manager.reload()
84
    run_fnc(
85
        app,
86
        host=args.host,
87
        port=args.port,
88
        debug=args.debug,
89
        use_reloader=False,
90
        threaded=True
91
        )
92
93
if __name__ == '__main__':
94
    main()
95