Skip to content

viciu/python-shelltools

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

python-shelltools (in development)

The bucket of python shell helpers, no dependencies, simple API.

  • Python2.6
  • Python2.7
  • Python3.3
  • PyPy1.9
  • shelltools: (shell)
    • path - path wraper around all methods related to path manipulation
    • run - subprocess wrapper
    • text: - strings helper functions

Documentation

https://shelltools.readthedocs.org/

Code

https://github.com/xando/python-shelltools

shelltools.path

>>> from shelltools import path

>>> path('/var/log')
/var/log

>>> path('/var', 'log')
/var/log

>>> path('/var/log').own
'766'

>>> path('/var/log').is_dir()
True

>>> for e in path('/var/log'):
...     print e
/var/log/boot.log
/var/log/dmesg
/var/log/faillog
/var/log/kern.log
/var/log/gdm

>>> path('/var/log/').ls('*log')
[/var/log/boot.log, /var/log/faillog, /var/log/kern.log]

>>> path('/var/log') / 'syslog'
/var/log/syslog

>>> (path('/var/log') / 'syslog').exists

>>> path('/var/log','syslog').open('r')
<open file '/var/log/syslog', mode 'r' at 0x294c5d0>

>>> path('/var/log').cp('copy', r=True)
copy

>>> path('/home/user/test_tmp_directory').replace('_', '-')
'/home/user/test-tmp-directory'

>>> location = path('/home/user/test_tmp_directory')
>>> location.mv( location.replace('_', '-') )

shelltools.run

>>> from shelltools import run

>>> print run('uname -r').stdout
3.7.0-7-generic

>>> run('uname -a').status
0

>>> print run('rm not_existing_directory').stderr
rm: cannot remove `not_existing_directory': No such file or directory

>>> print run('ls -la', 'wc -l', 'wc -c')
3

>>> print run('ls -la').stdout.lines
['total 20',
 'drwxrwxr-x 3 user user 4096 Dec 20 22:55 .',
 'drwxrwxr-x 5 user user 4096 Dec 20 22:57 ..',
 'drwxrwxr-x 2 user user 4096 Dec 20 22:37 dir',
 '-rw-rw-r-- 1 user user    0 Dec 20 22:52 file']
from shelltools import run

run('grep something', data=run.stdin)
$ ps aux | python script.py

shelltools.text

>>> print wrap("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis sollicitudin ", 30)
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
Duis sollicitudin

>>> print text_list(["black", "red", "blue", "green"])
black, red, blue or green

>>> print text_list(["black", "red", "blue", "green"], "and")
black, red, blue and green
from shelltools.text import progress

for i in progress(range(100)):
    sleep( 0.2)
[                                ] 0/5 - 00:00:00
[######                          ] 1/5 - 00:00:00
[############                    ] 2/5 - 00:00:00
[###################             ] 3/5 - 00:00:00
[#########################       ] 4/5 - 00:00:00
[################################] 5/5 - 00:00:00

tests

image

Travis CI, https://travis-ci.org/xando/python-shelltools

Tests are implemented with py.tests, to run:

python runtests.py

based on/inspired by

author

contributors

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published