osl-incubator / scicookie

Cookiecutter template for a Python package.
https://osl-incubator.github.io/scicookie
Other
11 stars 18 forks source link

fix: Fix the default slug used for the package name #245

Closed abhijeetSaroha closed 6 months ago

abhijeetSaroha commented 6 months ago

I added a function to sanitize the package_slug. So, now, It will remove the non-alphanumeric characters from the package_slug and will not give error.

Solves #197

abhijeetSaroha commented 6 months ago

@xmnlab Here, I think only the default value can be managed. What if a user enters their own package_slug with unwanted symbols? How can that be handled here?

xmnlab commented 6 months ago

Yep the point is to not have a default value wrong. If the user decide any wrong value it should just fail.

Does it make sense?

abhijeetSaroha commented 6 months ago
package_slug:
  message: Type the code name for your package (the name used to import your package)
  help: https://osl-incubator.github.io/scicookie/guide/#information-about-the-project
  type: text
  default: "{{project_slug.replace('-', '_').replace('.', '').replace('@', '').replace('&', '').replace('$', '').replace('%', '').replace('#', '')}}"
  visible: true

This is one of the way (works fine), but It doesn't seem to be good for production.

Then, I also tried regex, but I am not able to find Builtin Filter which has support of regex.

Then, I tried to build the logic which looks like this:

package_slug:
  message: Type the code name for your package (the name used to import your package)
  help: https://osl-incubator.github.io/scicookie/guide/#information-about-the-project
  type: text
  default: "{% set res='' %}{% for char in project_slug.replace('-','_') %}{% if char.isalnum() or char == '_' %}{% set res = res + char %}{% endif %}{% endfor %}{{ res }}"
  visible: true

But, after this, it gives me default value as empty string, if I give some initial value to the res, then default value would be the same. The for loop or the logic inside is not working.

Do you have any idea more about this?

xmnlab commented 6 months ago

I think that the way to go using jinja2 filters.

so you could create a more robust regex here: https://github.com/osl-incubator/scicookie/blob/main/src/scicookie/ui.py

Here is an example of implementation (pseudo code) and how to use it:

import re
from jinja2 import Environment

# Define the custom filter
def sanitize_package_slug(package_slug: str):
    """A custom Jinja2 filter to perform regex replacement."""
    #  do you trick here
    return sanitized_string

# Create a Jinja2 environment and add the custom filter
env = Environment()
env.filters['sanitize_package_slug'] = sanitize_package_slug

# Now you can use this filter in your templates
template_string = """
default: "{{project_slug | sanitize_package_slug }}"
"""

in the original code we are using Template, so you will need to change it to Environment (as the same in the example)

abhijeetSaroha commented 6 months ago

@xmnlab , can you please review the changes, on my side I test the changes and works fine.

abhijeetSaroha commented 6 months ago

I removed the comment and autoescape also, so it is now false by default.

xmnlab commented 6 months ago

thanks @abhijeetSaroha

github-actions[bot] commented 5 months ago

:tada: This PR is included in version 0.8.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: