ocarina-python.adb 6.62 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                       O C A R I N A . P Y T H O N                        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--                   Copyright (C) 2013-2015 ESA & ISAE.                    --
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. Ocarina is distributed in the hope that it will be useful, but     --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
26 27 28 29 30 31
--                                                                          --
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
--                                                                          --
------------------------------------------------------------------------------

32 33 34
pragma Warnings (Off);
--  Silence all warnings

yoogx's avatar
yoogx committed
35 36 37
with Ada.Directories;           use Ada.Directories;
with Ada.Environment_Variables; use Ada.Environment_Variables;
with Ada.Text_IO;               use Ada.Text_IO;
38

yoogx's avatar
yoogx committed
39 40
with GNATCOLL.Scripts;          use GNATCOLL.Scripts;
with Ocarina.Python_Cmd;
41

yoogx's avatar
yoogx committed
42
package body Ocarina.Python is
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

   -------------
   -- Console --
   -------------

   type Text_Console is new GNATCOLL.Scripts.Virtual_Console_Record with record
      Instances : GNATCOLL.Scripts.Instance_List;
   end record;

   overriding procedure Insert_Text
     (Console : access Text_Console; Txt : String);
   overriding procedure Insert_Prompt
     (Console : access Text_Console; Txt : String);
   overriding procedure Insert_Error
     (Console : access Text_Console; Txt : String);
   overriding procedure Set_Data_Primitive
     (Instance : GNATCOLL.Scripts.Class_Instance;
      Console : access Text_Console);
   overriding function Get_Instance
     (Script  : access GNATCOLL.Scripts.Scripting_Language_Record'Class;
      Console : access Text_Console) return GNATCOLL.Scripts.Class_Instance;

   ------------------------
   -- Set_Data_Primitive --
   ------------------------

   procedure Set_Data_Primitive
     (Instance : Class_Instance; Console  : access Text_Console) is
   begin
      Set (Console.Instances, Get_Script (Instance), Instance);
   end Set_Data_Primitive;

   ------------------
   -- Get_Instance --
   ------------------

   function Get_Instance
     (Script  : access Scripting_Language_Record'Class;
      Console : access Text_Console) return Class_Instance is
   begin
      return Get (Console.Instances, Script);
   end Get_Instance;

   -----------------
   -- Insert_Text --
   -----------------

   procedure Insert_Text (Console : access Text_Console; Txt : String) is
      pragma Unreferenced (Console);
   begin
      Put (Txt);
   end Insert_Text;

   -------------------
   -- Insert_Prompt --
   -------------------

   procedure Insert_Prompt (Console : access Text_Console; Txt : String) is
      pragma Unreferenced (Console);
   begin
      Put (Txt);
   end Insert_Prompt;

   ------------------
   -- Insert_Error --
   ------------------

   procedure Insert_Error (Console : access Text_Console; Txt : String) is
      pragma Unreferenced (Console);
   begin
      Put (Standard_Error, Txt);
   end Insert_Error;

   ----------------
   -- Run_Python --
   ----------------

   procedure Run_Python is
yoogx's avatar
yoogx committed
121 122
      Repo   : Scripts_Repository :=
        Ocarina.Python_Cmd.Register_Scripts_And_Functions;
123 124 125 126 127
      Buffer : String (1 .. 1000);
      Last   : Integer;
      Errors : Boolean;
      Console : aliased Text_Console;

yoogx's avatar
yoogx committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
      File : Ada.Text_IO.File_Type;
   begin
      --  Detect whether we are calling Ocarina directly, implying an
      --  interactive session, or using it through a specific scripts.

      declare
         Env_Underscore : constant String := Value ("_");
         --  This magic env. variable stores the name of the current
         --  function being used.

      begin
         if Base_Name (Env_Underscore) = "ocarina" then
            --  XXX to be tested on Windows ...

            Put_Line ("Ocarina interactive Python shell");
            Put_Line ("Please type python commands:");
            Set_Default_Console
              (Lookup_Scripting_Language (Repo, "python"),
               Console'Unchecked_Access);
         else
            Ada.Text_IO.Open (File, Ada.Text_IO.In_File, Env_Underscore);
            Set_Input (File);
         end if;
      end;

      --  Iterate over all lines for the input buffer
154 155

      loop
yoogx's avatar
yoogx committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
         Get_Line (Current_Input, Buffer, Last);

         --  Remove comments

         for J in Buffer'First .. Last loop
            if Buffer (J) = '#' then
               Last := J;
               exit;
            end if;
         end loop;

         if Last > Buffer'First then
            Execute_Command
              (Script       => Lookup_Scripting_Language (Repo, "python"),
               Command      => Buffer (Buffer'First .. Last),
               Show_Command => False,
               Hide_Output  => False,
               Errors       => Errors);
         end if;
175
      end loop;
yoogx's avatar
yoogx committed
176

177 178 179
   exception
      when End_Error =>
         Destroy (Repo);
yoogx's avatar
yoogx committed
180 181 182
         if Is_Open (File) then
            Close (File);
         end if;
183 184 185
   end Run_Python;

end Ocarina.Python;