Running black on python files
This commit is contained in:
		
							parent
							
								
									3d7428e76f
								
							
						
					
					
						commit
						7e52c5a532
					
				
					 1 changed files with 72 additions and 48 deletions
				
			
		| 
						 | 
					@ -25,7 +25,6 @@ def extract_zooms(src_folder):
 | 
				
			||||||
    idx = 0
 | 
					    idx = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for svg_path in folder.glob("*.svg"):
 | 
					    for svg_path in folder.glob("*.svg"):
 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        idx += 1
 | 
					        idx += 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        print(f"page {idx} : {svg_path.name}")
 | 
					        print(f"page {idx} : {svg_path.name}")
 | 
				
			||||||
| 
						 | 
					@ -41,36 +40,43 @@ def extract_zooms(src_folder):
 | 
				
			||||||
        root = tree.getroot()
 | 
					        root = tree.getroot()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if "." in root.get("width"):
 | 
					        if "." in root.get("width"):
 | 
				
			||||||
            print(f"WARNING: file {svg_path} has a floating width, it will be rounded", file=sys.stderr)
 | 
					            print(
 | 
				
			||||||
 | 
					                f"WARNING: file {svg_path} has a floating width, it will be rounded",
 | 
				
			||||||
 | 
					                file=sys.stderr,
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
        zooms[idx]["width"] = round(float(root.get("width")))
 | 
					        zooms[idx]["width"] = round(float(root.get("width")))
 | 
				
			||||||
        if "." in root.get("height"):
 | 
					        if "." in root.get("height"):
 | 
				
			||||||
            print(f"WARNING: file {svg_path} has a floating height, it will be rounded", file=sys.stderr)
 | 
					            print(
 | 
				
			||||||
 | 
					                f"WARNING: file {svg_path} has a floating height, it will be rounded",
 | 
				
			||||||
 | 
					                file=sys.stderr,
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
        zooms[idx]["height"] = round(float(root.get("height")))
 | 
					        zooms[idx]["height"] = round(float(root.get("height")))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for area in root.findall('.//{*}rect'):
 | 
					        for area in root.findall(".//{*}rect"):
 | 
				
			||||||
            zooms[idx]["zooms"].append([
 | 
					            zooms[idx]["zooms"].append(
 | 
				
			||||||
 | 
					                [
 | 
				
			||||||
                    float(area.get("width")),
 | 
					                    float(area.get("width")),
 | 
				
			||||||
                    float(area.get("height")),
 | 
					                    float(area.get("height")),
 | 
				
			||||||
                    float(area.get("x")),
 | 
					                    float(area.get("x")),
 | 
				
			||||||
                    float(area.get("y")),
 | 
					                    float(area.get("y")),
 | 
				
			||||||
            ])
 | 
					                ]
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return zooms, max_width, max_height
 | 
					    return zooms, max_width, max_height
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def write_json_or_js(zooms, dest_file, is_js):
 | 
					def write_json_or_js(zooms, dest_file, is_js):
 | 
				
			||||||
 | 
					 | 
				
			||||||
    with open(dest_file, "w") as data_file:
 | 
					    with open(dest_file, "w") as data_file:
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
        if is_js:
 | 
					        if is_js:
 | 
				
			||||||
            data_file.write("PAGES_ZOOMS = ")
 | 
					            data_file.write("PAGES_ZOOMS = ")
 | 
				
			||||||
        data_file.write("[\n")
 | 
					        data_file.write("[\n")
 | 
				
			||||||
        first_coma_skiped = False
 | 
					        first_coma_skiped = False
 | 
				
			||||||
        for page_idx in sorted(zooms.keys()):
 | 
					        for page_idx in sorted(zooms.keys()):
 | 
				
			||||||
            for zoom in zooms[page_idx]["zooms"]:
 | 
					            for zoom in zooms[page_idx]["zooms"]:
 | 
				
			||||||
            
 | 
					 | 
				
			||||||
                if zoom[2] < 0 or zoom[3] < 0:
 | 
					                if zoom[2] < 0 or zoom[3] < 0:
 | 
				
			||||||
                    print(f"WARNING: negative pos x / pos y in page {page_idx} for zoom {zoom} (is the rectangle flipped?)")
 | 
					                    print(
 | 
				
			||||||
 | 
					                        f"WARNING: negative pos x / pos y in page {page_idx} for zoom {zoom} (is the rectangle flipped?)"
 | 
				
			||||||
 | 
					                    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if first_coma_skiped:
 | 
					                if first_coma_skiped:
 | 
				
			||||||
                    data_file.write(",\n")
 | 
					                    data_file.write(",\n")
 | 
				
			||||||
| 
						 | 
					@ -81,18 +87,22 @@ def write_json_or_js(zooms, dest_file, is_js):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def write_html(zooms, dest_file, pages_width, pages_height, prefix, extention):
 | 
					def write_html(zooms, dest_file, pages_width, pages_height, prefix, extention):
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    img_tags = ""
 | 
					    img_tags = ""
 | 
				
			||||||
    for page_idx in sorted(zooms.keys()):
 | 
					    for page_idx in sorted(zooms.keys()):
 | 
				
			||||||
        img_url = f"{prefix}{zooms[page_idx]['name']}.{extention}"
 | 
					        img_url = f"{prefix}{zooms[page_idx]['name']}.{extention}"
 | 
				
			||||||
        zoom_html_data = [','.join([str(zoom) for zoom in page_zooms]) for page_zooms in zooms[page_idx]["zooms"]]
 | 
					        zoom_html_data = [
 | 
				
			||||||
        zoom_html_str = ';'.join(zoom_html_data)
 | 
					            ",".join([str(zoom) for zoom in page_zooms])
 | 
				
			||||||
        img_tags = img_tags + f'            <img loading="lazy" height="{zooms[page_idx]["height"]}" width="{zooms[page_idx]["width"]}" src="{img_url}" data-zooms="{zoom_html_str}"/>\n'
 | 
					            for page_zooms in zooms[page_idx]["zooms"]
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
 | 
					        zoom_html_str = ";".join(zoom_html_data)
 | 
				
			||||||
 | 
					        img_tags = (
 | 
				
			||||||
 | 
					            img_tags
 | 
				
			||||||
 | 
					            + f'            <img loading="lazy" height="{zooms[page_idx]["height"]}" width="{zooms[page_idx]["width"]}" src="{img_url}" data-zooms="{zoom_html_str}"/>\n'
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    img_tags = img_tags.strip()
 | 
					    img_tags = img_tags.strip()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    with open(HTML_TEMPLATE) as template_file, open(dest_file, "w") as data_file:
 | 
					    with open(HTML_TEMPLATE) as template_file, open(dest_file, "w") as data_file:
 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        data = template_file.read().replace(HTML_TO_REPLACE, img_tags)
 | 
					        data = template_file.read().replace(HTML_TO_REPLACE, img_tags)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        data_file.write(data)
 | 
					        data_file.write(data)
 | 
				
			||||||
| 
						 | 
					@ -105,20 +115,34 @@ def generate_argparse():
 | 
				
			||||||
        description="Helper that can generate JSON / JS / HTML files for Melpomene webcomic reader"
 | 
					        description="Helper that can generate JSON / JS / HTML files for Melpomene webcomic reader"
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    parser.add_argument("output_format", choices=["html", "json", "js"], help="The type of output to generate")
 | 
					    parser.add_argument(
 | 
				
			||||||
 | 
					        "output_format",
 | 
				
			||||||
 | 
					        choices=["html", "json", "js"],
 | 
				
			||||||
 | 
					        help="The type of output to generate",
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
    parser.add_argument("svg_folders", help="Path of the folder containing the SVGs")
 | 
					    parser.add_argument("svg_folders", help="Path of the folder containing the SVGs")
 | 
				
			||||||
    parser.add_argument("-o", metavar="dest_file", help="Where to write the generator output to")
 | 
					    parser.add_argument(
 | 
				
			||||||
    parser.add_argument("-p", default="", metavar="img_url_prefix", help="What to prefix the URL of the images when using HTML format.")
 | 
					        "-o", metavar="dest_file", help="Where to write the generator output to"
 | 
				
			||||||
    parser.add_argument("-e", default="png", metavar="img_ext", help="What extention to use in the URL of the images when using HTML format.")
 | 
					    )
 | 
				
			||||||
 | 
					    parser.add_argument(
 | 
				
			||||||
 | 
					        "-p",
 | 
				
			||||||
 | 
					        default="",
 | 
				
			||||||
 | 
					        metavar="img_url_prefix",
 | 
				
			||||||
 | 
					        help="What to prefix the URL of the images when using HTML format.",
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    parser.add_argument(
 | 
				
			||||||
 | 
					        "-e",
 | 
				
			||||||
 | 
					        default="png",
 | 
				
			||||||
 | 
					        metavar="img_ext",
 | 
				
			||||||
 | 
					        help="What extention to use in the URL of the images when using HTML format.",
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return parser
 | 
					    return parser
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
 | 
					 | 
				
			||||||
    args = generate_argparse().parse_args()
 | 
					    args = generate_argparse().parse_args()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    # Get the final outout name
 | 
					    # Get the final outout name
 | 
				
			||||||
    output = None
 | 
					    output = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue