annotate MoinMoin/request/request_cli.py @ 4027:0d6724b87856

Refactored TestConfig creation for py.test based unit tests. The method used before had multiple problems: config creation did not use inheritance (as it does when used the wiki in production). It also didn't do the post processing done in __init__ of Config class. Also, it first created the request with some default config, then created the TestConfig (but some stuff in request was not initialized correctly)... The new method now uses config inheritance in the same way as a production wiki does. The TestConfig instance is created in RequestBase __init__, it does not use sys.path any more to import the test wikiconfig, but does an absolute import from MoinMoin._tests.wikiconfig. TODO: * fix the failing test_pysupport.TestImportExisting.testExisting * auth tests are currently skipped because they need more work * find a better way than staticmethod(TestConfig)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 24 Aug 2008 02:01:34 +0200
parents fee546fba271
children 84c05e657b47
rev   line source
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
2 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
3 MoinMoin - CLI Request Implementation for commandline usage.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
4
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
5 @copyright: 2001-2003 Juergen Hermann <jh@web.de>,
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
6 2003-2006 MoinMoin:ThomasWaldmann
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
7 @license: GNU GPL, see COPYING for details.
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
8 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
9 import sys
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
10
3672
fee546fba271 fix xmlrpc request.read() call to use content-length, if available
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3412
diff changeset
11 from MoinMoin import log
fee546fba271 fix xmlrpc request.read() call to use content-length, if available
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3412
diff changeset
12 logging = log.getLogger(__name__)
fee546fba271 fix xmlrpc request.read() call to use content-length, if available
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3412
diff changeset
13
3412
91d38a790d8e request framework: handle connection aborts gracefully
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2338
diff changeset
14 from MoinMoin.request import RequestBase, RemoteClosedConnection
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
15
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
16 class Request(RequestBase):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
17 """ specialized on command line interface and script requests """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
18
4027
0d6724b87856 Refactored TestConfig creation for py.test based unit tests.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3672
diff changeset
19 def __init__(self, url='CLI', pagename='', properties={}, given_config=None):
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
20 self.saved_cookie = ''
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
21 self.path_info = '/' + pagename
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
22 self.query_string = ''
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
23 self.remote_addr = '127.0.0.1'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
24 self.is_ssl = 0
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
25 self.http_user_agent = 'CLI/Script'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
26 self.url = url
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
27 self.request_method = 'GET'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
28 self.request_uri = '/' + pagename # TODO check if /pagename works as URI for CLI usage
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
29 self.http_host = 'localhost'
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
30 self.http_referer = ''
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
31 self.script_name = '.'
3672
fee546fba271 fix xmlrpc request.read() call to use content-length, if available
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3412
diff changeset
32 self.content_length = None
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
33 self.if_modified_since = None
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
34 self.if_none_match = None
4027
0d6724b87856 Refactored TestConfig creation for py.test based unit tests.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3672
diff changeset
35 RequestBase.__init__(self, properties, given_config)
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
36 self.cfg.caching_formats = [] # don't spoil the cache
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
37 self.initTheme() # usually request.run() does this, but we don't use it
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
38
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
39 def _setup_args_from_cgi_form(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
40 """ Override to create cli form """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
41 #form = cgi.FieldStorage()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
42 #return RequestBase._setup_args_from_cgi_form(self, form)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
43 return {}
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
44
3672
fee546fba271 fix xmlrpc request.read() call to use content-length, if available
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3412
diff changeset
45 def read(self, n):
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
46 """ Read from input stream. """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
47 if n is None:
3672
fee546fba271 fix xmlrpc request.read() call to use content-length, if available
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3412
diff changeset
48 logging.warning("calling request.read(None) might block")
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
49 return sys.stdin.read()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
50 else:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
51 return sys.stdin.read(n)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
52
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
53 def write(self, *data):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
54 """ Write to output stream. """
3412
91d38a790d8e request framework: handle connection aborts gracefully
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2338
diff changeset
55 data = self.encode(data)
91d38a790d8e request framework: handle connection aborts gracefully
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2338
diff changeset
56 try:
91d38a790d8e request framework: handle connection aborts gracefully
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2338
diff changeset
57 sys.stdout.write(data)
91d38a790d8e request framework: handle connection aborts gracefully
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2338
diff changeset
58 except IOError:
91d38a790d8e request framework: handle connection aborts gracefully
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2338
diff changeset
59 raise RemoteClosedConnection()
2338
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
60
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
61 def flush(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
62 sys.stdout.flush()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
63
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
64 def finish(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
65 RequestBase.finish(self)
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
66 # flush the output, ignore errors caused by the user closing the socket
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
67 try:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
68 sys.stdout.flush()
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
69 except IOError, ex:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
70 import errno
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
71 if ex.errno != errno.EPIPE:
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
72 raise
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
73
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
74 def isForbidden(self):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
75 """ Nothing is forbidden """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
76 return 0
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
77
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
78 # Accessors --------------------------------------------------------
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
79
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
80 def getQualifiedURL(self, uri=None):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
81 """ Return a full URL starting with schema and host
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
82
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
83 TODO: does this create correct pages when you render wiki pages
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
84 within a cli request?!
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
85 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
86 return uri
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
87
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
88 # Headers ----------------------------------------------------------
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
89
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
90 def setHttpHeader(self, header):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
91 pass
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
92
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
93 def _emit_http_headers(self, headers):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
94 """ private method to send out preprocessed list of HTTP headers """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
95 pass
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
96
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
97 def http_redirect(self, url):
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
98 """ Redirect to a fully qualified, or server-rooted URL
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
99
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
100 TODO: Does this work for rendering redirect pages?
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
101 """
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
102 raise Exception("Redirect not supported for command line tools!")
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
103
b902f2397c68 rename server and request Implementations by adding a prefix server_, request_
Reimar Bauer <rb.proj AT googlemail DOT com>
parents:
diff changeset
104