def readRecords(filePath): facts = getFactories() assert facts is not None with open(filePath, 'r') as fi: txt = fi.read() if txt is not None and len(txt.rstrip().lstrip()) > 0: records = parser.parseTextBlock(txt, facts) else: return None return records['Record']
def _listEntries(match, factories, records): try: listed = _findEntries(match, factories, records, defaultAll = True) except Exception as e: return (False, str(e)) if listed is None or len(listed) == 0: return (True, None) allList = parser.parseTextBlock(match, factories, False) # Sorting if allList is not None and 'Modifier' in allList and len(allList['Modifier']) > 0: modifiers = allList['Modifier'] fields = None for i in modifiers: sortresult = re.match(r'\s*sort\s+(?P<Type>\w+)\s+by\s+(?P<Fields>.*)', i) if sortresult is None: return (False, "Could not interpret modifier '" + i + "'") else: results = sortresult.groups() if results[0] not in factories: return (False, results[0] + ' is not a valid record') unsorted_records = [i[1] for i in listed if i[0] == results[0]] listed = [x for x in listed if x[0] != results[0]] if len(unsorted_records) == 0: continue sortby = [] for a in results[1].split(','): a = a.lstrip().rstrip() if not unsorted_records[0].hasAttribute(a): return (False, 'asking to sort by invalid attribute ' + a + ' from ' + results[0]) sortby.append(a) # Now sort from least to most important (most important first) while len(sortby) > 0: x = sortby.pop() unsorted_records = sorted(unsorted_records, key = lambda z: z.getAttribute(x)) listed += [(results[0], x) for x in unsorted_records] # Making string of [potentially sorted] keys... result = "" for k, r in listed: result += "{0}: {1}\n".format(k, str(r)) return (True, result)
def _toRecords(recStr, factories, isMatch = False, justRecords = True): if recStr is None: return None x = parser.parseTextBlock(recStr, factories, isMatch) if x is None: return None if justRecords: if not "Record" in x: return None return x["Record"] return x