Skip to content

Commit

Permalink
adopt python 3 version of cmdln until it's package released somehow. …
Browse files Browse the repository at this point in the history
…add tests around the cli.
  • Loading branch information
nicferrier committed Nov 19, 2011
1 parent ba26192 commit 216bfae
Show file tree
Hide file tree
Showing 7 changed files with 2,046 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .veh.conf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
# or a vc reference:
# packagelabel = hg+http://domain/repo
pyproxyfs = pyproxyfs==0.8
cmdln = cmdln
#cmdln = cmdln

# End
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
28 changes: 21 additions & 7 deletions src/mdlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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" \
Expand All @@ -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 = (
Expand Down Expand Up @@ -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):
Expand Down
20 changes: 11 additions & 9 deletions src/mdlib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit 216bfae

Please sign in to comment.