Skip to content
This repository was archived by the owner on Jan 4, 2021. It is now read-only.

Commit b493337

Browse files
author
totokaka
committedMar 14, 2014
Let's see if this makes Sonar happy.
Added a StatusHandler to be able to move more logic into the backend modules
1 parent 431fc43 commit b493337

File tree

3 files changed

+108
-46
lines changed

3 files changed

+108
-46
lines changed
 

‎mcman/backend/plugins.py

+43-3
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def resolve_dependencies(server, plugin_name, status_hook,
308308

309309
plugin = bukget.find_by_name(server, plugin_name)
310310
if plugin is None:
311-
status_hook(1, (plugin_name,))
311+
status_hook(1, plugin_name)
312312
return stack
313313

314314
plugin = bukget.plugin_details(server, plugin, version,
@@ -327,7 +327,7 @@ def resolve_dependencies(server, plugin_name, status_hook,
327327
for dep in plugin['versions'][0]['hard_dependencies']:
328328
slug = bukget.find_by_name(server, dep)
329329
if slug is None:
330-
status_hook(3, (dep,))
330+
status_hook(3, dep)
331331
continue
332332
if slug not in stack:
333333
resolve_dependencies(server, slug, status_hook, stack=stack)
@@ -363,7 +363,7 @@ def find_plugins(server, queries, status_hook):
363363
version = ''
364364
slug = bukget.find_by_name(server, plugin)
365365
if slug is None:
366-
status_hook(1, (plugin,))
366+
status_hook(1, plugin)
367367
continue
368368
if len(plugin_version) > 1:
369369
version = plugin_version[1]
@@ -407,3 +407,43 @@ def download(question, frmt, plugins, skip=False):
407407
plugin = plugins[i]
408408
prefix = frmt.format(total=len(plugins), part=i+1)
409409
download_plugin(plugin, prefix)
410+
411+
412+
def find_updates(server, to_install, versions, status_hook):
413+
""" Find updates for plugins.
414+
415+
This function will also check if the plugins are allready installed.
416+
417+
Parameters:
418+
server The server
419+
to_install A list over plugins that should be installed
420+
versions A dict with optional specific versions for plugins
421+
status_hook A status hook
422+
423+
"""
424+
plugins = list()
425+
installed = list_plugins()
426+
427+
for slug in to_install:
428+
plugin = download_details(server, slug,
429+
versions[slug]
430+
if slug in versions
431+
else VERSION)
432+
433+
if plugin is None:
434+
status_hook(1, slug)
435+
continue
436+
elif len(plugin['versions']) < 1:
437+
status_hook(2, slug)
438+
continue
439+
440+
for i in installed:
441+
if i[0] == slug and i[1] >= plugin['versions'][0]['version']:
442+
status_hook(3, plugin['plugin_name'])
443+
break
444+
elif i[0] == slug:
445+
status_hook(4, plugin['plugin_name'])
446+
else:
447+
plugins.append(plugin)
448+
449+
return plugins

‎mcman/frontend/plugins_command.py

+36-43
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
from urllib.error import URLError
10+
from mcman.status_handler import StatusHandler
1011
from mcman.backend import plugins as backend
1112
from mcman.backend import common as utils
1213
from mcman.frontend.common import Command
@@ -34,18 +35,6 @@ def __init__(self, args):
3435

3536
self.invoke_subcommand(args.subcommand, (ValueError, URLError))
3637

37-
def status_find_plugins(self, key, values):
38-
""" Print status for backend.find_plugins. """
39-
self.p_sub('Could not find {}'.format(values[0]))
40-
41-
def status_dependencies(self, key, values):
42-
""" Print status for backend.dependencies. """
43-
if key == 1 or key == 3:
44-
self.p_sub('Could not find `{}`', values[0])
45-
return
46-
self.p_sub('Could not find version `{}` of `{}`'.format(
47-
values[1], values[0]))
48-
4938
def search(self):
5039
""" Search for plugins. """
5140
query = self.args.query
@@ -164,16 +153,27 @@ def download(self):
164153
""" Download plugins. """
165154
self.p_main('Finding plugins on BukGet')
166155

156+
status_handler = StatusHandler()
157+
status_handler.register_handler(1, lambda arguments:
158+
self.p_sub('Could not find {}'.format(
159+
arguments[0])))
167160
to_install, versions = backend.find_plugins(self.args.server,
168161
self.args.plugins,
169-
self.status_find_plugins)
162+
status_handler.get_hook())
170163

171164
self.p_main('Resolving dependencies')
172165

173-
to_install = backend.dependencies(self.args.server,
174-
to_install,
175-
versions,
176-
self.status_dependencies)
166+
status_handler = StatusHandler()
167+
one_three = lambda arguments: \
168+
self.p_sub('Could not find `{}`'.format(arguments))
169+
two = lambda arguments: \
170+
self.p_sub('Could not find version `{}` of `{}`'.format(
171+
arguments[0], arguments[1]))
172+
status_handler.register_handler(1, one_three)
173+
status_handler.register_handler(2, two)
174+
status_handler.register_handler(3, one_three)
175+
to_install = backend.dependencies(self.args.server, to_install,
176+
versions, status_handler.get_hook())
177177

178178
if len(to_install) < 1:
179179
self.p_main('Found no plugins!')
@@ -182,32 +182,25 @@ def download(self):
182182
self.p_main(
183183
'Resolving versions, and checking allready installed plugins')
184184

185-
installed = backend.list_plugins()
186-
plugins = list()
187-
for slug in to_install:
188-
plugin = backend.download_details(self.server, slug,
189-
versions[slug]
190-
if slug in versions
191-
else self.args.version)
192-
193-
if plugin is None:
194-
self.p_sub('Could not find plugin `{}` again'.format(slug))
195-
continue
196-
elif len(plugin['versions']) < 1:
197-
self.p_sub('Could not find version of `{}` again'.format(slug))
198-
continue
199-
200-
for i in installed:
201-
if i[0] == slug and i[1] >= plugin['versions'][0]['version']:
202-
self.p_sub('{} was allready installed.'.format(
203-
plugin['plugin_name']))
204-
break
205-
elif i[0] == slug:
206-
self.p_sub(
207-
'{} is allready installed, but out of date'.format(
208-
plugin['plugin_name']))
209-
else:
210-
plugins.append(plugin)
185+
status_handler = StatusHandler()
186+
one = lambda argument: \
187+
self.p_sub('Could not find plugin `{}` again'.format(argument))
188+
two = lambda argument: \
189+
self.p_sub('Could not find version of `{}` again'.format(
190+
argument))
191+
three = lambda argument: \
192+
self.p_sub('{} was allready installed.'.format(
193+
argument))
194+
four = lambda argument: \
195+
self.p_sub('{} is allready installed, but out of date'.format(
196+
argument))
197+
status_handler.register_handler(1, one)
198+
status_handler.register_handler(2, two)
199+
status_handler.register_handler(3, three)
200+
status_handler.register_handler(4, four)
201+
202+
plugins = backend.find_updates(self.server, to_install, versions,
203+
status_handler.get_hook())
211204

212205
if len(plugins) < 1:
213206
self.p_main('No plugins left to install')

‎mcman/status_handler.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
""" Status Handler. """
2+
3+
4+
class StatusHandler(object):
5+
6+
""" Status Handler.
7+
8+
A status handler can register functions to run when a status is sent.
9+
10+
"""
11+
12+
def __init__(self):
13+
""" Initialize. """
14+
self.statuses = dict()
15+
16+
def register_handler(self, key, function):
17+
""" Register a handler. """
18+
self.statuses[key] = function
19+
20+
def get_hook(self):
21+
""" Get the status hook. """
22+
def invoke(key, arguments):
23+
""" Invoke an handler. """
24+
if key in self.statuses:
25+
self.statuses[key](arguments)
26+
else:
27+
print('No handler found for key {}'.format(key))
28+
29+
return invoke

0 commit comments

Comments
 (0)
This repository has been archived.