1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
|
# coding=utf-8
import os, sys, shutil, unicodedata, re, types
from htmlentitydefs import name2codepoint
import xbmc, xbmcaddon, xbmcplugin, xbmcgui, xbmcvfs
import xml.etree.ElementTree as xmltree
import urllib
from unidecode import unidecode
from urlparse import parse_qs
from traceback import print_exc
import json
ADDON = xbmcaddon.Addon()
ADDONID = ADDON.getAddonInfo('id').decode( 'utf-8' )
ADDONVERSION = ADDON.getAddonInfo('version')
LANGUAGE = ADDON.getLocalizedString
CWD = ADDON.getAddonInfo('path').decode("utf-8")
ADDONNAME = ADDON.getAddonInfo('name').decode("utf-8")
RESOURCE = xbmc.translatePath( os.path.join( CWD, 'resources', 'lib' ) ).decode("utf-8")
DATAPATH = xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('profile')).decode('utf-8')
sys.path.append(RESOURCE)
# character entity reference
CHAR_ENTITY_REXP = re.compile('&(%s);' % '|'.join(name2codepoint))
# decimal character reference
DECIMAL_REXP = re.compile('&#(\d+);')
# hexadecimal character reference
HEX_REXP = re.compile('&#x([\da-fA-F]+);')
REPLACE1_REXP = re.compile(r'[\']+')
REPLACE2_REXP = re.compile(r'[^-a-z0-9]+')
REMOVE_REXP = re.compile('-{2,}')
def log(txt):
if isinstance (txt,str):
txt = txt.decode('utf-8')
message = u'%s: %s' % (ADDONID, txt)
xbmc.log(msg=message.encode('utf-8'), level=xbmc.LOGDEBUG)
class Main:
# MAIN ENTRY POINT
def __init__(self):
self._parse_argv()
# If there are no custom library nodes in the profile directory, copy them from the Kodi install
targetDir = os.path.join( xbmc.translatePath( "special://profile".decode('utf-8') ), "library", ltype )
if True:
if not os.path.exists( targetDir ):
xbmcvfs.mkdirs( targetDir )
originDir = os.path.join( xbmc.translatePath( "special://xbmc".decode( "utf-8" ) ), "system", "library", ltype )
dirs, files = xbmcvfs.listdir( originDir )
self.copyNode( dirs, files, targetDir, originDir )
else:
xbmcgui.Dialog().ok(ADDONNAME, LANGUAGE( 30400 ) )
print_exc
xbmcplugin.endOfDirectory(handle=int(sys.argv[1]))
return
# Create data if not exists
if not os.path.exists(DATAPATH):
xbmcvfs.mkdir(DATAPATH)
if "type" in self.PARAMS:
# We're performing a specific action
if self.PARAMS[ "type" ] == "delete":
message = LANGUAGE( 30401 )
if self.PARAMS[ "actionPath" ] == targetDir:
# Ask the user is they want to reset all nodes
message = LANGUAGE( 30402 )
result = xbmcgui.Dialog().yesno(ADDONNAME, message )
if result:
if self.PARAMS[ "actionPath" ].endswith( ".xml" ):
# Delete single file
xbmcvfs.delete( self.PARAMS[ "actionPath" ] )
else:
# Delete folder
RULE.deleteAllNodeRules( self.PARAMS[ "actionPath" ] )
shutil.rmtree( self.PARAMS[ "actionPath" ] )
else:
return
elif self.PARAMS[ "type" ] == "deletenode":
result = xbmcgui.Dialog().yesno(ADDONNAME, LANGUAGE( 30403 ) )
if result:
self.changeViewElement( self.PARAMS[ "actionPath" ], self.PARAMS[ "node" ], "" )
elif self.PARAMS[ "type" ] == "editlabel":
if self.PARAMS[ "label" ].isdigit():
label = xbmc.getLocalizedString( int( self.PARAMS[ "label" ] ) )
else:
label = self.PARAMS[ "label" ]
# Get new label from keyboard dialog
keyboard = xbmc.Keyboard( label, LANGUAGE( 30300 ), False )
keyboard.doModal()
if ( keyboard.isConfirmed() ):
newlabel = keyboard.getText().decode( "utf-8" )
if newlabel != "" and newlabel != label:
# We've got a new label, update the xml file
self.changeViewElement( self.PARAMS[ "actionPath" ], "label", newlabel )
else:
return
elif self.PARAMS[ "type" ] == "editvisibility":
currentVisibility = self.getRootAttrib( self.PARAMS[ "actionPath" ], "visible" )
# Get new visibility from keyboard dialog
keyboard = xbmc.Keyboard( currentVisibility, LANGUAGE( 30301 ), False )
keyboard.doModal()
if ( keyboard.isConfirmed() ):
newVisibility = keyboard.getText()
if newVisibility != currentVisibility:
# We've got a new label, update the xml file
self.changeRootAttrib( self.PARAMS[ "actionPath" ], "visible", newVisibility )
else:
return
elif self.PARAMS[ "type" ] == "moveNode":
self.indexCounter = -1
# Get existing nodes
nodes = {}
self.listNodes( self.PARAMS[ "actionPath" ], nodes )
# Get updated order
newOrder = moveNodes.getNewOrder( nodes, int( self.PARAMS[ "actionItem" ] ) )
if newOrder is not None:
# Update the orders
for i, node in enumerate( newOrder, 1 ):
path = urllib.unquote( node[ 2 ] )
if node[ 3 ] == "folder":
path = os.path.join( urllib.unquote( node[ 2 ] ), "index.xml" )
self.changeRootAttrib( path, "order", str( i * 10 ) )
elif self.PARAMS[ "type" ] == "newView":
# Get new view name from keyboard dialog
keyboard = xbmc.Keyboard( "", LANGUAGE( 30316 ), False )
keyboard.doModal()
if ( keyboard.isConfirmed() ):
newView = keyboard.getText().decode( "utf-8" )
if newView != "":
# Ensure filename is unique
filename = self.slugify( newView.lower().replace( " ", "" ) )
if os.path.exists( os.path.join( self.PARAMS[ "actionPath" ], filename + ".xml" ) ):
count = 0
while os.path.exists( os.path.join( self.PARAMS[ "actionPath" ], filename + "-" + str( count ) + ".xml" ) ):
count += 1
filename = filename + "-" + str( count )
# Create a new xml file
tree = xmltree.ElementTree( xmltree.Element( "node" ) )
root = tree.getroot()
subtree = xmltree.SubElement( root, "label" ).text = newView
# Add any node rules
RULE.addAllNodeRules( self.PARAMS[ "actionPath" ], root )
# Write the xml file
self.indent( root )
tree.write( os.path.join( self.PARAMS[ "actionPath" ], filename + ".xml" ), encoding="UTF-8" )
else:
return
elif self.PARAMS[ "type" ] == "newNode":
# Get new node name from the keyboard dialog
keyboard = xbmc.Keyboard( "", LANGUAGE( 30303 ), False )
keyboard.doModal()
if ( keyboard.isConfirmed() ):
newNode = keyboard.getText().decode( "utf8" )
if newNode == "":
return
# Ensure foldername is unique
foldername = self.slugify( newNode.lower().replace( " ", "" ) )
if os.path.exists( os.path.join( self.PARAMS[ "actionPath" ], foldername + os.pathsep ) ):
count = 0
while os.path.exists( os.path.join( self.PARAMS[ "actionPath" ], foldername + "-" + str( count ) + os.pathsep ) ):
count += 1
foldername = foldername + "-" + str( count )
foldername = os.path.join( self.PARAMS[ "actionPath" ], foldername )
# Create new node folder
xbmcvfs.mkdir( foldername )
# Create a new xml file
tree = xmltree.ElementTree( xmltree.Element( "node" ) )
root = tree.getroot()
subtree = xmltree.SubElement( root, "label" ).text = newNode
# Ask user if they want to import defaults
if ltype.startswith( "video" ):
defaultNames = [ xbmc.getLocalizedString( 231 ), xbmc.getLocalizedString( 342 ), xbmc.getLocalizedString( 20343 ), xbmc.getLocalizedString( 20389 ) ]
defaultValues = [ "", "movies", "tvshows", "musicvideos" ]
selected = xbmcgui.Dialog().select( LANGUAGE( 30304 ), defaultNames )
else:
selected = 0
# If the user selected some defaults...
if selected != -1 and selected != 0:
try:
# Copy those defaults across
originDir = os.path.join( xbmc.translatePath( "special://xbmc".decode( "utf-8" ) ), "system", "library", ltype, defaultValues[ selected ] )
dirs, files = xbmcvfs.listdir( originDir )
for file in files:
if file != "index.xml":
xbmcvfs.copy( os.path.join( originDir, file), os.path.join( foldername, file ) )
# Open index.xml and copy values across
index = xmltree.parse( os.path.join( originDir, "index.xml" ) ).getroot()
if "visible" in index.attrib:
root.set( "visible", index.attrib.get( "visible" ) )
icon = index.find( "icon" )
if icon is not None:
xmltree.SubElement( root, "icon" ).text = icon.text
except:
print_exc()
# Write the xml file
self.indent( root )
tree.write( os.path.join( foldername, "index.xml" ), encoding="UTF-8" )
else:
return
elif self.PARAMS[ "type" ] == "rule":
# Display list of all elements of a rule
RULE.displayRule( self.PARAMS[ "actionPath" ], self.PATH, self.PARAMS[ "rule" ] )
return
elif self.PARAMS[ "type" ] == "editMatch":
# Editing the field the rule is matched against
RULE.editMatch( self.PARAMS[ "actionPath" ], self.PARAMS[ "rule" ], self.PARAMS[ "content"], self.PARAMS[ "default" ] )
elif self.PARAMS[ "type" ] == "editOperator":
# Editing the operator of a rule
RULE.editOperator( self.PARAMS[ "actionPath" ], self.PARAMS[ "rule" ], self.PARAMS[ "group" ], self.PARAMS[ "default" ] )
elif self.PARAMS[ "type" ] == "editValue":
# Editing the value of a rule
RULE.editValue( self.PARAMS[ "actionPath" ], self.PARAMS[ "rule" ] )
elif self.PARAMS[ "type" ] == "browseValue":
# Browse for the new value of a rule
RULE.browse( self.PARAMS[ "actionPath" ], self.PARAMS[ "rule" ], self.PARAMS[ "match" ], self.PARAMS[ "content" ] )
elif self.PARAMS[ "type" ] == "deleteRule":
# Delete a rule
RULE.deleteRule( self.PARAMS[ "actionPath" ], self.PARAMS[ "rule" ] )
elif self.PARAMS[ "type" ] == "editRulesMatch":
# Editing whether any or all rules must match
ATTRIB.editMatch( self.PARAMS[ "actionPath" ] )
# --- Edit order-by ---
elif self.PARAMS[ "type" ] == "orderby":
# Display all elements of order by
ORDERBY.displayOrderBy( self.PARAMS[ "actionPath" ] )
return
elif self.PARAMS[ "type" ] == "editOrderBy":
ORDERBY.editOrderBy( self.PARAMS[ "actionPath" ], self.PARAMS[ "content" ], self.PARAMS[ "default" ] )
elif self.PARAMS[ "type" ] == "editOrderByDirection":
ORDERBY.editDirection( self.PARAMS[ "actionPath" ], self.PARAMS[ "default" ] )
# --- Edit paths ---
elif self.PARAMS[ "type" ] == "addPath":
ATTRIB.addPath( self.PARAMS[ "actionPath" ] )
elif self.PARAMS[ "type" ] == "editPath":
ATTRIB.editPath( self.PARAMS[ "actionPath" ], self.PARAMS[ "value" ] )
elif self.PARAMS[ "type" ] == "pathRule":
PATHRULE.displayRule( self.PARAMS[ "actionPath" ], int( self.PARAMS[ "rule" ] ) )
return
elif self.PARAMS[ "type" ] == "deletePathRule":
ATTRIB.deletePathRule( self.PARAMS[ "actionPath" ], int( self.PARAMS[ "rule" ] ) )
elif self.PARAMS[ "type" ] == "editPathMatch":
# Editing the field the rule is matched against
PATHRULE.editMatch( self.PARAMS[ "actionPath" ], int( self.PARAMS[ "rule" ] ) )
elif self.PARAMS[ "type" ] == "editPathValue":
# Editing the value of a rule
PATHRULE.editValue( self.PARAMS[ "actionPath" ], int( self.PARAMS[ "rule" ] ) )
elif self.PARAMS[ "type" ] == "browsePathValue":
# Browse for the new value of a rule
PATHRULE.browse( self.PARAMS[ "actionPath" ], int( self.PARAMS[ "rule" ] ) )
# --- Edit other attribute of view ---
# > Content
elif self.PARAMS[ "type" ] == "editContent":
ATTRIB.editContent( self.PARAMS[ "actionPath" ], "" ) # No default to pass, yet!
# > Grouping
elif self.PARAMS[ "type" ] == "editGroup":
ATTRIB.editGroup( self.PARAMS[ "actionPath" ], self.PARAMS[ "content" ], "" )
# > Limit
elif self.PARAMS[ "type" ] == "editLimit":
ATTRIB.editLimit( self.PARAMS[ "actionPath" ], self.PARAMS[ "value" ] )
# > Icon (also for node)
elif self.PARAMS[ "type" ] == "editIcon":
ATTRIB.editIcon( self.PARAMS[ "actionPath" ], self.PARAMS[ "value" ] )
elif self.PARAMS[ "type" ] == "browseIcon":
ATTRIB.browseIcon( self.PARAMS[ "actionPath" ] )
# Refresh the listings and exit
xbmc.executebuiltin("Container.Refresh")
return
if self.PATH.endswith( ".xml" ):
self.RulesList()
else:
self.NodesList(targetDir)
def NodesList( self, targetDir ):
# List nodes and views
nodes = {}
self.indexCounter = -1
if self.PATH != "":
self.listNodes( self.PATH, nodes )
else:
self.listNodes( targetDir, nodes )
self.PATH = urllib.quote( self.PATH )
for i, key in enumerate( sorted( nodes ) ):
# 0 = Label
# 1 = Icon
# 2 = Path
# 3 = Type
# 4 = Order
# Localize the label
if nodes[ key ][ 0 ].isdigit():
label = xbmc.getLocalizedString( int( nodes[ key ][ 0 ] ) )
else:
label = nodes[ key ][ 0 ]
# Create the listitem
if nodes[ key ][ 3 ] == "folder":
listitem = xbmcgui.ListItem( label="%s >" % ( label ), label2=nodes[ key ][ 4 ], iconImage=nodes[ key ][ 1 ] )
else:
listitem = xbmcgui.ListItem( label=label, label2=nodes[ key ][ 4 ], iconImage=nodes[ key ][ 1 ] )
# Add context menu items
commands = []
commandsNode = []
commandsView = []
commandsNode.append( ( LANGUAGE(30101), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=editlabel&actionPath=" % ltype + os.path.join( nodes[ key ][ 2 ], "index.xml" ) + "&label=" + nodes[ key ][ 0 ] + ")" ) )
commandsNode.append( ( LANGUAGE(30102), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=editIcon&actionPath=" % ltype + os.path.join( nodes[ key ][ 2 ], "index.xml" ) + "&value=" + nodes[ key ][ 1 ] + ")" ) )
commandsNode.append( ( LANGUAGE(30103), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=browseIcon&actionPath=" % ltype + os.path.join( nodes [ key ][ 2 ], "index.xml" ) + ")" ) )
if self.PATH == "":
commandsNode.append( ( LANGUAGE(30104), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=moveNode&actionPath=" % ltype + targetDir + "&actionItem=" + str( i ) + ")" ) )
else:
commandsNode.append( ( LANGUAGE(30104), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=moveNode&actionPath=" % ltype + self.PATH + "&actionItem=" + str( i ) + ")" ) )
commandsNode.append( ( LANGUAGE(30105), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=editvisibility&actionPath=" % ltype + os.path.join( nodes[ key ][ 2 ], "index.xml" ) + ")" ) )
commandsNode.append( ( LANGUAGE(30100), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=delete&actionPath=" % ltype + nodes[ key ][ 2 ] + ")" ) )
commandsView.append( ( LANGUAGE(30101), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=editlabel&actionPath=" % ltype + nodes[ key ][ 2 ] + "&label=" + nodes[ key ][ 0 ] + ")" ) )
commandsView.append( ( LANGUAGE(30102), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=editIcon&actionPath=" % ltype + nodes[ key ][ 2 ] + "&value=" + nodes[ key ][ 1 ] + ")" ) )
commandsView.append( ( LANGUAGE(30103), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=browseIcon&actionPath=" % ltype + nodes[ key ][ 2 ] + ")" ) )
if self.PATH == "":
commandsView.append( ( LANGUAGE(30104), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=moveNode&actionPath=" % ltype + targetDir + "&actionItem=" + str( i ) + ")" ) )
else:
commandsView.append( ( LANGUAGE(30104), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=moveNode&actionPath=" % ltype + self.PATH + "&actionItem=" + str( i ) + ")" ) )
commandsView.append( ( LANGUAGE(30105), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=editvisibility&actionPath=" % ltype + nodes[ key ][ 2 ] + ")" ) )
commandsView.append( ( LANGUAGE(30100), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=delete&actionPath=" % ltype + nodes[ key ][ 2 ] + ")" ) )
if nodes[ key ][ 3 ] == "folder":
listitem.addContextMenuItems( commandsNode, replaceItems = True )
xbmcplugin.addDirectoryItem( int(sys.argv[ 1 ]), "plugin://plugin.library.node.editor?ltype=%s&path=" % ltype + nodes[ key ][ 2 ], listitem, isFolder=True )
else:
listitem.addContextMenuItems( commandsView, replaceItems = True )
xbmcplugin.addDirectoryItem( int(sys.argv[ 1 ]), "plugin://plugin.library.node.editor?ltype=%s&path=" % ltype + nodes[ key ][ 2 ], listitem, isFolder=True )
if self.PATH != "":
# Get any rules from the index.xml
rules, nextRule = self.getRules( os.path.join( urllib.unquote( self.PATH ), "index.xml" ), True )
rulecount = 0
if rules is not None:
for rule in rules:
commands = []
if rule[ 0 ] == "rule":
# 1 = field
# 2 = operator
# 3 = value (optional)
if len(rule) == 3:
translated = RULE.translateRule( [ rule[ 1 ], rule[ 2 ] ] )
else:
translated = RULE.translateRule( [ rule[ 1 ], rule[ 2 ], rule[ 3 ] ] )
if len(translated) == 2:
listitem = xbmcgui.ListItem( label="%s: %s %s" % ( LANGUAGE(30205), translated[ 0 ][ 0 ], translated[ 1 ][ 0 ] ) )
else:
listitem = xbmcgui.ListItem( label="%s: %s %s %s" % ( LANGUAGE(30205), translated[ 0 ][ 0 ], translated[ 1 ][ 0 ], translated[ 2 ][ 1 ] ) )
commands.append( ( LANGUAGE( 30100 ), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=deleteRule&actionPath=" % ltype + os.path.join( self.PATH, "index.xml" ) + "&rule=" + str( rulecount ) + ")" ) )
action = "plugin://plugin.library.node.editor?ltype=%s&type=rule&actionPath=" % ltype + os.path.join( self.PATH, "index.xml" ) + "&rule=" + str( rulecount )
rulecount += 1
listitem.addContextMenuItems( commands, replaceItems = True )
if rule[ 0 ] == "rule" or rule[ 0 ] == "order":
xbmcplugin.addDirectoryItem( int(sys.argv[ 1 ]), action, listitem, isFolder=True )
else:
xbmcplugin.addDirectoryItem( int(sys.argv[ 1 ]), action, listitem, isFolder=False )
# New rule
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=rule&actionPath=" % ltype + os.path.join( self.PATH, "index.xml" ) + "&rule=" + str( nextRule), xbmcgui.ListItem( label="* %s" %( LANGUAGE(30005) ) ), isFolder=True )
showReset = False
if self.PATH == "":
self.PATH = urllib.quote( targetDir )
showReset = True
# New view and node
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=newView&actionPath=" % ltype + self.PATH, xbmcgui.ListItem( label="* %s" %( LANGUAGE(30006) ) ), isFolder=False )
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=newNode&actionPath=" % ltype + self.PATH, xbmcgui.ListItem( label="* %s" %( LANGUAGE(30007) ) ), isFolder=False )
if showReset:
xbmcplugin.addDirectoryItem( int(sys.argv[ 1 ]), "plugin://plugin.library.node.editor?ltype=%s&type=delete&actionPath=" % ltype + targetDir, xbmcgui.ListItem( label="* %s" %( LANGUAGE(30008) ) ), isFolder=False )
xbmcplugin.setContent(int(sys.argv[1]), 'files')
xbmcplugin.endOfDirectory(handle=int(sys.argv[1]))
def RulesList( self ):
# List rules for specific view
rules, nextRule = self.getRules( self.PATH )
hasContent = False
content = ""
hasOrder = False
hasGroup = False
hasLimit = False
hasPath = False
splitPath = None
rulecount = 0
self.PATH = self.PATH
if rules is not None:
for rule in rules:
commands = []
if rule[ 0 ] == "content":
# 1 = Content
listitem = xbmcgui.ListItem( label="%s: %s" % ( LANGUAGE(30200), ATTRIB.translateContent( rule[ 1 ] ) ) )
commands.append( ( LANGUAGE(30100), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=deletenode&actionPath=" % ltype + self.PATH + "&node=content)" ) )
action = "plugin://plugin.library.node.editor?ltype=%s&type=editContent&actionPath=" % ltype + self.PATH
hasContent = True
content = rule[ 1 ]
elif rule[ 0 ] == "order":
# 1 = orderby
# 2 = direction (optional?)
if len( rule ) == 3:
translate = ORDERBY.translateOrderBy( [ rule[ 1 ], rule[ 2 ] ] )
listitem = xbmcgui.ListItem( label="%s: %s (%s)" % ( LANGUAGE(30201), translate[ 0 ][ 0 ], translate[ 1 ][ 0 ] ) )
else:
translate = ORDERBY.translateOrderBy( [ rule[ 1 ], "" ] )
listitem = xbmcgui.ListItem( label="%s: %s" % ( LANGUAGE(30201), translate[ 0 ][ 0 ] ) )
commands.append( ( LANGUAGE(30100), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=deletenode&actionPath=" % ltype + self.PATH + "&node=order)" ) )
action = "plugin://plugin.library.node.editor?ltype=%s&type=orderby&actionPath=" % ltype + self.PATH
hasOrder = True
elif rule[ 0 ] == "group":
# 1 = group
listitem = xbmcgui.ListItem( label="%s: %s" % ( LANGUAGE(30202), ATTRIB.translateGroup( rule[ 1 ] ) ) )
commands.append( ( LANGUAGE(30100), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=deletenode&actionPath=" % ltype + self.PATH + "&node=group)" ) )
action = "plugin://plugin.library.node.editor?ltype=%s&type=editGroup&actionPath=" % ltype + self.PATH + "&value=" + rule[ 1 ] + "&content=" + content
hasGroup = True
elif rule[ 0 ] == "limit":
# 1 = limit
listitem = xbmcgui.ListItem( label="%s: %s" % ( LANGUAGE(30203), rule[ 1 ] ) )
commands.append( ( LANGUAGE(30100), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=deletenode&actionPath=" % ltype + self.PATH + "&node=limit)" ) )
action = "plugin://plugin.library.node.editor?ltype=%s&type=editLimit&actionPath=" % ltype + self.PATH + "&value=" + rule[ 1 ]
hasLimit = True
elif rule[ 0 ] == "path":
# 1 = path
# Split the path into components
splitPath = ATTRIB.splitPath( rule[ 1 ] )
# Add each element of the path to the list
for x, component in enumerate( splitPath ):
if x == 0:
# library://path/
listitem = xbmcgui.ListItem( label="%s: %s" % ( LANGUAGE(30204), ATTRIB.translatePath( component ) ) )
commands.append( ( LANGUAGE(30100), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=deletenode&actionPath=" % ltype + self.PATH + "&node=path)" ) )
action = "plugin://plugin.library.node.editor?ltype=%s&type=addPath&actionPath=" % ltype + self.PATH
# Get the rules
rules = PATHRULE.getRulesForPath( splitPath[ 0 ] )
if x != 0:
# Specific component
# Add the listitem generated from the last component we processed
listitem.addContextMenuItems( commands, replaceItems = True )
xbmcplugin.addDirectoryItem( int(sys.argv[ 1 ]), action, listitem, isFolder=True )
commands = []
# Get the rule for this component
componentRule = PATHRULE.getMatchingRule( component, rules )
translatedComponent = PATHRULE.translateComponent( componentRule, splitPath[ x ] )
translatedValue = PATHRULE.translateValue( componentRule, splitPath, x )
listitem = xbmcgui.ListItem( label="%s: %s" % ( translatedComponent, translatedValue ) )
commands.append( ( LANGUAGE(30100), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=deletePathRule&actionPath=%s&rule=%d)" %( ltype, self.PATH, x ) ) )
action = "plugin://plugin.library.node.editor?ltype=%s&type=pathRule&actionPath=%s&rule=%d" % ( ltype, self.PATH, x )
hasPath = True
elif rule[ 0 ] == "rule":
# 1 = field
# 2 = operator
# 3 = value (optional)
# 4 = ruleNum
if len(rule) == 3:
translated = RULE.translateRule( [ rule[ 1 ], rule[ 2 ] ] )
else:
translated = RULE.translateRule( [ rule[ 1 ], rule[ 2 ], rule[ 3 ] ] )
if translated[ 2 ][ 0 ] == "|NONE|":
listitem = xbmcgui.ListItem( label="%s: %s %s" % ( LANGUAGE(30205), translated[ 0 ][ 0 ], translated[ 1 ][ 0 ] ) )
else:
listitem = xbmcgui.ListItem( label="%s: %s %s %s" % ( LANGUAGE(30205), translated[ 0 ][ 0 ], translated[ 1 ][ 0 ], translated[ 2 ][ 1 ] ) )
commands.append( ( LANGUAGE(30100), "XBMC.RunPlugin(plugin://plugin.library.node.editor?ltype=%s&type=deleteRule&actionPath=" % ltype + self.PATH + "&rule=" + str( rule[ 4 ] ) + ")" ) )
action = "plugin://plugin.library.node.editor?ltype=%s&type=rule&actionPath=" % ltype + self.PATH + "&rule=" + str( rule[ 4 ] )
rulecount += 1
elif rule[ 0 ] == "match":
# 1 = value
listitem = xbmcgui.ListItem( label="%s: %s" % ( LANGUAGE(30206), ATTRIB.translateMatch( rule[ 1 ] ) ) )
action = "plugin://plugin.library.node.editor?ltype=%s&type=editRulesMatch&actionPath=%s" %( ltype, self.PATH )
hasGroup = True
listitem.addContextMenuItems( commands, replaceItems = True )
if rule[ 0 ] == "rule" or rule[ 0 ] == "order" or rule[ 0 ] == "path":
xbmcplugin.addDirectoryItem( int(sys.argv[ 1 ]), action, listitem, isFolder=True )
else:
xbmcplugin.addDirectoryItem( int(sys.argv[ 1 ]), action, listitem, isFolder=False )
if not hasContent and not hasPath:
# Add content
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=editContent&actionPath=" % ltype + self.PATH, xbmcgui.ListItem( label="* %s" %( LANGUAGE(30000) ) ) )
if not hasOrder and hasContent:
# Add order
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=orderby&actionPath=" % ltype + self.PATH, xbmcgui.ListItem( label="* %s" %( LANGUAGE(30002) ) ), isFolder=True )
if not hasGroup and hasContent:
# Add group
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=editGroup&actionPath=" % ltype + self.PATH + "&content=" + content, xbmcgui.ListItem( label="* %s" %( LANGUAGE(30004) ) ) )
if not hasLimit and hasContent:
# Add limit
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=editLimit&actionPath=" % ltype + self.PATH + "&value=25", xbmcgui.ListItem( label="* %s" %( LANGUAGE(30003) ) ) )
if not hasPath and not hasContent:
# Add path
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=addPath&actionPath=" % ltype + self.PATH, xbmcgui.ListItem( label="* %s" %( LANGUAGE(30001) ) ) )
if hasContent:
# Add rule
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=rule&actionPath=" % ltype + self.PATH + "&rule=" + str( nextRule ), xbmcgui.ListItem( label="* %s" %( LANGUAGE(30005) ) ), isFolder = True )
if hasPath:
if "plugin://" not in splitPath[0][0]:
# Add component
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=pathRule&actionPath=%s&rule=%d" % ( ltype, self.PATH, x + 1 ), xbmcgui.ListItem( label="* %s" %( LANGUAGE(30009) ) ), isFolder = True )
# Manually edit path
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s&type=editPath&actionPath=" % ltype + self.PATH + "&value=" + urllib.quote( rule[ 1 ] ), xbmcgui.ListItem( label="* %s" %( LANGUAGE(30010) ) ), isFolder = True )
xbmcplugin.setContent(int(sys.argv[1]), 'files')
xbmcplugin.endOfDirectory(handle=int(sys.argv[1]))
def _parse_argv( self ):
try:
p = parse_qs(sys.argv[2][1:])
for i in p.keys():
p[i] = p[i][0].decode( "utf-8" )
self.PARAMS = p
except:
p = parse_qs(sys.argv[1])
for i in p.keys():
p[i] = p[i][0].decode( "utf-8" )
self.PARAMS = p
if "path" in self.PARAMS:
self.PATH = self.PARAMS[ "path" ]
else:
self.PATH = ""
def getRules( self, actionPath, justRules = False ):
returnVal = []
try:
# Load the xml file
tree = xmltree.parse( actionPath )
root = tree.getroot()
if justRules == False:
# Look for a 'content'
content = root.find( "content" )
if content is not None:
returnVal.append( ( "content", content.text.decode( "utf-8" ) ) )
# Look for an 'order'
order = root.find( "order" )
if order is not None:
if "direction" in order.attrib:
returnVal.append( ( "order", order.text, order.attrib.get( "direction" ) ) )
else:
returnVal.append( ( "order", order.text ) )
# Look for a 'group'
group = root.find( "group" )
if group is not None:
returnVal.append( ( "group", group.text ) )
# Look for a 'limit'
limit = root.find( "limit" )
if limit is not None:
returnVal.append( ( "limit", limit.text ) )
# Look for a 'path'
path = root.find( "path" )
if path is not None:
returnVal.append( ( "path", path.text ) )
# Save the current length of the returnVal - we'll insert Match here if there are two or more rules
currentLen = len( returnVal )
# Look for any rules
ruleNum = 0
if actionPath.endswith( "index.xml" ):
# Load the rules from RULE module
rules = RULE.getNodeRules( actionPath )
if rules is not None:
for rule in rules:
returnVal.append( ( "rule", rule[ 0 ], rule[ 1 ], rule[ 2 ], ruleNum ) )
ruleNum += 1
return returnVal, len( rules )
else:
return returnVal, 0
else:
rules = root.findall( "rule" )
# Process the rules
if rules is not None:
for rule in rules:
value = rule.find( "value" )
if value is not None and value.text is not None:
translated = RULE.translateRule( [ rule.attrib.get( "field" ), rule.attrib.get( "operator" ), value.text ] )
if not RULE.isNodeRule( translated, actionPath ):
returnVal.append( ( "rule", rule.attrib.get( "field" ), rule.attrib.get( "operator" ), value.text, ruleNum ) )
else:
translated = RULE.translateRule( [ rule.attrib.get( "field" ), rule.attrib.get( "operator" ), "" ] )
if not RULE.isNodeRule( translated, actionPath ):
returnVal.append( ( "rule", rule.attrib.get( "field" ), rule.attrib.get( "operator" ), "", ruleNum ) )
ruleNum += 1
# Get any current match value if there are more than two rules
# (if there's only one, the match value doesn't matter)
if ruleNum >= 2:
matchRules = "all"
match = root.find( "match" )
if match is not None:
matchRules = match.text
returnVal.insert( currentLen, ( "match", matchRules ) )
return returnVal, len( rules )
return returnVal, 0
except:
print_exc()
def listNodes( self, targetDir, nodes ):
dirs, files = xbmcvfs.listdir( targetDir )
for dir in dirs:
self.parseNode( os.path.join( targetDir, dir ), nodes )
for file in files:
self.parseItem( os.path.join( targetDir, file.decode( "utf-8" ) ), nodes )
def parseNode( self, node, nodes ):
# If the folder we've been passed contains an index.xml, send that file to be processed
if os.path.exists( os.path.join( node, "index.xml" ) ):
# BETA2 ONLY CODE
RULE.moveNodeRuleToAppdata( node, os.path.join( node, "index.xml" ) )
# /BETA2 ONLY CODE
self.parseItem( os.path.join( node, "index.xml" ), nodes, True, node )
def parseItem( self, file, nodes, isFolder = False, origFolder = None ):
if not isFolder and file.endswith( "index.xml" ):
return
try:
# Load the xml file
tree = xmltree.parse( file )
root = tree.getroot()
# Get the item index
if "order" in tree.getroot().attrib:
index = tree.getroot().attrib.get( "order" )
origIndex = index
while int( index ) in nodes:
index = int( index )
index += 1
index = str( index )
else:
self.indexCounter -= 1
index = str( self.indexCounter )
origIndex = "-"
# Get label and icon
label = root.find( "label" ).text
icon = root.find( "icon" )
if icon is not None:
icon = icon.text
else:
icon = ""
# Add it to our list of nodes
if isFolder:
nodes[ int( index ) ] = [ label, icon, urllib.quote( origFolder.decode( "utf-8" ) ), "folder", origIndex ]
else:
nodes[ int( index ) ] = [ label, icon, file, "item", origIndex ]
except:
print_exc()
def getViewElement( self, file, element, newvalue ):
try:
# Load the file
tree = xmltree.parse( file )
root = tree.getroot()
# Change the element
node = root.find( element )
if node is not None:
return node.text
else:
return ""
except:
print_exc()
def changeViewElement( self, file, element, newvalue ):
try:
# Load the file
tree = xmltree.parse( file )
root = tree.getroot()
# If the element is content, we can only delete this if there are no
# rules, limits, orders
if element == "content":
rule = root.find( "rule" )
order = root.find( "order" )
limit = root.find( "limit" )
if rule is not None or order is not None or limit is not None:
xbmcgui.Dialog().ok( ADDONNAME, LANGUAGE( 30404 ) )
return
# Find the element
node = root.find( element )
if node is not None:
# If we've been passed an empty value, delete the node
if newvalue == "":
root.remove( node )
else:
node.text = newvalue
else:
# Add a new node
if newvalue != "":
xmltree.SubElement( root, element ).text = newvalue
# Pretty print and save
self.indent( root )
tree.write( file, encoding="UTF-8" )
except:
print_exc()
def getRootAttrib( self, file, attrib ):
try:
# Load the file
tree = xmltree.parse( file )
root = tree.getroot()
# Find the element
if attrib in root.attrib:
return root.attrib.get( attrib )
else:
return ""
except:
print_exc()
def changeRootAttrib( self, file, attrib, newvalue ):
try:
# Load the file
tree = xmltree.parse( file )
root = tree.getroot()
# If empty newvalue, delete the attribute
if newvalue == "":
if attrib in root.attrib:
root.attrib.pop( attrib )
else:
# Change or add the attribute
root.set( attrib, newvalue )
# Pretty print and save
self.indent( root )
tree.write( file, encoding="UTF-8" )
except:
print_exc()
def copyNode(self, dirs, files, target, origin):
for file in files:
success = xbmcvfs.copy( os.path.join( origin, file ), os.path.join( target, file ) )
for dir in dirs:
nextDirs, nextFiles = xbmcvfs.listdir( os.path.join( origin, dir ) )
self.copyNode( nextDirs, nextFiles, os.path.join( target, dir ), os.path.join( origin, dir ) )
# in-place prettyprint formatter
def indent( self, elem, level=0 ):
i = "\n" + level*"\t"
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + "\t"
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
self.indent(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
# Slugify functions
def smart_truncate(string, max_length=0, word_boundaries=False, separator=' '):
string = string.strip(separator)
if not max_length:
return string
if len(string) < max_length:
return string
if not word_boundaries:
return string[:max_length].strip(separator)
if separator not in string:
return string[:max_length]
truncated = ''
for word in string.split(separator):
if word:
next_len = len(truncated) + len(word) + len(separator)
if next_len <= max_length:
truncated += '{0}{1}'.format(word, separator)
if not truncated:
truncated = string[:max_length]
return truncated.strip(separator)
def slugify(self, text, entities=True, decimal=True, hexadecimal=True, max_length=0, word_boundary=False, separator='-', convertInteger=False):
# Handle integers
if convertInteger and text.isdigit():
text = "NUM-" + text
# text to unicode
if type(text) != types.UnicodeType:
text = unicode(text, 'utf-8', 'ignore')
# decode unicode ( ??? = Ying Shi Ma)
text = unidecode(text)
# text back to unicode
if type(text) != types.UnicodeType:
text = unicode(text, 'utf-8', 'ignore')
# character entity reference
if entities:
text = CHAR_ENTITY_REXP.sub(lambda m: unichr(name2codepoint[m.group(1)]), text)
# decimal character reference
if decimal:
try:
text = DECIMAL_REXP.sub(lambda m: unichr(int(m.group(1))), text)
except:
pass
# hexadecimal character reference
if hexadecimal:
try:
text = HEX_REXP.sub(lambda m: unichr(int(m.group(1), 16)), text)
except:
pass
# translate
text = unicodedata.normalize('NFKD', text)
if sys.version_info < (3,):
text = text.encode('ascii', 'ignore')
# replace unwanted characters
text = REPLACE1_REXP.sub('', text.lower()) # replace ' with nothing instead with -
text = REPLACE2_REXP.sub('-', text.lower())
# remove redundant -
text = REMOVE_REXP.sub('-', text).strip('-')
# smart truncate if requested
if max_length > 0:
text = smart_truncate(text, max_length, word_boundary, '-')
if separator != '-':
text = text.replace('-', separator)
return text
def getVideoLibraryLType():
json_query = xbmc.executeJSONRPC('{ "jsonrpc": "2.0", "id": 0, "method": "Settings.GetSettingValue", "params": {"setting": "myvideos.flatten"}}')
json_query = unicode(json_query, 'utf-8', errors='ignore')
json_response = json.loads(json_query)
if json_response.has_key('result') and json_response['result'].has_key('value'):
if json_response['result']['value']:
return "video_flat"
return "video"
if ( __name__ == "__main__" ):
log('script version %s started' % ADDONVERSION)
# Profiling
#filename = os.path.join( DATAPATH, strftime( "%Y%m%d%H%M%S",gmtime() ) + "-" + str( random.randrange(0,100000) ) + ".log" )
#cProfile.run( 'Main()', filename )
#stream = open( filename + ".txt", 'w')
#p = pstats.Stats( filename, stream = stream )
#p.sort_stats( "cumulative" )
#p.print_stats()
# No profiling
ltype = ''
if sys.argv[2] == '':
videoltype = getVideoLibraryLType()
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=%s" %( videoltype ), xbmcgui.ListItem( label=LANGUAGE(30091) ), isFolder=True )
xbmcplugin.addDirectoryItem( int( sys.argv[ 1 ] ), "plugin://plugin.library.node.editor?ltype=music", xbmcgui.ListItem( label=LANGUAGE(30092) ), isFolder=True )
xbmcplugin.setContent(int(sys.argv[1]), 'files')
xbmcplugin.endOfDirectory(handle=int(sys.argv[1]))
else:
params = dict( arg.split( "=" ) for arg in sys.argv[ 2 ][1:].split( "&" ) )
ltype = params['ltype']
if ltype != '':
import rules, pathrules, viewattrib, orderby, moveNodes
RULE = rules.RuleFunctions()
ATTRIB = viewattrib.ViewAttribFunctions()
PATHRULE = pathrules.PathRuleFunctions()
PATHRULE.ATTRIB = ATTRIB
ORDERBY = orderby.OrderByFunctions()
Main()
log('script stopped')
|