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.
155 lines
4.4 KiB
Python
155 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']))
|
|
|
|
if not q.count():
|
|
pattern = Pattern(show_id = int(patternDescriptor['show_id']),
|
|
pattern = str(patternDescriptor['pattern']))
|
|
s.add(pattern)
|
|
s.commit()
|
|
return pattern.getId()
|
|
else:
|
|
return 0
|
|
|
|
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 getPattern(self, patternId : int):
|
|
|
|
if type(patternId) is not int:
|
|
raise ValueError(f"PatternController.getPattern(): Argument patternId is required to be of type int")
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.id == int(patternId))
|
|
|
|
return q.first() if q.count() else None
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.getPattern(): {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 : str) -> dict:
|
|
"""Returns dict {'match': <a regex match obj>, 'pattern': <ffx pattern obj>} or empty dict of no pattern was found"""
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern)
|
|
|
|
matchResult = {}
|
|
|
|
for pattern in q.all():
|
|
patternMatch = re.search(str(pattern.pattern), str(filename))
|
|
if patternMatch is not None:
|
|
matchResult['match'] = patternMatch
|
|
matchResult['pattern'] = pattern
|
|
|
|
return matchResult
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.matchFilename(): {repr(ex)}")
|
|
finally:
|
|
s.close()
|
|
|
|
def getMediaDescriptor(self, context, patternId):
|
|
|
|
try:
|
|
s = self.Session()
|
|
q = s.query(Pattern).filter(Pattern.id == int(patternId))
|
|
|
|
if q.count():
|
|
return q.first().getMediaDescriptor(context)
|
|
else:
|
|
return None
|
|
|
|
except Exception as ex:
|
|
raise click.ClickException(f"PatternController.getPatternDescriptor(): {repr(ex)}")
|
|
finally:
|
|
s.close() |