def load_extract(body, ctype): ''' Read two JSON files merged with a LINEFEED, and use the specification to extract an array from the JSON source Sample queries: * curl --request POST --data-binary @- http://localhost:8880/load.extract.json < test/data/load/json_plus_spec.dat ''' file1, file2 = body.split('\f') file1_parsed = [] if ejsonify.is_json(file1, file1_parsed): file1_parsed = file1_parsed[0] file2_parsed = [] if not file2 or ejsonify.is_json(file2, file2_parsed): if file2: file2_parsed = file2_parsed[0] else: file2_parsed = None result = ejsonify.pull_ejson_by_patterns(file1_parsed, file2_parsed) items = result[u'items'] profile = add_ejson_profile(items) result = json.dumps({'items': items, 'data_profile': profile}, indent=4) return result raise ValueError('Unable to process content')
def json_nav_prep(body, ctype): ''' Read and analyze a JSON file to support the UI for having the user navigate and select an array for extract (see load_extract) Sample queries: * curl --request POST --data-binary @- http://localhost:8880/json.nav.prep.json < test/data/load/somefeed.json ''' ejsonify_output = [] if ejsonify.is_json(body, ejsonify_output): obj = ejsonify_output[0] result = ejsonify.analyze_for_nav(obj) result = json.dumps(result, indent=4) return result else: raise ValueError('Unable to process content')
else: lead = body.strip() if lead.startswith('%%') or lead.startswith('@'): #BibTex cmdline = 'bib2xml' process = Popen(cmdline, stdin=PIPE, stdout=PIPE, universal_newlines=True, shell=True) mods, perr = process.communicate(input=body) if not mods: #FIXME: L10N raise ValueError('Empty output from the command line. Probably a failure. Command line: "%s"'%cmdline) #print >> sys.stderr, mods[:100] data, diag_info = mods2json(body, diagnostics) imt = 'application/x-bibtex' else: ejsonify_output = [] if ejsonify.is_json(body, ejsonify_output): obj = ejsonify_output[0] data = obj[u'items'] fixup_obj_labels = False imt = 'application/json' else: #FIXME: how to deal with CSV character sets? data = readcsv(body) if maxcount: data = data[:int(maxcount)] #FIXME: Following redundant now that we have add_ejson_profile #Keeping it to avoid breaking what's working for now objkeys = dict([ (k, k) for obj in data for k in obj ]) #FIXME: reduce from 3 full passes through obj to 2 (don't think we can go lower than 2)