Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eyefi fixer #13

Merged
merged 6 commits into from
Jun 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
)