def load_json(self, dump_path, verbose=False): dump_struct = json.load(open(dump_path)) if verbose: print 'Building table...' # Protect against writing into created database. ckan.model.metadata.create_all() for model_class in self.model_classes: if model.Session.query(model_class).count(): raise Exception, "Existing '%s' records in database" % model_class records = {} for model_class in self.model_classes: table = self.get_table(model_class) collection_objects = {} model_class_name = model_class.__name__ records[model_class_name] = collection_objects if verbose: print model_class_name, '--------------------------------' collectionStruct = dump_struct[model_class_name] if verbose: print collectionStruct.keys() recordIds = collectionStruct.keys() recordIds.sort() for recordId in recordIds: record_struct = collectionStruct[recordId] record_struct = self.switch_names(record_struct) if verbose: print record_struct q = table.insert(values=record_struct) result = q.execute() self.fix_sequences() if verbose: print 'OK'
def load_basemaps(basemapsFile): try: with open(basemapsFile) as config_file: basemapsConfig = json.load(config_file) except Exception, inst: msg = "Couldn't read basemaps config from %r: %s" % (basemapsFile, inst) raise Exception(msg)
def i18n_js_translations(lang, ver=API_REST_DEFAULT_VERSION): ckan_path = os.path.join(os.path.dirname(__file__), u'..') source = os.path.abspath(os.path.join(ckan_path, u'public', u'base', u'i18n', u'{0}.js'.format(lang))) if not os.path.exists(source): return u'{}' translations = json.load(open(source, u'r', encoding='utf-8')) return _finish_ok(translations)
def i18n_js_translations(lang, ver=API_REST_DEFAULT_VERSION): ckan_path = os.path.join(os.path.dirname(__file__), u'..') source = os.path.abspath(os.path.join(ckan_path, u'public', u'base', u'i18n', u'{0}.js'.format(lang))) if not os.path.exists(source): return u'{}' translations = json.load(open(source, u'r')) return _finish_ok(translations)
def i18n_js_translations(lang, ver=API_REST_DEFAULT_VERSION): if lang not in get_locales_from_config(): return _finish_bad_request('Unknown locale: {}'.format(lang)) ckan_path = os.path.join(os.path.dirname(__file__), u'..') source = os.path.abspath(os.path.join(ckan_path, u'public', u'base', u'i18n', u'{0}.js'.format(lang))) if not os.path.exists(source): return u'{}' translations = json.load(io.open(source, u'r', encoding='utf-8')) return _finish_ok(translations)
def test_dump(self): assert os.path.exists(self.outpath) dumpeddata = json.load(open(self.outpath)) assert dumpeddata['version'] == ckan.__version__ tables = dumpeddata.keys() for key in ['Package', 'Tag', 'Group', 'Member', 'PackageExtra']: assert key in tables, '%r not in %s' % (key, tables) for key in ['User']: assert key not in tables, '%s should not be in %s' % (key, tables) assert len(dumpeddata['Package']) == 2, len(dumpeddata['Package']) assert len(dumpeddata['Tag']) == 3, len(dumpeddata['Tag']) assert len(dumpeddata['PackageRevision']) == 2, len(dumpeddata['PackageRevision']) assert len(dumpeddata['Group']) == 2, len(dumpeddata['Group'])
def test_dump(self): assert os.path.exists(self.outpath) dumpeddata = json.load(open(self.outpath)) assert dumpeddata["version"] == ckan.__version__ tables = dumpeddata.keys() for key in ["Package", "Tag", "Group", "Member", "PackageExtra"]: assert key in tables, "%r not in %s" % (key, tables) for key in ["User"]: assert key not in tables, "%s should not be in %s" % (key, tables) assert len(dumpeddata["Package"]) == 2, len(dumpeddata["Package"]) assert len(dumpeddata["Tag"]) == 3, len(dumpeddata["Tag"]) assert len(dumpeddata["PackageRevision"]) == 2, len(dumpeddata["PackageRevision"]) assert len(dumpeddata["Group"]) == 2, len(dumpeddata["Group"])
def test_dump(self): assert os.path.exists(self.outpath) dumpeddata = json.load(open(self.outpath)) assert dumpeddata['version'] == ckan.__version__ tables = dumpeddata.keys() for key in ['Package', 'Tag', 'Group', 'Member', 'PackageExtra']: assert key in tables, '%r not in %s' % (key, tables) for key in ['User']: assert key not in tables, '%s should not be in %s' % (key, tables) assert len(dumpeddata['Package']) == 2, len(dumpeddata['Package']) assert len(dumpeddata['Tag']) == 3, len(dumpeddata['Tag']) assert len(dumpeddata['PackageRevision']) == 2, len( dumpeddata['PackageRevision']) assert len(dumpeddata['Group']) == 2, len(dumpeddata['Group'])
def load_licenses(self, license_url): try: if license_url.startswith('file://'): with open(license_url.replace('file://', ''), 'r') as f: license_data = json.load(f) else: response = requests.get(license_url) license_data = response.json() except requests.RequestException as e: msg = "Couldn't get the licenses file {}: {}".format(license_url, e) raise Exception(msg) except ValueError as e: msg = "Couldn't parse the licenses file {}: {}".format(license_url, e) raise Exception(msg) for license in license_data: if isinstance(license, str): license = license_data[license] self._create_license_list(license_data, license_url)
def update_search_schema_definitions(self, definitions): groups = u"field-type", u"field", u"dynamic-field", "copy-field" here = os.path.dirname(__file__) for group in groups: with open(os.path.join(here, u"schemas", group + u".json")) as fp: definitions[group].extend(json.load(fp))