<commit_before>import argparse
from datetime import datetime, timedelta
import logging
import os
import re
import stat
import sys
import pyexiv2
import photo_rename
from photo_rename import Filemap, FilemapList, FileMetadata, Harvester
from photo_rename.utils import CustomArgumentParser


logger = logging.getLogger(__name__)


def process_all_files(workdir, initial_dt, interval, simon_sez=None):
    """
    Manage the entire process of gathering data and renaming files.
    """
    error = False

    if not os.path.exists(workdir):
        logger.error(
                "Directory {0} does not exist. Exiting.".format(workdir))
        error = True

    if not os.access(workdir, os.W_OK):
        logger.error(
                "Destination directory {0} is not writable. Exiting.".format(
                    workdir))
        error = True

    if error:
        logger.warn("Exiting due to errors.")
        sys.exit(1)

    start_datetime = datetime.strptime(initial_dt, '%Y-%m-%d %H:%M:%S')
    counter = 0

    harvester = Harvester(workdir)
    files = harvester["files"]

    fmds = []
    for fn in files:
        dt_delta = counter * interval
        this_dt = start_datetime + timedelta(0, dt_delta)
        fmd = FileMetadata(os.path.join(workdir, fn))

        fmds.append(fmd)
        print("*************** {} : {}".format(fn, this_dt.strftime('%Y:%m:%d %H:%M:%S')))
        for md in fmd["metadata"].keys():
            if "Date" in md or "SubSec" in md or "Time" in md or "Offset" in md:
                print("{} : {}".format(md, fmd.metadata[md]))

        fmd.set_datetime(this_dt)
        counter += 1


def main():
    """
    Parse command-line arguments. Initiate file processing.
    """
    parser = CustomArgumentParser()
    parser.add_argument("-s", "--simon-sez",
            help="Really, Simon sez copy the data!", action="store_true")
    parser.add_argument("-d", "--directory",
            help="Set EXIF DateTime/XMP on files in this directory.")
    parser.add_argument("-t", "--datetime",
            help="Initial datetime YYYY-mm-DD HH:MM:SS.")
    parser.add_argument("-i", "--interval",
            help="Interval in seconds to use for successive files.")<commit_msg>Refactor code to be conditional on error. Replace print statements with log messages. General cleanup of hackathon quality code.<commit_after>import argparse
from datetime import datetime, timedelta
import logging
import os
import re
import stat
import sys
import pyexiv2
import photo_rename
from photo_rename import Filemap, FilemapList, FileMetadata, Harvester
from photo_rename.utils import CustomArgumentParser


logger = logging.getLogger(__name__)


def process_all_files(workdir, initial_dt, interval, simon_sez=None):
    """
    Manage the entire process of gathering data and renaming files.
    """
    error = False

    if not os.path.exists(workdir):
        logger.error(
                "Directory {0} does not exist. Exiting.".format(workdir))
        error = True

    if not os.access(workdir, os.W_OK):
        logger.error(
                "Destination directory {0} is not writable. Exiting.".format(
                    workdir))
        error = True

    if error:
        logger.warn("Exiting due to errors.")
        sys.exit(1)
    else:
        start_datetime = datetime.strptime(initial_dt, '%Y-%m-%d %H:%M:%S')
        counter = 0

        harvester = Harvester(workdir)
        files = harvester["files"]

        fmds = []
        for fn in files:
            # Compute delta. Add to start_datetime.
            dt_delta = counter * interval
            this_dt = start_datetime + timedelta(0, dt_delta)

            fmd = FileMetadata(os.path.join(workdir, fn))
            fmds.append(fmd)

            logger.info(
                "{} : {}".format(fn, this_dt.strftime('%Y:%m:%d %H:%M:%S')))

            for md in fmd["metadata"].keys():
                if ("Date" in md or "SubSec" in md or "Time" in md or
                        "Offset" in md):
                    logger.debug("{} : {}".format(md, fmd.metadata[md]))

            # Set the date and time
            fmd.set_datetime(this_dt)
            counter += 1


def main():
    """
    Parse command-line arguments. Initiate file processing.
    """
    parser = CustomArgumentParser()
    parser.add_argument("-s", "--simon-sez",
            help="Really, Simon sez copy the data!", action="store_true")
    parser.add_argument("-d", "--directory",
            help="Set EXIF DateTime/XMP on files in this directory.")
    parser.add_argument("-t", "--datetime",
            help="Initial datetime YYYY-mm-DD HH:MM:SS.")
    parser.add_argument("-i", "--interval",
            help="Interval in seconds to use for successive files.")
{'fraction_non_alphanumeric': 0.12284028655710072, 'fraction_numerical': 0.0029498525073746312, 'mean_word_length': 2.4448476052249637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '215529', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1372, 'n_words': 400}