def getUnqlite(file, filters={}): """Given a specific UnQLite file (string) and a set of filters (dictionary key-values pairs), will return a JSON-formatted tree of the matching data entries from that file (starting as a null-key list of objects). Key-value pairs can be selected using the '_key' parameter, while specific collections can be selected using the '_collection' parameter. If neither parameter is used, all contents of the database are returned. """ db = unq(file) if '_collection' in filters: c = filters.pop('_collection') coll = db.collection(c) all = coll.all() elif '_key' in filters: key = filters.pop('_key') all = [{key: db[key]}] else: all = [{'_root':{}}] for key,value in db: c = db.collection(key) if c.exists(): all.append({key: c.all()}) else: isKey = True if re.search('_\d+$', key): c = db.collection(re.sub('_\d+$', '', key)) isKey = not c.exists() if isKey: all.append({key:value}) all[0]['_root'][key] = value dicts = basic.filter(all, filters) if len(dicts) > 0: return formatJson(dicts) else: raise Exception('No matching data entries found')
def getExcel(file, filters={}): """Given a specific Excel file (string) and a set of filters (dictionary key-values pairs), will return a CSV-formatted table of the matching data entries from that file (including a header row). Defaults to the first sheet in the workbook; other sheets can be specified by name or zero-based index using the '_sheet' parameter in the URL's query segment. """ wb = xlrd.open_workbook(file) if '_sheet' not in filters: warnings.warn('No worksheet specified (_sheet filter expected); defaulting to first worksheet') sheet = 0 else: sheet = filters.pop('_sheet') if type(sheet) is type(''): ws = wb.sheet_by_name(sheet) else: ws = wb.sheet_by_index(int(sheet)) header = [basic.parseStrValue(h.value) for h in ws.row(0)] all = [] for i in range(1,ws.nrows): d = {} row = ws.row(i) for j,h in enumerate(header): entry = row[j] if entry.ctype == 4: value = str(entry.value == 1) else: value = str(entry.value) d[h] = basic.parseStrValue(value) all.append(d) dicts = basic.filter(all, filters) if len(dicts) > 0: return formatCsv(dicts) else: raise Exception('No matching data entries found')
def getXml(file, filters={}): """Given a specific XML file (string) and a set of filters (dictionary key-values pairs), will return a JSON-formatted tree of the matching data entries from that file (starting as a null-key list of objects). """ root = xet.parse(file).getroot() all = [parseElement(el) for el in root] dicts = basic.filter(all, filters) if len(dicts) > 0: return formatJson(dicts,root.tag) else: raise Exception('No matching data entries found')
def getJson(file, filters={}): """Given a specific JSON file (string) and a set of filters (dictionary key-values pairs), will return a JSON-formatted tree of the matching data entries from that file (starting as a null-key list of objects). """ with open(file, 'r') as f: j = json.loads(f.read()) all = j[''] dicts = basic.filter(all, filters) if len(dicts) > 0: return formatJson(dicts) else: raise Exception('No matching data entries found')
def getCsv(file, filters={}): """Given a specific CSV file (string) and a set of filters (dictionary key-values pairs), will return a CSV-formatted table of the matching data entries from that file (including a header row). """ all = [] with open(file) as f: reader = csv.DictReader(f) for row in reader: for field in row: row[field] = basic.parseStrValue(row[field]) all.append(row) dicts = basic.filter(all, filters) if len(dicts) > 0: return formatCsv(dicts) else: raise Exception('No matching data entries found')
def test_basic(self): dicts = [{'one': 1}, {'two': 2}] dicts = basic.filter(dicts, {})