def procTimeAndRe(criteria): """ Parse the criteria, produce the SQL and the Values time points in the criteria are unix timestamps, they must be converted to text format to suit the SQL needs. """ whereSql = '' whereVals = [] times = criteria.get('times') tmField = times.get('field') if times else None tmPoints = times.get('points', []) if times else [] tmPoints = [(isodatetime(t1), isodatetime(t2)) for t1, t2 in tmPoints] regxs = criteria.get('regxs') allMatch = regxs.get('allMatch', False) if regxs else False patterns = regxs.get('patterns') if regxs else [] # time matching SQL tmSqls = [] tmVals = [] for t1, t2 in tmPoints: tmSqls.append('(? <= %s AND %s <= ?)' % (tmField, tmField)) tmVals.extend([t1, t2]) tmSqls = ' OR '.join(tmSqls) # the regular expression matching SQL (LIKE for now ) # unfortunately, Python sqlites module seems not support RE # we use LIKE operator instead, temporarily. matchSqls = [] matchVals = [] texts = ['author', 'subject', 'scene', 'people', 'tag'] for pat, flag, field in patterns: """ one pattern against all texts, or a specific field. """ pat = '%%%s%%' % pat # A 'in' LIKE if field: matchSqls.append('%s LIKE ?' % field) matchVals.append(pat) else: ss = ['%s LIKE ?' % x for x in texts] ss.append("(binary = 'false' AND data LIKE ?)") ss = ' OR '.join(ss) matchSqls.append('(%s)' % ss) matchVals.extend([pat] * (len(texts) + 1)) if allMatch: matchSqls = ' AND '.join(matchSqls) else: matchSqls = ' OR '.join(matchSqls) subSqls = [] if tmSqls: subSqls.append('(%s)' % tmSqls) whereVals.extend(tmVals) if matchSqls: subSqls.append('(%s)' % matchSqls) whereVals.extend(matchVals) whereSql = ' AND '.join(subSqls) return whereSql, whereVals
def procTimeAndRe(criteria): """ Parse the criteria, produce the SQL and the Values time points in the criteria are unix timestamps, they must be converted to text format to suit the SQL needs. """ whereSql = '' whereVals = [] times = criteria.get('times') tmField = times.get('field') if times else None tmPoints = times.get('points', []) if times else [] tmPoints = [(isodatetime(t1), isodatetime(t2)) for t1, t2 in tmPoints] regxs = criteria.get('regxs') allMatch = regxs.get('allMatch', False) if regxs else False patterns = regxs.get('patterns') if regxs else [] # time matching SQL tmSqls = [] tmVals = [] for t1, t2 in tmPoints: tmSqls.append('(? <= %s AND %s <= ?)' % (tmField, tmField)) tmVals.extend([t1, t2]) tmSqls = ' OR '.join(tmSqls) # the regular expression matching SQL (LIKE for now ) # unfortunately, Python sqlites module seems not support RE # we use LIKE operator instead, temporarily. matchSqls = [] matchVals = [] texts = ['author', 'subject', 'scene', 'people', 'tag'] for pat, flag, field in patterns: """ one pattern against all texts, or a specific field. """ pat = '%%%s%%' % pat # A 'in' LIKE if field: matchSqls.append('%s LIKE ?' % field) matchVals.append(pat) else: ss = ['%s LIKE ?' % x for x in texts] ss.append('(binary = 0 AND data LIKE ?)') ss = ' OR '.join(ss) matchSqls.append('(%s)' % ss) matchVals.extend([pat] * (len(texts) + 1)) if allMatch: matchSqls = ' AND '.join(matchSqls) else: matchSqls = ' OR '.join(matchSqls) subSqls = [] if tmSqls: subSqls.append('(%s)' % tmSqls) whereVals.extend(tmVals) if matchSqls: subSqls.append('(%s)' % matchSqls) whereVals.extend(matchVals) whereSql = ' AND '.join(subSqls) return whereSql, whereVals
def makeRequests(self, *, record=None, time=None, host=None, protocol=None, port=None, user=None, password=None, **junk): """ Create the necessary requests data for collecting information for a Record from the user interactively. """ if record: # a Record instance provided time = record.time host = record.host protocol = record.protocol port = record.port user = record.user password = record.password else: time = time if time else timeutils.isodatetime() host = host if host else '' protocol = protocol if protocol else '' port = port if port else '' user = user if user else '' password = password if password else '' requests = [] # arguments: name, default, datatype, reader, desc requests.append(applib.makeOneRequest('time', time, str, None, 'time')) requests.append(applib.makeOneRequest('host', host, str, None, 'host')) requests.append(applib.makeOneRequest('protocol', protocol, str, None, 'protocol')) requests.append(applib.makeOneRequest('port', port, str, None, 'port')) requests.append(applib.makeOneRequest('user', user, str, None, 'user')) requests.append(applib.makeOneRequest('password', password, str, None, 'password')) return requests
def makeRequests(self, *, record=None, time=None, scene=None, people=None, tag=None, **junk): """ Create the necessary requests data for collecting information for a Record from the user interactively. """ if record: # a Record instance provided time = record.time scene = record.scene people = record.people tag = record.tag else: time = time if time else isodatetime() people = people if people else '' # take the recently used scene and # tag from the most recent log. if not scene: lastLog = self.lastLog() scene = self.lastScene(lastLog) if not tag: if not lastLog: lastLog = self.lastLog() tag = self.lastTag(lastLog) requests = [] # arguments: name, default, datatype, reader, desc requests.append(applib.makeOneRequest('time', time, str, None, 'time')) requests.append(applib.makeOneRequest('scene', scene, str, None, 'scene')) requests.append(applib.makeOneRequest('people', people, str, None, 'people')) requests.append(applib.makeOneRequest('tag', tag, str, None, 'tag')) return requests
def makeLogEntry(self, *junk, **args): id = args.pop('id') author = args.pop('author') fields = self.collectLogInfo(**args) fields['id'] = id fields['author'] = author fields['mtime'] = isodatetime() # update with current time assert self.checkRequirement(**fields), "field data not sufficient" fields = Record.convertFields(fields.items()) return Record(**fields)
def add(self, interactive=False, **fields): """ Add a log record to the system When interactive is True, ask data for subject, time, scene, people, tag, and log data from the use interactively, the provided arguments are used as the default value for user's choice. """ if interactive: fields = self.collectLogInfo(**fields) author = '%s <%s>' % (self.config['authorName'], self.config['authorEmail']) fields['author'] = author fields['mtime'] = isodatetime() # current time for mtime assert self.checkRequirement(**fields), "field data not sufficient" fields = Record.convertFields(fields.items()) record = Record(**fields) record.save()
def makeRequests(self, *, record=None, time=None, scene=None, people=None, tag=None, **junk): """ Create the necessary requests data for collecting information for a Record from the user interactively. """ if record: # a Record instance provided time = record.time scene = record.scene people = record.people tag = record.tag else: time = time if time else isodatetime() people = people if people else '' # take the recently used scene and # tag from the most recent log. if not scene: lastLog = self.lastLog() scene = self.lastScene(lastLog) if not tag: if not lastLog: lastLog = self.lastLog() tag = self.lastTag(lastLog) requests = [] # arguments: name, default, datatype, reader, desc requests.append(applib.makeOneRequest('time', time, str, None, 'time')) requests.append( applib.makeOneRequest('scene', scene, str, None, 'scene')) requests.append( applib.makeOneRequest('people', people, str, None, 'people')) requests.append(applib.makeOneRequest('tag', tag, str, None, 'tag')) return requests