Skip to content
New issue

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

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

Already on GitHub? # to your account

Provide a library function/method for converting a glob to an uncompiled regex string. #11

Closed
kwlzn opened this issue May 25, 2016 · 4 comments

Comments

@kwlzn
Copy link

kwlzn commented May 25, 2016

currently, the way to get a regex string for a given gitignore-style glob is:

>>> pathspec.GitIgnorePattern('/dist/').regex.pattern
'^dist/.*$'

which incurs the glob->regex translation inside GitIgnorePattern.__init__ which in turn calls RegexPattern.__init__ which automatically compiles the regex.

for the simple case of just wanting to convert a glob into a non-compiled regex string, it'd be great to have a utility function/method that could both be used inside GitIgnorePattern.__init__ and outside as part of the public API.

@cpburnz
Copy link
Owner

cpburnz commented May 27, 2016

I could make a class method part of RegexPattern to convert the glob patterns to the regular expression strings.

@classmethod
def pattern_to_regex(cls, pattern) -> (regex, include)

GitIgnorePattern would then implement it and use it inside of its __init__(). If you want access to the raw regex string, you could acquire it via:

>>> pathspec.GitIgnorePattern.pattern_to_regex('/dist/')
('^dist/.*$', True)

@kwlzn
Copy link
Author

kwlzn commented May 28, 2016

sgtm!

@cpburnz
Copy link
Owner

cpburnz commented Jul 15, 2016

@kwlzn This has been implemented.

@cpburnz cpburnz closed this as completed Jul 15, 2016
@kwlzn
Copy link
Author

kwlzn commented Jul 15, 2016

@cpburnz awesome, thank you!

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants