Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adopt python 3 version of cmdln until it's package released somehow. …
…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
Expand Up @@ -14,6 +14,6 @@
# or a vc reference: # or a vc reference:
# packagelabel = hg+http://domain/repo # packagelabel = hg+http://domain/repo
pyproxyfs = pyproxyfs==0.8 pyproxyfs = pyproxyfs==0.8
cmdln = cmdln #cmdln = cmdln


# End # End
6 changes: 5 additions & 1 deletion setup.py
Expand Up @@ -30,6 +30,10 @@
packages = ["mdlib"], packages = ["mdlib"],
package_dir = {"":"src"}, package_dir = {"":"src"},
test_suite = "mdlib", test_suite = "mdlib",
scripts=['src/md'], entry_points = {
'console_scripts': [
'md = mdlib.cli:main'
],
},
classifiers = classifiers classifiers = classifiers
) )
28 changes: 21 additions & 7 deletions src/mdlib/__init__.py
Expand Up @@ -37,8 +37,8 @@
import simplejson as json import simplejson as json


import logging import logging
from md.api import MdFolder from mdlib.api import MdFolder
from md.api import SEPERATOR from mdlib.api import SEPERATOR


logger = logging.getLogger("mdlib") logger = logging.getLogger("mdlib")
logging.basicConfig() logging.basicConfig()
Expand All @@ -53,8 +53,13 @@ def _escape(match_obj):
class MdClient(object): class MdClient(object):
def __init__(self, maildir, filesystem=None): def __init__(self, maildir, filesystem=None):
self.logger = logging.getLogger("MdClient.%s" % maildir) self.logger = logging.getLogger("MdClient.%s" % maildir)
foldername = maildir.split("/")[-1] # Why would we do this? it requires that maildir's have to end
base = splitpath(maildir)[0] # 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( self.folder = MdFolder(
foldername if foldername else "", foldername if foldername else "",
base=base, base=base,
Expand All @@ -76,6 +81,8 @@ def _list(self, foldername="INBOX", reverse=False, since=None):
"""Do structured list output. """Do structured list output.
Sorts the list by date, possibly reversed, filtered from 'since'. Sorts the list by date, possibly reversed, filtered from 'since'.
The returned list is: foldername, message key, message object
""" """
folder = self.folder \ folder = self.folder \
if foldername == "INBOX" \ if foldername == "INBOX" \
Expand All @@ -87,22 +94,28 @@ def sortcmp(d):
except: except:
return -1 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) sorted_lst = sorted(lst, key=sortcmp, reverse=1 if reverse else 0)
itemlist = [(folder, key, msg) for key,msg in sorted_lst] itemlist = [(folder, key, msg) for key,msg in sorted_lst]
return itemlist return itemlist


def ls(self, foldername="INBOX", reverse=False, since=None, grep=None, field=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. """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 'since' allows the listing to be date filtered since that
date. It should be a float, a time since epoch. date. It should be a float, a time since epoch.
'grep' allows text matching on the whole record 'grep' allows text matching on the whole record
'field' allows only 1 field to be output '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: try:
# I am very unsure about this defaulting of foldername # I am very unsure about this defaulting of foldername
output_items = ( 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")]) val = " ".join([l.strip() for l in val.split("\n")])
print("%s: %s" % (name,val), file=stream) print("%s: %s" % (name,val), file=stream)
print(splitter, 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 break


def getrawpart(self, msgid, stream=sys.stdout): def getrawpart(self, msgid, stream=sys.stdout):
Expand Down
20 changes: 11 additions & 9 deletions src/mdlib/api.py
Expand Up @@ -128,7 +128,7 @@ def iteritems(self):


def items(self): def items(self):
"""Present the email headers""" """Present the email headers"""
return list(self.items()) return list(self.iteritems())


def _flags(self): def _flags(self):
m = self.msgpathre.match(self.filename) 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): if isinstance(stored, dict):
filename = stored["path"] filename = stored["path"]
folder = stored["folder"] folder = stored["folder"]
if since > 0.0: if since and since > 0.0:
st = stat(filename) st = stat(filename)
if st.st_mtime < since: if st.st_mtime < since:
return None return None
Expand All @@ -230,7 +230,7 @@ def _get_message(self, key, since=None):
) )
self.store[key] = stored self.store[key] = stored
else: else:
if since > 0.0: if since and since > 0.0:
st = stat(stored.filename) st = stat(stored.filename)
if st.st_mtime < since: if st.st_mtime < since:
return None return None
Expand All @@ -256,7 +256,7 @@ def iteritems_since(self, since=None):
return return


def items(self): def items(self):
return list(self.items()) return list(self.iteritems())


def items_since(self, since=None): def items_since(self, since=None):
return list(self.iteritems_since(since=since)) 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 return ".%s" % self.folder if self.is_subfolder else self.folder


def _foldername(self, additionalpath=""): def _foldername(self, additionalpath=""):
"""Dot decorate a folder name."""
if not self._foldername_cache.get(additionalpath): if not self._foldername_cache.get(additionalpath):
fn = joinpath(self.base, self.folder, additionalpath) \ fn = joinpath(self.base, self.folder, additionalpath) \
if not self.is_subfolder \ if not self.is_subfolder \
Expand Down Expand Up @@ -374,7 +375,8 @@ def __repr__(self):


def _muaprocessnew(self): def _muaprocessnew(self):
"""Moves all 'new' files into cur, correctly flagging""" """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: for filename in files:
if filename == "": if filename == "":
continue continue
Expand Down Expand Up @@ -457,18 +459,18 @@ def iterkeys(self):
return self.__iter__() return self.__iter__()


def iteritems(self): def iteritems(self):
for k in self.keys(): for k in self.iterkeys():
yield k, self[k] yield k, self[k]


def keys(self): def keys(self):
return list(self.keys()) return list(self.iterkeys())


def values(self): def values(self):
return list([self[k] for k in self.keys()]) return list([self[k] for k in self.iterkeys()])


def items(self): def items(self):
filestore, keystore = self._fileslist() filestore, keystore = self._fileslist()
return list(keystore.items()) return keystore.items()


def items_since(self, since=None): def items_since(self, since=None):
filestore, keystore = self._fileslist() filestore, keystore = self._fileslist()
Expand Down

0 comments on commit 216bfae

Please sign in to comment.