##// END OF EJS Templates
i18n: added pluralize to renderer globals.
marcink -
r1304:9a1b7bc4 default
parent child Browse files
Show More
@@ -1,156 +1,157 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2017 RhodeCode GmbH
3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21
21
22 import logging
22 import logging
23 import pylons
23 import pylons
24 import Queue
24 import Queue
25 import subprocess32
25 import subprocess32
26
26
27 from pyramid.i18n import get_localizer
27 from pyramid.i18n import get_localizer
28 from pyramid.threadlocal import get_current_request
28 from pyramid.threadlocal import get_current_request
29 from threading import Thread
29 from threading import Thread
30
30
31 from rhodecode.translation import _ as tsf
31 from rhodecode.translation import _ as tsf
32
32
33
33
34 log = logging.getLogger(__name__)
34 log = logging.getLogger(__name__)
35
35
36
36
37 def add_renderer_globals(event):
37 def add_renderer_globals(event):
38 # Put pylons stuff into the context. This will be removed as soon as
38 # Put pylons stuff into the context. This will be removed as soon as
39 # migration to pyramid is finished.
39 # migration to pyramid is finished.
40 conf = pylons.config._current_obj()
40 conf = pylons.config._current_obj()
41 event['h'] = conf.get('pylons.h')
41 event['h'] = conf.get('pylons.h')
42 event['c'] = pylons.tmpl_context
42 event['c'] = pylons.tmpl_context
43 event['url'] = pylons.url
43 event['url'] = pylons.url
44
44
45 # TODO: When executed in pyramid view context the request is not available
45 # TODO: When executed in pyramid view context the request is not available
46 # in the event. Find a better solution to get the request.
46 # in the event. Find a better solution to get the request.
47 request = event['request'] or get_current_request()
47 request = event['request'] or get_current_request()
48
48
49 # Add Pyramid translation as '_' to context
49 # Add Pyramid translation as '_' to context
50 event['_'] = request.translate
50 event['_'] = request.translate
51 event['localizer'] = request.localizer
51 event['_ungettext'] = request.plularize
52
52
53
53
54 def add_localizer(event):
54 def add_localizer(event):
55 request = event.request
55 request = event.request
56 localizer = get_localizer(request)
56 localizer = get_localizer(request)
57
57
58 def auto_translate(*args, **kwargs):
58 def auto_translate(*args, **kwargs):
59 return localizer.translate(tsf(*args, **kwargs))
59 return localizer.translate(tsf(*args, **kwargs))
60
60
61 request.localizer = localizer
61 request.localizer = localizer
62 request.translate = auto_translate
62 request.translate = auto_translate
63 request.plularize = localizer.pluralize
63
64
64
65
65 def scan_repositories_if_enabled(event):
66 def scan_repositories_if_enabled(event):
66 """
67 """
67 This is subscribed to the `pyramid.events.ApplicationCreated` event. It
68 This is subscribed to the `pyramid.events.ApplicationCreated` event. It
68 does a repository scan if enabled in the settings.
69 does a repository scan if enabled in the settings.
69 """
70 """
70 from rhodecode.model.scm import ScmModel
71 from rhodecode.model.scm import ScmModel
71 from rhodecode.lib.utils import repo2db_mapper, get_rhodecode_base_path
72 from rhodecode.lib.utils import repo2db_mapper, get_rhodecode_base_path
72 settings = event.app.registry.settings
73 settings = event.app.registry.settings
73 vcs_server_enabled = settings['vcs.server.enable']
74 vcs_server_enabled = settings['vcs.server.enable']
74 import_on_startup = settings['startup.import_repos']
75 import_on_startup = settings['startup.import_repos']
75 if vcs_server_enabled and import_on_startup:
76 if vcs_server_enabled and import_on_startup:
76 repositories = ScmModel().repo_scan(get_rhodecode_base_path())
77 repositories = ScmModel().repo_scan(get_rhodecode_base_path())
77 repo2db_mapper(repositories, remove_obsolete=False)
78 repo2db_mapper(repositories, remove_obsolete=False)
78
79
79
80
80 class Subscriber(object):
81 class Subscriber(object):
81 """
82 """
82 Base class for subscribers to the pyramid event system.
83 Base class for subscribers to the pyramid event system.
83 """
84 """
84 def __call__(self, event):
85 def __call__(self, event):
85 self.run(event)
86 self.run(event)
86
87
87 def run(self, event):
88 def run(self, event):
88 raise NotImplementedError('Subclass has to implement this.')
89 raise NotImplementedError('Subclass has to implement this.')
89
90
90
91
91 class AsyncSubscriber(Subscriber):
92 class AsyncSubscriber(Subscriber):
92 """
93 """
93 Subscriber that handles the execution of events in a separate task to not
94 Subscriber that handles the execution of events in a separate task to not
94 block the execution of the code which triggers the event. It puts the
95 block the execution of the code which triggers the event. It puts the
95 received events into a queue from which the worker process takes them in
96 received events into a queue from which the worker process takes them in
96 order.
97 order.
97 """
98 """
98 def __init__(self):
99 def __init__(self):
99 self._stop = False
100 self._stop = False
100 self._eventq = Queue.Queue()
101 self._eventq = Queue.Queue()
101 self._worker = self.create_worker()
102 self._worker = self.create_worker()
102 self._worker.start()
103 self._worker.start()
103
104
104 def __call__(self, event):
105 def __call__(self, event):
105 self._eventq.put(event)
106 self._eventq.put(event)
106
107
107 def create_worker(self):
108 def create_worker(self):
108 worker = Thread(target=self.do_work)
109 worker = Thread(target=self.do_work)
109 worker.daemon = True
110 worker.daemon = True
110 return worker
111 return worker
111
112
112 def stop_worker(self):
113 def stop_worker(self):
113 self._stop = False
114 self._stop = False
114 self._eventq.put(None)
115 self._eventq.put(None)
115 self._worker.join()
116 self._worker.join()
116
117
117 def do_work(self):
118 def do_work(self):
118 while not self._stop:
119 while not self._stop:
119 event = self._eventq.get()
120 event = self._eventq.get()
120 if event is not None:
121 if event is not None:
121 self.run(event)
122 self.run(event)
122
123
123
124
124 class AsyncSubprocessSubscriber(AsyncSubscriber):
125 class AsyncSubprocessSubscriber(AsyncSubscriber):
125 """
126 """
126 Subscriber that uses the subprocess32 module to execute a command if an
127 Subscriber that uses the subprocess32 module to execute a command if an
127 event is received. Events are handled asynchronously.
128 event is received. Events are handled asynchronously.
128 """
129 """
129
130
130 def __init__(self, cmd, timeout=None):
131 def __init__(self, cmd, timeout=None):
131 super(AsyncSubprocessSubscriber, self).__init__()
132 super(AsyncSubprocessSubscriber, self).__init__()
132 self._cmd = cmd
133 self._cmd = cmd
133 self._timeout = timeout
134 self._timeout = timeout
134
135
135 def run(self, event):
136 def run(self, event):
136 cmd = self._cmd
137 cmd = self._cmd
137 timeout = self._timeout
138 timeout = self._timeout
138 log.debug('Executing command %s.', cmd)
139 log.debug('Executing command %s.', cmd)
139
140
140 try:
141 try:
141 output = subprocess32.check_output(
142 output = subprocess32.check_output(
142 cmd, timeout=timeout, stderr=subprocess32.STDOUT)
143 cmd, timeout=timeout, stderr=subprocess32.STDOUT)
143 log.debug('Command finished %s', cmd)
144 log.debug('Command finished %s', cmd)
144 if output:
145 if output:
145 log.debug('Command output: %s', output)
146 log.debug('Command output: %s', output)
146 except subprocess32.TimeoutExpired as e:
147 except subprocess32.TimeoutExpired as e:
147 log.exception('Timeout while executing command.')
148 log.exception('Timeout while executing command.')
148 if e.output:
149 if e.output:
149 log.error('Command output: %s', e.output)
150 log.error('Command output: %s', e.output)
150 except subprocess32.CalledProcessError as e:
151 except subprocess32.CalledProcessError as e:
151 log.exception('Error while executing command.')
152 log.exception('Error while executing command.')
152 if e.output:
153 if e.output:
153 log.error('Command output: %s', e.output)
154 log.error('Command output: %s', e.output)
154 except:
155 except:
155 log.exception(
156 log.exception(
156 'Exception while executing command %s.', cmd)
157 'Exception while executing command %s.', cmd)
General Comments 0
You need to be logged in to leave comments. Login now