Skip to content

Commit

Permalink
🐛 pydantic fields must not start with underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Oct 12, 2023
1 parent d50932a commit d53a3bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions ocrd/ocrd/mets_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ def create(file_grp : str, file_id : str, page_id : Optional[str], url : Optiona

class OcrdAgentModel(BaseModel):
name : str = Field()
_type : str = Field()
type : str = Field()
role : str = Field()
otherrole : Optional[str] = Field()
othertype : str = Field()
notes : Optional[List[Tuple[Dict[str, str], Optional[str]]]] = Field()

@staticmethod
def create(name : str, _type : str, role : str, otherrole : str, othertype : str, notes : List[Tuple[Dict[str, str], Optional[str]]]):
return OcrdAgentModel(name=name, _type=_type, role=role, otherrole=otherrole, othertype=othertype, notes=notes)
return OcrdAgentModel(name=name, type=_type, role=role, otherrole=otherrole, othertype=othertype, notes=notes)


class OcrdFileListModel(BaseModel):
Expand Down Expand Up @@ -79,7 +79,7 @@ class OcrdAgentListModel(BaseModel):
@staticmethod
def create(agents : List[OcrdAgent]):
return OcrdAgentListModel(
agents=[OcrdAgentModel(name=a.name, _type=a.type, role=a.role, otherrole=a.otherrole, othertype=a.othertype, notes=a.notes) for a in agents]
agents=[OcrdAgentModel.create(name=a.name, _type=a.type, role=a.role, otherrole=a.otherrole, othertype=a.othertype, notes=a.notes) for a in agents]
)

#
Expand Down Expand Up @@ -137,7 +137,10 @@ def add_agent(self, *args, **kwargs):

@property
def agents(self):
return [ClientSideOcrdAgent(None, **agent_dict) for agent_dict in self.session.request('GET', f'{self.url}/agent').json()['agents']]
agent_dicts = self.session.request('GET', f'{self.url}/agent').json()['agents']
for agent_dict in agent_dicts:
agent_dict['_type'] = agent_dict.pop('type')
return [ClientSideOcrdAgent(None, **agent_dict) for agent_dict in agent_dicts]

@property
def unique_identifier(self):
Expand Down Expand Up @@ -264,6 +267,7 @@ async def file_groups():
@app.post('/agent', response_model=OcrdAgentModel)
async def add_agent(agent : OcrdAgentModel):
kwargs = agent.dict()
kwargs['_type'] = kwargs.pop('type')
workspace.mets.add_agent(**kwargs)
return agent

Expand Down
2 changes: 1 addition & 1 deletion tests/test_mets_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_mets_server_str(start_mets_server):
f = next(workspace_server.find_files())
assert str(f) == '<ClientSideOcrdFile fileGrp=OCR-D-IMG, ID=FILE_0001_IMAGE, mimetype=image/tiff, url=---, local_filename=OCR-D-IMG/FILE_0001_IMAGE.tif]/>'
a = workspace_server.mets.agents[0]
assert str(a) == '<ClientSideOcrdAgent [type=---, othertype=SOFTWARE, role=CREATOR, otherrole=---, name=DFG-Koordinierungsprojekt zur Weiterentwicklung von Verfahren der Optical Character Recognition (OCR-D)]/>'
assert str(a) == '<ClientSideOcrdAgent [type=OTHER, othertype=SOFTWARE, role=CREATOR, otherrole=---, name=DFG-Koordinierungsprojekt zur Weiterentwicklung von Verfahren der Optical Character Recognition (OCR-D)]/>'
assert str(workspace_server.mets) == '<ClientSideOcrdMets[url=%s]>' % ('http+unix://%2Ftmp%2Focrd-mets-server.sock' if mets_server_url == TRANSPORTS[0] else TRANSPORTS[1])

def test_mets_test_unimplemented(start_mets_server):
Expand Down

0 comments on commit d53a3bf

Please sign in to comment.