1 | # -*- coding: utf-8 -*- |
---|
2 | |
---|
3 | # This file is part of Seismic Handler eXtended (SHX). |
---|
4 | # |
---|
5 | # SHX is free software: you can redistribute it and/or modify |
---|
6 | # it under the terms of the GNU Lesser General Public License as published |
---|
7 | # by the Free Software Foundation, either version 3 of the License, or |
---|
8 | # (at your option) any later version. |
---|
9 | # |
---|
10 | # SHX is distributed in the hope that it will be useful, |
---|
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | # GNU Lesser General Public License for more details. |
---|
14 | # |
---|
15 | # You should have received a copy of the GNU Lesser General Public License |
---|
16 | # along with SHX. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | from SeismicHandler.core.command import BaseCommand |
---|
19 | from SeismicHandler.core import Settings |
---|
20 | |
---|
21 | provides = {"sdef": "sdef"} |
---|
22 | class sdef(BaseCommand): |
---|
23 | """ |
---|
24 | Defines local and global symbols (variables). If a seconds parameter is |
---|
25 | entered the new symbol is initialized with this value. |
---|
26 | |
---|
27 | This command deals with the local and/or global symbol set. This is defined |
---|
28 | a parser level, for doctest is has to done manually. |
---|
29 | >>> from SeismicHandler.core.parser import symbol |
---|
30 | >>> s = symbol() |
---|
31 | |
---|
32 | Define a local symbol: |
---|
33 | >>> _ = sdef("foo", 1, shx_symbols=s) |
---|
34 | >>> s.foo |
---|
35 | 1 |
---|
36 | |
---|
37 | Don't overwrite a already present symbol: |
---|
38 | >>> _ = sdef("foo", 2, shx_qualifiers={"CHECK": True}, shx_symbols=s) |
---|
39 | >>> s.foo |
---|
40 | 1 |
---|
41 | |
---|
42 | A global symbol needs a qualifier: |
---|
43 | >>> _ = sdef("bar", 3, shx_qualifiers={"GLOBAL": True}, shx_symbols=s) |
---|
44 | >>> s.bar |
---|
45 | 3 |
---|
46 | |
---|
47 | Defining an identically local symbol masks the global one: |
---|
48 | >>> _ = sdef("bar", 4, shx_symbols=s) |
---|
49 | >>> s.bar |
---|
50 | 4 |
---|
51 | |
---|
52 | If we initialize another symbol set, the globally defined symbols should |
---|
53 | be still there: |
---|
54 | >>> s1 = symbol() |
---|
55 | >>> s1.bar |
---|
56 | 3 |
---|
57 | |
---|
58 | URI:http://www.seismic-handler.org/portal/wiki/ShDef |
---|
59 | """ |
---|
60 | numberOfParameters = [1, 2] |
---|
61 | known_qualifiers = [ |
---|
62 | "CHECK", |
---|
63 | "REPLACE", |
---|
64 | "GLOBAL", |
---|
65 | ] |
---|
66 | expectFilename = False |
---|
67 | |
---|
68 | def __init__(self, *args, **kwargs): |
---|
69 | # unroll args & kwargs |
---|
70 | BaseCommand.__init__(self, *args, **kwargs) |
---|
71 | |
---|
72 | def run(self): |
---|
73 | # Don't change symbol if already present |
---|
74 | if self.qualifiers["CHECK"]: |
---|
75 | try: |
---|
76 | _ = getattr(self.symbols, self.parameters[0]) |
---|
77 | except KeyError: |
---|
78 | pass |
---|
79 | else: |
---|
80 | return |
---|
81 | |
---|
82 | if self.qualifiers["GLOBAL"]: |
---|
83 | self.symbols.setGlobal(*self.parameters) |
---|
84 | else: |
---|
85 | try: |
---|
86 | value = self.parameters[1] |
---|
87 | except: |
---|
88 | value = None |
---|
89 | |
---|
90 | setattr(self.symbols, self.parameters[0], value) |
---|
91 | |
---|
92 | if __name__ == "__main__": |
---|
93 | import doctest |
---|
94 | doctest.testmod(exclude_empty=True) |
---|