Skip to content

Commit

Permalink
implement fnmatch polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 committed Nov 23, 2022
1 parent f0f292c commit b8a40ea
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion Scanners/Series/Absolute Series Scanner.py
Expand Up @@ -8,7 +8,6 @@
import time # strftime
import datetime # datetime
import re # match, compilef, sub
import fnmatch # translate
import logging, logging.handlers # FileHandler, Formatter, getLogger, DEBUG | RotatingFileHandler
import inspect # getfile, currentframe
import ssl # SSLContext
Expand All @@ -28,6 +27,48 @@
import VideoFiles # Scan
import Stack # Scan

###### fnmatch polyfill #############################################

_cache = {}
_MAXCACHE = 100

class fnmatch:
@staticmethod
def fnmatch(name, pat):
name = os.path.normcase(name)
pat = os.path.normcase(pat)
return fnmatch.fnmatchcase(name, pat)

@staticmethod
def fnmatchcase(name, pat):
try:
re_pat = _cache[pat]
except KeyError:
res = fnmatch.translate(pat)
if len(_cache) >= _MAXCACHE:
_cache.clear()
_cache[pat] = re_pat = re.compile(res)
return re_pat.match(name) is not None

@staticmethod
def translate(pat):
"""Translate a shell PATTERN to a regular expression.
There is no way to quote meta-characters.
"""

i, n = 0, len(pat)
res = ''
while i < n:
c = pat[i]
i = i+1
if c == '*':
res = res + '.*'
else:
res = res + re.escape(c)
return res + '\Z(?ms)'

###### fnmatch polyfill end #########################################

### http://www.zytrax.com/tech/web/regex.htm # http://regex101.com/#python
def com(string): return re.compile(string, re.UNICODE) #RE Compile
def cic(string): return re.compile(string, re.IGNORECASE | re.UNICODE) #RE Compile Ignore Case
Expand Down

0 comments on commit b8a40ea

Please sign in to comment.