From 85f3bd5988400dde57eb96286270f7c757fe8dd6 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sun, 12 Feb 2017 19:08:52 +0100 Subject: [PATCH] core, frontends: getProfilesList renamed to profilesGetList + behaviour change: - profilesGetList now handles clients and components boolean arguments, to filter profiles - jp: added --clients and --components options to profile/list - profilesGetList now returns sorted profiles --- frontends/src/bridge/dbus_bridge.py | 28 +++++------ frontends/src/jp/cmd_profile.py | 49 +++++++++++-------- frontends/src/primitivus/profile_manager.py | 2 +- .../quick_frontend/quick_profile_manager.py | 2 +- .../bridge_constructor/bridge_template.ini | 10 ++-- src/bridge/dbus_bridge.py | 12 ++--- src/core/sat_main.py | 2 +- src/memory/memory.py | 22 ++++++++- 8 files changed, 79 insertions(+), 48 deletions(-) diff --git a/frontends/src/bridge/dbus_bridge.py b/frontends/src/bridge/dbus_bridge.py index 55b95c0d..5264825b 100644 --- a/frontends/src/bridge/dbus_bridge.py +++ b/frontends/src/bridge/dbus_bridge.py @@ -389,20 +389,6 @@ class Bridge(BridgeFrontend): kwargs['error_handler'] = error_handler return unicode(self.db_core_iface.getProfileName(profile_key, **kwargs)) - def getProfilesList(self, callback=None, errback=None): - if callback is None: - error_handler = None - else: - if errback is None: - errback = log.error - error_handler = lambda err:errback(dbus_to_bridge_exception(err)) - kwargs={} - if callback is not None: - kwargs['timeout'] = const_TIMEOUT - kwargs['reply_handler'] = callback - kwargs['error_handler'] = error_handler - return self.db_core_iface.getProfilesList(**kwargs) - def getReady(self, callback=None, errback=None): if callback is None: error_handler = None @@ -592,6 +578,20 @@ class Bridge(BridgeFrontend): error_handler = lambda err:errback(dbus_to_bridge_exception(err)) return self.db_core_iface.profileStartSession(password, profile_key, timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler) + def profilesListGet(self, clients=True, components=False, callback=None, errback=None): + if callback is None: + error_handler = None + else: + if errback is None: + errback = log.error + error_handler = lambda err:errback(dbus_to_bridge_exception(err)) + kwargs={} + if callback is not None: + kwargs['timeout'] = const_TIMEOUT + kwargs['reply_handler'] = callback + kwargs['error_handler'] = error_handler + return self.db_core_iface.profilesListGet(clients, components, **kwargs) + def progressGet(self, id, profile, callback=None, errback=None): if callback is None: error_handler = None diff --git a/frontends/src/jp/cmd_profile.py b/frontends/src/jp/cmd_profile.py index 1842dded..55e1dd2c 100644 --- a/frontends/src/jp/cmd_profile.py +++ b/frontends/src/jp/cmd_profile.py @@ -35,7 +35,7 @@ class ProfileConnect(base.CommandBase): def __init__(self, host): # it's weird to have a command named "connect" with need_connect=False, but it can be handy to be able # to launch just the session, so some paradox don't hurt - super(ProfileConnect, self).__init__(host, 'connect', need_connect=False, help=_('connect a profile')) + super(ProfileConnect, self).__init__(host, 'connect', need_connect=False, help=(u'connect a profile')) def add_parser_options(self): pass @@ -43,7 +43,7 @@ class ProfileConnect(base.CommandBase): class ProfileDefault(base.CommandBase): def __init__(self, host): - super(ProfileDefault, self).__init__(host, 'default', use_profile=False, help=_('print default profile')) + super(ProfileDefault, self).__init__(host, 'default', use_profile=False, help=(u'print default profile')) def add_parser_options(self): pass @@ -54,14 +54,14 @@ class ProfileDefault(base.CommandBase): class ProfileDelete(base.CommandBase): def __init__(self, host): - super(ProfileDelete, self).__init__(host, 'delete', use_profile=False, help=_('delete a profile')) + super(ProfileDelete, self).__init__(host, 'delete', use_profile=False, help=(u'delete a profile')) def add_parser_options(self): self.parser.add_argument('profile', type=str, help=PROFILE_HELP) self.parser.add_argument('-f', '--force', action='store_true', help=_(u'delete profile without confirmation')) def start(self): - if self.args.profile not in self.host.bridge.getProfilesList(): + if self.args.profile not in self.host.bridge.profilesListGet(): log.error("Profile %s doesn't exist." % self.args.profile) self.host.quit(1) message = u"Are you sure to delete profile [{}] ?".format(self.args.profile) @@ -76,7 +76,7 @@ class ProfileDelete(base.CommandBase): class ProfileInfo(base.CommandBase): def __init__(self, host): - super(ProfileInfo, self).__init__(host, 'info', need_connect=False, help=_('get information about a profile')) + super(ProfileInfo, self).__init__(host, 'info', need_connect=False, help=_(u'get information about a profile')) self.need_loop = True self.to_show = [(_(u"jid"), "Connection", "JabberID"),] self.largest = max([len(item[0]) for item in self.to_show]) @@ -105,25 +105,34 @@ class ProfileInfo(base.CommandBase): class ProfileList(base.CommandBase): def __init__(self, host): - super(ProfileList, self).__init__(host, 'list', use_profile=False, use_output='list', help=_('list profiles')) + super(ProfileList, self).__init__(host, 'list', use_profile=False, use_output='list', help=(u'list profiles')) def add_parser_options(self): - pass + group = self.parser.add_mutually_exclusive_group() + group.add_argument('-c', '--clients', action='store_true', help=_(u'the password of the profile')) + group.add_argument('-C', '--components', action='store_true', help=(u'the password of the profile')) + def start(self): - self.output(self.host.bridge.getProfilesList()) + if self.args.clients: + clients, components = True, False + elif self.args.components: + clients, components = False, True + else: + clients, components = True, True + self.output(self.host.bridge.profilesListGet(clients, components)) class ProfileCreate(base.CommandBase): def __init__(self, host): - super(ProfileCreate, self).__init__(host, 'create', use_profile=False, help=_('create a new profile')) + super(ProfileCreate, self).__init__(host, 'create', use_profile=False, help=(u'create a new profile')) self.need_loop = True def add_parser_options(self): - self.parser.add_argument('profile', type=str, help=_('the name of the profile')) - self.parser.add_argument('-p', '--password', type=str, default='', help=_('the password of the profile')) - self.parser.add_argument('-j', '--jid', type=str, help=_('the jid of the profile')) - self.parser.add_argument('-x', '--xmpp-password', type=str, help=_('the password of the XMPP account (use profile password if not specified)'), + self.parser.add_argument('profile', type=str, help=(u'the name of the profile')) + self.parser.add_argument('-p', '--password', type=str, default='', help=(u'the password of the profile')) + self.parser.add_argument('-j', '--jid', type=str, help=(u'the jid of the profile')) + self.parser.add_argument('-x', '--xmpp-password', type=str, help=(u'the password of the XMPP account (use profile password if not specified)'), metavar='PASSWORD') self.parser.add_argument('-C', '--component', type=base.unicode_decoder, default='', help=_(u'set to component import name (entry point) if this is a component')) @@ -141,7 +150,7 @@ class ProfileCreate(base.CommandBase): def start(self): """Create a new profile""" - if self.args.profile in self.host.bridge.getProfilesList(): + if self.args.profile in self.host.bridge.profilesListGet(): log.error("Profile %s already exists." % self.args.profile) self.host.quit(1) self.host.bridge.profileCreate(self.args.profile, self.args.password, self.args.component, callback=self._profile_created, errback=None) @@ -149,14 +158,14 @@ class ProfileCreate(base.CommandBase): class ProfileModify(base.CommandBase): def __init__(self, host): - super(ProfileModify, self).__init__(host, 'modify', need_connect=False, help=_('modify an existing profile')) + super(ProfileModify, self).__init__(host, 'modify', need_connect=False, help=(u'modify an existing profile')) def add_parser_options(self): profile_pwd_group = self.parser.add_mutually_exclusive_group() - profile_pwd_group.add_argument('-w', '--password', type=base.unicode_decoder, help=_('change the password of the profile')) - profile_pwd_group.add_argument('--disable-password', action='store_true', help=_('disable profile password (dangerous!)')) - self.parser.add_argument('-j', '--jid', type=base.unicode_decoder, help=_('the jid of the profile')) - self.parser.add_argument('-x', '--xmpp-password', type=base.unicode_decoder, help=_('change the password of the XMPP account'), + profile_pwd_group.add_argument('-w', '--password', type=base.unicode_decoder, help=(u'change the password of the profile')) + profile_pwd_group.add_argument('--disable-password', action='store_true', help=(u'disable profile password (dangerous!)')) + self.parser.add_argument('-j', '--jid', type=base.unicode_decoder, help=(u'the jid of the profile')) + self.parser.add_argument('-x', '--xmpp-password', type=base.unicode_decoder, help=(u'change the password of the XMPP account'), metavar='PASSWORD') self.parser.add_argument('-D', '--default', action='store_true', help=_(u'set as default profile')) @@ -177,4 +186,4 @@ class Profile(base.CommandBase): subcommands = (ProfileConnect, ProfileCreate, ProfileDefault, ProfileDelete, ProfileInfo, ProfileList, ProfileModify) def __init__(self, host): - super(Profile, self).__init__(host, 'profile', use_profile=False, help=_('Profile commands')) + super(Profile, self).__init__(host, 'profile', use_profile=False, help=(u'Profile commands')) diff --git a/frontends/src/primitivus/profile_manager.py b/frontends/src/primitivus/profile_manager.py index 8e0ab5ea..db6d9141 100644 --- a/frontends/src/primitivus/profile_manager.py +++ b/frontends/src/primitivus/profile_manager.py @@ -37,7 +37,7 @@ class ProfileManager(QuickProfileManager, urwid.WidgetWrap): self.pass_wid = sat_widgets.Password(_('Password:'), align='center') style = ['no_first_select'] - profiles = host.bridge.getProfilesList() + profiles = host.bridge.profilesListGet() profiles.sort() self.list_profile = sat_widgets.List(profiles, style=style, align='center', on_change=self.onProfileChange) diff --git a/frontends/src/quick_frontend/quick_profile_manager.py b/frontends/src/quick_frontend/quick_profile_manager.py index 914a96a4..5b260f8c 100644 --- a/frontends/src/quick_frontend/quick_profile_manager.py +++ b/frontends/src/quick_frontend/quick_profile_manager.py @@ -202,7 +202,7 @@ class QuickProfileManager(object): def refillProfiles(self): """Rebuild the list of profiles""" - profiles = self.host.bridge.getProfilesList() + profiles = self.host.bridge.profilesListGet() profiles.sort() self.setProfiles(profiles) diff --git a/src/bridge/bridge_constructor/bridge_template.ini b/src/bridge/bridge_constructor/bridge_template.ini index 0393ed6a..e8187146 100644 --- a/src/bridge/bridge_constructor/bridge_template.ini +++ b/src/bridge/bridge_constructor/bridge_template.ini @@ -197,12 +197,16 @@ doc=Get real profile name from profile key doc_param_0=%(doc_profile_key)s doc_return=Real profile name -[getProfilesList] +[profilesListGet] type=method category=core -sig_in= +sig_in=bb sig_out=as -doc=Get all profiles +param_0_default=True +param_1_default=False +doc_param_0=clients: get clients profiles +doc_param_1=components: get components profiles +doc=Get list of profiles [profileSetDefault] type=method diff --git a/src/bridge/dbus_bridge.py b/src/bridge/dbus_bridge.py index 8a7b5609..253e3b9c 100755 --- a/src/bridge/dbus_bridge.py +++ b/src/bridge/dbus_bridge.py @@ -323,12 +323,6 @@ class DbusObject(dbus.service.Object): def getProfileName(self, profile_key="@DEFAULT@"): return self._callback("getProfileName", unicode(profile_key)) - @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, - in_signature='', out_signature='as', - async_callbacks=None) - def getProfilesList(self, ): - return self._callback("getProfilesList", ) - @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, in_signature='', out_signature='', async_callbacks=('callback', 'errback')) @@ -425,6 +419,12 @@ class DbusObject(dbus.service.Object): def profileStartSession(self, password='', profile_key="@DEFAULT@", callback=None, errback=None): return self._callback("profileStartSession", unicode(password), unicode(profile_key), callback=callback, errback=errback) + @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, + in_signature='bb', out_signature='as', + async_callbacks=None) + def profilesListGet(self, clients=True, components=False): + return self._callback("profilesListGet", clients, components) + @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX, in_signature='ss', out_signature='a{ss}', async_callbacks=None) diff --git a/src/core/sat_main.py b/src/core/sat_main.py index 8f898550..6d870dbc 100644 --- a/src/core/sat_main.py +++ b/src/core/sat_main.py @@ -76,7 +76,7 @@ class SAT(service.Service): self.bridge.register_method("getVersion", lambda: self.full_version) self.bridge.register_method("getFeatures", self.getFeatures) self.bridge.register_method("getProfileName", self.memory.getProfileName) - self.bridge.register_method("getProfilesList", self.memory.getProfilesList) + self.bridge.register_method("profilesListGet", self.memory.getProfilesList) self.bridge.register_method("getEntityData", lambda jid_, keys, profile: self.memory.getEntityData(jid.JID(jid_), keys, profile)) self.bridge.register_method("getEntitiesData", self.memory._getEntitiesData) self.bridge.register_method("profileCreate", self.memory.createProfile) diff --git a/src/memory/memory.py b/src/memory/memory.py index 70ecbef3..0e1f12df 100644 --- a/src/memory/memory.py +++ b/src/memory/memory.py @@ -430,8 +430,26 @@ class Memory(object): except KeyError: log.error(_(u"Trying to purge roster status cache for a profile not in memory: [%s]") % profile) - def getProfilesList(self): - return self.storage.getProfilesList() + def getProfilesList(self, clients=True, components=False): + """retrieve profiles list + + @param clients(bool): if True return clients profiles + @param components(bool): if True return components profiles + @return (list[unicode]): selected profiles + """ + if not clients and not components: + log.warning(_(u"requesting no profiles at all")) + return [] + profiles = self.storage.getProfilesList() + if clients and components: + return sorted(profiles) + isComponent = self.storage.profileIsComponent + if clients: + p_filter = lambda p: not isComponent(p) + else: + p_filter = lambda p: isComponent(p) + + return sorted(p for p in profiles if p_filter(p)) def getProfileName(self, profile_key, return_profile_keys=False): """Return name of profile from keyword