Skip to content

Commit

Permalink
small filter helpers on ls command
Browse files Browse the repository at this point in the history
  • Loading branch information
nferrier committed Apr 12, 2011
1 parent c8106a6 commit bb19ffd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class MdCLI(cmdln.Cmdln):
client.lsfolders(stream=sys.stdout)

@cmdln.option("-r", "--reverse", help="reverse the listing", action="store_true")
@cmdln.option("-f", "--field", help="specify the field")
@cmdln.option("-g", "--grep", help="pattern match on the output string")
@cmdln.option(
"-s",
"--since",
Expand All @@ -88,10 +90,12 @@ class MdCLI(cmdln.Cmdln):
"""
client = MdClient(self.maildir)
client.ls(
foldername=folder,
stream=sys.stdout,
reverse=getattr(opts, "reverse", False),
since=float(getattr(opts, "since", -1))
foldername = folder,
stream = sys.stdout,
reverse = getattr(opts, "reverse", False),
grep = getattr(opts, "grep", None),
field = getattr(opts, "field", None),
since = float(getattr(opts, "since", -1))
)

@cmdln.option("-r", "--reverse", help="reverse the listing", action="store_true")
Expand Down
15 changes: 13 additions & 2 deletions src/mdlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,33 @@ def sortcmp(d):
itemlist = [(folder, key, msg) for key,msg in sorted_lst]
return itemlist

def ls(self, foldername="INBOX", reverse=False, since=None, stream=sys.stdout):
def ls(self, foldername="INBOX", reverse=False, since=None, grep=None, field=None, stream=sys.stdout):
"""Do standard text list of the folder to the stream.
'since' allows the listing to be date filtered since that
date. It should be a float, a time since epoch.
'grep' allows text matching on the whole record
'field' allows only 1 field to be output
"""
for folder, mk, m in self._list(foldername, reverse, since):
try:
# I am very unsure about this defaulting of foldername
print >>stream, "% -20s % 20s % 50s [%s] %s" % (
output_items = (
"%s%s%s" % (folder.folder or foldername or "INBOX", SEPERATOR, mk),
m.date,
m.get_from()[0:50] if m.get_from() else "",
m.get_flags(),
re.sub("\n", "", m.get_subject() or "")
)

output_string = "% -20s % 20s % 50s [%s] %s" % output_items
if not grep or (grep and grep in output_string):
if field:
print >>stream, output_items[int(field)]
else:
print >>stream, output_string
except IOError,e:
if e.errno == errno.EPIPE:
# Broken pipe we can ignore
Expand Down

0 comments on commit bb19ffd

Please sign in to comment.