77
88import stagger
99import npyscreen
10- import configobj
10+
11+ from . import _const
1112
1213CONFIG = os .path .expanduser ('~/.clid.ini' )
1314
@@ -19,24 +20,45 @@ class Mp3DataBase(npyscreen.NPSFilteredDataBase):
1920 file_dict(dict):
2021 dict with the filename as key and absolute path to
2122 file as value. Used for displaying files and changing
22- metadata
23+ metadata.
24+ settings(configobj.ConfigObj):
25+ used for accessing the ini file.
26+ pre_format(str):
27+ string with format specifiers used to display preview of
28+ files' tags.
29+ specifiers(list):
30+ list of format specifiers in pre_format
31+ meta_cache(dict):
32+ cache which holds the metadata of files as they are selected.
33+
2334 """
2435 def __init__ (self ):
2536 super ().__init__ ()
2637
27- self .load_files_and_set_values () # set `file_dict` and `_values` attribute
28- self .meta_cache = dict () # cache which holds the metadata of files as they are selected
38+ self .settings = None # set by main.ClidInterface
39+ self .file_dict = None
40+ self .meta_cache = dict ()
2941
3042 # IDEA: set_values and set_search_list for updating values and search_list when refreshed
3143
44+ def filter_data (self ):
45+ if self ._filter and self ._values :
46+ return [mp3 for mp3 in self .get_all_values () if self ._filter in mp3 .lower ()]
47+ else :
48+ return self .get_all_values ()
49+
50+ def load_preview_format (self ):
51+ """Make approriate varibles to hold preview formats"""
52+ self .pre_format = self .settings ['preview_format' ]
53+ self .specifiers = _const .FORMAT_PAT .findall (self .pre_format )
3254
3355 def load_files_and_set_values (self ):
3456 """- Get a list of mp3 files in BASE_DIR recursively
3557 - Make a dict out of it
3658 - Assign it to `file_dict`
3759 - Set `_values` attribute
3860 """
39- base = configobj . ConfigObj ( CONFIG ) ['music_dir' ]
61+ base = self . settings ['music_dir' ]
4062
4163 ret_list = []
4264 for dir_tree in os .walk (base , followlinks = True ): # get all mp3 files in the dir and sub-dirs
@@ -46,11 +68,6 @@ def load_files_and_set_values(self):
4668 self .file_dict = dict ([(os .path .basename (abspath ), abspath ) for abspath in ret_list ])
4769 self ._values = tuple (sorted (self .file_dict .keys ())) # sorted tuple of filenames
4870
49- def filter_data (self ):
50- if self ._filter and self ._values :
51- return [mp3 for mp3 in self .get_all_values () if self ._filter in mp3 .lower ()]
52- else :
53- return self .get_all_values ()
5471
5572 def parse_meta_for_status (self , filename ):
5673 """Make a string like 'artist - album - track_number. title' from a filename
@@ -61,16 +78,14 @@ def parse_meta_for_status(self, filename):
6178 """
6279 if not filename in self .meta_cache :
6380 try :
64- metadata = stagger .read_tag (self .file_dict [filename ])
65- self .meta_cache [filename ] = '{art} - {alb} - {tno}. {title} ' .format (
66- art = metadata .artist ,
67- alb = metadata .album ,
68- # stagger saves track number as 0 if it is not given(won't be shown in players)
69- tno = metadata .track if metadata .track != 0 else ' ' ,
70- title = metadata .title
71- )
81+ meta = stagger .read_tag (self .file_dict [filename ])
82+ temp = self .pre_format # make a copy of format and replace specifiers with tags
83+
84+ for spec in self .specifiers :
85+ temp = temp .replace (spec , getattr (meta , _const .FORMAT [spec ]))
86+ self .meta_cache [filename ] = temp
7287 except stagger .errors .NoTagError :
73- self .meta_cache [filename ] = ' - - . '
88+ self .meta_cache [filename ] = _const . FORMAT_PAT . sub ( '' , self . specifiers )
7489
7590 return self .meta_cache [filename ]
7691
@@ -82,9 +97,9 @@ class SettingsDataBase(object):
8297 """Class to manage the settings/config file.
8398
8499 Attributes:
85- _settings (configobj.ConfigObj):
100+ settings (configobj.ConfigObj):
86101 `ConfigObj` object for the clid.ini file
87- parent(pref.PreferencesView ):
102+ parent(npyscreen.FormMuttActiveTraditional ):
88103 used to refer to parent form(pref.PreferencesView)
89104 disp_strings(list):
90105 list of formatted strings which will be used to display settings in the window
@@ -94,31 +109,36 @@ class SettingsDataBase(object):
94109 """
95110 def __init__ (self ):
96111 self .parent = None # set by parent; see docstring
112+ self .settings = None # also set by parent
97113 self .when_changed = {
98114 'music_dir' : self .music_dir ,
99115 'preview_format' : self .preview_format
100116 }
101117
102- self ._settings = configobj .ConfigObj (CONFIG )
103- self .make_strings ()
104118
105119 def make_strings (self ):
106120 """Make a list of strings which will be used to display the settings
107121 in the editing window
108122 """
109- self .disp_strings = [key + ' ' + value for key , value in self ._settings .items ()]
123+ self .disp_strings = [key + ' ' + value for key , value in self .settings .items ()]
110124
111125 def change_setting (self , key , new ):
112126 """Change a setting in the clid.ini"""
113- if key in self ._settings :
114- self ._settings [key ] = new
115- self ._settings .write ()
127+ if key in self .settings :
128+ self .settings [key ] = new
129+ self .settings .write ()
116130 self .when_changed [key ]()
117131
118132 def music_dir (self ):
133+ """To be executed when `music_dir` option is changed"""
119134 main_form = self .parent .parentApp .getForm ("MAIN" )
120135 main_form .value .load_files_and_set_values ()
121136 main_form .load_files ()
122137
123138 def preview_format (self ):
124- pass
139+ """To be executed when `preview_format` option is changed"""
140+ main_form = self .parent .parentApp .getForm ("MAIN" )
141+ main_form .value .meta_cache = dict ()
142+ main_form .value .load_preview_format ()
143+ main_form .wMain .set_status (main_form .wMain .get_selected ()) # change current file's preview into new format
144+
0 commit comments