-
Notifications
You must be signed in to change notification settings - Fork 15
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
Single file processing #17
Open
borondics
wants to merge
1
commit into
biolab:master
Choose a base branch
from
borondics:single_file_reduction
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,29 @@ | |
import pathlib | ||
import tinify | ||
import time | ||
|
||
|
||
def tinify_images(path, extensions): | ||
|
||
for file_path in [file for file in pathlib.Path(path).rglob('*')]: | ||
if file_path.suffix in extensions: | ||
print(f'Compressing image {file_path}') | ||
source = tinify.from_file(file_path) | ||
source.to_file(file_path) | ||
import os | ||
|
||
|
||
def tinify_images(target, extensions): | ||
|
||
if os.path.isdir(target): | ||
for file_path in [file for file in pathlib.Path(path).rglob('*')]: | ||
if file_path.suffix in extensions: | ||
print(f'Compressing image {file_path}') | ||
source = tinify.from_file(file_path) | ||
source.to_file(file_path) | ||
time.sleep(0.1) | ||
elif os.path.isfile(target): | ||
_, file_extension = os.path.splitext(target) | ||
print(f"This is a file. {file_extension}") | ||
if file_extension in extensions: | ||
print(f'Compressing image {target}') | ||
source = tinify.from_file(target) | ||
source.to_file(target) | ||
time.sleep(0.1) | ||
else: | ||
print(f'Your target {target} is neither a file nor a directory.') | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
|
@@ -20,8 +33,8 @@ def tinify_images(path, extensions): | |
parser.add_argument('-k', '--key', type=str, required=True, | ||
help='API key') | ||
|
||
parser.add_argument('-p', '--path', type=str, required=True, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't change existing argument names. Old saved commands should still work. |
||
help='The directory to search.') | ||
parser.add_argument('-t', '--target', type=str, required=True, | ||
help='The directory to search or the file to tinify.') | ||
|
||
parser.add_argument('-e', '--extensions', type=str, nargs='*', | ||
default=['.png', '.jpg'], | ||
|
@@ -30,4 +43,4 @@ def tinify_images(path, extensions): | |
args = parser.parse_args() | ||
|
||
tinify.key = args.key | ||
tinify_images(args.path, args.extensions) | ||
tinify_images(args.target, args.extensions) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should test this or previous solution depending on how it works with paths that have to expanded, such as
~/dev/orange3
.