Skip to content

Commit

Permalink
ENH,BUG,CLN: #10, #12, #13
Browse files Browse the repository at this point in the history
  • Loading branch information
westurner committed Oct 27, 2014
1 parent 984b8a6 commit a75d2f9
Showing 1 changed file with 71 additions and 34 deletions.
105 changes: 71 additions & 34 deletions pyline/pyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import logging
import operator
import textwrap
import pprint

from collections import namedtuple

Expand All @@ -74,6 +75,7 @@

log = logging.getLogger()
log.setLevel(logging.INFO)
log.setLevel(logging.DEBUG)


class NullHandler(logging.Handler):
Expand Down Expand Up @@ -131,15 +133,24 @@ def _numbered_str(self, odelim):


def _import_path_module():
"""
Attempt to import a path module (path.py, pathlib, str)
Returns:
function: function to apply to each line
"""
Path = None
try:
# First, try to import path.py (pip install path.py)
from path import path as Path
except ImportError:
log.debug("path.py not found (pip install path.py)")
try:
# Otherwise, try to import pathlib (pip install pathlib OR Py3.4+)
from pathlib import Path
pass
except ImportError:
log.error("pip install pathlib (or path.py)")
log.error("pathlib not found (pip install pathlib)")
Path = str # os.exists, os
pass
return Path
Expand All @@ -156,12 +167,13 @@ def pyline(iterable,
modules=[],
regex=None,
regex_options=None,
path_tools=False,
path_tools_pathpy=False,
path_tools_pathlib=False,
idelim=None,
odelim="\t",
**kwargs):
"""
Pyline: process an iterable
Process an iterable of lines
Args:
iterable (iterable): iterable of strings (e.g. sys.stdin or a file)
Expand Down Expand Up @@ -201,12 +213,15 @@ def pyline(iterable,
# cmd = "rgx and rgx.groupdict()"
else:
cmd = "line"
if path_tools:
if path_tools_pathpy or path_tools_pathlib:
cmd = "p"

Path = None
if path_tools:
Path = get_path_module()
Path = str
if path_tools_pathpy:
Path = path.path
if path_tools_pathlib:
Path = pathlib.Path


try:
log.info("_cmd: %r" % cmd)
Expand All @@ -231,12 +246,20 @@ def item_keys(obj, keys):
# from itertools import imap, repeat
# j = lambda args: imap(str, izip_longest(args, repeat(odelim)))

i_last = None
if 'i_last' in cmd:
# Consume the whole file into a list (to count lines)
iterable = list(iterable)
i_last = len(iterable)

pp = pprint.pformat

for i, line in enumerate(iterable):
l = line
w = words = [w for w in line.strip().split(idelim)]

p = path = None
if path_tools and line.rstrip():
if path_tools_pathpy or path_tools_pathlib and line.rstrip():
try:
p = path = Path(line.strip()) or None
except Exception as e:
Expand Down Expand Up @@ -288,8 +311,7 @@ def sort_by(sortstr, nl, reverse=False):
columns = get_list_from_str(sortstr)
log.debug("columns: %r" % columns)

get_columns = operator.itemgetter(*columns)

# get_columns = operator.itemgetter(*columns)
get_columns = itemgetter_default(columns, default=None)

return sorted(nl,
Expand Down Expand Up @@ -508,10 +530,15 @@ def get_option_parser():
default=[],
help='Module name to import (default: []) see -p and -r')

prs.add_option('-p', '--path-tools',
dest='path_tools',
prs.add_option('-p', '--pathpy',
dest='path_tools_pathpy',
action='store_true',
help='Create path.py objects (p) from each ``line``')

prs.add_option('--pathlib',
dest='path_tools_pathlib',
action='store_true',
help='Create path objects from each ``line``')
help='Create pathlib objects (p) from each ``line``')

prs.add_option('-r', '--regex',
dest='regex',
Expand Down Expand Up @@ -557,32 +584,42 @@ def get_option_parser():

def get_sort_function(opts): # (sort_asc, sort_desc)
# FIXME
if hasattr(opts, 'sort_asc'):
_sort_asc = opts.sort_asc
_sort_desc = opts.sort_desc
else:
_sort_asc = opts.get('sort_asc')
_sort_desc = opts.get('sort_desc')

sortfunc = None
if opts.sort_asc:
logging.debug("sort_asc: %r" % opts.sort_asc)
if _sort_asc:
logging.debug("sort_asc: %r" % _sort_asc)
if sortfunc is None:
sortfunc = (
lambda _output:
sort_by(opts.sort_asc,
_output,
reverse=False))
def sortfunc(_output):
return sort_by(
_sort_asc,
_output,
reverse=False)
else:
sortfunc = (
lambda _output:
sort_by(opts.sort_asc, sortfunc(_output)))
if opts.sort_desc:
logging.debug("sort_desc: %r" % opts.sort_desc)
def sortfunc(_output):
return sort_by(
_sort_asc,
sortfunc(_output),
reverse=False)
if _sort_desc:
logging.debug("sort_desc: %r" % _sort_desc)
if sortfunc is None:
sortfunc = (
lambda _output:
sort_by(opts.sort_desc,
_output,
reverse=True))
def sortfunc(_output):
return sort_by(
_sort_desc,
_output,
reverse=True)
else:
sortfunc = (
lambda _output:
sort_by(opts.sort_desc,
sortfunc(_output)))
def sortfunc(_output):
return sort_by(
_sort_desc,
sortfunc(_output),
reverse=True)
return sortfunc


Expand Down

0 comments on commit a75d2f9

Please sign in to comment.