Skip to content

Commit

Permalink
Fixes incorrect return count from dynamic browse call.
Browse files Browse the repository at this point in the history
Removes need to specify -s to use SMAPI interface for WMP.
Fixes search called from GUI.
Adds ini settings to allow non display of empty prefix/suffix.
  • Loading branch information
Mark Henkelis committed Feb 25, 2013
1 parent 430ece7 commit 19f0dfb
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 98 deletions.
Binary file added queries.ods
Binary file not shown.
133 changes: 95 additions & 38 deletions sonospy/mediaserver.py
Expand Up @@ -429,24 +429,46 @@ def load_proxy_ini(self):
self.suffix_sep = u'\u007f'

# get chunk metadata characters
prefix_start, self.chunk_metadata_delimiter_prefix_start = self.proxy.get_delim('entry_prefix_start_separator', '[', self.prefix_sep)
prefix_end, self.chunk_metadata_delimiter_prefix_end = self.proxy.get_delim('entry_prefix_end_separator', ']', self.prefix_sep, 'after')
prefix_start, self.chunk_metadata_delimiter_prefix_start = self.get_delim('entry_prefix_start_separator', '[', self.prefix_sep)
prefix_end, self.chunk_metadata_delimiter_prefix_end = self.get_delim('entry_prefix_end_separator', ']', self.prefix_sep, 'after')

# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.proxy.get_delim('entry_prefix_start_separator', '[', 'before', u'\u0092')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.proxy.get_delim('entry_prefix_start_separator', '[', 'before', u'\u200B\u034F\u0082\u0083\u0091\u0092\u2007\u2060\uFEFF\uFE00')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.proxy.get_delim('entry_prefix_start_separator', '[', 'before', u'\u2029\u2028\u202f\u2061\u2062\u2063\uE000\uE001')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.proxy.get_delim('entry_prefix_start_separator', '[', 'before', u'1 \uF7002 \uF7013 \uF85D4 \uF85C5 \uF8D76 \u000a7 \u000d')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.proxy.get_delim('entry_prefix_start_separator', '[', 'before', u'1 \u000d')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.proxy.get_delim('entry_prefix_start_separator', '[', 'before', u'\u007f')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.proxy.get_delim('entry_prefix_start_separator', '[', 'before', u'\u0f0c')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.proxy.get_delim('entry_prefix_start_separator', '[', 'before', u'\u007f \u232b \u0080 \u000a \u000d \u001b \u009f')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.proxy.get_delim('entry_prefix_start_separator', '[')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.get_delim('entry_prefix_start_separator', '[', 'before', u'\u0092')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.get_delim('entry_prefix_start_separator', '[', 'before', u'\u200B\u034F\u0082\u0083\u0091\u0092\u2007\u2060\uFEFF\uFE00')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.get_delim('entry_prefix_start_separator', '[', 'before', u'\u2029\u2028\u202f\u2061\u2062\u2063\uE000\uE001')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.get_delim('entry_prefix_start_separator', '[', 'before', u'1 \uF7002 \uF7013 \uF85D4 \uF85C5 \uF8D76 \u000a7 \u000d')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.get_delim('entry_prefix_start_separator', '[', 'before', u'1 \u000d')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.get_delim('entry_prefix_start_separator', '[', 'before', u'\u007f')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.get_delim('entry_prefix_start_separator', '[', 'before', u'\u0f0c')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.get_delim('entry_prefix_start_separator', '[', 'before', u'\u007f \u232b \u0080 \u000a \u000d \u001b \u009f')
# suffix_start, self.chunk_metadata_delimiter_suffix_start = self.get_delim('entry_prefix_start_separator', '[')

suffix_start, self.chunk_metadata_delimiter_suffix_start = self.proxy.get_delim('entry_suffix_start_separator', '[', self.suffix_sep, 'before')
suffix_end, self.chunk_metadata_delimiter_suffix_end = self.proxy.get_delim('entry_suffix_end_separator', ']', self.suffix_sep)
suffix_start, self.chunk_metadata_delimiter_suffix_start = self.get_delim('entry_suffix_start_separator', '[', self.suffix_sep, 'before')
suffix_end, self.chunk_metadata_delimiter_suffix_end = self.get_delim('entry_suffix_end_separator', ']', self.suffix_sep)

missing, self.chunk_metadata_empty = self.proxy.get_delim('entry_extras_empty', '_', self.prefix_sep)
dateformat, self.chunk_metadata_date_format = self.proxy.get_delim('entry_extras_date_format', '%d/%m/%Y', self.prefix_sep)
missing, self.chunk_metadata_empty = self.get_delim('entry_extras_empty', '_', self.prefix_sep)

