def run(args=None):
    args = parse_args(args)
    locale.setlocale(locale.LC_ALL, args.locale)

    # determine output format
    if args.format is None:
        args.format = os.path.splitext(args.output)[1][1:] if args.output \
                      else 'html'
    if args.format.lower() in ['md', 'markdown', 'mdown', 'mkdn']:
        args.format = 'md'

    # Check journal files exist
    args.journal = os.path.expanduser(args.journal)
    if not os.path.exists(args.journal):
        return "File not found: " + args.journal
    if not os.path.exists(os.path.join(args.journal, 'entries')):
        return "Not a valid Day One package: " + args.journal

    # tags
    tags = args.tags
    if tags is not None:
        if tags != 'any':
            tags = [tag.strip() for tag in tags.split(',')]

    # excluded tags
    excluded_tags = args.exclude
    if excluded_tags is not None:
        excluded_tags = [tag.strip() for tag in excluded_tags.split(',')]

    # parse before and after date
    dates = [args.before, args.after]
    for i, date in enumerate(dates):
        if date:
            try:
                dates[i] = dateutil.parser.parse(date)
            except (__HOLE__, OverflowError):
                return "Unable to parse date '{0}'".format(date)
    before, after = dates

    generator = dayone_export(
            args.journal,
            template=args.template,
            reverse=args.reverse,
            tags=tags,
            exclude=excluded_tags,
            before=before,
            after=after,
            format=args.format,
            template_dir=args.template_dir,
            autobold=args.autobold,
            nl2br=args.nl2br,
            filename_template=args.output)

    try:

        # Output is a generator returning each file's name and contents one at a time
        for filename, output in generator:
            if args.output:
                with codecs.open(filename, 'w', encoding='utf-8') as f:
                    f.write(output)
            else:
                compat.print_bytes(output.encode('utf-8'))
                compat.print_bytes("\n".encode('utf-8'))

    except jinja2.TemplateNotFound as err:
        return template_not_found_message(err)
    except PlistError as err:
        return str(err)
dataset/ETHPy150Open nathangrigg/dayone_export/dayone_export/cli.py/run