Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Defines a Tornado application. 

4Tutorial `chat <https://github.com/tornadoweb/tornado/tree/stable/demos/chat>`_. 

5""" 

6import logging 

7import os 

8import pprint 

9from tornado.web import Application 

10from tornado.web import StaticFileHandler 

11from tornado.log import enable_pretty_logging 

12from .handlersml import MainHandler, LoginHandler, LogoutHandler, UploadData, SubmitForm, CompetitionHandler 

13from .default_options import LightMLBoardDefaultOptions 

14from .options_helpers import read_options, read_users 

15from .dbmanager import DatabaseCompetition 

16 

17 

18class LightMLBoard(Application): 

19 """ 

20 Overloads a :epkg:`tornado:Application`. 

21 """ 

22 

23 def __init__(self, handlers=None, default_host=None, transforms=None, 

24 **settings): 

25 """ 

26 Constructor. 

27 See `wep.py <https://github.com/tornadoweb/tornado/blob/master/tornado/web.py>`_. 

28 """ 

29 if "login_url" not in settings: 

30 settings['login_url'] = "/login" 

31 if "xsrf_cookies" not in settings: 

32 settings["xsrf_cookies"] = True 

33 

34 Application.__init__(self, handlers=handlers, default_host=default_host, 

35 transforms=transforms, **settings) 

36 app_log = logging.getLogger("tornado.application") 

37 app_log.info('[LightMLBoard] Settings: {0}'.format(settings)) 

38 

39 @staticmethod 

40 def update_options(config_options): 

41 """ 

42 Returns updated options, includes the default ones. 

43 """ 

44 context = {} 

45 lang = None 

46 debug = False 

47 cookie_secret = None 

48 enablelog = False 

49 for opts in [LightMLBoardDefaultOptions.__dict__, 

50 config_options]: 

51 for k, v in opts.items(): 

52 if not k.startswith("_"): 

53 if k == 'cookie_secret': 

54 cookie_secret = v 

55 elif k == "lang": 

56 lang = v 

57 elif k == "debug": 

58 pass 

59 elif k == "logging": 

60 enablelog = v 

61 elif k == "allowed_users": 

62 context[k] = v 

63 else: 

64 context["tmpl_" + k] = v 

65 app_options = dict(lang=lang, debug=debug, enablelog=enablelog, 

66 cookie_secret=cookie_secret) 

67 return context, app_options 

68 

69 @staticmethod 

70 def make_app(config=None, logged=None, dbfile=":memory:"): 

71 """ 

72 Creates a *LightMLBoard* application. 

73 

74 @param config configuration file 

75 @param logged to log one user 

76 @param dbfile file for the db file 

77 @return @see cl LightMLBoard 

78 """ 

79 this = os.path.dirname(__file__) 

80 st = os.path.join(this, "static") 

81 

82 # Update the context for static files. 

83 updated_context = {'path': st} 

84 

85 # Options. 

86 config_options = read_options(config) 

87 context, local_context = LightMLBoard.update_options(config_options) 

88 allowed = context["allowed_users"] 

89 if allowed is None: 

90 raise ValueError("No allowed users.\n{0}".format( 

91 pprint.pformat(context))) 

92 users = read_users(allowed) 

93 

94 # db manager 

95 if config is not None: 

96 dbman = DatabaseCompetition(dbfile) 

97 dbman.init_from_options(context) 

98 context['dbman'] = dbman 

99 

100 # We remove the users. 

101 del context['allowed_users'] 

102 

103 # Specifies a logged user. 

104 if logged: 

105 context["ut_logged"] = logged 

106 updated_context["ut_logged"] = logged 

107 

108 context_users = context.copy() 

109 context_users['users'] = users 

110 

111 # Logging. 

112 if local_context['debug'] or local_context['enablelog']: 

113 enable_pretty_logging() 

114 

115 pages = [ 

116 (r"/", MainHandler, context), 

117 (r'/login', LoginHandler, context_users), 

118 (r'/logout', LogoutHandler, context), 

119 (r'/static/(.*)', StaticFileHandler, updated_context), 

120 (r'/submit', SubmitForm, context), 

121 (r'/upload', UploadData, context), 

122 (r'/competition', CompetitionHandler, context), 

123 ] 

124 

125 args = dict(lang=local_context["lang"], debug=local_context['debug']) 

126 if 'cookie_secret' in local_context: 

127 args['cookie_secret'] = local_context['cookie_secret'] 

128 return LightMLBoard(pages, **args) 

129 

130 @staticmethod 

131 def start_app(port=8897, **kwargs): 

132 """ 

133 Starts the application. 

134 

135 @param port port to listen 

136 @param kwargs @see me make_app 

137 """ 

138 import tornado.ioloop # pylint: disable=C0415 

139 app = LightMLBoard.make_app(**kwargs) 

140 app.listen(port) 

141 tornado.ioloop.IOLoop.current().start()