self.dont_display_separator_for_empty_prefix = False
try:
ini_dont_display_separator_for_empty_prefix = self.proxy.config.get('index section headers', 'dont_display_separator_for_empty_prefix')
if ini_dont_display_separator_for_empty_prefix.lower() == 'y':
self.dont_display_separator_for_empty_prefix = True
except ConfigParser.NoSectionError:
self.dont_display_separator_for_empty_prefix = False
except ConfigParser.NoOptionError:
self.dont_display_separator_for_empty_prefix = False

self.dont_display_separator_for_empty_suffix = False
try:
ini_dont_display_separator_for_empty_suffix = self.proxy.config.get('index section headers', 'dont_display_separator_for_empty_suffix')
if ini_dont_display_separator_for_empty_suffix.lower() == 'y':
self.dont_display_separator_for_empty_suffix = True
except ConfigParser.NoSectionError:
self.dont_display_separator_for_empty_suffix = False
except ConfigParser.NoOptionError:
self.dont_display_separator_for_empty_suffix = False


dateformat, self.chunk_metadata_date_format = self.get_delim('entry_extras_date_format', '%d/%m/%Y', self.prefix_sep)

self.searchre_pre = '%s[^%s]*%s' % (prefix_start, prefix_end, prefix_end)
if not suffix_end:
Expand Down Expand Up @@ -638,7 +660,7 @@ def load_smapi_ini(self):
self.debugout('rootitems', self.rootitems)
self.debugout('searchitems', self.searchitems)

dummy, self.smapi_date_format = self.proxy.get_delim('smapi_date_format', '%Y/%m/%d', ' ', section='SMAPI formats')
dummy, self.smapi_date_format = self.get_delim('smapi_date_format', '%Y/%m/%d', ' ', section='SMAPI formats')

# get sorts setting
ini_alternative_index_sorting = 'N'
Expand Down Expand Up @@ -786,7 +808,7 @@ def load_smapi_default(self):
self.debugout('rootitems', self.rootitems)
self.debugout('searchitems', self.searchitems)

dummy, self.smapi_date_format = self.proxy.get_delim('smapi_date_format', '%Y/%m/%d', ' ', section='SMAPI formats')
dummy, self.smapi_date_format = self.get_delim('smapi_date_format', '%Y/%m/%d', ' ', section='SMAPI formats')

# get sorts setting
ais = 'N'
Expand Down Expand Up @@ -1166,8 +1188,6 @@ def hierarchicalQuery(self, kwargs):
# a playlist
# a list of IDs (a track or a playlist should be the last entry in a list and not alone)

# TODO: support for playlists?

items = None
track = False
playlist = False
Expand Down Expand Up @@ -1515,6 +1535,10 @@ def staticQuery(self, *args, **kwargs):

smapiservice = kwargs.get('smapiservice', False)

searchCriteria = kwargs.get('SearchCriteria', '')
searchCriteria = self.fixcriteria(searchCriteria)
log.debug('searchCriteria: %s' % searchCriteria.encode(enc, 'replace'))

# query ID is a dummy for SMAPI
# check SMAPI special cases for browse
if SMAPI:
Expand All @@ -1528,12 +1552,16 @@ def staticQuery(self, *args, **kwargs):
objectEntry = None
track = False
playlist = False
search = False
if len(queryID) == 32 and not ':' in queryID and not '_' in queryID:
# track id is 32 hex chars
track = True
elif len(queryID) == 8 and not ':' in queryID and not '_' in queryID:
# playlist id is 8 hex chars, everything else will be 9 or more
playlist = True
elif searchCriteria.startswith('SEARCH::'):
# search requested
search = True
else:
if '__' in queryID:
objectfacets = queryID.split('__')
Expand Down Expand Up @@ -1592,6 +1620,8 @@ def staticQuery(self, *args, **kwargs):
browsetype = 'Track'
elif playlist:
browsetype = 'Playlist'
elif search:
browsetype = 'Search'
else:
browsetype = 'Track'

Expand Down Expand Up @@ -4959,9 +4989,6 @@ def search(self, *args, **kwargs):



###########################################################################################################################
###########################################################################################################################
###########################################################################################################################



Expand Down Expand Up @@ -5281,10 +5308,10 @@ def dynamicQuery(self, *args, **kwargs):
itemid = ':'.join(filter(None,(itemidprefix, str(itemid))))

if numprefix:
prefix = self.smapi_makepresuffix([prefixes[0]], self.replace_pre, [prevtitle])
prefix = self.smapi_makepresuffix([prefixes[0]], self.replace_pre, [prevtitle], 'P')
if prefix: title = '%s%s' % (prefix, title)
if numsuffix:
suffix = self.smapi_makepresuffix([suffixes[0]], self.replace_suf, [prevtitle])
suffix = self.smapi_makepresuffix([suffixes[0]], self.replace_suf, [prevtitle], 'S')
if suffix: title = '%s%s' % (title, suffix)

items += [(itemid, escape(title))]
Expand Down Expand Up @@ -5609,14 +5636,14 @@ def dynamicQuery(self, *args, **kwargs):
prefixdata = []
for i in range(numprefix):
prefixdata.append(row[prefixstart+i])
prefix = self.smapi_makepresuffix(prefixes, self.replace_pre, prefixdata)
prefix = self.smapi_makepresuffix(prefixes, self.replace_pre, prefixdata, 'P')
if prefix: title = '%s%s' % (prefix, title)
if numsuffix:
# suffixdata = list(row[suffixstart:suffixstart+numsuffix])
suffixdata = []
for i in range(numsuffix):
suffixdata.append(row[suffixstart+i])
suffix = self.smapi_makepresuffix(suffixes, self.replace_suf, suffixdata)
suffix = self.smapi_makepresuffix(suffixes, self.replace_suf, suffixdata, 'S')
if suffix: title = '%s%s' % (title, suffix)

title = escape(title)
Expand Down Expand Up @@ -5692,14 +5719,14 @@ def dynamicQuery(self, *args, **kwargs):
prefixdata = []
for i in range(numprefix):
prefixdata.append(row[prefixstart+i])
prefix = self.smapi_makepresuffix(prefixes, self.replace_pre, prefixdata)
prefix = self.smapi_makepresuffix(prefixes, self.replace_pre, prefixdata, 'P')
if prefix: title = '%s%s' % (prefix, title)
if numsuffix:
# suffixdata = list(row[suffixstart:suffixstart+numsuffix])
suffixdata = []
for i in range(numsuffix):
suffixdata.append(row[suffixstart+i])
suffix = self.smapi_makepresuffix(suffixes, self.replace_suf, suffixdata)
suffix = self.smapi_makepresuffix(suffixes, self.replace_suf, suffixdata, 'S')
if suffix: title = '%s%s' % (title, suffix)


Expand Down Expand Up @@ -5833,7 +5860,8 @@ def dynamicQuery(self, *args, **kwargs):

ret += '</DIDL-Lite>'
count = len(items)
totalMatches = len(items)

if count == 0: ret = ''

return ret, count, totalMatches

Expand Down Expand Up @@ -6699,40 +6727,45 @@ def get_smapi_orderby(self, sorttype, controller, proxy=False):
else:
return (foundvalues['range_field'], foundvalues['index_range'], foundvalues['sort_order'], foundvalues['entry_prefix'], foundvalues['entry_suffix'], at, 'dummy', '')

def smapi_makepresuffix(self, fixes, replace, fixdata):
def smapi_makepresuffix(self, fixes, replace, fixdata, ps):
log.debug(fixes)
log.debug(replace)
log.debug(fixdata)

EMPTY = '__EMPTY__'
outfix = ''
if fixes and fixes != []:
fixcount = 0
for fix in fixes:
data = fixdata[fixcount]
if fix in ['lastplayed', 'inserted', 'created', 'lastmodified', 'lastscanned', 'lastplayed']:
if data == '' or data == 0:
data = self.chunk_metadata_empty
data = EMPTY
else:
try:
data = float(data)
data = time.strftime(self.chunk_metadata_date_format, time.gmtime(data))
except TypeError:
data = self.chunk_metadata_empty
outfix += replace % data
data = EMPTY
elif fix == 'playcount':
if data == '': data = '0'
outfix += replace % data
elif fix == 'year':
if data == '':
data = self.chunk_metadata_empty
data = EMPTY
else:
try:
data = datetime.date.fromordinal(data).strftime(self.chunk_metadata_date_format)
except TypeError:
data = self.chunk_metadata_empty
outfix += replace % data
data = EMPTY
else:
if data == '': data = EMPTY

if data == EMPTY and ps == 'P' and self.dont_display_separator_for_empty_prefix == False:
pass
elif data == EMPTY and ps == 'S' and self.dont_display_separator_for_empty_suffix == False:
pass
else:
if data == '': data = self.chunk_metadata_empty
if data == EMPTY: data = self.chunk_metadata_empty
outfix += replace % data
fixcount += 1
return outfix
Expand Down Expand Up @@ -6886,6 +6919,30 @@ def get_date_epoch_range(self, sdate):

return int(startdate), int(enddate)

def get_delim(self, delimname, default, special, when=None, section=None):
delimsection = 'index entry extras'
if section: delimsection = section
delim = default
try:
delim = self.proxy.config.get(delimsection, delimname)
except ConfigParser.NoSectionError:
pass
except ConfigParser.NoOptionError:
pass
if delim.startswith("'") and delim.endswith("'"):
delim = delim[1:-1]
delim = unicode(delim)
delim = delim.replace(' ', special)
if when:
if when == 'before':
if delim[0] != special:
delim = '%s%s' % (special, delim)
elif when == 'after':
if delim[-1] != special:
delim = '%s%s' % (delim, special)
delim2 = re.escape(delim)
return delim2, delim

def debugout(self, label, data):
if isinstance(data, dict):
log.debug('%s:' % (label))
Expand Down
59 changes: 3 additions & 56 deletions sonospy/proxy.py
Expand Up @@ -393,31 +393,6 @@ def get_Track(self, objectname):
if not self.db_persist_connection:
db.close()

def get_delim(self, delimname, default, special, when=None, section=None):
delimsection = 'index entry extras'
if section: delimsection = section
delim = default
try:
delim = self.config.get(delimsection, delimname)
except ConfigParser.NoSectionError:
pass
except ConfigParser.NoOptionError:
pass
if delim.startswith("'") and delim.endswith("'"):
delim = delim[1:-1]
delim = unicode(delim)
delim = delim.replace(' ', special)
if when:
if when == 'before':
if delim[0] != special:
delim = '%s%s' % (special, delim)
elif when == 'after':
if delim[-1] != special:
delim = '%s%s' % (delim, special)
delim2 = re.escape(delim)
return delim2, delim


class ProxyServerController(webserver.SonosResource):

def __init__(self, proxy, res):
Expand Down Expand Up @@ -1501,12 +1476,12 @@ def __init__(self, proxyaddress, proxy , webserverurl, wmpurl, dbspec, wmpudn):
# check what structure we should use
self.load_ini()

if self.use_browse_for_WMP and not self.use_SMAPI_indexes_for_WMP and self.proxy.smapi:
if self.use_browse_for_WMP and not self.use_SMAPI_indexes_for_WMP:

# create MediaServer with default SMAPI structure
self.mediaServer = MediaServer(self.proxy, self.dbspec, 'HIERARCHY_DEFAULT', self.proxyaddress, self.webserverurl, self.wmpurl)

elif self.use_SMAPI_indexes_for_WMP and self.proxy.smapi:
elif self.use_SMAPI_indexes_for_WMP:

# create MediaServer with user defined SMAPI structure
self.mediaServer = MediaServer(self.proxy, self.dbspec, 'HIERARCHY', self.proxyaddress, self.webserverurl, self.wmpurl)
Expand Down Expand Up @@ -1566,33 +1541,6 @@ def load_ini(self):
except ConfigParser.NoOptionError:
self.use_SMAPI_indexes_for_WMP = False


'''
def get_delim(self, delimname, default, special, when=None, section=None):
delimsection = 'index entry extras'
if section: delimsection = section
delim = default
try:
delim = self.proxy.config.get(delimsection, delimname)
except ConfigParser.NoSectionError:
pass
except ConfigParser.NoOptionError:
pass
if delim.startswith("'") and delim.endswith("'"):
delim = delim[1:-1]
delim = unicode(delim)
delim = delim.replace(' ', special)
if when:
if when == 'before':
if delim[0] != special:
delim = '%s%s' % (special, delim)
elif when == 'after':
if delim[-1] != special:
delim = '%s%s' % (delim, special)
delim2 = re.escape(delim)
return delim2, delim
'''

###################
# DCD soap services
###################
Expand Down Expand Up @@ -1656,9 +1604,8 @@ def soap_Browse(self, *args, **kwargs):

log.debug("self.use_browse_for_WMP: %s", self.use_browse_for_WMP)
log.debug("self.use_SMAPI_indexes_for_WMP: %s", self.use_SMAPI_indexes_for_WMP)
log.debug("self.proxy.smapi: %s", self.proxy.smapi)

if (self.use_SMAPI_indexes_for_WMP or self.use_browse_for_WMP)and self.proxy.smapi:
if (self.use_SMAPI_indexes_for_WMP or self.use_browse_for_WMP):
structure = 'HIERARCHY'
else:
structure = 'FLAT'
Expand Down

0 comments on commit 19f0dfb

Please sign in to comment.