You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
4.4 KiB
Python
162 lines
4.4 KiB
Python
import click, re
|
|
|
|
from ffx.model.pattern import Pattern
|
|
|
|
|
|
class PatternController():
|
|
|
|
def __init__(self, context):
|
|
|
|
self.context = context
|
|
self.Session = self.context['database_session'] # convenience
|
|
|
|
|
|
def addPattern(self, patternDescriptor):
|
|
|
|
try:
|
|
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.show_id == int(patternDescriptor['show_id']), Pattern.pattern == str(patternDescriptor['pattern']))
|
|
|
|
if not q.count():
|
|
pattern = Pattern(show_id = int(patternDescriptor['show_id']),
|
|
pattern = str(patternDescriptor['pattern']))
|
|
s.add(pattern)
|
|
s.commit()
|
|
return patternDescriptor
|
|
else:
|
|
return {}
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.addPattern(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def updatePattern(self, patternId, patternDescriptor):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.id == int(patternId))
|
|
|
|
if q.count():
|
|
|
|
pattern = q.first()
|
|
|
|
pattern.show_id = int(patternDescriptor['show_id'])
|
|
pattern.pattern = str(patternDescriptor['pattern'])
|
|
|
|
s.commit()
|
|
return True
|
|
|
|
else:
|
|
return False
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.updatePattern(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
|
|
def findPattern(self, patternDescriptor):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.show_id == int(patternDescriptor['show_id']), Pattern.pattern == str(patternDescriptor['pattern']))
|
|
|
|
if q.count():
|
|
pattern = q.first()
|
|
return int(pattern.id)
|
|
else:
|
|
return None
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.findPattern(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def getPatternDict(self, pattern):
|
|
patternDescriptor = {}
|
|
patternDescriptor['pattern'] = str(pattern.pattern)
|
|
patternDescriptor['show_id'] = int(pattern.show_id)
|
|
return patternDescriptor
|
|
|
|
|
|
def getPatternDescriptor(self, patternId):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.id == int(patternId))
|
|
|
|
if q.count():
|
|
pattern = q.first()
|
|
return self.getPatternDict(pattern)
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.getPatternDescriptor(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def deletePattern(self, patternId):
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.id == int(patternId))
|
|
|
|
if q.count():
|
|
|
|
#DAFUQ: https://stackoverflow.com/a/19245058
|
|
# q.delete()
|
|
pattern = q.first()
|
|
s.delete(pattern)
|
|
|
|
s.commit()
|
|
return True
|
|
return False
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.deletePattern(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
def matchFilename(self, filename):
|
|
|
|
SEASON_PATTERN = '[sS]([0-9]+)'
|
|
EPISODE_PATTERN = '[eE]([0-9]+)'
|
|
|
|
result = {}
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern)
|
|
|
|
for pattern in q.all():
|
|
|
|
match = re.search(pattern.pattern, filename)
|
|
|
|
if match:
|
|
|
|
result['pattern_id'] = pattern.id
|
|
result['show_id'] = pattern.show_id
|
|
|
|
result['indicator'] = match.group(1)
|
|
|
|
seasonMatch = re.search(SEASON_PATTERN, result['indicator'])
|
|
if seasonMatch:
|
|
result['season'] = int(seasonMatch.group(1))
|
|
|
|
episodeMatch = re.search(EPISODE_PATTERN, result['indicator'])
|
|
if episodeMatch:
|
|
result['episode'] = int(episodeMatch.group(1))
|
|
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.matchFilename(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
return result
|
|
|