How to draw a 2D path and project it on object's surface from python script? (Like grease pencil draw freehand)

I have asked a question here: Projecting grease pencil on object surface from python script - Blender Stack Exchange ; I am going to ask it here again and I hope some one can help. (I am using blender 2.79, and I am new to the whole blender stuff! :smiley: )

I need to generate some random path on a surface using grease pencil from my python script. I actually could generate some strokes using grease pencil, I will show you the code, but could not understand how to set the grease pencil source to ‘OBJECT’ and stroke placement on ‘SURFACE’; Using the GUI, I can select grease pencil, check the freehand draw mode, set the source to ‘OBJECT’ and stroke placement to ‘SURFACE’, and then start drawing above my already added mesh, a landscape here, and then the drawing automatically projects on the surface.

I want to do this from my python script so I can feed it with random points to make the stroke. I am talking about setting these:
bpy.context.scene.tool_settings.grease_pencil_source = ‘OBJECT’ bpy.context.scene.tool_settings.gpencil_stroke_placement_view3d= ‘SURFACE’
for such a code:
https://paste.ubuntu.com/p/YRmmWwYcBb/
which draws a stroke, but not projected on any object’s surface.
The following code also works:
https://paste.ubuntu.com/p/2jnpRCSVc7/
but it is really messy as it changes the context so your text editor will become a 3D View when you run it, but it uses ops so I can be more hopeful to do something with bpy.ops.gpencil.draw for example, while it is not the best as I want to run this in a for loop for generating too many random paths.

Any help would be appreciated.