mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[fix] Move imports at the top in yunohost and yunohost-api
This commit is contained in:
parent
d11dd38e42
commit
ee7c4e84aa
2 changed files with 24 additions and 36 deletions
28
bin/yunohost
28
bin/yunohost
|
@ -1,8 +1,13 @@
|
||||||
#! /usr/bin/python
|
#! /usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
import moulinette
|
||||||
|
from moulinette.actionsmap import ActionsMap
|
||||||
|
from moulinette.interfaces.cli import colorize, get_locale
|
||||||
|
|
||||||
# Either we are in a development environment or not
|
# Either we are in a development environment or not
|
||||||
IN_DEVEL = False
|
IN_DEVEL = False
|
||||||
|
@ -34,17 +39,11 @@ if IN_DEVEL:
|
||||||
|
|
||||||
def _die(message, title='Error:'):
|
def _die(message, title='Error:'):
|
||||||
"""Print error message and exit"""
|
"""Print error message and exit"""
|
||||||
try:
|
|
||||||
from moulinette.interfaces.cli import colorize
|
|
||||||
except ImportError:
|
|
||||||
colorize = lambda msg, c: msg
|
|
||||||
print('%s %s' % (colorize(title, 'red'), message))
|
print('%s %s' % (colorize(title, 'red'), message))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def _parse_cli_args():
|
def _parse_cli_args():
|
||||||
"""Parse additional arguments for the cli"""
|
"""Parse additional arguments for the cli"""
|
||||||
import argparse
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(add_help=False)
|
parser = argparse.ArgumentParser(add_help=False)
|
||||||
parser.add_argument('--no-cache',
|
parser.add_argument('--no-cache',
|
||||||
action='store_false', default=True, dest='use_cache',
|
action='store_false', default=True, dest='use_cache',
|
||||||
|
@ -90,8 +89,6 @@ def _parse_cli_args():
|
||||||
|
|
||||||
def _init_moulinette(debug=False, verbose=False, quiet=False):
|
def _init_moulinette(debug=False, verbose=False, quiet=False):
|
||||||
"""Configure logging and initialize the moulinette"""
|
"""Configure logging and initialize the moulinette"""
|
||||||
from moulinette import init
|
|
||||||
|
|
||||||
# Define loggers handlers
|
# Define loggers handlers
|
||||||
handlers = set(LOGGERS_HANDLERS)
|
handlers = set(LOGGERS_HANDLERS)
|
||||||
if quiet and 'tty' in handlers:
|
if quiet and 'tty' in handlers:
|
||||||
|
@ -167,11 +164,10 @@ def _init_moulinette(debug=False, verbose=False, quiet=False):
|
||||||
_die(str(e))
|
_die(str(e))
|
||||||
|
|
||||||
# Initialize moulinette
|
# Initialize moulinette
|
||||||
init(logging_config=logging, _from_source=IN_DEVEL)
|
moulinette.init(logging_config=logging, _from_source=IN_DEVEL)
|
||||||
|
|
||||||
def _retrieve_namespaces():
|
def _retrieve_namespaces():
|
||||||
"""Return the list of namespaces to load"""
|
"""Return the list of namespaces to load"""
|
||||||
from moulinette.actionsmap import ActionsMap
|
|
||||||
ret = ['yunohost']
|
ret = ['yunohost']
|
||||||
for n in ActionsMap.get_namespaces():
|
for n in ActionsMap.get_namespaces():
|
||||||
# Append YunoHost modules
|
# Append YunoHost modules
|
||||||
|
@ -190,8 +186,6 @@ if __name__ == '__main__':
|
||||||
if not os.path.isfile('/etc/yunohost/installed') and \
|
if not os.path.isfile('/etc/yunohost/installed') and \
|
||||||
(len(args) < 2 or (args[0] +' '+ args[1] != 'tools postinstall' and \
|
(len(args) < 2 or (args[0] +' '+ args[1] != 'tools postinstall' and \
|
||||||
args[0] +' '+ args[1] != 'backup restore')):
|
args[0] +' '+ args[1] != 'backup restore')):
|
||||||
from moulinette.interfaces.cli import get_locale
|
|
||||||
|
|
||||||
# Init i18n
|
# Init i18n
|
||||||
m18n.load_namespace('yunohost')
|
m18n.load_namespace('yunohost')
|
||||||
m18n.set_locale(get_locale())
|
m18n.set_locale(get_locale())
|
||||||
|
@ -200,9 +194,9 @@ if __name__ == '__main__':
|
||||||
_die(m18n.n('yunohost_not_installed'), m18n.g('error'))
|
_die(m18n.n('yunohost_not_installed'), m18n.g('error'))
|
||||||
|
|
||||||
# Execute the action
|
# Execute the action
|
||||||
from moulinette import cli
|
ret = moulinette.cli(
|
||||||
ret = cli(_retrieve_namespaces(), args,
|
_retrieve_namespaces(), args,
|
||||||
use_cache=opts.use_cache, output_as=opts.output_as,
|
use_cache=opts.use_cache, output_as=opts.output_as,
|
||||||
password=opts.password, parser_kwargs={'top_parser': parser}
|
password=opts.password, parser_kwargs={'top_parser': parser}
|
||||||
)
|
)
|
||||||
sys.exit(ret)
|
sys.exit(ret)
|
||||||
|
|
|
@ -1,8 +1,13 @@
|
||||||
#! /usr/bin/python
|
#! /usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import os.path
|
import argparse
|
||||||
|
|
||||||
|
import moulinette
|
||||||
|
from moulinette.actionsmap import ActionsMap
|
||||||
|
from moulinette.interfaces.cli import colorize
|
||||||
|
|
||||||
# Either we are in a development environment or not
|
# Either we are in a development environment or not
|
||||||
IN_DEVEL = False
|
IN_DEVEL = False
|
||||||
|
@ -38,17 +43,11 @@ if IN_DEVEL:
|
||||||
|
|
||||||
def _die(message, title='Error:'):
|
def _die(message, title='Error:'):
|
||||||
"""Print error message and exit"""
|
"""Print error message and exit"""
|
||||||
try:
|
|
||||||
from moulinette.interfaces.cli import colorize
|
|
||||||
except ImportError:
|
|
||||||
colorize = lambda msg, c: msg
|
|
||||||
print('%s %s' % (colorize(title, 'red'), message))
|
print('%s %s' % (colorize(title, 'red'), message))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def _parse_api_args():
|
def _parse_api_args():
|
||||||
"""Parse main arguments for the api"""
|
"""Parse main arguments for the api"""
|
||||||
import argparse
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(add_help=False,
|
parser = argparse.ArgumentParser(add_help=False,
|
||||||
description="Run the YunoHost API to manage your server.",
|
description="Run the YunoHost API to manage your server.",
|
||||||
)
|
)
|
||||||
|
@ -87,8 +86,6 @@ def _parse_api_args():
|
||||||
|
|
||||||
def _init_moulinette(use_websocket=True, debug=False, verbose=False):
|
def _init_moulinette(use_websocket=True, debug=False, verbose=False):
|
||||||
"""Configure logging and initialize the moulinette"""
|
"""Configure logging and initialize the moulinette"""
|
||||||
from moulinette import init
|
|
||||||
|
|
||||||
# Define loggers handlers
|
# Define loggers handlers
|
||||||
handlers = set(LOGGERS_HANDLERS)
|
handlers = set(LOGGERS_HANDLERS)
|
||||||
if not use_websocket and 'api' in handlers:
|
if not use_websocket and 'api' in handlers:
|
||||||
|
@ -162,11 +159,10 @@ def _init_moulinette(use_websocket=True, debug=False, verbose=False):
|
||||||
_die(str(e))
|
_die(str(e))
|
||||||
|
|
||||||
# Initialize moulinette
|
# Initialize moulinette
|
||||||
init(logging_config=logging, _from_source=IN_DEVEL)
|
moulinette.init(logging_config=logging, _from_source=IN_DEVEL)
|
||||||
|
|
||||||
def _retrieve_namespaces():
|
def _retrieve_namespaces():
|
||||||
"""Return the list of namespaces to load"""
|
"""Return the list of namespaces to load"""
|
||||||
from moulinette.actionsmap import ActionsMap
|
|
||||||
ret = ['yunohost']
|
ret = ['yunohost']
|
||||||
for n in ActionsMap.get_namespaces():
|
for n in ActionsMap.get_namespaces():
|
||||||
# Append YunoHost modules
|
# Append YunoHost modules
|
||||||
|
@ -195,14 +191,12 @@ if __name__ == '__main__':
|
||||||
_init_moulinette(opts.use_websocket, opts.debug, opts.verbose)
|
_init_moulinette(opts.use_websocket, opts.debug, opts.verbose)
|
||||||
|
|
||||||
# Run the server
|
# Run the server
|
||||||
from moulinette import api, MoulinetteError
|
|
||||||
from yunohost.utils.packages import ynh_packages_version
|
from yunohost.utils.packages import ynh_packages_version
|
||||||
ret = api(_retrieve_namespaces(),
|
ret = moulinette.api(
|
||||||
host=opts.host, port=opts.port,
|
_retrieve_namespaces(),
|
||||||
routes={
|
host=opts.host, port=opts.port, routes={
|
||||||
('GET', '/installed'): is_installed,
|
('GET', '/installed'): is_installed,
|
||||||
('GET', '/version'): ynh_packages_version,
|
('GET', '/version'): ynh_packages_version,
|
||||||
},
|
}, use_cache=opts.use_cache, use_websocket=opts.use_websocket
|
||||||
use_cache=opts.use_cache, use_websocket=opts.use_websocket
|
|
||||||
)
|
)
|
||||||
sys.exit(ret)
|
sys.exit(ret)
|
||||||
|
|
Loading…
Add table
Reference in a new issue