aboutsummaryrefslogtreecommitdiff
blob: febe88bf9b59592e01fc56790f82e3f4deea3314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
121
122
123
124
125
126
127
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# R overlay -- package rule parser, action-block context
# -*- coding: utf-8 -*-
# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
# Distributed under the terms of the GNU General Public License;
# either version 2 of the License, or (at your option) any later version.

import re

import roverlay.strutil

import roverlay.packagerules.actions.evar
import roverlay.packagerules.actions.info
import roverlay.packagerules.actions.relocate
import roverlay.packagerules.actions.trace
import roverlay.packagerules.parser.context.base

class RuleActionException ( ValueError ):
   pass
# --- end of RuleActionException ---

class ActionUnknown ( RuleActionException ):
   pass
# --- end of ActionUnknown ---

class ActionNeedsValue ( RuleActionException ):
   pass
# --- end of ActionNeedsValue ---

class ActionInvalid ( RuleActionException ):
   pass
# --- end of ActionInvalid ---


class RuleActionContext (
   roverlay.packagerules.parser.context.base.BaseContext
):
   """RuleActionContext parses action-blocks."""

   # keywords for the "ignore" action
   KEYWORDS_ACTION_IGNORE = frozenset ({
      'ignore',
      'do-not-process'
   })

   KEYWORDS_ACTION_TRACE = frozenset ({
      'trace',
   })


   # dict ( <keyword> => <evar class> )
   # Dict of evar action keywords (with corresponding classes)
   #
   KEYWORDS_EVAR = {
      'keywords' : roverlay.packagerules.actions.evar.KeywordsEvarAction,
   }

   # default info set-to/rename actions
   #  using the lazy variant for renaming
   #
   DEFAULT_MODIFY_INFO_ACTIONS = (
      roverlay.packagerules.actions.info.InfoSetToAction,
      roverlay.packagerules.actions.info.InfoRenameAction,
   )

   # dict { key => None | ( None|False|SetTo_Action, None|False|Rename_Action )
   #   where None  is "use default action(s)"
   #   and   False is "invalid"/"not supported"
   #
   # (see comment in packageinfo.py concerning keys that exist when calling
   #  apply_action() and enable lazy actions if necessary)
   #
   MODIFIABLE_INFO_KEYS = {
      'name'     : None,
      'category' : ( None, False ),
      'destfile' : (
         None,
         roverlay.packagerules.actions.relocate.SrcDestRenameAction
      ),
   }

   # TODO / Notes:
   #
   # * "combined" actions, e.g. name,category as "pseudo" actions?
   #
   # dict { pseudo_key => tuple ( argparse_function, set { real key[s] } ) }
   #
##   DEMUX_INFO_KEYS = {
##      'cp' : ( argstr_parse(), { 'name', 'category', } )
##   }

   def __init__ ( self, namespace ):
      super ( RuleActionContext, self ).__init__ ( namespace )
      self._actions = list()
   # --- end of __init__ (...) ---

   def _add_as_info_action ( self, keyword, argstr, orig_str, lino ):
      """Tries to add <keyword, argstr> as package info-manipulating action.

      Returns true if such an action has been created and added, else False.
      Invalid values/lines will be catched here. A return value of False
      simply means that keyword/argstr do not represent an info action.

      arguments:
      * keyword  --
      * argstr   --
      * orig_str --
      * lino     --

      Raises:
      * ActionUnknown
      * ActionNeedsValue
      """
      # get action_type_str (and, possibly, key)
      action_type_str, sepa, key = keyword.partition ( "_" )
      action_type_str = action_type_str.lower()

      if action_type_str == "set":
         # is a set-to info action, continue
         action_type = 0
      elif action_type_str == "rename":
         # is a rename info action, continue
         action_type = 1
      else:
         # not an info action
         return False
      # -- end if;

      if not sepa:
         # get key from argstr
         argv = roverlay.strutil.split_whitespace ( argstr, maxsplit=1 )
         if argv:
            key = roverlay.strutil.unquote ( argv [0].lower() )
            if not key:
               # better safe than sorry ;)
               #return False
               raise ActionUnknown ( orig_str )
         else:
            #return False
            raise ActionUnknown ( orig_str )


         # dont unquote value here,
         #  this operation might depend on key in future
         value = argv [1]
      else:
         value = argstr
      # -- end if;

      # get action class (raises KeyError)
      try:

         # ( ( cls_tuple or <default> ) [action_type] ) or <default>
         action_cls = (
            self.MODIFIABLE_INFO_KEYS [key]
            or self.DEFAULT_MODIFY_INFO_ACTIONS
         ) [action_type]

         if action_cls is None:
            action_cls = self.DEFAULT_MODIFY_INFO_ACTIONS [action_type]
      except KeyError:
         raise ActionUnknown ( orig_str )

      # create and add action
      if action_cls is False:
         raise ActionInvalid ( orig_str )
      elif action_type == 0:
         # info action (1 arg)
         value = roverlay.strutil.unquote ( value )

         if value:
            self._actions.append ( action_cls ( key, value, lino ) )
         else:
            raise ActionNeedsValue ( orig_str )
      else:
         # rename action (1 arg)
         #  sed-like replace statement "s/a/b/flags?"
         #
         # FIXME/TODO *** flags are not implemented ***
         #
         # len ( value ) >= len (
         #    "s" + sepa + len(a)>=1 + sepa + len(b)>=1 + sepa + len(flags)>=0
         # )
         # => 6 relevant parts (3x sepa, 2x value), len (value) has to be > 5
         #

         value = roverlay.strutil.unquote ( value )

         if len ( value ) > 5 and value[0] == 's' and value[1] == value[-1]:

            # double-escaped backslash
            re_splitter = self.namespace.get_object (
               re.compile, '(?<!\\\)' + value [1]
            )

            # (redef argv)
            argv = re_splitter.split ( value, maxsplit=3 )

            if len ( argv ) > 3 and all ( argv[:4] ):
               raise NotImplementedError ( "flags are not supported yet." )

            elif len ( argv ) > 2 and all ( argv[:3] ):
               self._actions.append (
                  action_cls (
                     key,
                     self.namespace.get_object ( re.compile, argv [1] ),
                     argv [2],
                     lino
                  )
               )
            else:
               raise ActionNeedsValue ( orig_str )
         else:
            raise ActionNeedsValue ( orig_str )
      # -- end if;

      return True
   # --- end of _add_as_info_action (...) ---

   def feed ( self, _str, lino ):
      """Feeds this action block with input.

      arguments:
      * _str --
      * lino --

      Raises:
      * InvalidContext
      """
      if _str in self.KEYWORDS_NOP_STATEMENT:
         pass
      elif _str in self.KEYWORDS_ACTION_IGNORE:
         if not self._actions:
            self._actions = None
         else:
            raise self.InvalidContext (
               "ignore action-block does not accept any other action."
            )
      elif self._actions is None:
         raise self.InvalidContext (
            "ignore action-block does not accept any other action."
         )
      else:
         # split _str into (<keyword>,<value>)
         argv = roverlay.strutil.split_whitespace ( _str, maxsplit=1 )

         if argv [0] in self.KEYWORDS_ACTION_TRACE:
            if len ( argv ) > 1 and argv [1]:
               self._actions.append (
                  roverlay.packagerules.actions.trace.TraceAction (
                     roverlay.strutil.unquote ( argv [1] ),
                     lino
                  )
               )
            else:
               self._actions.append (
                  roverlay.packagerules.actions.trace.MarkAsModifiedAction (
                     lino
                  )
               )

         elif len ( argv ) > 1 and (
            self._add_as_info_action ( argv [0], argv [1], _str, lino )
         ):
            pass

         else:
            evar_cls = self.KEYWORDS_EVAR.get ( argv [0], None )

            try:
               if evar_cls:
                  self._actions.append (
                     evar_cls ( roverlay.strutil.unquote ( argv [1] ), lino )
                  )
               else:
                  raise ActionUnknown ( _str )

            except IndexError:
               raise ActionNeedsValue ( _str )
   # --- end of feed (...) ---

   def create ( self ):
      """Returns all created actions."""
      return self._actions
   # --- end of create (...) ---

# --- end of RuleActionContext ---