# Path: src/jane/waveforms/utils.py
# def to_datetime(timestamp):
#     if not timestamp:
#         return None
#     return datetime.datetime.fromtimestamp(float(timestamp))


import os
from django.db import transaction
from obspy.core import read
from obspy.core.preview import create_preview
from psycopg2.extras import DateTimeTZRange
from jane.exceptions import JaneWaveformTaskException
from . import models
from .utils import to_datetime
# -*- coding: utf-8 -*-






def process_file(filename):
    """
    Process a single waveform file.

    This is a bit more complex as it needs to update existing database
    objects and cannot just always create new ones. Otherwise the
    identifiers quickly reach very high numbers.
    """
    # Resolve symlinks and make a canonical simple path.
    filename = os.path.realpath(os.path.normpath(os.path.abspath(filename)))

    # ------------------------------------------------------------------------
    # Step 1: Get the file if it exists.
    try:
        file = models.File.objects.get(
            path__name=os.path.dirname(filename),
            name=os.path.basename(filename))

        # This path is only reached if the file exists. Check size, mtime,
        # and ctime and if it all remains the same, return.
        stats = os.stat(filename)
        mtime = to_datetime(stats.st_mtime)

# Path: src/jane/waveforms/utils.py
# def to_datetime(timestamp):
#     if not timestamp:
#         return None
#     return datetime.datetime.fromtimestamp(float(timestamp))


# Path: src/jane/waveforms/process_waveforms.py
import os
from django.db import transaction
from obspy.core import read
from obspy.core.preview import create_preview
from psycopg2.extras import DateTimeTZRange
from jane.exceptions import JaneWaveformTaskException
from . import models
from .utils import to_datetime

# -*- coding: utf-8 -*-






def process_file(filename):
    """
    Process a single waveform file.

    This is a bit more complex as it needs to update existing database
    objects and cannot just always create new ones. Otherwise the
    identifiers quickly reach very high numbers.
    """
    # Resolve symlinks and make a canonical simple path.
    filename = os.path.realpath(os.path.normpath(os.path.abspath(filename)))

    # ------------------------------------------------------------------------
    # Step 1: Get the file if it exists.
    try:
        file = models.File.objects.get(
            path__name=os.path.dirname(filename),
            name=os.path.basename(filename))

        # This path is only reached if the file exists. Check size, mtime,
        # and ctime and if it all remains the same, return.
        stats = os.stat(filename)
        mtime = to_datetime(stats.st_mtime)

        ctime = to_datetime(stats.st_ctime)