source: SHX/trunk/src/SeismicHandler/core/modules/Command.py @ 152

Revision 152, 3.1 KB checked in by marcus, 14 years ago (diff)
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Rev Id Date
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2008-2010 Marcus Walther (walther@szgrf.bgr.de)
4#
5# This file is part of Seismic Handler eXtended (SHX)
6# Full details can be found at project website http://www.seismic-handler.org/
7#
8# SHX is free software; you can redistribute it and/or modify
9# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10# the Free Software Foundation; either version 3 of the License, or
11# (at your option) any later version.
12#
13# SHX is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU Lesser General Public License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public License
19# along with SHX (see license.txt); if not, write to the Free Software
20# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
21
22"""Command base class
23
24    - derived classes (e.g. commands) must have "Run" method
25    - optional is "Build" method which constructs a command line suitable for
26      old command line interface
27    - if a cmdstr attribute is found, the cmdstr will automatically promoted
28      to shlib interface
29"""
30
31from SeismicHandler.core import history
32from SeismicHandler.core.log import logging
33from SeismicHandler.core.shlib import shlib
34from  SeismicHandler.core.modules.Events import MessageService, Message
35
36class Command(object):
37    logger = None
38    _stop = False
39
40    def __init__(self, args, kwargs):
41        """run command
42           - add history entry
43           - set initial returnCode"""
44
45        history(self.__class__.__name__, args, kwargs)
46
47        if not self.logger:
48            self.logger = logging.getLogger("command.run")
49
50        self.logger.debug(self.__class__.__name__)
51
52        # switch to another shlib instance if keyword given
53        try:
54            self.id = kwargs["id"]
55        except KeyError:
56            pass
57
58        if hasattr(self, "id"):
59            shlib().switch(self.id)
60
61        self.returnCode = None
62
63        if self._stop:
64            self._stop = False
65            return
66
67        # we assume that a command is either run in python context
68        # or in "old" c lib context - no parallel execution
69        # of course it's still possible to run the c method from python
70
71        # running in python takes over precedence
72        if hasattr(self, "run"):
73            self.run()
74        else:
75            self.promote()
76
77        MessageService.trigger(Message(MessageService.COMMANDRUN))
78
79    def Cancel(self):
80        """stop command execution"""
81        self.logger.warning("command execution stopped")
82        self._stop = True
83
84    def promote(self):
85        """run command on seismic handler c routine
86
87           - command line also processed by c lib"""
88
89        # run method build (if any) in order to construct command string suitable for c lib processing
90        if hasattr(self, "build"):
91            self.build()
92
93        slib = shlib()
94        slib.call(self.cmdstr)
95
96    def __repr__(self):
97        if not self.returnCode:
98            return ""
99        else:
100            return str(self.returnCode)
Note: See TracBrowser for help on using the repository browser.