Skip to content

Commit

Permalink
Merge pull request riolet#17 from riolet/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rrezel authored Apr 1, 2017
2 parents b399493 + 58d9724 commit b338e14
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 169 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ dist/
poline.egg-info/
*.pyc
build/

.coverage
htmlcov/
poline_venv/
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,29 @@ get *i*th element from list *l* if the *i*th element exists, or return value d
```


## sh (c, F=None)
## sh (*args, **kwargs)

Executes shell command specified in string c, and returns stdout in the form of a list of lists.
Executes shell command and arguments specified in *args*, returns stdout in the form of a list of lists.

Recognized kwargs:
F SEPARATOR split each line by SEPARATOR
s True|False split each line

Example:

The following displays the inode of each file using *stat*

Python>=3.6
```
$ ls | pol "f'{l:20.20}\t%s' % [i[3] for i in sh(['stat',l]) if 'Inode:' in i[2]][0] for l in _"
```

Python>=2.7
```
$ ls | pol2 "[columns(20,None).format(l,i[3]) for i in sh('stat',l,s=True) if 'Inode:' in i[2]][0] for l in _"
```

```
$ ls | pol "f'{l:10.10}\t%s' % [i[3] for i in sh(['stat',l]) if 'Inode:' in i[2]][0] for l in _"
LICENSE 360621
Makefile 360653
pol 360606
Expand All @@ -172,12 +185,14 @@ pol.o 360599
README.md 360623
```

**N.B.** The syntax of *sh* has changed, but old syntax is still supported for the sake of backward compatibility.

As well, the popular shell commands *cp*, *df*, *docker*, *du*, *find*, *git*, *history*, *ln*, *ls*, *lsof*, *mv*, *netstat*, *nmcli*, *ps*, *rm* are all functions. For example

They behave as if the command is being passed to *sh* above.

```
$ pol "ls(['-lah'])"
$ pol "ls('-lah')"
total 24K
drwxr-xr-x 7 default root 185 Mar 31 02:21 .
drwxrwxrwt 9 root root 185 Mar 31 02:24 ..
Expand Down
Empty file removed _compatibility/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion cicd/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node {
node ('python27') {
stage("Checkout") {
checkout scm
}
Expand Down
File renamed without changes.
85 changes: 85 additions & 0 deletions poline/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from __future__ import print_function

import re
import os
import sys
import argparse
import collections
import subprocess
import json
from poline.utilfuncs import *
from poline.fields import Fields

from itertools import islice
from operator import itemgetter, attrgetter
if sys.version_info >= (3,0):
from urllib.parse import urlparse
else:
from urlparse import urlparse

from pprint import pprint, pformat

if sys.version_info >= (3,5):
_collections_Generator = collections.Generator
else:
from poline import _com_collections
_collections_Generator = _com_collections.Generator


T = True
F = False

def _len(value):
if isinstance(value, _collections_Generator):
return sum(1 for x in value)
else:
return len(value)


def _stdin(args):
for line in sys.stdin:
if args.separator is not None:
yield Fields(line.strip().split(args.separator))
elif args.split:
yield Fields(line.strip().split())
else:
yield line.strip()

# Hello old friends
_shell_commands= ['cp', 'df', 'docker', 'du', 'find', 'git', 'history',
'ln', 'ls', 'lsof', 'mv', 'netstat', 'nmcli', 'ps', 'rm']
for _shell_command in _shell_commands:
exec ("""{funcname} = lambda *args, **kwargs: sh(['{funcname}']+list(args), **kwargs)""".format(funcname=_shell_command))


def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('expression', help="python expression")
parser.add_argument('-F', '--separator', default=None, help="split each line by SEPARATOR")
parser.add_argument('-s', '--split', const=True, default=False, action='store_const', help="split each line")
parser.add_argument('-q', '--quiet', const=True, default=False, action='store_const',
help="don't implicitly print results")

if argv is not None:
args = parser.parse_args(argv)
else:
args = parser.parse_args()

result = eval('(%s)' % args.expression, globals(), {
'_': _stdin(args),
'len': _len,
})

if not args.quiet:
if isinstance(result, (list, _collections_Generator)):
for line in result:
if isinstance(line, (list, tuple)):
print(*line)
else:
print(line)
else:
print(result)


if __name__ == "__main__":
main()
15 changes: 15 additions & 0 deletions poline/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sys

class Fields(list):

def __getitem__(self, i):
if isinstance(i, int):
if sys.version_info >= (3, 0):
return super().__getitem__(i) if len(self) > i else ''
else:
return super(Fields, self).__getitem__(i) if len(self) > i else ''
else:
if sys.version_info >= (3, 0):
return super().__getitem__(i)
else:
return super(Fields, self).__getitem__(i)
154 changes: 0 additions & 154 deletions poline/poline.py

This file was deleted.

Loading

0 comments on commit b338e14

Please sign in to comment.