Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

small filter helpers on ls command

  • Loading branch information...
commit bb19ffd6edf57bb2e5dfa73c0894f73bc00d499b 1 parent c8106a6
nferrier authored
Showing with 21 additions and 6 deletions.
  1. +8 −4 src/md
  2. +13 −2 src/mdlib/__init__.py
12 src/md
View
@@ -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",
@@ -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")
15 src/mdlib/__init__.py
View
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.