Skip to content

Commit

Permalink
Merge pull request #13 from kdmurray91/eyefi_fixer
Browse files Browse the repository at this point in the history
Eyefi fixer
  • Loading branch information
Kevin Murray committed Jun 20, 2014
2 parents a1e0842 + 3026bee commit abbf356
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
28 changes: 28 additions & 0 deletions fixeyefi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
*.swp

# python compiled files
*.pyc
*.pyo

# testing
.coverage
.tox

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
var
sdist
develop-eggs
.installed.cfg
MANIFEST*

# Installer logs
pip-log.txt

# ctags
tags
87 changes: 87 additions & 0 deletions fixeyefi/fixeyefi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from __future__ import print_function
import docopt
import csv
import glob
from os import path, makedirs
from shutil import copyfile, move
import sys

FILETYPES = {
b'\xFF\xD8\xFF': "JPG",
b'\x49\x49\x2A': "CR2",
}
CLI_DOC = """
USAGE:
fixeyefi.py [-m] -c CONFIG -i EYEFI_BASE
OPTIONS:
-i EYEFI_BASE Base directory of eyefi cache. Should contain MAC address
sub-folders.
-c CONFIG Config csv in format: MAC,Destination
-m Move files, don't copy
"""

def parse_config(opts):
cameras = []
with open(opts['-c']) as csvfh:
csv_lines = csv.DictReader(csvfh)
for line in csv_lines:
try:
mac = line["MAC"]
dest = line["Destination"]
except KeyError:
print("ERROR: Malformed csv file. It should look like:\n")
print("MAC,Destination\n18:03:73:3d:b7:56,/destination/dir")
exit(1)
camera = {
"MAC": mac,
"Source": path.join(opts['-i'], mac),
"Destination": dest,
}
cameras.append(camera)
return cameras

def find_imgs(cam):
imgs = glob.glob("{}/*".format(cam['Source']))
for img in imgs:
yield (path.basename(img), img)

def get_img_format(img):
with open(img, "rb") as img_fh:
magic = img_fh.read(3)
try:
return FILETYPES[magic]
except KeyError:
return None


def main(opts):
cams = parse_config(opts)
for cam in cams:
count = 0
print("Processing {}".format(cam["MAC"]))
for name, img in find_imgs(cam):
fmt = get_img_format(img)
if not fmt:
print("Skipping {}, not a JPG or TIFF".format(img))
count += 1
continue
dest = path.join(cam['Destination'], fmt,
"{}.{}".format(name, fmt))
destdir = path.dirname(dest)
if not path.exists(destdir):
makedirs(destdir)
if opts['-m']:
move(img, dest)
else:
copyfile(img, dest)
if count % 10 == 0:
print("Processed {: 5} images".format(count), end='\r')
sys.stdout.flush()
count += 1
print("Processed {: 5} images. Done!".format(count))

if __name__ == '__main__':
opts = docopt.docopt(CLI_DOC)
main(opts)

42 changes: 42 additions & 0 deletions fixeyefi/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from setuptools import setup

desc = """
fixeyefi: fix eyefi cache dumps
"""

install_requires = [
"docopt==0.6.1",
]

test_requires = [
"coverage==3.7.1",
"nose==1.3.0",
"pep8==1.4.6",
"pylint==1.0.0",
]

setup(
name="fixeyefi",
py_modules=['fixeyefi', ],
version="0.1.0rc1",
install_requires=install_requires,
tests_require=test_requires,
description=desc,
author="Kevin Murray",
author_email="spam@kdmurray.id.au",
url="https://github.com/borevitzlab/traitcapture-bin",
keywords=["timestream", "timelapse", "photography", "video"],
classifiers=[
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Development Status :: 3 - Alpha",
"Environment :: Console",
"Intended Audience :: Developers",
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Scientific/Engineering :: Bio-Informatics",
"License :: OSI Approved :: GNU General Public License v3 or later " +
"(GPLv3+)",
],
test_suite="test",
)

0 comments on commit abbf356

Please sign in to comment.