Skip to content

Commit

Permalink
Fxing more linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazim-crim committed Oct 15, 2024
1 parent 71f6f96 commit 34ddd87
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 86 deletions.
14 changes: 7 additions & 7 deletions tests/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def transform(f, cmt="", wmt=""):
with tempfile.TemporaryDirectory() as tmp_path:
shutil.copy(f, os.path.join(tmp_path, os.path.basename(f)))
f = os.path.join(tmp_path, os.path.basename(f))
t = Transform(file_path=f, current_media_type=cmt, wanted_media_type=wmt)
assert isinstance(t.get(), FileResponse), f"{cmt} -> {wmt} {str(t['error'])}"
trans = Transform(file_path=f, current_media_type=cmt, wanted_media_type=wmt)
assert isinstance(trans.get(), FileResponse), f"{cmt} -> {wmt}"
print(f"{cmt} -> {wmt} passed")
return t.output_path
except Exception as e:
return trans.output_path
except Exception as err:
print(f"{cmt} -> {wmt} failed")
assert False, f"{os.path.splitext(f)[1]} -> {f} {str(e)}"
assert False, f"{os.path.splitext(f)[1]} -> {f} {str(err)}"


def test_transformations():
for fn in os.listdir(TRANSFORM_PATH):
transform(os.path.join(TRANSFORM_PATH, fn))
for file_name in os.listdir(TRANSFORM_PATH):
transform(os.path.join(TRANSFORM_PATH, file_name))
78 changes: 39 additions & 39 deletions weaver/transform/png2svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@
from PIL import Image


def add_tuple(a, b):
return tuple(map(operator.add, a, b))
def add_tuple(first_tuple, second_tuple):
return tuple(map(operator.add, first_tuple, second_tuple))


def sub_tuple(a, b):
return tuple(map(operator.sub, a, b))
def sub_tuple(first_tuple, second_tuple):
return tuple(map(operator.sub, first_tuple, second_tuple))


def neg_tuple(a):
return tuple(map(operator.neg, a))
def neg_tuple(first_tuple):
return tuple(map(operator.neg, first_tuple))


def direction(edge):
return sub_tuple(edge[1], edge[0])


def magnitude(a):
return int(pow(pow(a[0], 2) + pow(a[1], 2), .5))
def magnitude(tpl):
return int(pow(pow(tpl[0], 2) + pow(tpl[1], 2), .5))


def normalize(a):
mag = magnitude(a)
def normalize(tpl):
mag = magnitude(tpl)
assert mag > 0, "Cannot normalize a zero-length vector"
return tuple(map(operator.truediv, a, [mag] * len(a)))
return tuple(map(operator.truediv, tpl, [mag] * len(tpl)))


def svg_header(width, height):
return """<?xml version="1.0" encoding="UTF-8" standalone="no"?>
return f"""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="%d" height="%d"
<svg width="{width}" height="{height}"
xmlns="http://www.w3.org/2000/svg" version="1.1">
""" % (width, height)
"""


def joined_edges(assorted_edges, keep_every_point=False):
Expand Down Expand Up @@ -77,21 +77,21 @@ def joined_edges(assorted_edges, keep_every_point=False):
return pieces


def rgba_image_to_svg_contiguous(im, opaque=None, keep_every_point=False):
def rgba_image_to_svg_contiguous(img, opaque=None, keep_every_point=False):
# collect contiguous pixel groups

adjacent = ((1, 0), (0, 1), (-1, 0), (0, -1))
visited = Image.new("1", im.size, 0)
visited = Image.new("1", img.size, 0)

color_pixel_lists = {}

width, height = im.size
width, height = img.size
for x in range(width):
for y in range(height):
here = (x, y)
if visited.getpixel(here):
continue
rgba = im.getpixel((x, y))
rgba = img.getpixel((x, y))
if opaque and not rgba[3]:
continue
piece = []
Expand All @@ -101,11 +101,11 @@ def rgba_image_to_svg_contiguous(im, opaque=None, keep_every_point=False):
here = queue.pop()
for offset in adjacent:
neighbour = add_tuple(here, offset)
if not (0 <= neighbour[0] < width) or not (0 <= neighbour[1] < height):
if not 0 <= neighbour[0] < width or not 0 <= neighbour[1] < height:
continue
if visited.getpixel(neighbour):
continue
neighbour_rgba = im.getpixel(neighbour)
neighbour_rgba = img.getpixel(neighbour)
if neighbour_rgba != rgba:
continue
queue.append(neighbour)
Expand Down Expand Up @@ -158,39 +158,39 @@ def rgba_image_to_svg_contiguous(im, opaque=None, keep_every_point=False):
for assorted_edges in pieces:
color_joined_pieces[color].append(joined_edges(assorted_edges, keep_every_point))

