File.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from contextlib import closing
  2. from json import dumps
  3. from typing import List, Optional
  4. from pycs.database.Result import Result
  5. class File:
  6. """
  7. database class for files
  8. """
  9. def __init__(self, database, row):
  10. self.database = database
  11. self.identifier = row[0]
  12. self.uuid = row[1]
  13. self.project_id = row[2]
  14. self.type = row[3]
  15. self.name = row[4]
  16. self.extension = row[5]
  17. self.size = row[6]
  18. self.created = row[7]
  19. self.path = row[8]
  20. self.frames = row[9]
  21. self.fps = row[10]
  22. def project(self):
  23. """
  24. get the project associated with this file
  25. :return: project
  26. """
  27. return self.database.project(self.project_id)
  28. def remove(self) -> None:
  29. """
  30. remove this file from the database
  31. :return:
  32. """
  33. with closing(self.database.con.cursor()) as cursor:
  34. cursor.execute('DELETE FROM files WHERE id = ?', [self.identifier])
  35. def previous(self):
  36. """
  37. get the predecessor of this file
  38. :return: another file
  39. """
  40. with closing(self.database.con.cursor()) as cursor:
  41. cursor.execute('''
  42. SELECT * FROM files WHERE id < ? AND project = ? ORDER BY id DESC LIMIT 1
  43. ''', (self.identifier, self.project_id))
  44. row = cursor.fetchone()
  45. if row is not None:
  46. return File(self.database, row)
  47. return None
  48. def next(self):
  49. """
  50. get the successor of this file
  51. :return: another file
  52. """
  53. with closing(self.database.con.cursor()) as cursor:
  54. cursor.execute('''
  55. SELECT * FROM files WHERE id > ? AND project = ? ORDER BY id ASC LIMIT 1
  56. ''', (self.identifier, self.project_id))
  57. row = cursor.fetchone()
  58. if row is not None:
  59. return File(self.database, row)
  60. return None
  61. def results(self) -> List[Result]:
  62. """
  63. get a list of all results associated with this file
  64. :return: list of results
  65. """
  66. with closing(self.database.con.cursor()) as cursor:
  67. cursor.execute('SELECT * FROM results WHERE file = ?', [self.identifier])
  68. return list(map(
  69. lambda row: Result(self.database, row),
  70. cursor.fetchall()
  71. ))
  72. def result(self, identifier) -> Optional[Result]:
  73. """
  74. get a specific result using its unique identifier
  75. :param identifier: unique identifier
  76. :return: result
  77. """
  78. with closing(self.database.con.cursor()) as cursor:
  79. cursor.execute('''
  80. SELECT * FROM results WHERE id = ? AND file = ?
  81. ''', (identifier, self.identifier))
  82. row = cursor.fetchone()
  83. if row is not None:
  84. return Result(self.database, row)
  85. return None
  86. def create_result(self, origin, result_type, label, data):
  87. """
  88. create a result
  89. :param origin:
  90. :param result_type:
  91. :param label:
  92. :param data:
  93. :return:
  94. """
  95. if data is not None:
  96. data = dumps(data)
  97. with closing(self.database.con.cursor()) as cursor:
  98. cursor.execute('''
  99. INSERT INTO results (file, origin, type, label, data)
  100. VALUES ( ?, ?, ?, ?, ?)
  101. ''', (self.identifier, origin, result_type, label, data))
  102. return self.result(cursor.lastrowid)