EmbASP-Python
desktop_service.py
1 from base.service import Service
2 from abc import abstractmethod
3 from base.option_descriptor import OptionDescriptor
4 from base.input_program import InputProgram
5 from base.output import Output
6 import subprocess
7 import time
8 from threading import Thread
9 
10 
12  """Specialization for a desktop platform."""
13 
14  def __init__(self, exe_path):
15  self._exe_path = exe_path # Stores solver's executable path
16  # Stores option string in order to enable solver to read from standard input
17  self._load_from_stdin_option = None
18 
19  def get_exe_path(self):
20  """Return execution path of DesktopService."""
21  return self._exe_path
22 
23  @abstractmethod
24  def _get_output(self, output, error):
25  pass
26 
27  def set_exe_path(self, exe_path):
28  """Set _exe_path to a new path.
29 
30  The parameter exe_path is a string representing the path for the
31  new solver.
32  """
33  self._exe_path = exe_path
34 
35  def start_async(self, callback, programs, options):
36  """Start a new process for the _exe_path and starts solving
37  asyncronously."""
38  class MyThread(Thread):
39  def __init__(self, start_sync):
40  Thread.__init__(self)
41  self.start_sync = start_sync
42 
43  def run(self):
44  callback.callback(self.start_sync(programs, options))
45 
46  th = MyThread(self.start_sync)
47  th.start()
48 
49  def start_sync(self, programs, options):
50  """Start a new process for the _exe_path and starts solving
51  syncronously."""
52  option = ""
53  for o in options:
54  if o is not None:
55  option += o.get_options()
56  option += o.get_separator()
57  else:
58  print("Warning : wrong " +
59  str(OptionDescriptor().__class__.__name__))
60 
61  files_paths = list()
62  final_program = ""
63  for p in programs:
64  if p is not None:
65  final_program += p.get_programs()
66  for path in p.get_files_paths():
67  if len(path) != 0:
68  files_paths.append(path)
69  else:
70  print("Warning : wrong " +
71  str(InputProgram().__class__.__name__))
72 
73  if self._exe_path is None:
74  return Output("", "Error: executable not found")
75 
76  exep = str(self._exe_path)
77  opt = str(option)
78 
79  lis = list()
80  lis.append(exep)
81  if opt != "":
82  lis.append(opt)
83  for path in files_paths:
84  lis.append(path)
85  if self._load_from_stdin_option != "" and final_program != "":
86  lis.append(self._load_from_stdin_option)
87 
88  print(exep + " ", end='')
89  if opt != "":
90  print(opt + " ", end='')
91  for path in files_paths:
92  print(path + " ", end='')
93  if final_program != "":
94  print(self._load_from_stdin_option)
95  else:
96  print()
97 
98  start = int(time.time() * 1e+9)
99 
100  proc = subprocess.Popen(
101  lis,
102  universal_newlines=True,
103  stdout=subprocess.PIPE,
104  stderr=subprocess.PIPE,
105  stdin=subprocess.PIPE,
106  )
107 
108  output, error = proc.communicate(final_program)
109 
110  end = int(time.time() * 1e+9)
111 
112  print("Total time : " + str(end - start))
113  print("")
114 
115  return self._get_output(output, error)
platforms.desktop.desktop_service.DesktopService._exe_path
_exe_path
Definition: desktop_service.py:15
base.output.Output
Definition: output.py:1
platforms.desktop.desktop_service.DesktopService.start_async
def start_async(self, callback, programs, options)
Definition: desktop_service.py:35
base.option_descriptor.OptionDescriptor
Definition: option_descriptor.py:1
base.input_program.InputProgram
Definition: input_program.py:1
platforms.desktop.desktop_service.DesktopService.get_exe_path
def get_exe_path(self)
Definition: desktop_service.py:19
base.output
Definition: output.py:1
base.option_descriptor
Definition: option_descriptor.py:1
platforms.desktop.desktop_service.DesktopService
Definition: desktop_service.py:11
base.service
Definition: service.py:1
platforms.desktop.desktop_service.DesktopService.set_exe_path
def set_exe_path(self, exe_path)
Definition: desktop_service.py:27
platforms.desktop.desktop_service.DesktopService._load_from_stdin_option
_load_from_stdin_option
Definition: desktop_service.py:17
base.service.Service
Definition: service.py:4
base.input_program
Definition: input_program.py:1
platforms.desktop.desktop_service.DesktopService.start_sync
start_sync
Definition: desktop_service.py:41
platforms.desktop.desktop_service.DesktopService._get_output
def _get_output(self, output, error)
Definition: desktop_service.py:24