Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get the original file name from the temp file name #1289

Closed
lrlunin opened this issue Dec 11, 2024 · 1 comment
Closed

get the original file name from the temp file name #1289

lrlunin opened this issue Dec 11, 2024 · 1 comment

Comments

@lrlunin
Copy link
Contributor

lrlunin commented Dec 11, 2024

Hello,

first of all I would like to thank you for brining so much effort to build such a useful and customizable tool for python notebooks!

I have faced a little issue using the --pipe option. While I convert the .py file to .ipynb I want to append a "open in colab" badge which refers to the notebook in the GH repository.

To do so I use the following pipeline:

  - repo: https://github.com/mwouts/jupytext
    rev: v1.16.4
    hooks:
     - id: jupytext
       name: convert .py to .ipynb
       args:
         - --update
         - --pipe
         - "python scripts/insert_badge.py {}"
         - --to
         - "../notebooks//ipynb"
         - examples/scripts/*.py

while in insert_badge.py I have something like:

import sys

temp_filename = sys.argv[1]
...

The temp filenames look like original_filename_randomsuffix.py and I thought I can just rstrip the the temp_filename and take the substring left from last _ to get the basename. However, once I had a suffix of tempfile which contained another underscore in a way the tempfile was like: original_filename_randomsuffix_randomsuffix.py.

Is there any workarounds to get the original name? A possible sollution would be to basically set the suffix for the tempfiles to be constant or either restrict an underscore (really dirty mess).

@lrlunin lrlunin changed the title get the original file name for temp file name get the original file name from the temp file name Dec 11, 2024
@lrlunin
Copy link
Contributor Author

lrlunin commented Dec 11, 2024

Okay, this was much more interesting thing then I have expected.

I have expected that python relies on the standard libc and creates the random file with the posix mkstemp() function which generates a temp file with the template filenameXXXXXX where X can be an any alphanumeric (A-Za-z0-9) character.

However, in the reality they created their own posix-like random filename generator in the tempfile https://github.com/python/cpython/blob/8b24d042b4d23700f873c23f78cee0972ad24f62/Lib/tempfile.py#L140

This contains only lowcase letters, also numbers but also includes an "_" in the list for the allowed characters. This is really not clever to make the behaviour expected to be simillar to mkstemp but then make it not posix conform.

What would you think about to change "_" to any other character ("@", "^" or even ".") which cannot be possibly be within the tempfile name?

prefix = prefix + (" " if " " in prefix else "_")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants