CLIController.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 14 Feb 2023 19:44:25 +0100
branchwxPython4
changeset 3723 58c74d098f09
parent 3619 f75b11506fb2
child 3750 f62625418bff
permissions -rw-r--r--
More explicit default value for generic makefile target command. Assume Makfile is located in project_files
3501
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
import os
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
import sys
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
from functools import wraps
3619
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
     7
from threading import Timer
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
     8
from datetime import datetime
3501
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
import click
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
import fake_wx
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
from ProjectController import ProjectController
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    15
from LocalRuntimeMixin import LocalRuntimeMixin
3619
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    16
from runtime.loglevels import LogLevelsCount, LogLevels
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    17
3501
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    18
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
class Log:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
    def __init__(self):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
        self.crlfpending = False
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    24
    def write(self, s):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    25
        if s:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    26
            if self.crlfpending:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    27
                sys.stdout.write("\n")
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    28
            sys.stdout.write(s)
3548
da7b461f54b7 CLI: Explicitely wait for user interrupt (-k) and flush stdout each line.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3539
diff changeset
    29
            sys.stdout.flush()
3501
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    30
            self.crlfpending = 0
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    32
    def write_error(self, s):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    33
        if s:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    34
            self.write("Error: "+s)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    35
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
    def write_warning(self, s):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
        if s:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
            self.write("Warning: "+s)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    40
    def flush(self):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    41
        sys.stdout.flush()
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    42
        
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    43
    def isatty(self):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    44
        return False
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    45
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    46
    def progress(self, s):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    47
        if s:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    48
            sys.stdout.write(s+"\r")
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    49
            self.crlfpending = True
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    50
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    51
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    52
def with_project_loaded(func):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    53
    @wraps(func)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    54
    def func_wrapper(self, *args, **kwargs):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    55
        if not self.HasOpenedProject():
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    56
            if self.check_and_load_project():
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    57
                return 1 
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    58
            self.apply_config()
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    59
        return func(self, *args, **kwargs)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    60
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    61
    return func_wrapper
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    62
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    63
def connected(func):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    64
    @wraps(func)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    65
    def func_wrapper(self, *args, **kwargs):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    66
        if self._connector is None:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    67
            if self.session.uri:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    68
                self.BeremizRoot.setURI_location(self.session.uri)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    69
            if not self._Connect():
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    70
                return 1
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    71
        return func(self, *args, **kwargs)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    72
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    73
    return func_wrapper
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    74
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    75
class CLIController(LocalRuntimeMixin, ProjectController):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    76
    def __init__(self, session):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    77
        self.session = session
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    78
        log = Log()
3539
c2eec6aae07e CLI: prevent LOCAL:// runtime to use GUI (tray icon) when launch from CLI.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3501
diff changeset
    79
        LocalRuntimeMixin.__init__(self, log, use_gui=False)
3501
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    80
        ProjectController.__init__(self, None, log)
3619
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    81
        self.CLIStatusTimer = None
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    82
        self.KillCLIStatusTimer = False
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    83
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    84
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    85
    def StartCLIStatusTimer(self):
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    86
        if self.CLIStatusTimer is not None:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    87
            return
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    88
        self.CLIStatusTimer = Timer(0.5, self.CLIStatusTimerProc)
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    89
        self.KillCLIStatusTimer = False
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    90
        self.CLIStatusTimer.start()
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    91
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    92
    def StopCLIStatusTimer(self):
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    93
        if self.CLIStatusTimer is None:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    94
            return
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    95
        self.KillCLIStatusTimer = True
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    96
        self.CLIStatusTimer.cancel()
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    97
        self.CLIStatusTimer = None
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    98
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
    99
    def CLIStatusTimerProc(self):
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   100
        self.CLIStatusTimer = None
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   101
        if not self.KillCLIStatusTimer:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   102
            self.PullPLCStatusProc(None)
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   103
            self.StartCLIStatusTimer()
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   104
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   105
    def _SetConnector(self, connector, update_status=True):
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   106
        self._connector = connector
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   107
        self.previous_log_count = [None]*LogLevelsCount
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   108
        if connector is not None:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   109
            self.StartCLIStatusTimer()
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   110
        else:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   111
            self.StopCLIStatusTimer()
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   112
            if update_status:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   113
                self.UpdateMethodsFromPLCStatus()
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   114
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   115
    def UpdatePLCLog(self, log_count):
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   116
        connector = self._connector
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   117
        new_messages = []
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   118
        if connector:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   119
            for level, count, prev in zip(
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   120
                xrange(LogLevelsCount), log_count, self.previous_log_count):
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   121
                if count is not None and prev != count:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   122
                    if prev is None:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   123
                        dump_end = max(-1, count - 10)
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   124
                    else:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   125
                        dump_end = prev - 1
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   126
                    for msgidx in range(count-1, dump_end, -1):
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   127
                        message = connector.GetLogMessage(level, msgidx)
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   128
                        if message is not None:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   129
                            msg, _tick, tv_sec, tv_nsec = message
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   130
                            date = datetime.utcfromtimestamp(tv_sec + tv_nsec * 1e-9)
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   131
                            txt = "%s at %s: %s\n" % (LogLevels[level], date.isoformat(' '), msg)
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   132
                            new_messages.append((date,txt))
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   133
                        else:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   134
                            break
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   135
                self.previous_log_count[level] = count
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   136
            new_messages.sort()
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   137
            for date, txt in new_messages:
f75b11506fb2 CLI: poll runtime log and output new entries to stdout.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3548
diff changeset
   138
                self.logger.write(txt)
3501
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   139
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   140
    def check_and_load_project(self):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   141
        if not os.path.isdir(self.session.project_home):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   142
            self.logger.write_error(
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   143
                _("\"%s\" is not a valid Beremiz project\n") % self.session.project_home)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   144
            return True
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   145
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   146
        errmsg, error = self.LoadProject(self.session.project_home, self.session.buildpath)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   147
        if error:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   148
            self.logger.write_error(errmsg)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   149
            return True
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   150
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   151
    def apply_config(self):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   152
        for k,v in self.session.config:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   153
            self.SetParamsAttribute("BeremizRoot."+k, v)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   154
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   155
    @with_project_loaded
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   156
    def build_project(self, target):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   157
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   158
        if target:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   159
            self.SetParamsAttribute("BeremizRoot.TargetType", target)
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   160
            
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   161
        return 0 if self._Build() else 1
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   162
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   163
    @with_project_loaded
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   164
    @connected
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   165
    def transfer_project(self):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   166
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   167
        return 0 if self._Transfer() else 1
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   168
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   169
    @with_project_loaded
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   170
    @connected
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   171
    def run_project(self):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   172
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   173
        return 0 if self._Run() else 1
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   174
        
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   175
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   176
    def finish(self):
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   177
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   178
        self._Disconnect()
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   179
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   180
        if not self.session.keep:
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   181
            self.KillLocalRuntime()
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   182
fa291393aac7 Added preliminary CLI. Supports building a project, transfering and running on PLC.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   183