import os
import re
import subprocess

import log

from collections import namedtuple


Flight = namedtuple('Flight', ['fs_path',  # Path to find this flight on disk
                               'raw_path',  # Upload path for the raw data
                               'processed_path',  # TODO: Post processed data
                               ])


class Flysight(object):
    DATE_RE = re.compile('(?P<year>\d{2})-(?P<month>\d{2})-(?P<day>\d{2})')

    def __init__(self, path):
        self.path = path
        # Assert that we're really a flysight
        assert os.path.exists("%s/config.txt" % self.path), \
            "Specified path isn't obviously a flysight"

    def dates(self):
        dirs = os.listdir(self.path)
        dates = filter(self.DATE_RE.match, dirs)
        return dates

    def files(self, date):
        files = os.listdir(os.path.join(self.path, date))
        return files

    def flights(self):
        for date in self.dates():
            for filename in self.files(date):
                # TODO: Move the logical path construction into flight itself.
                yield Flight(
                    os.path.join(self.path, date, filename),
                    os.path.join('/', date, 'raw', filename),
                    os.path.join('/', date, 'post', filename),
                )

    def unmount(self):
        log.info("Trying to unmount %s" % self.path)
        subprocess.check_call(['sudo', 'umount', self.path])

    def __del__(self):
        try:
            log.info("Trying to unmount %s" % self.path)
            subprocess.check_call(['sudo', 'umount', self.path])
        except:
            log.warn("Error unmounting flysight, continuing")

@@ -1632,16 +1632,31 @@
   except
+ Exception as e
 :%0A      
@@ -1696,16 +1696,21 @@
 flysight
+ (%25s)
 , contin
@@ -1714,10 +1714,21 @@
 tinuing%22
+ %25 (str(e))
 )%0A

Print the exception