Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions projects/singularity_cinema/create_background/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,23 @@ def __init__(self,
self.slogan = getattr(self.config, 'slogan', [])

def get_font(self, size):
for font_name in self.fonts:
candidates = list(self.fonts)
import subprocess
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The import subprocess statement should be moved to the top of the file with other module imports. Importing modules inside a function can lead to performance overhead as the module is re-imported every time the function is called. It also improves code readability and organization.

for name in candidates:
try:
font_path = fm.findfont(fm.FontProperties(family=font_name))
return ImageFont.truetype(font_path, size)
except OSError or ValueError:
font_path = subprocess.check_output(
['fc-match', '-f', '%{file}\n', name], text=True).strip()
Comment on lines +35 to +36
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The use of fc-match introduces a strong dependency on Linux systems with the fontconfig library. This might limit the portability of the application to other operating systems like Windows or macOS where fc-match is not natively available. Consider if this platform-specific tool is universally available in all target deployment environments or if a more cross-platform font discovery method is necessary.


font = ImageFont.truetype(font_path, size)
font.getmask('中')

logger.info(f"Using font '{name}' -> {font_path}")
return font
except Exception as e:
logger.warning(f"Font '{name}' not usable: {e}")
continue
Comment on lines +43 to 45
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Catching a general Exception can sometimes mask specific issues, making debugging more challenging. It's generally better to catch more specific exceptions that might be raised by subprocess.check_output (e.g., FileNotFoundError if fc-match is not found, subprocess.CalledProcessError if the command fails) or ImageFont.truetype (e.g., OSError, ValueError). This provides clearer error messages and allows for more targeted error handling if needed.

except (FileNotFoundError, subprocess.CalledProcessError, OSError, ValueError) as e:
                logger.warning(f"Font '{name}' not usable: {e}")
                continue


logger.error('No Chinese font found, falling back to default.')
return ImageFont.load_default()

async def execute_code(self, messages, **kwargs):
Expand Down
Loading