-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ImageDimensionBear: Add image dimension bear
Added ImageDimensionBear.py in bears/general, that runs the gem img_checker to see if images follow provided dimensions. Also added unittest for the bear, ImageDimensionBearTest.py in tests/general
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
import yaml | ||
|
||
from coalib.bears.GlobalBear import GlobalBear | ||
from coalib.results.Result import Result | ||
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY | ||
from dependency_management.requirements.GemRequirement import GemRequirement | ||
from dependency_management.requirements.PipRequirement import PipRequirement | ||
from sarge import run, Capture | ||
|
||
|
||
class ImageDimensionBear(GlobalBear): | ||
""" | ||
Checks the dimension of an image. | ||
More information is available at <github.com/Abhi2424shek/img_checker> | ||
""" | ||
|
||
AUTHORS = {'The coala developers'} | ||
AUTHORS_EMAILS = {'[email protected]'} | ||
REQUIREMENTS = {GemRequirement(' img_checker'), | ||
PipRequirement('pyyaml', '3.12')} | ||
LICENSE = 'AGPL-3.0' | ||
CAN_FIX = {'Image Dimension'} | ||
|
||
def run(self, | ||
image_config): | ||
|
||
with open('img_config.yml', 'w') as yaml_file: | ||
for each in image_config: | ||
each = each.split(' ') | ||
config = [{'directory': each[0], | ||
'width': int(each[1]), | ||
'height': int(each[2])}] | ||
yaml.dump(config, yaml_file, default_flow_style=False) | ||
|
||
cmd = 'ruby -r "img_checker.rb" -e ' | ||
cmd += '"ImgChecker.new.initialize img_config.yml"' | ||
output = run(cmd, stdout=Capture(), stderr=Capture()) | ||
if (output.returncode): | ||
lines = output.stdout.text.split('\n')[1:-2] | ||
for line in lines: | ||
if '.png' in line: | ||
fileName = line[10:line.index('.png')+4] | ||
elif '.jpg' in line: | ||
fileName = line[10:line.index('.jpg')+4] | ||
|
||
yield Result.from_values(origin=self, | ||
message=line, | ||
file=fileName, | ||
severity=RESULT_SEVERITY.NORMAL) | ||
|
||
os.remove('img_config.yml') |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import unittest | ||
from queue import Queue | ||
|
||
from bears.general.ImageDimensionBear import ImageDimensionBear | ||
from coalib.settings.Section import Section | ||
|
||
|
||
class ImageDimensionBearTest(unittest.TestCase): | ||
""" | ||
Runs unittests for ImageDimensionBear | ||
""" | ||
|
||
def setUp(self): | ||
self.section = Section('name') | ||
self.queue = Queue() | ||
self.file_dict = {} | ||
self.idb = ImageDimensionBear(self.file_dict, self.section, self.queue) | ||
|
||
def test_run_with_png(self): | ||
message = "message='The image ./test-img/img.png is larger" | ||
message += " than 240px x 240px [w x h]'" | ||
self.assertIn(message, | ||
str(list(self.idb.run( | ||
image_config=['./test-img/*.png 240 240'])))) | ||
|
||
def test_run_with_jpg(self): | ||
self.assertEqual('[]', | ||
str(list(self.idb.run( | ||
image_config=['./test-img/*.jpg 240 240'])))) | ||
|
||
def test_run_with_jpg_fail(self): | ||
message = "message='The image ./test-img/images.jpg is larger" | ||
message += " than 50px x 50px [w x h]'" | ||
self.assertIn(message, | ||
str(list(self.idb.run( | ||
image_config=['./test-img/*.jpg 50 50'])))) | ||
|
||
def test_run_with_both(self): | ||
message_png = "message='The image ./test-img/img.png is larger" | ||
message_png += " than 50px x 50px [w x h]'" | ||
message_jpg = "message='The image ./test-img/images.jpg is larger" | ||
message_jpg += " than 50px x 50px [w x h]'" | ||
output = str(list(self.idb.run(image_config=['./test-img/*.* 50 50']))) | ||
self.assertIn(message_png, | ||
output) | ||
self.assertIn(message_jpg, | ||
output) |