Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
output_config.py
Go to the documentation of this file.
1 ## @package data_structs.output_config
2 
3 from data_structs.base_objects import DBObject
4 from data_structs.output import Output
5 
6 ## An OutputConfig is a set of Outputs that can be 'run' on a particular TRS file. These groupings are recorded in the database.
8  ## Constructor
9  # @param self
10  # @param name (string) user-defined name for this config
11  # @param desc (string) user-defined description of this config
12  # @param outputs (list) list of the Output objects that this config wraps
13  # @param created (string=None) creation timestamp string. This is set via db_insert() and db_select(), so you shouldn't often need to use it. A value of None indicates this config isn't in the DB yet.
14  # @param db_id (int=None) if this config is in the DB, this is the corresponding primary key value from the output_configs table (else it is None). This is set via db_insert() and db_select(), so you shouldn't often need to use it.
15  def __init__(self, name, desc, outputs, created=None, db_id=None):
16  self.name = name
17  self.desc = desc
18  self.outputs = outputs
19  self.created = created
20  self.db_id = db_id
21 
22  ## See superclass description.
23  def db_insert(self, db):
24  super(OutputConfig, self).db_insert(db)
25 
26  #insert into the output_configs DB table, and set the db_id
27  last_ids = db.insert('output_configs',
28  'name desc'.split(),
29  [[self.name, self.desc]]
30  )
31  self.db_id = last_ids[0]
32 
33  #perform a follow-up select to grab the created timestamp (the DB handles the setting of this upon insertion)
34  rows = db.select('output_configs',
35  ["datetime(created,'localtime')"],
36  'id=?',
37  [str(self.db_id)],
38  )
39  self.created = rows[0][0]
40 
41  #make sure all of the outputs are in the DB
42  for cur_output in self.outputs:
43  if not cur_output.db_id:
44  cur_output.db_insert(db)
45 
46  #insert a record into the relationship table that links the outputs to this config in the DB
47  db.insert('output_configs_to_outputs',
48  'config_id output_id'.split(),
49  [[self.db_id, cur_output.db_id]],
50  )
51 
52  ## See superclass description.
53  def db_delete(self, db):
54  super(OutputConfig, self).db_delete(db)
55 
56  #remove the outputs from the DB.
57  #note: this will also delete the corresponding rows in output_configs_to_outputs due to foreign key cascades
58  for cur_output in self.outputs:
59  cur_output.db_delete(db)
60 
61  if db.delete('output_configs', 'id=?', [self.db_id]) > 0:
62  self.db_id = None
63 
64  ## See superclass description.
65  @staticmethod
66  def db_select(db, ids=[]):
67  DBObject.db_select(db, ids)
68 
69  #select the configs
70  rows = db.select('output_configs',
71  "id name desc datetime(created,'localtime')".split(),
72  DBObject._build_where_cond_from_ids(ids),
73  )
74 
75  #construct the OutputConfig objects
76  config_list = []
77  for cur_row in rows:
78  #select the corresponding Outputs for this config using a relationship table
79  outputs = Output.db_select_by_ref(db,
80  'output_configs_to_outputs',
81  'output_id',
82  'config_id',
83  cur_row[0],
84  )
85 
86  config = OutputConfig(cur_row[1],
87  cur_row[2],
88  outputs,
89  cur_row[3],
90  cur_row[0],
91  )
92  config_list.append(config)
93 
94  return config_list
95