Skip to content

Commit 16f82fe

Browse files
committed
refactored handlers using decorators
1 parent 3aba55d commit 16f82fe

File tree

6 files changed

+70
-43
lines changed

6 files changed

+70
-43
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ CHANGELOG
44
v0.7.0
55
------
66

7-
- [ ] Batch tag files
7+
- [x] Batch tag files
88
- [ ] Customizable keybindings
99
- [ ] Autocomplete in genre tag field
1010
- [x] Filename field in tag editing view
1111
- [ ] Short description of preferences option in status line
12-
- [x] Use `l` to invoke a search box for searching for files(`n` for next match, 'N' for previous match
1312

1413
v0.6.3
1514
------

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ $ [sudo] pip install clid
4545
### From Source
4646

4747
```shell
48-
$ git clone https://github.com
49-
$ /GokulSoumya/clid.git
48+
$ git clone https://github.com/GokulSoumya/clid.git
5049
$ cd clid
5150
$ [sudo] python3 setup.py install
5251
```

clid/_const.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@
212212
'%A': 'album_artist'
213213
}
214214

215+
# for matching format specifiers
215216
FORMAT_PAT = re.compile(r'%.')
216217

218+
# dict with {name of textbox: name of field like artist. album, etc}
217219
TAG_FIELDS = {
218220
'tit': 'title',
219221
'alb': 'album',
@@ -224,7 +226,7 @@
224226
'ala': 'album_artist'
225227
}
226228

227-
229+
# value of date must match this regex
228230
DATE_PATTERN = re.compile(r"""(?x)\s*
229231
((?P<year>[0-9]{4}) # YYYY
230232
(-(?P<month>[01][0-9]) # -MM
@@ -236,3 +238,11 @@
236238
(:(?P<sec>[0-6][0-9]) # :SS
237239
)?)?)?\s*
238240
""")
241+
242+
# name of keypress handler which require a update of status line
243+
HANDLERS_REQUIRING_STATUS_UDPATE = (
244+
'h_cursor_line_up',
245+
'h_cursor_line_down',
246+
'h_cursor_page_up',
247+
'h_cursor_page_down'
248+
)

clid/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ def on_ok(self): # char is for handlers
250250
tag = track if field == 'track' else getattr(self, tbox).value # get value to be written to file
251251
setattr(meta, field, tag)
252252
meta.write(mp3)
253-
# show the new tags in the status line
254-
main_form.wMain.set_status(filename=os.path.basename(mp3), force=True)
253+
# update meta cache
254+
main_form.value.parse_meta_for_status(filename=os.path.basename(mp3), force=True)
255255

256+
# show the new tags in the status line
257+
main_form.wMain.set_current_status()
256258
return True

clid/database.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ def music_dir(self):
204204
def preview_format(self):
205205
self.main_form.value.meta_cache = dict()
206206
self.main_form.value.load_preview_format()
207-
self.main_form.wMain.set_status(self.main_form.wMain.get_selected()) # change current file's preview into new format
207+
# change current file's preview into new format
208+
self.main_form.wMain.set_current_status()
208209

209210
def smooth_scroll(self):
210211
smooth = self.settings['smooth_scroll']

clid/main.py

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from . import base
1212
from . import pref
13+
from . import _const
1314
from . import database
1415
from . import editmeta
1516

@@ -48,7 +49,27 @@ def search_for_files(self, command_line, widget_proxy, live):
4849

4950
self.parent.after_search_now_filter_view = True
5051
self.parent.wMain.values = self.parent.value.get()
51-
self.parent.wMain.display()
52+
self.parent.display()
53+
if self.parent.wMain.values:
54+
self.parent.wMain.set_current_status()
55+
else: # search didn't match
56+
self.parent.wStatus2.value = ' '
57+
self.parent.display()
58+
59+
60+
def special_handler(handler):
61+
"""Decorator which accepts a handler as param and executes it
62+
only if the window is not empty(if there are files to display)
63+
If the handler requires the status line to be updated(like with
64+
movement handlers like h_cursor_line_up to show metadata preview
65+
of file under cursor), that is also done
66+
"""
67+
def wrapper(self, char):
68+
if self.values:
69+
handler(self, char)
70+
if handler.__name__ in _const.HANDLERS_REQUIRING_STATUS_UDPATE:
71+
self.set_current_status()
72+
return wrapper
5273

5374

