added int type to unrepr. Nice output for

list contacts
This commit is contained in:
Dimitur Kirov 2005-07-26 15:04:40 +00:00
parent 19edb7b2d9
commit 35559bed7b

View file

@ -148,23 +148,9 @@ sent using this account'), False),
print self.compose_help() print self.compose_help()
sys.exit() sys.exit()
try: self.init_connection()
self.sbus = dbus.SessionBus()
except:
self.send_error(_('Session bus is not available.'))
if _version[1] >= 30 and _version[1] <= 42:
self.object = self.sbus.get_object(SERVICE, OBJ_PATH)
self.interface = dbus.Interface(object, INTERFACE)
elif _version[1] < 30:
self.service = self.sbus.get_service(SERVICE)
self.interface = self.service.get_object(OBJ_PATH, INTERFACE)
else:
send_error(_('Unknow dbus version: %s') % _version)
# get the function asked
self.method = self.interface.__getattr__(self.command)
self.check_arguments() self.check_arguments()
if self.command == 'contact_info': if self.command == 'contact_info':
if self.argv_len < 3: if self.argv_len < 3:
self.send_error(_('Missing argument "contact_jid"')) self.send_error(_('Missing argument "contact_jid"'))
@ -175,7 +161,14 @@ sent using this account'), False),
self.send_error(_('Service not available')) self.send_error(_('Service not available'))
res = self.call_remote_method() res = self.call_remote_method()
self.print_result(res)
if self.command == 'contact_info':
gobject.timeout_add(10000, self.gtk_quit) # wait 10 sec for response
gtk.main()
def print_result(self, res):
''' Print retrieved result to the output '''
if res is not None: if res is not None:
if self.command in ['open_chat', 'send_message']: if self.command in ['open_chat', 'send_message']:
if self.command == 'send_message': if self.command == 'send_message':
@ -191,11 +184,32 @@ Please specify account for sending the message.') % sys.argv[2])
if type(res) == list: if type(res) == list:
for account in res: for account in res:
print account print account
elif self.command == 'list_contacts':
for single_res in res:
accounts = self.unrepr(single_res)
for account_dict in accounts:
print self.print_info(0, account_dict)
elif res: elif res:
print res print res
if self.command == 'contact_info':
gobject.timeout_add(5000, self.gtk_quit) # wait 5 sec maximum def init_connection(self):
gtk.main() ''' create the onnection to the session dbus,
or exit if it is not possible '''
try:
self.sbus = dbus.SessionBus()
except:
self.send_error(_('Session bus is not available.'))
if _version[1] >= 30 and _version[1] <= 42:
self.object = self.sbus.get_object(SERVICE, OBJ_PATH)
self.interface = dbus.Interface(object, INTERFACE)
elif _version[1] < 30:
self.service = self.sbus.get_service(SERVICE)
self.interface = self.service.get_object(OBJ_PATH, INTERFACE)
else:
send_error(_('Unknow dbus version: %s') % _version)
# get the function asked
self.method = self.interface.__getattr__(self.command)
def make_arguments_row(self, args): def make_arguments_row(self, args):
''' return arguments list. Mandatory arguments are enclosed with: ''' return arguments list. Mandatory arguments are enclosed with:
@ -248,29 +262,47 @@ Please specify account for sending the message.') % sys.argv[2])
def print_info(self, level, prop_dict): def print_info(self, level, prop_dict):
''' return formated string from serialized vcard data ''' ''' return formated string from serialized vcard data '''
if prop_dict is None: if prop_dict is None or type(prop_dict) \
not in [dict, list, tuple]:
return '' return ''
ret_str = '' ret_str = ''
for key in prop_dict.keys(): if type(prop_dict) in [list, tuple]:
val = prop_dict[key] ret_str = ''
spacing = ' ' * level * 4 spacing = ' ' * level * 4
if type(val) == unicode or type(val) == int or \ for val in prop_dict:
type(val) == str: if val is None:
if val is not None: ret_str +='\t'
val = val.strip() elif type(val) == unicode or type(val) == int or \
ret_str += '%s%-10s: %s\n' % (spacing, key, val) type(val) == str:
elif type(val) == list or type(val) == tuple: ret_str +='\t' + str(val)
res = '' elif type(val) == list or type(val) == tuple:
for items in val: res = ''
res += self.print_info(level+1, items) for items in val:
if res != '': res += self.print_info(level+1, items)
ret_str += '%s%s: \n%s' % (spacing, key, res) if res != '':
elif type(val) == dict: ret_str += '\t' + res
res = self.print_info(level+1, val) ret_str = '%s(%s)\n' % (spacing, ret_str[1:])
if res != '': elif type(prop_dict) is dict:
ret_str += '%s%s: \n%s' % (spacing, key, res) for key in prop_dict.keys():
else: val = prop_dict[key]
self.send_warning(_('Unknown type %s ') % type(val)) spacing = ' ' * level * 4
if type(val) == unicode or type(val) == int or \
type(val) == str:
if val is not None:
val = val.strip()
ret_str += '%s%-10s: %s\n' % (spacing, key, val)
elif type(val) == list or type(val) == tuple:
res = ''
for items in val:
res += self.print_info(level+1, items)
if res != '':
ret_str += '%s%s: \n%s' % (spacing, key, res)
elif type(val) == dict:
res = self.print_info(level+1, val)
if res != '':
ret_str += '%s%s: \n%s' % (spacing, key, res)
else:
self.send_warning(_('Unknown type %s ') % type(val))
return ret_str return ret_str
def unrepr(self, serialized_data): def unrepr(self, serialized_data):
@ -281,10 +313,35 @@ Please specify account for sending the message.') % sys.argv[2])
value = serialized_data.strip() value = serialized_data.strip()
first_char = value[0] first_char = value[0]
is_unicode = False is_unicode = False
is_int = False
if first_char == 'u': if first_char == 'u':
is_unicode = True is_unicode = True
value = value[1:] value = value[1:]
first_char = value[0] first_char = value[0]
elif '0123456789.'.find(first_char) != -1:
is_int = True
_str = first_char
if first_char == '.':
is_float = True
else:
is_float = False
for i in range(len(value) - 1):
chr = value[i+1]
if chr == '.':
is_float = True
elif '0123456789'.find(chr) == -1:
break
_str += chr
if is_float:
return (float(_str), value[len(_str):])
else:
return (int(_str), value[len(_str):])
elif first_char == 'N':
if value[1:4] == 'one':
return (None, value[4:])
else:
return (None, '')
if first_char == "'" or first_char == '"': # handle strings and unicode if first_char == "'" or first_char == '"': # handle strings and unicode
if len(value) < 2: if len(value) < 2:
return ('',value[1:]) return ('',value[1:])
@ -425,4 +482,3 @@ Type "%s help %s" for more info') % (args[argv_len][0], BASENAME, self.command))
if __name__ == '__main__': if __name__ == '__main__':
GajimRemote() GajimRemote()
pass