Closed abhijeetSaroha closed 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?
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?
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?
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)
@xmnlab , can you please review the changes, on my side I test the changes and works fine.
I removed the comment and autoescape
also, so it is now false by default.
thanks @abhijeetSaroha
:tada: This PR is included in version 0.8.0 :tada:
The release is available on:
0.8.0
Your semantic-release bot :package::rocket:
I added a function to sanitize the
package_slug
. So, now, It will remove the non-alphanumeric characters from thepackage_slug
and will not give error.Solves #197