MCPcopy
hub / github.com/iBaa/PlexConnect / XML_ExpandNode

Function XML_ExpandNode

XMLConverter.py:473–532  ·  view source on GitHub ↗
(CommandCollection, elem, child, src, srcXML, text_tail)

Source from the content-addressed store, hash-verified

471
472
473def XML_ExpandNode(CommandCollection, elem, child, src, srcXML, text_tail):
474 if text_tail=='TEXT': # read line from text or tail
475 line = child.text
476 elif text_tail=='TAIL':
477 line = child.tail
478 else:
479 dprint(__name__, 0, "XML_ExpandNode - text_tail badly specified: {0}", text_tail)
480 return False
481
482 pos = 0
483 while line!=None:
484 cmd_start = line.find('{{',pos)
485 cmd_end = line.find('}}',pos)
486 next_start = line.find('{{',cmd_start+2)
487 while next_start!=-1 and next_start<cmd_end:
488 cmd_end = line.find('}}',cmd_end+2)
489 next_start = line.find('{{',next_start+2)
490 if cmd_start==-1 or cmd_end==-1 or cmd_start>cmd_end:
491 return False # tree not touched, line unchanged
492
493 dprint(__name__, 2, "XML_ExpandNode: {0}", line)
494
495 cmd = line[cmd_start+2:cmd_end]
496 if cmd[-1]!=')':
497 dprint(__name__, 0, "XML_ExpandNode - closing bracket missing: {0} ", line)
498
499 parts = cmd.split('(',1)
500 cmd = parts[0]
501 param = parts[1].strip(')') # remove ending bracket
502
503 res = False
504 if hasattr(CCommandCollection, 'TREE_'+cmd): # expand tree, work COPY, CUT
505 line = line[:cmd_start] + line[cmd_end+2:] # remove cmd from text and tail
506 if text_tail=='TEXT':
507 child.text = line
508 elif text_tail=='TAIL':
509 child.tail = line
510
511 try:
512 param = XML_ExpandLine(CommandCollection, src, srcXML, param) # expand any attributes in the parameter
513 res = getattr(CommandCollection, 'TREE_'+cmd)(elem, child, src, srcXML, param)
514 except:
515 dprint(__name__, 0, "XML_ExpandNode - Error in cmd {0}, line {1}\n{2}", cmd, line, traceback.format_exc())
516
517 if res==True:
518 return True # tree modified, node added/removed: restart from 1st elem
519
520 elif hasattr(CCommandCollection, 'ATTRIB_'+cmd): # check other known cmds: VAL, EVAL...
521 dprint(__name__, 2, "XML_ExpandNode - Stumbled over {0} in line {1}", cmd, line)
522 pos = cmd_end
523 else:
524 dprint(__name__, 0, "XML_ExpandNode - Found unknown cmd {0} in line {1}", cmd, line)
525 line = line[:cmd_start] + "((UNKNOWN:"+cmd+"))" + line[cmd_end+2:] # mark unknown cmd in text or tail
526 if text_tail=='TEXT':
527 child.text = line
528 elif text_tail=='TAIL':
529 child.tail = line
530

Callers 1

XML_ExpandTreeFunction · 0.85

Calls 2

dprintFunction · 0.85
XML_ExpandLineFunction · 0.85

Tested by

no test coverage detected