Skip to content

Commit

Permalink
Add option to keep the original filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
ivandokov committed Sep 17, 2018
1 parent 5ebc4c5 commit b584bc2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
20 changes: 13 additions & 7 deletions phockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from src.phockup import Phockup
from src.printer import Printer

version = '1.5.5'
version = '1.5.6'
printer = Printer()


Expand All @@ -21,10 +21,11 @@ def main(argv):
link = False
date_regex = None
dir_format = os.path.sep.join(['%Y', '%m', '%d'])
original_filenames = False
timestamp = False

try:
opts, args = getopt.getopt(argv[2:], "d:r:mlth", ["date=", "regex=", "move", "link", "timestamp", "help"])
opts, args = getopt.getopt(argv[2:], "d:r:mltoh", ["date=", "regex=", "move", "link", "original-names", "timestamp", "help"])
except getopt.GetoptError:
help(version)
sys.exit(2)
Expand All @@ -41,26 +42,30 @@ def main(argv):

if opt in ("-m", "--move"):
move = True
printer.line("Using move strategy!")
printer.line("Using move strategy")

if opt in ("-l", "--link"):
link = True
printer.line("Using link strategy!")
printer.line("Using link strategy")

if opt in ("-o", "--original-names"):
original_filenames = True
printer.line("Using original filenames")

if opt in ("-r", "--regex"):
try:
date_regex = re.compile(arg)
except:
printer.error("Provided regex is invalid!")
printer.error("Provided regex is invalid")
sys.exit(2)

if opt in ("-t", "--timestamp"):
timestamp = True
printer.line("Using file's timestamp!")
printer.line("Using file's timestamp")


if link and move:
printer.error("Can't use move and link strategy together!")
printer.error("Can't use move and link strategy together")
sys.exit(1)

if len(argv) < 2:
Expand All @@ -73,6 +78,7 @@ def main(argv):
move=move,
link=link,
date_regex=date_regex,
original_filenames=original_filenames,
timestamp=timestamp
)

Expand Down
7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,16 @@ Instead of copying the process will move all files from the INPUTDIR to the OUTP
### Link files
Instead of copying the process will create hard link all files from the INPUTDIR into new structure in OUTPUTDIR by using the flag `-l | --link`. This is useful when working with good structure of photos in INPUTDIR (like folders per device).

### Original filenames
Organize the files in selected format or using the defauly year/month/day format but keep original filenames by using the flag `-o | --original-names`.

## Development

### Running tests
To run the tests, first install the dev dependencies using

```bash
pip install -r requirements-dev.txt
pip3 install -r requirements-dev.txt
```

Then run the tests using
Expand All @@ -114,6 +117,8 @@ pytest
```

## Changelog
##### `1.5.6`
* Add `-o | --original-names` option to allow keeping the original filenames
##### `1.5.5`
* Add `-t` option to allow using file modification time as a last resort
* Workaround EXIF DateTaken time of all-zeros
Expand Down
2 changes: 2 additions & 0 deletions src/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def help(version):
Instead of copying the process will make hard links to all files in INPUTDIR and place them in the OUTPUTDIR.
This is useful when working with working structure and want to create YYYY/MM/DD structure to point to same files.
-o | --original-names
Organize the files in selected format or using the defauly year/month/day format but keep original filenames.
-r | --regex
Specify date format for date extraction from filenames if there is no EXIF date information.
Expand Down
7 changes: 6 additions & 1 deletion src/phockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, input, output, **args):
self.dir_format = args.get('dir_format', os.path.sep.join(['%Y', '%m', '%d']))
self.move = args.get('move', False)
self.link = args.get('link', False)
self.original_filenames = args.get('original_filenames', False)
self.date_regex = args.get('date_regex', None)
self.timestamp = args.get('timestamp', False)

Expand Down Expand Up @@ -106,8 +107,12 @@ def get_output_dir(self, date):

def get_file_name(self, file, date):
"""
Generate file name based on exif data unless it is missing. Then use original file name
Generate file name based on exif data unless it is missing or
original filenames are required. Then use original file name
"""
if self.original_filenames:
return os.path.basename(file)

try:
filename = [
'%04d' % date['date'].year,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_phockup.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,13 @@ def test_process_skip_ignored_file():
assert not os.path.isfile("output/unknown/.DS_Store")
shutil.rmtree('output', ignore_errors=True)
shutil.rmtree('input_ignored', ignore_errors=True)


def test_keep_original_filenames(mocker):
shutil.rmtree('output', ignore_errors=True)
mocker.patch.object(Phockup, 'check_directories')
mocker.patch.object(Phockup, 'walk_directory')
Phockup('input', 'output', original_filenames=True).process_file("input/exif.jpg")
assert os.path.isfile("output/2017/01/01/exif.jpg")
assert not os.path.isfile("output/2017/01/01/20170101-010101.jpg")
shutil.rmtree('output', ignore_errors=True)

0 comments on commit b584bc2

Please sign in to comment.