profile
viewpoint

Ask questionsbug: _templates_suffix: "" gives UnicodeDecodeError on binary files ( .ico or .png for example )

Describe the problem <!-- A clear and concise description of what the bug is. -->

I want to use _templates_suffix: "" The problem is if I use it, then even image files will be processed by copier. And it will give error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 14: invalid start byte

Environment

  • OS: Ubuntu on WSL2 on Win10
  • Copier version: copier 6.0.0a6.post43.dev0+2fe3072
  • Python version: 3.9.6
  • Installation method: pipx+git

Additional context <!-- Add any other context about the problem here. -->

    create  .
    create  .dependabot
    create  .dependabot/config.yml
    create  .en v copy
    create  .env
    create  .gitignore
    create  LICENSE
    create  README.md
    create  Banana
    create  Banana/.prettierignore
    create  Banana/backend
    create  Banana/backend/alembic.ini
    create  Banana/backend/app
    create  Banana/backend/app/alembic
    create  Banana/backend/app/alembic/env.py
    create  Banana/backend/app/alembic/README
    create  Banana/backend/app/alembic/script.py.mako
    create  Banana/backend/app/alembic/versions
    create  Banana/backend/app/alembic/versions/91979b40eb38_create_users_table.py
    create  Banana/backend/app/alembic/__init__.py
    create  Banana/backend/app/alembic.ini
    create  Banana/backend/app/api
    create  Banana/backend/app/api/api_v1
    create  Banana/backend/app/api/api_v1/routers
    create  Banana/backend/app/api/api_v1/routers/auth.py
    create  Banana/backend/app/api/api_v1/routers/tests
    create  Banana/backend/app/api/api_v1/routers/tests/test_auth.py
    create  Banana/backend/app/api/api_v1/routers/tests/test_users.py
    create  Banana/backend/app/api/api_v1/routers/tests/__init__.py
    create  Banana/backend/app/api/api_v1/routers/users.py
    create  Banana/backend/app/api/api_v1/routers/__init__.py
    create  Banana/backend/app/api/api_v1/__init__.py
    create  Banana/backend/app/api/dependencies
    create  Banana/backend/app/api/dependencies/__init__.py
    create  Banana/backend/app/api/__init__.py
    create  Banana/backend/app/core
    create  Banana/backend/app/core/auth.py
    create  Banana/backend/app/core/celery_app.py
    create  Banana/backend/app/core/config.py
    create  Banana/backend/app/core/security.py
    create  Banana/backend/app/core/__init__.py
    create  Banana/backend/app/db
    create  Banana/backend/app/db/crud.py
    create  Banana/backend/app/db/models.py
    create  Banana/backend/app/db/schemas.py
    create  Banana/backend/app/db/session.py
    create  Banana/backend/app/db/__init__.py
    create  Banana/backend/app/initial_data.py
    create  Banana/backend/app/main.py
    create  Banana/backend/app/tasks.py
    create  Banana/backend/app/tests
    create  Banana/backend/app/tests/test_main.py
    create  Banana/backend/app/tests/test_tasks.py
    create  Banana/backend/app/tests/__init__.py
    create  Banana/backend/app/__init__.py
    create  Banana/backend/conftest.py
    create  Banana/backend/Dockerfile
    create  Banana/backend/pyproject.toml
    create  Banana/backend/requirements.txt
    create  Banana/docker-compose.yml
    create  Banana/frontend
    create  Banana/frontend/.dockerignore
    create  Banana/frontend/.eslintrc.js
    create  Banana/frontend/.prettierrc.js
    create  Banana/frontend/Dockerfile
    create  Banana/frontend/package.json
    create  Banana/frontend/public
Traceback (most recent call last):
  File "/home/orange/.local/bin/copier", line 8, in <module>
    sys.exit(CopierApp.run())
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/plumbum/cli/application.py", line 614, in run
    inst, retcode = subapp.run(argv, exit=False)
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/plumbum/cli/application.py", line 609, in run
    retcode = inst.main(*tailargs)
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/copier/cli.py", line 70, in _wrapper
    return method(*args, **kwargs)
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/copier/cli.py", line 293, in main
    self.parent._worker(
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/copier/main.py", line 575, in run_copy
    self._render_folder(src_abspath)
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/copier/main.py", line 467, in _render_folder
    self._render_folder(file)
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/copier/main.py", line 467, in _render_folder
    self._render_folder(file)
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/copier/main.py", line 467, in _render_folder
    self._render_folder(file)
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/copier/main.py", line 469, in _render_folder
    self._render_file(file)
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/copier/main.py", line 429, in _render_file
    tpl = self.jinja_env.get_template(str(src_relpath))
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/jinja2/environment.py", line 883, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/jinja2/environment.py", line 857, in _load_template
    template = self.loader.load(self, name, globals)
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/jinja2/loaders.py", line 115, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/home/orange/.local/pipx/venvs/copier/lib/python3.8/site-packages/jinja2/loaders.py", line 184, in get_source
    contents = f.read().decode(self.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 14: invalid start byte
copier-org/copier

Answer questions pawamoy

@karolzlot

it is faster to exclude files this way than to check each file and catch Exception

You might have misunderstood @Yajo's suggestion (or I misunderstood it myself!): this would be implemented in Copier itself, the template writers/users would not have to catch any exceptions themselves. It would automatically copy images and other binary files without trying to render them (well, trying, failing, and falling back to a simple copy). This would actually be faster than asking the user to exclude them using a _copy_without_render option.

I don't want to have different extensions before and after parsing because it makes correct syntax highlighing more difficult.

Could you expand on this? In my opinion, having a README.md.jinja in the template makes it very clear to the IDE that you want a Jinja+Markdown syntax highlighting, not just a Markdown one.


I think we can start by fixing the issue by falling back to a simple copy if the file is not UTF8 decodable. Later we could add an option to make this explicit and at the hands of the template writer.

useful!

Related questions

No questions were found.
source:https://uonfu.com/
Github User Rank List