async def make_a_snapshot( file_name: str, output_name: str, delay: float = DEFAULT_DELAY, pixel_ratio: int = DEFAULT_PIXEL_RATIO, verbose: bool = True, ): logger.VERBOSE = verbose logger.info(MESSAGE_GENERATING) file_type = output_name.split(".")[-1] content = await async_make_snapshot(file_name, file_type, pixel_ratio, delay) if file_type in [SVG_FORMAT, B64_FORMAT]: save_as_text(content, output_name) else: # pdf, gif, png, jpeg content_array = content.split(",") if len(content_array) != 2: raise OSError(content_array) base64_imagedata = content_array[1] imagedata = decode_base64(base64_imagedata) if file_type in [PDF_FORMAT, GIF_FORMAT, EPS_FORMAT]: save_as(imagedata, output_name, file_type) elif file_type in [PNG_FORMAT, JPG_FORMAT]: save_as_png(imagedata, output_name) else: pass if "/" not in output_name: output_name = os.path.join(os.getcwd(), output_name) logger.info(MESSAGE_FILE_SAVED_AS % output_name)
def chk_phantomjs(): try: phantomjs_version = subprocess.check_output( [PHANTOMJS_EXEC, '--version'], shell=get_shell_flag()) phantomjs_version = phantomjs_version.decode('utf-8') logger.info(MESSAGE_PHANTOMJS_VERSION % phantomjs_version) except Exception: logger.warn(MESSAGE_NO_PHANTOMJS) sys.exit(1)
def make_a_snapshot( file_name, output_name, delay=DEFAULT_DELAY, pixel_ratio=DEFAULT_PIXEL_RATIO, verbose=True, ): chk_phantomjs() logger.VERBOSE = verbose logger.info(MESSAGE_GENERATING) file_type = output_name.split(".")[-1] __actual_delay_in_ms = int(delay * 1000) # add shell=True and it works on Windows now. proc_params = [ PHANTOMJS_EXEC, os.path.join(get_resource_dir("phantomjs"), "snapshot.js"), to_file_uri(file_name), file_type, str(__actual_delay_in_ms), str(pixel_ratio), ] proc = subprocess.Popen( proc_params, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=get_shell_flag(), ) if PY2: content = proc.stdout.read() content = content.decode("utf-8") else: content = io.TextIOWrapper(proc.stdout, encoding="utf-8").read() if file_type == SVG_FORMAT: save_as_svg(content, output_name) else: # pdf, gif, png, jpeg content_array = content.split(",") if len(content_array) != 2: raise OSError(content_array) base64_imagedata = content_array[1] imagedata = decode_base64(base64_imagedata) if file_type in [PDF_FORMAT, GIF_FORMAT]: save_as(imagedata, output_name, file_type) elif file_type in [PNG_FORMAT, JPG_FORMAT]: save_as_png(imagedata, output_name) else: raise TypeError(NOT_SUPPORTED_FILE_TYPE.format(file_type)) if "/" not in output_name: output_name = os.path.join(os.getcwd(), output_name) logger.info(MESSAGE_FILE_SAVED_AS % output_name)
def show_help(): logger.info(HELP_TEXT) exit(0)