Skip to content

Commit

Permalink
Use the new Placeholders.substitute() method
Browse files Browse the repository at this point in the history
  • Loading branch information
manolomartinez committed Sep 5, 2021
1 parent d85c8a2 commit a8da40d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
13 changes: 6 additions & 7 deletions greg/aux_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ def check_directory(placeholders):
elif "yes" in subdirectory:
subdnametemplate = feed.retrieve_config(
"subdirectory_name", "{podcasttitle}")
subdname = substitute_placeholders(
subdnametemplate, placeholders)
subdname = placeholders.substitute(subdnametemplate)
placeholders.directory = os.path.join(download_path, subdname)
ensure_dir(placeholders.directory)
placeholders.fullpath = os.path.join(
Expand Down Expand Up @@ -173,7 +172,7 @@ def tag(placeholders):
"""
# We first recover the name of the file to be tagged...
template = placeholders.feed.retrieve_config("file_to_tag", "{filename}")
filename = substitute_placeholders(template, placeholders)
filename = placeholders.substitute(template)
podpath = os.path.join(placeholders.directory, filename)
# ... and this is it

Expand All @@ -191,7 +190,7 @@ def tag(placeholders):
except configparser.NoSectionError:
pass
for tag in tagdict:
metadata = substitute_placeholders(tagdict[tag], placeholders)
metadata = placeholders.substitute(tagdict[tag])
tagdict[tag] = metadata
file_to_tag = eyed3.load(podpath)
if file_to_tag.tag == None:
Expand All @@ -211,7 +210,7 @@ def tag(placeholders):

def filtercond(placeholders):
template = placeholders.feed.retrieve_config("filter", "True")
condition = substitute_placeholders(template, placeholders)
condition = placeholders.substitute(template)
return eval(condition)


Expand Down Expand Up @@ -240,7 +239,7 @@ def download_handler(feed, placeholders):
value = feed.retrieve_config('downloadhandler', 'greg')
if value == 'greg':
filename_placeholders = feed.retrieve_config('downloaded_filename', '{filename}')
filename = substitute_placeholders(filename_placeholders, placeholders)
filename = placeholders.substitute(filename_placeholders)
with urlopen(placeholders.link) as fin:
# check if request went ok
if fin.getcode() != 200:
Expand All @@ -257,7 +256,7 @@ def download_handler(feed, placeholders):
fout.write(fin.read())
else:
value_list = shlex.split(value)
instruction_list = [substitute_placeholders(part, placeholders) for
instruction_list = [placeholders.substitute(part) for
part in value_list]
returncode = subprocess.call(instruction_list)
if returncode:
Expand Down
19 changes: 19 additions & 0 deletions greg/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,22 @@ def __init__(self, feed, entry, link, filename, title, summary):
def date_string(self):
date_format = self.feed.retrieve_config("date_format", "%Y-%m-%d")
return time.strftime(date_format, self.date)

def substitute(self, inputstring):
"""
Take a string with placeholders, and return the strings with substitutions.
"""
newst = inputstring.format(link=self.link,
filename=self.filename,
directory=self.directory,
fullpath=self.fullpath,
title=self.title,
filename_title=self.filename_title,
date=self.date_string(),
podcasttitle=self.podcasttitle,
filename_podcasttitle=
self.filename_podcasttitle,
name=self.name,
subtitle=self.sanitizedsubtitle,
entrysummary=self.entrysummary)
return newst

0 comments on commit a8da40d

Please sign in to comment.