Skip to content

Commit

Permalink
#9 add option to compile only one email or their subset
Browse files Browse the repository at this point in the history
  • Loading branch information
snejku committed Feb 25, 2016
1 parent 2f3423c commit ccbdd29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
7 changes: 5 additions & 2 deletions email_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

def parse_emails(settings):
result = True
shutil.rmtree(settings.destination, ignore_errors=True)
emails = fs.emails(settings.source, settings.pattern)

if not settings.exclusive:
shutil.rmtree(settings.destination, ignore_errors=True)

emails = fs.emails(settings.source, settings.pattern, settings.exclusive)
for email in emails:
if not placeholder.validate_email(email, settings.source):
result = False
Expand Down
9 changes: 7 additions & 2 deletions email_parser/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
'strict',
'force',
'verbose',
'shortener'
'shortener',
'exclusive',
'default_locale'
])


Expand All @@ -40,7 +42,9 @@ def default_settings():
images='http://www.getkeepsafe.com/emails/img',
right_to_left=['ar', 'he'],
pattern='{locale}/{name}.xml',
shortener={}
shortener={},
exclusive=None,
default_locale='en'
)


Expand All @@ -50,6 +54,7 @@ def read_args(argsargs=argparse.ArgumentParser):
args = argsargs(epilog='Brought to you by KeepSafe - www.getkeepsafe.com')

args.add_argument('-s', '--source', help='args\'s source folder, default: %s' % settings.source)
args.add_argument('-e', '--exclusive', help='Exclusive path of subset emails to compile, default: %s' % settings.exclusive)
args.add_argument('-d', '--destination',
help='args\'s destination folder, default: %s' % settings.destination)
args.add_argument('-t', '--templates', help='Templates folder, default: %s' % settings.templates)
Expand Down
20 changes: 15 additions & 5 deletions email_parser/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,41 @@ def _parse_params(pattern):
'{{name}} is a required parameter in the pattern but it is not present in {}'.format(pattern))
return params

def _emails(src_dir, pattern, params):
def _emails(src_dir, pattern, params, exclusive_path=None):
wildcard_params = {k: '*' for k in params}
wildcard_pattern = pattern.format(**wildcard_params)
parser = parse.compile(pattern)
for path in sorted(Path(src_dir).glob(wildcard_pattern), key=lambda path: str(path)):
if not path.is_dir():

if exclusive_path:
glob_path = Path('.').glob(exclusive_path)
else:
glob_path = Path(src_dir).glob(wildcard_pattern)

for path in sorted(glob_path, key=lambda path: str(path)):
if not path.is_dir() and (not exclusive_path or _has_correct_ext(path, pattern)):
str_path = str(path.relative_to(src_dir))
result = parser.parse(str_path)
result.named['path'] = str_path
result.named['full_path'] = str(path.resolve())
logging.debug('loading email %s', result.named['full_path'])
yield result

def emails(src_dir, pattern):
def _has_correct_ext(path, pattern):
return os.path.splitext(str(path))[1] == os.path.splitext(pattern)[1]

def emails(src_dir, pattern, exclusive_path=None):
"""
Resolves a pattern to a collection of emails. The pattern needs to have 'name' and 'locale' as this is used later
to produce the results.
:param src_dir: base dir for the search
:param pattern: search pattern
:exclusive_path: single email path, glob path for emails subset or None to not affect emails set
:returns: generator for the emails matching the pattern
"""
params = _parse_params(pattern)
for result in _emails(src_dir, pattern, params):
for result in _emails(src_dir, pattern, params, exclusive_path):
yield Email(**result.named)


Expand Down

0 comments on commit ccbdd29

Please sign in to comment.