5475
class ClidMultiline(npy.MultiLine):
@@ -70,7 +91,6 @@ class ClidMultiline(npy.MultiLine):
7091
self.parent refers to ClidInterface -> class
7192
self.parent.value refers to database.Mp3DataBase -> class
7293
"""
73-
7494
def __init__(self, *args, **kwargs):
7595
super().__init__(*args, **kwargs)
7696
self.allow_filtering = False # does NOT refer to search invoked with '/'
@@ -79,49 +99,45 @@ def __init__(self, *args, **kwargs):
7999
smooth = self.parent.parentApp.settings['smooth_scroll'] # is smooth scroll enabled ?
80100
self.slow_scroll = True if smooth == 'true' else False
81101

82-
def set_up_handlers(self):
83-
super().set_up_handlers()
84102
self.handlers.update({
85103
'u': self.h_reload_files,
86104
'2': self.h_switch_to_settings,
87105
curses.ascii.SP: self.h_multi_select,
88106
curses.ascii.ESC: self.h_revert_escape,
89107
})
90108

91-
self.h_cursor_line_down = self.handler_with_status_updating(self.h_cursor_line_down)
92-
self.h_cursor_line_up = self.handler_with_status_updating(self.h_cursor_line_up)
93-
self.h_cursor_page_down = self.handler_with_status_updating(self.h_cursor_page_down)
94-
self.h_cursor_page_up = self.handler_with_status_updating(self.h_cursor_page_up)
109+
# self.h_cursor_line_up = special_handler(self.h_cursor_line_up)
110+
# self.h_cursor_line_down = special_handler(self.h_cursor_line_down)
111+
# self.h_cursor_page_up = special_handler(self.h_cursor_page_up)
112+
# self.h_cursor_page_down = special_handler(self.h_cursor_page_down)
95113

114+
# Movement Handlers
115+
@special_handler
116+
def h_cursor_page_up(self, char):
117+
super().h_cursor_page_up(char)
96118

97-
def run_only_if_window_is_not_empty(handler):
98-
"""Decorator which accepts a handler as param and executes it
99-
only if the window is not empty(if there are files to display)
100-
"""
101-
def wrapper(self, char):
102-
if self.values:
103-
handler(self, char)
104-
return wrapper
105-
106-
@staticmethod
107-
def handler_with_status_updating(handler):
108-
"""Decorator which adds status line updating(set status line's value
109-
according to tags of file undef cursor) and `run_only_if_window_is_not_empty`
110-
functionality to movement handler(up, down, page up, etc)
111-
"""
112-
def wrapper(self, char):
113-
if self.values:
114-
handler(self, char)
115-
self.set_status(self.get_selected())
116-
return wrapper
119+
@special_handler
120+
def h_cursor_page_down(self, char):
121+
super().h_cursor_page_down(char)
122+
123+
@special_handler
124+
def h_cursor_line_up(self, char):
125+
super().h_cursor_line_up(char)
126+
127+
@special_handler
128+
def h_cursor_line_down(self, char):
129+
super().h_cursor_line_down(char)
117130

118131
@property
119132
def _relative_index_of_space_selected_values(self):
120-
return [self.values.index(file) for file in self.space_selected_values if file in self.values]
121-
122-
def set_status(self, filename, **kwargs):
123-
"""Set the the value of self.parent.wStatus2 with metadata of file under cursor."""
124-
self.parent.wStatus2.value = self.parent.value.parse_meta_for_status(filename=filename, **kwargs)
133+
return [self.values.index(file) for file in self.space_selected_values\
134+
if file in self.values]
135+
136+
def set_current_status(self, *args, **kwargs):
137+
"""Show metadata(preview) of file under cursor in the status line"""
138+
s = self.parent.value.parse_meta_for_status(
139+
filename=self.get_selected(), *args, **kwargs)
140+
self.parent.wStatus2.value = s
125141
self.parent.display()
126142

127143
def get_selected(self):
@@ -154,7 +170,7 @@ def h_switch_to_settings(self, char):
154170
self.parent.parentApp.switchForm("SETTINGS")
155171

156172

157-
@run_only_if_window_is_not_empty
173+
@special_handler
158174
def h_select(self, char):
159175
app = self.parent.parentApp
160176
file_dict = self.parent.value.file_dict
@@ -171,7 +187,7 @@ def h_select(self, char):
171187
self.parent.parentApp.current_files = [file_dict[file_under_cursor]]
172188
self.parent.parentApp.switchForm("SINGLEEDIT")
173189

174-
@run_only_if_window_is_not_empty
190+
@special_handler
175191
def h_multi_select(self, char):
176192
"""Add or remove current line from list of lines
177193
to be highlighted, when <Space> is pressed.

0 commit comments

Comments
 (0)