s = StringIO()
s.write(svg_header(*im.size))
str = StringIO()
str.write(svg_header(*img.size))

for color, shapes in color_joined_pieces.items():
for shape in shapes:
s.write(""" <path d=" """)
str.write(""" <path d=" """)
for sub_shape in shape:
here = sub_shape.pop(0)[0]
s.write(""" M %d,%d """ % here)
str.write(f" M {here[0]},{here[1]} ")
for edge in sub_shape:
here = edge[0]
s.write(""" L %d,%d """ % here)
s.write(""" Z """)
s.write(
str.write(f" L {here[0]},{here[1]} ")
str.write(" Z ")
str.write(
f""" " style="fill:rgb{color[0:3]}; fill-opacity:{float(color[3]) / 255:.3f}; stroke:none;" />\n""")

s.write("""</svg>\n""")
return s.getvalue()
str.write("""</svg>\n""")
return str.getvalue()


def rgba_image_to_svg_pixels(im, opaque=None):
s = StringIO()
s.write(svg_header(*im.size))
def rgba_image_to_svg_pixels(img, opaque=None):
str = StringIO()
str.write(svg_header(*img.size))

width, height = im.size
width, height = img.size
for x in range(width):
for y in range(height):
here = (x, y)
rgba = im.getpixel(here)
rgba = img.getpixel(here)
if opaque and not rgba[3]:
continue
s.write(
""" <rect x="%d" y="%d" width="1" height="1" style="fill:rgb%s;
fill-opacity:%.3f; stroke:none;" />\n""" % (x, y, rgba[0:3], float(rgba[2]) / 255))
s.write("""</svg>\n""")
return s.getvalue()
str.write(
f""" <rect x="{x}" y="{y}" width="1" height="1" style="fill:rgb{rgba[0:3]};
fill-opacity:{float(rgba[2]) / 255:.3f}; stroke:none;" />\n""")
str.write("""</svg>\n""")
return str.getvalue()
14 changes: 7 additions & 7 deletions weaver/transform/tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def brighten_band(image_band: np.ndarray, alpha: float = 0.13, beta: float = 0.0

class Tiff:
def __init__(self, file_path):
self.fp = file_path
self.dataset = rasterio.open(self.fp)
self.file_path = file_path
self.dataset = rasterio.open(self.file_path)

self.is_geotiff = self.dataset.crs is not None

if not self.is_geotiff:
try:
self.images = mtif.read_stack(self.fp)
self.images = mtif.read_stack(self.file_path)
self._images = self.images.copy()
except Exception as ex:
if isinstance(ex, UnidentifiedImageError):
Expand All @@ -52,8 +52,8 @@ def get_band(self, index):
if index in self.range:
return self.dataset.read(index)
return None
except KeyError as e:
raise RuntimeError(f"Failed to read data at index {index}") from e
except KeyError as err:
raise RuntimeError(f"Failed to read data at index {index}") from err

def get_images(self, red_band: int = 1, green_band: int = 2, blue_band: int = 3):
if self.is_geotiff:
Expand All @@ -70,8 +70,8 @@ def get_images(self, red_band: int = 1, green_band: int = 2, blue_band: int = 3)
return [Image.fromarray(array)]
else:
imlist = []
for m in self.images.pages:
imlist.append(Image.fromarray(m))
for page in self.images.pages:
imlist.append(Image.fromarray(page))
return imlist

def convert_to_png(self, output_file, red: int = 1, green: int = 2, blue: int = 3):
Expand Down
49 changes: 25 additions & 24 deletions weaver/transform/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ def image_to_any(i, out):

if is_svg(i):
png = f"{i}.png"
svg2png(open(i, "rb").read(), write_to=open(png, "wb"))
with open(i, "rb") as svg_file:
svg_data = svg_file.read()
with open(png, "wb") as png_file:
svg2png(svg_data, write_to=png_file)
i = png

return images_to_any([Image.open(i)], out)
Expand All @@ -101,9 +104,9 @@ def images_to_any(ims, out):

if not is_png(_o) and len(clrs) == 4:
img.load()
bg = Image.new("RGB", img.size, (255, 255, 255))
bg.paste(img, mask=img.split()[3])
bg.save(_o)
rbg = Image.new("RGB", img.size, (255, 255, 255))
rbg.paste(img, mask=img.split()[3])
rbg.save(_o)
else:
img.save(_o)

Expand All @@ -127,9 +130,9 @@ def images_to_any(ims, out):
out += ".tar.gz"

with tarfile.open(out, "w:gz") as tar:
for fn in ret:
p = os.path.join(tmp_path, fn)
tar.add(p, arcname=fn)
for file_name in ret:
path = os.path.join(tmp_path, file_name)
tar.add(path, arcname=file_name)


@exception_handler
Expand Down Expand Up @@ -207,24 +210,22 @@ def any_to_pdf(i, out):
@exception_handler
def csv_to_json(i, out):
with open(i, encoding="utf-8") as csvf:
csvReader = csv.DictReader(csvf)

for x in range(len(csvReader.fieldnames)):
if csvReader.fieldnames[x] == "":
csvReader.fieldnames[x] = f"unknown_{str(x)}"
csv_reader = csv.DictReader(csvf)

for idx, fieldname in enumerate(csv_reader.fieldnames):
if fieldname == "":
csv_reader.fieldnames[idx] = f"unknown_{idx}"
ret = []
for rows in csvReader:
for rows in csv_reader:
ret.append({"data": rows})
# datas = {"datas": ret}
write_content(out, {"datas": ret})


@exception_handler
def csv_to_xml(i, out):
p = f"{i}.json"
csv_to_json(i, p)
data = readfromjson(p)
file = f"{i}.json"
csv_to_json(i, file)
data = readfromjson(file)
write_content(out, json2xml.Json2xml(data, item_wrap=False).to_xml())


Expand All @@ -236,25 +237,25 @@ def json_to_xml(i, out):

@exception_handler
def json_to_yaml(i, out):
with open(i, "r") as file:
with open(i, "r", encoding="utf-8") as file:
configuration = json.load(file)
with open(out, "w") as yaml_file:
with open(out, "w", encoding="utf-8") as yaml_file:
yaml.dump(configuration, yaml_file)


@exception_handler
def yaml_to_json(i, out):
with open(i, "r") as file:
with open(i, "r", encoding="utf-8") as file:
configuration = yaml.safe_load(file)
with open(out, "w") as json_file:
with open(out, "w", encoding="utf-8") as json_file:
json.dump(configuration, json_file)


@exception_handler
def json_to_csv(i, out):
with open(i, encoding="utf-8") as inputfile:
df = pd.read_json(inputfile)
df.to_csv(out, encoding="utf-8", index=False)
with open(i, encoding="utf-8") as file:
data_file = pd.read_json(file, encoding="utf-8")
data_file.to_csv(out, encoding="utf-8", index=False)


@exception_handler
Expand Down
19 changes: 10 additions & 9 deletions weaver/transform/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ def is_gif(i):
return i.lower().endswith(".gif")


def get_content(fp, t="r"):
with open(fp, t) as f:
def get_content(file_path, mode="r"):
with open(file_path, mode, encoding="utf-8") as f:
return f.read()


def write_content(fp, content):
def write_content(file_path, content):
if isinstance(content, dict):
content = json.dumps(content)

with open(fp, "w") as f:
with open(file_path, "w", encoding="utf-8") as f:
f.write(content)


Expand All @@ -64,16 +64,17 @@ def write_images(images, output_file, ext="png"):
with tempfile.TemporaryDirectory() as tmp_path:
imgs = []
for i, img in enumerate(images):
ip = f"{os.path.join(tmp_path, str(i).zfill(4))}.{ext}"
img.save(ip), imgs.append(img)
img_path = f"{os.path.join(tmp_path, str(i).zfill(4))}.{ext}"
img.save(img_path)
imgs.append(img)

if len(imgs) > 1:
if not output_file.endswith(".tar.gz"):
output_file += ".tar.gz"

with tarfile.open(output_file, "w:gz") as tar:
for fn in os.listdir(tmp_path):
p = os.path.join(tmp_path, fn)
tar.add(p, arcname=fn)
for file_name in os.listdir(tmp_path):
path = os.path.join(tmp_path, file_name)
tar.add(path, arcname=file_name)
else:
shutil.copy(imgs[0], output_file)

0 comments on commit 34ddd87

Please sign in to comment.