@@ -41,26 +41,54 @@ def __init__(self):
41
41
42
42
self .config .add ({
43
43
'fields' : [],
44
+ 'keep_fields' : [],
44
45
'update_database' : False ,
45
46
})
46
47
47
48
self .patterns = {}
48
49
self .warned = False
49
50
50
- for field in self .config ['fields' ].as_str_seq ():
51
- if field in ('id' , 'path' , 'album_id' ):
52
- self ._log .warn (u'field \' {0}\' ignored, zeroing '
53
- u'it would be dangerous' , field )
54
- continue
51
+ if self .config ['fields' ]:
52
+ self .validate_config ('fields' )
53
+ for field in self .config ['fields' ].as_str_seq ():
54
+ self .set_pattern (field )
55
+
56
+ elif self .config ['keep_fields' ]:
57
+ self .validate_config ('keep_fields' )
58
+
59
+ for field in MediaFile .fields ():
60
+ if field in self .config ['keep_fields' ].as_str_seq ():
61
+ continue
62
+ self .set_pattern (field )
63
+
64
+ # These fields should be preserved
65
+ for key in ('id' , 'path' , 'album_id' ):
66
+ if key in self .patterns :
67
+ del self .patterns [key ]
68
+
69
+ def validate_config (self , mode ):
70
+ """Check if fields written in config are correct."""
71
+ if self .config ['fields' ] and self .config ['keep_fields' ]:
72
+ self ._log .warn (u'cannot blacklist and whitelist at the same time' )
73
+ for field in self .config [mode ].as_str_seq ():
55
74
if field not in MediaFile .fields ():
56
75
self ._log .error (u'invalid field: {0}' , field )
57
76
continue
77
+ if mode == 'fields' and field in ('id' , 'path' , 'album_id' ):
78
+ self ._log .warn (u'field \' {0}\' ignored, zeroing '
79
+ u'it would be dangerous' , field )
80
+ continue
58
81
59
- try :
60
- self .patterns [field ] = self .config [field ].as_str_seq ()
61
- except confit .NotFoundError :
62
- # Matches everything
63
- self .patterns [field ] = True
82
+ def set_pattern (self , field ):
83
+ """Set a field in `self.patterns` to a string list corresponding to
84
+ the configuration, or `True` if the field has no specific
85
+ configuration.
86
+ """
87
+ try :
88
+ self .patterns [field ] = self .config [field ].as_str_seq ()
89
+ except confit .NotFoundError :
90
+ # Matches everything
91
+ self .patterns [field ] = True
64
92
65
93
def import_task_choice_event (self , session , task ):
66
94
"""Listen for import_task_choice event."""
0 commit comments