Jump to content
Sign in to follow this  
hsauro

Adding docs strings to wrapped functions

Recommended Posts

I'd like to add doc strings to functions (or methods) that are in a wrapped Delphi object. I can see how to do it when adding the methods manually using AddDelphiMethod but haven't been able to figure out how to do it when using  RegisterDelphiWrapper. I looked at the module RegisterDelphiWrapper creates but I couldn't see the methods in my Delphi object and I could trace where the methods were added,

 

For example, given this class:

 

{$METHODINFO ON}
  ThostAPI =  class (TPersistent)
     function getVersion() : string;
  end;
{$METHODINFO OFF}

 

 

and registering via:

 

 host := THostAPI.Create (controller);
 DelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<THostAPI>).Initialize;
 DelphiWrapper.DefineVar('host', host);

 

I'd like to add a doc string to getVersion()
 

 

Share this post


Link to post
Posted (edited)

WrapDelphi defines the following:

 

  IDocServer = interface
    ['{4AF0D319-47E9-4F0A-9C71-97B8CBB559FF}']
    function ReadTypeDocStr(ATypeInfo: PTypeInfo; out ADocStr: string): Boolean;
    function ReadMemberDocStr(AMember: TRttiMember; out ADocStr: string): Boolean;
    procedure Initialize;
    procedure Finalize;
    function Initialized: Boolean;
  end;

  var
    PyDocServer: IDocServer = nil;

You need to implement the interface and assign the implemented interface to PyDocServer.   

 

PythonDocs.pas provides an implementation based on xml files, which is used by delphivcl and delphifmx.   But if, what you are after is to provide docstrings to a few methods,  it would be easier to create your own implementation.

 

 

Edited by pyscripter

Share this post


Link to post

I saw that interface, I wondered if that would be something relevant. I’ll look into it

 

Share this post


Link to post
Posted (edited)

Forgot to say that unfortunately you cannot just set the __doc__ property of Wrapped classes and methods.  It has to be done at the time of wrapping.

 

A trivial implementation of IDocServer would be:

uses
  TypInfo;

type
  TMyDocServer = class(TInterfacedObject, IDocServer)
  private
    function ReadTypeDocStr(ATypeInfo: PTypeInfo; out ADocStr: string): Boolean;
    function ReadMemberDocStr(AMember: TRttiMember; out ADocStr: string): Boolean;
    procedure Initialize;
    procedure Finalize;
    function Initialized: Boolean;
  end;{ TMyDocServer }

procedure TMyDocServer.Finalize;
begin
end;

procedure TMyDocServer.Initialize;
begin
end;

function TMyDocServer.Initialized: Boolean;
begin
  Result := True;
end;

function TMyDocServer.ReadMemberDocStr(AMember: TRttiMember;
  out ADocStr: string): Boolean;
begin
  Result := False;
  if AMember.Name = 'getVersion' then
  begin
    Result := True;
    ADocStr := 'getVersion';
  end;
end;

function TMyDocServer.ReadTypeDocStr(ATypeInfo: PTypeInfo;
  out ADocStr: string): Boolean;
begin
  Result := False;
  if ATypeInfo = TypeInfo(ThostAPI ) then
  begin
    Result := True;
    ADocStr := 'ThostAPI doc';
  end;
end;

and then

  PyDocServer := TMyDocServer.Create;
  DelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<THostAPI>).Initialize;

 

Edited by pyscripter

Share this post


Link to post

Thanks for the additional information, it would have taken me sometime to figure it out how to do it.  The code works as given.  

 

One way to make it more manageable, which I might try, is to add the docs as attributes to the methods, then use rtti to pick out the attribute before wrapping, add the information to a dictionary then call the wrapper.  The DocSever could then just look up the docs in the dictionary. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×