2011-09-05, 19:39
Hey,
I have 3 items I'm not satisfied with in the favorite code, and I hope for some input on it.
1) Passing addon.url to ContextMenu
The only way I have found to get hold of the addon.url is to initialize ContextMenu with it as an arg.
2) Getting hold of the addon's ContextMenu object
In order to utilize the existing code to generate the "Delete favorite" menu, the addon needs to pass the cm object in order to generate the menu.
3) A way to make a dict to a string and the other way around
As I need to hide data from xbmc when it's sent to the mode showfavoties, I had to base64 encode it, how ever only strings can be base64 encoded. I could not find an existing function that could do this without it "molested" the data by adding other stuff to it. So I created 2 new functions in Addon.
I would really like to utilize existing functions as not to clutter the code.
Other than that it's looking pretty good now, I'll see if I can quickly make a new video of how it all fits together now that it's a class by itself.
I have 3 items I'm not satisfied with in the favorite code, and I hope for some input on it.
1) Passing addon.url to ContextMenu
The only way I have found to get hold of the addon.url is to initialize ContextMenu with it as an arg.
Code:
addon = Addon('plugin.video.solarmovie', sys.argv)
cm = ContextMenu(addon.url)
2) Getting hold of the addon's ContextMenu object
In order to utilize the existing code to generate the "Delete favorite" menu, the addon needs to pass the cm object in order to generate the menu.
Code:
addon.show_favorites(cm)
3) A way to make a dict to a string and the other way around
As I need to hide data from xbmc when it's sent to the mode showfavoties, I had to base64 encode it, how ever only strings can be base64 encoded. I could not find an existing function that could do this without it "molested" the data by adding other stuff to it. So I created 2 new functions in Addon.
I would really like to utilize existing functions as not to clutter the code.
Code:
def dict_to_string(self, dictionary):
'''
This is a convinient way to convert a dictionary to a string. It can
only be 2 layers deep e.g.
{ 'mylist' : ('foo', 'bar', 'chimera'), 'mydict' : { 'key1' : 'key1value',
'key2' : 'key2value' }, 'myurl' : 'http://xmbx.org' }
Args:
dictionary (dict): Each value in the dict can contain one of the
following: bol, Nontype, str, dict, list, tuple, int. Note that the
dict in dict, list in dict, tuple in dict's contents must be
one of the above types, also any int values will have become
str values, and you need to set it again after using string_to_dict.
See :meth:`string_to_dict` for infomation.
Returns:
A string in the format &key=value, how ever it's not url safe.
'''
dictstring = ''
for key in dictionary:
string = key
m = re.match(r'([\d|\w|\s]+)', string)
if m:
if type(dictionary[key]) == tuple:
tuplestring = '&%s=%s' % (key,'__tuple__/')
for value in dictionary[key]:
tuplestring = tuplestring + '__%s' % (value)
dictstring = dictstring + tuplestring
elif type(dictionary[key]) == list:
liststring = '&%s=%s' % (key,'__list__/')
for value in dictionary[key]:
liststring = liststring + '__%s' % (value)
dictstring = dictstring + liststring
elif type(dictionary[key]) == dict:
dictstring = '&%s=%s' % (key, '__dict__/')
for middlekey in dictionary[key]:
dictstring = dictstring + '___%s__%s' % \
(middlekey, dictionary[key][middlekey])
dictstring = dictstring + dictstring
else:
dictstring = dictstring + '&%s=__str__/%s' % \
(key, dictionary[key])
return dictstring
def string_to_dict(self, dictstring):
'''
This function rebuilts a dict that was previously turned into a string
using dict_to_string. Remember as it was turned into a string the
values and keys are all str now.
Args:
dictstring (str): A string priveously generated by dict_to_string.
See :meth:`dict_to_string` for further infomation.
Returns:
A rebuilt dictionary
'''
restoreddict = {}
splitter=re.split('&', dictstring)
for string in splitter:
if re.match('.+?__str__/', string):
key, value = re.split('=__str__/', string)
restoreddict[key] = value
elif re.match('.+?__tuple__/', string):
key, values = re.split('=__tuple__/', string)
splits = re.split('__', values)
splits.pop(0)
tmptuple = tuple(splits)
restoreddict[key] = tmptuple
elif re.match('.+?__list__/', string):
key, values = re.split('=__list__/', string)
splits = re.split('__', values)
splits.pop(0)
restoreddict[key] = splits
elif re.match('.+?__dict__/', string):
tmpdict = {}
key, values = re.split('=__dict__/', string)
entrypairs = re.split('___', values)
for pair in entrypairs:
if pair:
pairkey, pairvalue = re.split('__', pair)
tmpdict[pairkey] = pairvalue
restoreddict[key] = tmpdict
return restoreddict
Other than that it's looking pretty good now, I'll see if I can quickly make a new video of how it all fits together now that it's a class by itself.