コード例 #1
0
ファイル: test_init.py プロジェクト: densho/ddr-cmdln
def test_format_json():
    class Unserializable():
        pass
    data0 = {'a': 1}
    data1 = {'a': datetime(2018,8,30,11,35,54)}
    data2 = {'a': Unserializable()}
    expected0 = '{\n    "a": 1\n}'
    expected1 = '{\n    "a": "2018-08-30T11:35:54"\n}'
    expected3 = '{\n    "a": 1\n}'
    out0 = format_json(data0)
    out1 = format_json(data1)
    assert out0 == expected0
    assert out1 == expected1
    with pytest.raises(TypeError):
        out2 = format_json(data2)
コード例 #2
0
ファイル: entity.py プロジェクト: denshoproject/ddr-cmdln
    def dump_json(self, template=False, doc_metadata=False, obj_metadata={}):
        """Dump Entity data to JSON-formatted text.
        
        @param template: [optional] Boolean. If true, write default field values.
        @param doc_metadata: boolean. Insert object_metadata().
        @param obj_metadata: dict Cached results of object_metadata.
        @returns: JSON-formatted text
        """
        module = self.identifier.fields_module()
        self.children()
        data = common.dump_json(
            self,
            module,
            exceptions=['files', 'filemeta'],
            template=template,
        )
        if obj_metadata:
            data.insert(0, obj_metadata)
        elif doc_metadata:
            data.insert(0, common.object_metadata(module, self.parent_path))

        data.append({
            'children': [
                entity_to_childrenmeta(o) for o in self.children()
                if o.identifier.model in ['entity', 'segment']
            ]
        })
        data.append({
            'file_groups':
            files_to_filegroups(
                [o for o in self.children() if o.identifier.model == 'file'])
        })
        return format_json(data)
コード例 #3
0
ファイル: entity.py プロジェクト: densho/ddr-cmdln
 def dump_json(self, template=False, doc_metadata=False, obj_metadata={}):
     """Dump Entity data to JSON-formatted text.
     
     @param template: [optional] Boolean. If true, write default field values.
     @param doc_metadata: boolean. Insert object_metadata().
     @param obj_metadata: dict Cached results of object_metadata.
     @returns: JSON-formatted text
     """
     module = self.identifier.fields_module()
     self.children(force_read=True)
     data = common.dump_json(self, module,
                      exceptions=['files', 'filemeta'],
                      template=template,)
     if obj_metadata:
         data.insert(0, obj_metadata)
     elif doc_metadata:
         data.insert(0, common.object_metadata(module, self.parent_path))
     
     data.append({
         'children': [
             entity_to_childrenmeta(o)
             for o in self.children()
             if o.identifier.model in ['entity', 'segment']
         ]
     })
     data.append({
         'file_groups': files_to_filegroups([
             o for o in self.children()
             if o.identifier.model == 'file'
         ])
     })
     return format_json(data)
コード例 #4
0
ファイル: test_init.py プロジェクト: denshoproject/ddr-cmdln
def test_format_json():
    class Unserializable():
        pass

    data0 = {'a': 1}
    data1 = {'a': datetime(2018, 8, 30, 11, 35, 54)}
    data2 = {'a': Unserializable()}
    expected0 = '{\n    "a": 1\n}'
    expected1 = '{\n    "a": "2018-08-30T11:35:54"\n}'
    expected3 = '{\n    "a": 1\n}'
    out0 = format_json(data0)
    out1 = format_json(data1)
    assert out0 == expected0
    assert out1 == expected1
    with pytest.raises(TypeError):
        out2 = format_json(data2)
コード例 #5
0
 def dump_json(self, template=False, doc_metadata=False, obj_metadata={}):
     """Dump Collection data to JSON-formatted text.
     
     @param template: [optional] Boolean. If true, write default values for fields.
     @param doc_metadata: boolean. Insert object_metadata().
     @param obj_metadata: dict Cached results of object_metadata.
     @returns: JSON-formatted text
     """
     module = self.identifier.fields_module()
     data = common.dump_json(self, module, template=template)
     if obj_metadata:
         data.insert(0, obj_metadata)
     elif doc_metadata:
         data.insert(0, common.object_metadata(module, self.path))
     return format_json(data)
コード例 #6
0
ファイル: vocab.py プロジェクト: gjost/ddr-cmdln-bkup
 def dump_json( self ):
     """JSON format of the entire index.
     
     Terms list plus a keyword, title, and description.
     This is the same format used for Elasticsearch facets.
     
     @param id
     @param title
     @param description
     """
     data = {
         'id': self.id,
         'title': self.title,
         'description': self.description,
         'terms': [term._flatten_json() for term in self.terms()],
     }
     return format_json(data)
コード例 #7
0
ファイル: vocab.py プロジェクト: denshoproject/ddr-cmdln
 def dump_json(self):
     """JSON format of the entire index.
     
     Terms list plus a keyword, title, and description.
     This is the same format used for Elasticsearch facets.
     
     @param id
     @param title
     @param description
     """
     data = {
         'id': self.id,
         'title': self.title,
         'description': self.description,
         'terms': [term._flatten_json() for term in self.terms()],
     }
     return format_json(data)
コード例 #8
0
ファイル: files.py プロジェクト: densho/ddr-cmdln
 def dump_json(self, doc_metadata=False, obj_metadata={}):
     """Dump File data to JSON-formatted text.
     
     @param doc_metadata: boolean. Insert object_metadata().
     @param obj_metadata: dict Cached results of object_metadata.
     @returns: JSON-formatted text
     """
     module = self.identifier.fields_module()
     if self.basename and not self.mimetype:
         self.mimetype = self.get_mimetype(force=True)
     data = common.dump_json(self, module)
     if obj_metadata:
         data.insert(0, obj_metadata)
     elif doc_metadata:
         data.insert(0, common.object_metadata(module, self.collection_path))
     # what we call path_rel in the .json is actually basename
     data.insert(1, {'path_rel': self.basename})
     return format_json(data)
コード例 #9
0
 def dump_json(self, doc_metadata=False, obj_metadata={}):
     """Dump File data to JSON-formatted text.
     
     @param doc_metadata: boolean. Insert object_metadata().
     @param obj_metadata: dict Cached results of object_metadata.
     @returns: JSON-formatted text
     """
     module = self.identifier.fields_module()
     if self.basename and not self.mimetype:
         self.mimetype = self.get_mimetype(force=True)
     data = common.dump_json(self, module)
     if obj_metadata:
         data.insert(0, obj_metadata)
     elif doc_metadata:
         data.insert(0, common.object_metadata(module,
                                               self.collection_path))
     # what we call path_rel in the .json is actually basename
     data.insert(1, {'path_rel': self.basename})
     return format_json(data)