Skip to content

Commit 7a72041

Browse files
authored
Added some docstrings
Also a minor change in checking of availability of subtitle file, the whole procedure is now performed in a single for loop.
1 parent c29abf3 commit 7a72041

File tree

1 file changed

+52
-26
lines changed

1 file changed

+52
-26
lines changed

sbox.py

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ def tk_get_file_path():
2929
"""
3030
This function will ask the user for the movie file/s and return the
3131
file path/s of them. Tkinter module is used to show the File dialog.
32-
32+
3333
Parameters
3434
----------
3535
None
3636
"""
3737

3838
root = Tk()
3939
root.withdraw()
40-
40+
4141
file_path = root.tk.splitlist(
4242
filedialog.askopenfilenames(parent=root, title='Choose a file'))
4343
if not file_path:
@@ -141,7 +141,7 @@ def download(file_path, data):
141141
----------
142142
file_path : str
143143
The path of the file/s for which sub/s should be found.
144-
144+
145145
data : bytes
146146
Binary data of the subtitle file.
147147
"""
@@ -158,8 +158,10 @@ def check_existence_of_subtitles(files_path):
158158
file in files_path.
159159
160160
If a file is not a video file or is corrupted, abort and exit.
161-
162-
Otherwise, return a list with the all the
161+
Otherwise, returns 3 output parameters:
162+
1 - List of all available languages for each selected file.
163+
2 - A list of Booleans, True if subtitles for specific file exists and False if not.
164+
3 - The sequence number of the file whose subtitles do not exist.
163165
164166
Parameters
165167
----------
@@ -169,39 +171,62 @@ def check_existence_of_subtitles(files_path):
169171
"""
170172

171173
all_available_languages_selection = []
172-
for file_path in files_path:
174+
bool_find_subtitles = np.ones(
175+
len(files_path), dtype=bool)
176+
loc_none = []
177+
for i, file_path in enumerate(files_path):
173178
try:
174-
all_available_languages_selection.append(
175-
request_subtitle_languages(file_path))
179+
found_languages = request_subtitle_languages(file_path)
180+
all_available_languages_selection.append(found_languages)
181+
if found_languages == None:
182+
loc_none.append(i)
183+
bool_find_subtitles[i] = False
176184
except:
177185
print(_("The selected file cannot be used to find subtitles:"))
178186
print(f" x {os.path.basename(file_path)}")
179187
print(_("Cancelled"))
180188
sys.exit()
181-
return all_available_languages_selection
189+
return all_available_languages_selection, bool_find_subtitles, loc_none
182190

183191

184-
def bool_existence_of_subtitles_regarding_selected_files(all_available_languages_selection):
185-
bool_find_subtitles = np.ones(
186-
len(all_available_languages_selection), dtype=bool)
187-
loc_none = []
188-
if None in all_available_languages_selection:
189-
loc_none = [i for i, x in enumerate(
190-
all_available_languages_selection) if x == None]
191-
bool_find_subtitles[loc_none] = False
192-
return bool_find_subtitles, loc_none
192+
def select_files_with_subtitles(all_files, bool_loc):
193+
"""
194+
Returns the selected elements from the all_file list that are selected
195+
based on the values of the elements in bool_loc (list of Booleans)
193196
197+
Parameters
198+
----------
199+
all_files : list
200+
The path containing all the files for which subs should
201+
be found. Or a list of all available languages for each selected file.
202+
203+
bool_loc : list
204+
List of Booleans, True if subtitles for specific file exists and False if not.
205+
"""
194206

195-
def select_files_with_subtitles(all_files, bool_loc):
196207
return np.asarray(all_files)[bool_loc]
197208

198209

199210
def get_common_languages_for_all_files(all_available_languages_selection, lang):
211+
"""
212+
This function checks which common languages are available for all selected files.
213+
Returns a list of common languages.
214+
215+
Parameters
216+
----------
217+
all_available_languages_selection : list
218+
List containing all the languages available for the individual
219+
files for which subtitles exist at all.
220+
221+
lang : list
222+
List of all language codes that can be found in the database.
223+
"""
224+
200225
availability_in_all_files = []
201-
if all_available_languages_selection.size>0:
226+
if all_available_languages_selection.size > 0:
202227
for _lang in lang:
203228
availability_in_all_files.append(all((_lang in x)
204-
for x in all_available_languages_selection))
229+
for x in all_available_languages_selection))
205230
return np.asarray(lang)[availability_in_all_files]
206231
else:
207232
print(_("There is no common language available for the selected files."))
@@ -232,8 +257,8 @@ def main(cli_file_path, language_code_cli):
232257
sys.exit()
233258

234259
# Check, which files do and do not have subtitle file
235-
files_with_available_subs = check_existence_of_subtitles(files_path)
236-
find_subtitles, loc_none = bool_existence_of_subtitles_regarding_selected_files(files_with_available_subs)
260+
files_with_available_subs, find_subtitles, loc_none = check_existence_of_subtitles(
261+
files_path)
237262

238263
# Check, which languages appears in all requested episodes
239264
all_available_languages = select_files_with_subtitles(
@@ -275,7 +300,8 @@ def main(cli_file_path, language_code_cli):
275300

276301
# If no language code was given as CLI argument, ask it to the user
277302
if language_code_cli is None:
278-
selected_language = input(_("Choose your language (Please use language codes): ")).lower()
303+
selected_language = input(
304+
_("Choose your language (Please use language codes): ")).lower()
279305
else:
280306
selected_language = language_code_cli
281307

@@ -289,7 +315,8 @@ def main(cli_file_path, language_code_cli):
289315
if req.status_code == 200:
290316
data = req.content
291317
download(file_path=file_path, data=data)
292-
print(f"\n{index+1}/{len(files_path)}" + " " + _("Subtitle downloaded successfully"))
318+
print(f"\n{index+1}/{len(files_path)}" + " " +
319+
_("Subtitle downloaded successfully"))
293320
else:
294321
print("\n", end="")
295322
print(_("Unknown Error"))
@@ -298,7 +325,6 @@ def main(cli_file_path, language_code_cli):
298325
print(_("Invalid language code selected. Please try again."))
299326

300327

301-
302328
if __name__ == "__main__":
303329
parser = argparse.ArgumentParser(description='SubtitleBOX CLI')
304330
parser.add_argument("-f", "--file_path",

0 commit comments

Comments
 (0)