annotate pylons_app/lib/utils.py @ 114:cc5cf1a93902

Implemented simplehg middleware,moved make_ui functions to lib.utils
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 28 Apr 2010 02:08:45 +0200
parents f24b9a2934cf
children 2811259dc12d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
1 from mercurial import ui, config
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
2 import os
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
3 import logging
82
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
4
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
5 def get_repo_slug(request):
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
6 path_info = request.environ.get('PATH_INFO')
96
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
7 uri_lst = path_info.split('/')
93
aec4c0071cb3 added empty controllers for branches tags files graph, routing and test for them
Marcin Kuzminski <marcin@python-works.com>
parents: 82
diff changeset
8 repo_name = uri_lst[1]
82
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
9 return repo_name
96
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
10
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
11 def is_mercurial(environ):
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
12 """
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
13 Returns True if request's target is mercurial server - header
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
14 ``HTTP_ACCEPT`` of such request would start with ``application/mercurial``.
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
15 """
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
16 http_accept = environ.get('HTTP_ACCEPT')
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
17 if http_accept and http_accept.startswith('application/mercurial'):
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
18 return True
f24b9a2934cf added is mercurial method in utils,
Marcin Kuzminski <marcin@python-works.com>
parents: 93
diff changeset
19 return False
114
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
20
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
21 def check_repo_dir(paths):
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
22 repos_path = paths[0][1].split('/')
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
23 if repos_path[-1] in ['*', '**']:
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
24 repos_path = repos_path[:-1]
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
25 if repos_path[0] != '/':
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
26 repos_path[0] = '/'
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
27 if not os.path.isdir(os.path.join(*repos_path)):
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
28 raise Exception('Not a valid repository in %s' % paths[0][1])
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
29
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
30 def make_ui(path='hgwebdir.config', checkpaths=True):
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
31 """
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
32 A funcion that will read python rc files and make an ui from read options
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
33
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
34 @param path: path to mercurial config file
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
35 """
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
36 if not os.path.isfile(path):
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
37 logging.error('Unable to read config file %s' % path)
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
38 return False
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
39 #propagated from mercurial documentation
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
40 sections = [
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
41 'alias',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
42 'auth',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
43 'decode/encode',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
44 'defaults',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
45 'diff',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
46 'email',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
47 'extensions',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
48 'format',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
49 'merge-patterns',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
50 'merge-tools',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
51 'hooks',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
52 'http_proxy',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
53 'smtp',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
54 'patch',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
55 'paths',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
56 'profiling',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
57 'server',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
58 'trusted',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
59 'ui',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
60 'web',
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
61 ]
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
62
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
63 baseui = ui.ui()
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
64 cfg = config.config()
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
65 cfg.read(path)
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
66 if checkpaths:check_repo_dir(cfg.items('paths'))
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
67
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
68 for section in sections:
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
69 for k, v in cfg.items(section):
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
70 baseui.setconfig(section, k, v)
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
71
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
72 return baseui
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
73
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
74
cc5cf1a93902 Implemented simplehg middleware,moved make_ui functions to lib.utils
Marcin Kuzminski <marcin@python-works.com>
parents: 96
diff changeset
75