50
launchpad_factory = Launchpad.login_with
52
def __init__(self, **kwargs):
53
self.launchpad_functor = Functor(self.launchpad_factory, **kwargs)
51
def __init__(self, launchpad_factory, logger):
52
self.launchpad_factory = launchpad_factory
55
55
def getLaunchpad(self, *args, **kwargs):
57
return self.launchpad_functor(*args, **kwargs)
57
return self.launchpad_factory(*args, **kwargs)
58
58
except AttributeError:
59
59
# In stock httplib2, this exception is raised when failing
60
60
# to connect to the remote host:
113
113
default_service_root = "http://localhost:8000/+api/"
114
114
default_version = "1.0"
117
default_log_level = "info"
116
119
def __init__(self, application_factory=None, usage=None):
117
120
if application_factory is not None:
118
121
self.application_factory = application_factory
119
122
if usage is not None:
120
123
self.usage = usage
123
parser = OptionParser(usage=self.usage)
125
def getParser(self, **kwargs):
126
parser = OptionParser(usage=self.usage, **kwargs)
125
128
group = OptionGroup(parser, "Launchpad options")
126
129
group.add_option("--consumer-name",
127
130
default=self.default_consumer_name,
128
132
help="The consumer appropriate for this application,"
129
133
" defaults to: %default")
130
134
group.add_option("--credentials-file",
131
136
help="If given, the credentials are stored in that file.")
132
137
group.add_option("--launchpadlib-dir",
133
139
help="The directory for cache and credentials.")
134
140
group.add_option("--service-root",
135
141
default=self.default_service_root,
136
143
help="The URL to the root of the web service,"
137
144
" defaults to: %default")
138
145
group.add_option("--version",
141
148
" defaults to: %default")
142
149
parser.add_option_group(group)
151
group = OptionGroup(parser, "Logging options")
152
group.add_option("-l", "--log",
154
help="The file to write the log to.")
155
group.add_option("--log-level",
156
default=self.default_log_level,
157
help="One of debug, info, warning, error or critical.")
158
parser.add_option_group(group)
146
162
def parseArgs(self, args):
147
163
parser = self.getParser()
148
164
options, args = parser.parse_args(args)
165
kwargs = options.__dict__
167
# Define logger early
168
log = kwargs.pop("log")
171
log_stream, log_file = sys.stdout, None
173
log_stream, log_file = None, log
175
log_stream, log_file = sys.stderr, None
176
kwargs["logger"] = get_logger(
177
kwargs.pop("log_level"), log_stream, log_file)
150
179
# Define launchpadlib_dir and credentials_file if empty
151
if options.launchpadlib_dir is None:
180
launchpadlib_dir = kwargs.pop("launchpadlib_dir")
181
if launchpadlib_dir is None:
152
182
home_dir = environ["HOME"]
153
183
launchpadlib_dir = path.join(home_dir, ".lpresults")
155
launchpadlib_dir = options.launchpadlib_dir
157
if options.credentials_file is None:
185
credentials_file = kwargs.pop("credentials_file")
186
if credentials_file is None:
158
187
host_name = urlsplit(options.service_root)[1]
159
188
credentials_file = path.join(
160
189
launchpadlib_dir, host_name, "credentials",
161
190
options.consumer_name)
163
credentials_file = options.credentials_file
165
kwargs = options.__dict__
166
kwargs["launchpadlib_dir"] = path.expanduser(launchpadlib_dir)
167
kwargs["credentials_file"] = path.expanduser(credentials_file)
192
kwargs["launchpad_factory"] = Functor(Launchpad.login_with,
193
launchpadlib_dir=path.expanduser(launchpadlib_dir),
194
credentials_file=path.expanduser(credentials_file),
195
consumer_name=kwargs.pop("consumer_name"),
196
service_root=kwargs.pop("service_root"),
197
version=kwargs.pop("version"))
169
199
return args, kwargs
171
def createApplication(self, args=None):
201
def createApplication(self, args):
172
202
args, kwargs = self.parseArgs(args)
174
204
return self.application_factory(*args, **kwargs)
184
214
This will be called after the application has finished.
187
def run(self, *args, **kwargs):
217
def run(self, args=None):
188
218
"""Run the application."""
189
219
self.preApplication()
192
application = self.createApplication(*args, **kwargs)
222
application = self.createApplication(args)
193
223
application.start()
195
225
except ApplicationError, error: