Permalink
Browse files

adopt python 3 version of cmdln until it's package released somehow. …

…add tests around the cli.
  • Loading branch information...
1 parent ba26192 commit 216bfae114c61257a51a026ed8a6b72e2ed62059 @nicferrier committed Nov 19, 2011
Showing with 2,046 additions and 123 deletions.
  1. +1 −1 .veh.conf
  2. +5 −1 setup.py
  3. +21 −7 src/mdlib/__init__.py
  4. +11 −9 src/mdlib/api.py
  5. +75 −70 src/{md → mdlib/cli.py}
  6. +1,845 −0 src/mdlib/cmdln.py
  7. +88 −35 src/mdlib/tests.py
View
@@ -14,6 +14,6 @@
# or a vc reference:
# packagelabel = hg+http://domain/repo
pyproxyfs = pyproxyfs==0.8
-cmdln = cmdln
+#cmdln = cmdln
# End
View
@@ -30,6 +30,10 @@
packages = ["mdlib"],
package_dir = {"":"src"},
test_suite = "mdlib",
- scripts=['src/md'],
+ entry_points = {
+ 'console_scripts': [
+ 'md = mdlib.cli:main'
+ ],
+ },
classifiers = classifiers
)
View
@@ -37,8 +37,8 @@
import simplejson as json
import logging
-from md.api import MdFolder
-from md.api import SEPERATOR
+from mdlib.api import MdFolder
+from mdlib.api import SEPERATOR
logger = logging.getLogger("mdlib")
logging.basicConfig()
@@ -53,8 +53,13 @@ def _escape(match_obj):
class MdClient(object):
def __init__(self, maildir, filesystem=None):
self.logger = logging.getLogger("MdClient.%s" % maildir)
- foldername = maildir.split("/")[-1]
- base = splitpath(maildir)[0]
+ # Why would we do this? it requires that maildir's have to end
+ # in a slash. Bad idea? What benefit does it bring??
+ ## foldername = maildir.split("/")[-1]
+ ## base = splitpath(maildir)[0]
+ # Instead of doing that, let's do this:
+ foldername = ""
+ base = maildir
self.folder = MdFolder(
foldername if foldername else "",
base=base,
@@ -76,6 +81,8 @@ def _list(self, foldername="INBOX", reverse=False, since=None):
"""Do structured list output.
Sorts the list by date, possibly reversed, filtered from 'since'.
+
+ The returned list is: foldername, message key, message object
"""
folder = self.folder \
if foldername == "INBOX" \
@@ -87,22 +94,28 @@ def sortcmp(d):
except:
return -1
- lst = list(folder.items()) if not since else folder.items_since(since)
+ lst = folder.items() if not since else folder.items_since(since)
sorted_lst = sorted(lst, key=sortcmp, reverse=1 if reverse else 0)
itemlist = [(folder, key, msg) for key,msg in sorted_lst]
return itemlist
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.
+ 'foldername' is the folder to list.. INBOX by default.
+
'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):
+ if foldername == "":
+ foldername = "INBOX"
+
+ msg_list = self._list(foldername, reverse, since)
+ for folder, mk, m in msg_list:
try:
# I am very unsure about this defaulting of foldername
output_items = (
@@ -201,7 +214,8 @@ def gettext(self, msgid, stream=sys.stdout, splitter="--text follows this line--
val = " ".join([l.strip() for l in val.split("\n")])
print("%s: %s" % (name,val), file=stream)
print(splitter, file=stream)
- print(part.get_payload(decode=True), file=stream)
+ payload = part.get_payload(decode=True)
+ print(payload.decode("ascii"), file=stream)
break
def getrawpart(self, msgid, stream=sys.stdout):
View
@@ -128,7 +128,7 @@ def iteritems(self):
def items(self):
"""Present the email headers"""
- return list(self.items())
+ return list(self.iteritems())
def _flags(self):
m = self.msgpathre.match(self.filename)
@@ -218,7 +218,7 @@ def _get_message(self, key, since=None):
if isinstance(stored, dict):
filename = stored["path"]
folder = stored["folder"]
- if since > 0.0:
+ if since and since > 0.0:
st = stat(filename)
if st.st_mtime < since:
return None
@@ -230,7 +230,7 @@ def _get_message(self, key, since=None):
)
self.store[key] = stored
else:
- if since > 0.0:
+ if since and since > 0.0:
st = stat(stored.filename)
if st.st_mtime < since:
return None
@@ -256,7 +256,7 @@ def iteritems_since(self, since=None):
return
def items(self):
- return list(self.items())
+ return list(self.iteritems())
def items_since(self, since=None):
return list(self.iteritems_since(since=since))
@@ -290,6 +290,7 @@ def get_name(self):
return ".%s" % self.folder if self.is_subfolder else self.folder
def _foldername(self, additionalpath=""):
+ """Dot decorate a folder name."""
if not self._foldername_cache.get(additionalpath):
fn = joinpath(self.base, self.folder, additionalpath) \
if not self.is_subfolder \
@@ -374,7 +375,8 @@ def __repr__(self):
def _muaprocessnew(self):
"""Moves all 'new' files into cur, correctly flagging"""
- files = self.filesystem.listdir(self._foldername("new"))
+ foldername = self._foldername("new")
+ files = self.filesystem.listdir(foldername)
for filename in files:
if filename == "":
continue
@@ -457,18 +459,18 @@ def iterkeys(self):
return self.__iter__()
def iteritems(self):
- for k in self.keys():
+ for k in self.iterkeys():
yield k, self[k]
def keys(self):
- return list(self.keys())
+ return list(self.iterkeys())
def values(self):
- return list([self[k] for k in self.keys()])
+ return list([self[k] for k in self.iterkeys()])
def items(self):
filestore, keystore = self._fileslist()
- return list(keystore.items())
+ return keystore.items()
def items_since(self, since=None):
filestore, keystore = self._fileslist()
Oops, something went wrong. Retry.

0 comments on commit 216bfae

Please sign in to comment.