Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion sievelib/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import sys

from future.utils import python_2_unicode_compatible
from collections import OrderedDict

from . import tools

Expand Down Expand Up @@ -118,6 +119,7 @@ class Command(object):

"""
_type = None
_name = None
variable_args_nb = False
non_deterministic_args = False
accept_children = False
Expand All @@ -134,14 +136,84 @@ 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 = []

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

Expand Down
2 changes: 1 addition & 1 deletion sievelib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down