diff --git a/sievelib/commands.py b/sievelib/commands.py index 75808cf..a87b119 100644 --- a/sievelib/commands.py +++ b/sievelib/commands.py @@ -27,6 +27,7 @@ import sys from future.utils import python_2_unicode_compatible +from collections import OrderedDict from . import tools @@ -118,6 +119,7 @@ class Command(object): """ _type = None + _name = None variable_args_nb = False non_deterministic_args = False accept_children = False @@ -134,7 +136,11 @@ def __init__(self, parent=None): self.rargs_cnt = 0 self.curarg = None # for arguments that expect an argument :p (ex: :comparator) - self.name = self.__class__.__name__.replace("Command", "") + if self._name == None: + self.name = self.__class__.__name__.replace("Command", "") + else: + self.name = self._name + self.name = self.name.lower() self.hash_comments = [] @@ -142,6 +148,72 @@ def __init__(self, parent=None): def __repr__(self): return "%s (type: %s)" % (self.name, self._type) + def manglearg(self,str): + return str + + def todict(self,attributemap=None): + """Generate the dict representation corresponding to this command + + Recursive method. + + """ + j = OrderedDict() + comms = [] + j.update({"type": self.get_type()}) + j.update({"command": self.name}) + + if self.hash_comments: + for comment in self.hash_comments: + comment_s = comment.decode("utf-8") + comms = comms + [comment_s] + j.update({"comments": comms}) + + if self.arguments: + if self.variable_args_nb: + j.update({"arguments": []}) + else: + j.update({"arguments": OrderedDict()}) + for argname in self.arguments: + outargname=argname + if attributemap: + mappedname=attributemap.get(argname) + if mappedname: + outargname=mappedname + + args = self[argname] + if isinstance(args, list): + for arg in args: + if isinstance(arg, Command): + if self.variable_args_nb: + j["arguments"].append(arg.todict(attributemap=attributemap)) + else: + j["arguments"].update(arg.todict(attributemap=attributemap)) + else: + argvalue = str(arg).replace('"', '') + if isinstance(j["arguments"].get(outargname), list): + j["arguments"][outargname].append(argvalue) + else: + j["arguments"][outargname] = [argvalue] + else: + if isinstance(args, Command): + if self.variable_args_nb: + j["arguments"].append(args.todict(attributemap=attributemap)) + else: + j["arguments"].update(args.todict(attributemap=attributemap)) + else: + argvalue = str(args).replace('"', '') + if self.variable_args_nb: + j["arguments"].append({outargname: argvalue}) + else: + j["arguments"].update({outargname: argvalue}) + + if self.children: + j.update({"children": []}) + for child in self.children: + if isinstance(child, Command): + j["children"].append(child.todict(attributemap=attributemap)) + return j + def tosieve(self, indentlevel=0, target=sys.stdout): """Generate the sieve syntax corresponding to this command diff --git a/sievelib/parser.py b/sievelib/parser.py index 4c46eb2..a96c533 100755 --- a/sievelib/parser.py +++ b/sievelib/parser.py @@ -236,7 +236,7 @@ def __stringlist(self, ttype, tvalue): ; are optional """ if ttype == "string": - self.__curstringlist += [tvalue.decode("utf-8")] + self.__curstringlist += [self.__curcommand.manglearg(tvalue.decode("utf-8"))] self.__set_expected("comma", "right_bracket") return True if ttype == "comma":