Skip to content
Open
Changes from 1 commit
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
54 changes: 53 additions & 1 deletion sievelib/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import sys

from future.utils import python_2_unicode_compatible

from collections import OrderedDict

class CommandError(Exception):
"""Base command exception class."""
Expand Down Expand Up @@ -138,6 +138,58 @@ def __init__(self, parent=None):
def __repr__(self):
return "%s (type: %s)" % (self.name, self._type)

def iterify(self,iterable):
if not isinstance(iterable, list):
iterable = [iterable]
return iterable


def todict(self):
"""Generate the dict representation corresponding to this command

Recursive method.

"""

j=OrderedDict()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you respect PEP8 please?

comms=[]

if isinstance(self,TestCommand):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

j.update({"command": "test"})
elif isinstance(self,ControlCommand):
j.update({"command": "control"})
elif isinstance(self,ActionCommand):
j.update({"command": "action"})
else:
j.update({"command": "unsupported"})

j.update({"type": 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:
j.update({"arguments": OrderedDict()})
for argname in self.arguments:
args=self.iterify(self[argname])
for arg in args:
if isinstance(arg,Command):
j["arguments"].update(arg.todict())
else:
j["arguments"].update({argname: str(arg).replace('"', '')})

if self.children:
j.update({"children": []})
for child in self.children:
#target.write(("*" * (indentlevel+10))+'{"'+argname+'": "'+str(arg)+'"},\n')

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this comment?

if isinstance(child,Command):
j["children"].append(child.todict())
return j


def tosieve(self, indentlevel=0, target=sys.stdout):
"""Generate the sieve syntax corresponding to this command

Expand Down