Skip to content

Commit

Permalink
Small update
Browse files Browse the repository at this point in the history
  • Loading branch information
KazukiPrzyborowski committed Nov 17, 2024
1 parent 8ab65a9 commit 5b8ffd1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
57 changes: 43 additions & 14 deletions upcean/predraw/premagick.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,59 @@ def new_image_surface(sizex, sizey, bgcolor):
return [upc_img, None]

def get_save_filename(outfile):
"""Processes the `outfile` parameter to determine a suitable filename and extension."""
"""
Processes the `outfile` parameter to determine a suitable filename and its corresponding
file extension for saving files. Returns a tuple (filename, EXTENSION),
the original `outfile` if it's of type None, bool, or a file object, or
False for unsupported input types.
"""
# Handle None or boolean types directly
if outfile is None or isinstance(outfile, bool):
return outfile
if isinstance(outfile, str):
# Handle file objects directly
if isinstance(outfile, file) or isinstance(outfile, IOBase) or outfile == "-":
return (outfile, "PNG")
# Handle string types
if isinstance(outfile, basestring):
outfile = outfile.strip()
if outfile in ["-", ""]:
return (outfile, "PNG")
# Extract extension using os.path.splitext
base, ext = os.path.splitext(outfile)
ext = ext[1:].upper() if ext else None
if ext and ext in PYTHONMAGICK_SUPPORTED_EXTENSIONS:
return (outfile, PYTHONMAGICK_SUPPORTED_EXTENSIONS[ext])
elif ext:
return (outfile, "PNG")
return (outfile, "PNG")
if ext:
ext = ext[1:].upper() # Remove the dot and convert to uppercase
else:
# Check for custom format 'name:EXT'
custom_match = re.match("^(?P<name>.+):(?P<ext>[A-Za-z]+)$", outfile)
if custom_match:
outfile = custom_match.group('name')
ext = custom_match.group('ext').upper()
else:
ext = None

# Default to "PNG" if no valid extension was found or if unsupported
if not ext or ext not in PYTHONMAGICK_SUPPORTED_EXTENSIONS:
ext = "PNG"
return (outfile, ext)
# Handle tuple or list types
if isinstance(outfile, (tuple, list)) and len(outfile) == 2:
filename, ext = outfile
if isinstance(filename, str):
# Allow file objects or strings as the first element
if isinstance(filename, file) or filename == "-":
pass # file object is valid as-is
elif isinstance(filename, basestring):
filename = filename.strip()
ext = ext.strip().upper()
if ext in PYTHONMAGICK_SUPPORTED_EXTENSIONS:
ext = PYTHONMAGICK_SUPPORTED_EXTENSIONS[ext]
else:
ext = "PNG"
return False # Invalid first element type
# Ensure the extension is a valid string
if not isinstance(ext, basestring):
return False

ext = ext.strip().upper()
if ext not in PYTHONMAGICK_SUPPORTED_EXTENSIONS:
ext = "PNG" # Default to PNG if unsupported
return (filename, ext)
# Unsupported type
return False

def save_to_file(inimage, outfile, outfileext, imgcomment="barcode"):
Expand Down Expand Up @@ -182,7 +211,7 @@ def save_to_file(inimage, outfile, outfileext, imgcomment="barcode"):

# Handle output destinations
if isinstance(outfile, file) or isinstance(outfile, IOBase):
blob = Blob()
blob = PythonMagick.Blob()
upc_img.write(blob)
data = blob.data
outfile.write(data) # Save to a file-like object
Expand Down
21 changes: 20 additions & 1 deletion upcean/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,29 @@
import ConfigParser as configparser
from upcean.versioninfo import getcuryear, __author__, __copyright__, __credits__, __copyright_year__, __license__, __license_string__, __maintainer__, __email__, __status__, __project__, __project_url__, __version_info__, __build_time__, __build_time_utc__, __build_python_info__, __build_python_is_set__, get_build_python_info, __revision__, __version__, __version_alt__, version_info, __version_date_info__, __version_date__, __version_date_alt__, version_date

__config_file__ = 'upcean.ini'
def get_importing_script_path():
# Inspect the stack and get the frame of the caller
stack = inspect.stack()
for frame_info in stack:
# In Python 2, frame_info is a tuple; in Python 3, it's a named tuple
filename = frame_info[1] if isinstance(frame_info, tuple) else frame_info.filename
if filename != __file__: # Ignore current module's file
return os.path.abspath(filename)
return None

scriptconf = os.path.join(os.path.dirname(get_importing_script_path()), "upcean.ini")
if os.path.exists(scriptconf)
__config_file__ = scriptconf
else:
__config_file__ = os.path.join(os.path.dirname(os.path.realpath(__file__)), "upcean.ini")
__use_ini_file__ = True

if os.path.exists(__config_file__) and __use_ini_file__:
# Create a ConfigParser object
config = configparser.ConfigParser()
# Read the configuration file
config.read(__config_file__)
# Accessing values from the config file
enable_pilsupport = config.getboolean('main', 'enable_pilsupport')
enable_cairosupport = config.getboolean('main', 'enable_cairosupport')
enable_qahirahsupport = config.getboolean('main', 'enable_qahirahsupport')
Expand Down

0 comments on commit 5b8ffd1

Please sign in to comment.