mirror of https://github.com/flaggo/pydu.git
commit
23f4b049bb
36
docs/cmd.rst
36
docs/cmd.rst
|
@ -9,42 +9,6 @@ Cmd
|
|||
will return object of ``Popen``.
|
||||
``shell`` is same to parameter of ``Popen``.
|
||||
|
||||
>>> from pydu.cmd import run
|
||||
>>> execute('echo hello')
|
||||
(0, b'hello\r\n') # Python 3
|
||||
>>> execute('echo hello', wait=False)
|
||||
<subprocess.Popen at 0x22e4010f9e8>
|
||||
|
||||
|
||||
|
||||
|
||||
>>> from pydu.cmd import run
|
||||
>>> execute('echo hello')
|
||||
(0, b'hello\r\n') # Python 3
|
||||
>>> execute('echo hello', wait=False)
|
||||
<subprocess.Popen at 0x22e4010f9e8>
|
||||
|
||||
|
||||
|
||||
|
||||
>>> from pydu.cmd import execute
|
||||
>>> execute('echo hello')
|
||||
(0, b'hello\r\n') # Python 3
|
||||
>>> run('echo hello', wait=False)
|
||||
<subprocess.Popen at 0x22e4010f9e8>
|
||||
|
||||
|
||||
|
||||
|
||||
>>> from pydu.cmd import execute
|
||||
>>> execute('echo hello')
|
||||
(0, b'hello\r\n') # Python 3
|
||||
>>> run('echo hello', wait=False)
|
||||
<subprocess.Popen at 0x22e4010f9e8>
|
||||
|
||||
|
||||
|
||||
|
||||
>>> from pydu.cmd import execute
|
||||
>>> execute('echo hello')
|
||||
(0, b'hello\r\n') # Python 3
|
||||
|
|
|
@ -27,6 +27,7 @@ Content
|
|||
dict
|
||||
inspect
|
||||
misc
|
||||
network
|
||||
request
|
||||
set
|
||||
string
|
||||
|
|
|
@ -21,7 +21,7 @@ Inspect
|
|||
.. py:function:: pydu.inspect.get_func_args(func)
|
||||
|
||||
Return a list of the argument names. Arguments such as
|
||||
*args and **kwargs are not included.
|
||||
``*args`` and ``**kwargs`` are not included.
|
||||
|
||||
>>> from pydu.inspect import get_func_args
|
||||
>>> def f(name, address='home', age=25, *args, **kwargs):
|
||||
|
@ -35,7 +35,7 @@ Inspect
|
|||
|
||||
Return a list of (argument name, default value) tuples. If the argument
|
||||
does not have a default value, omit it in the tuple. Arguments such as
|
||||
*args and **kwargs are also included.
|
||||
``*args`` and ``**kwargs`` are also included.
|
||||
|
||||
>>> from pydu.inspect import get_func_full_args
|
||||
>>> def f(name, address='home', age=25, *args, **kwargs):
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
Network
|
||||
-------
|
||||
|
||||
.. py:function:: pydu.network.dotted_netmask(mask)
|
||||
|
||||
Converts mask from /`xx` format to `xxx.xxx.xxx.xxx`.
|
||||
``mask`` can be either ``int`` or ``str``.
|
||||
|
||||
>>> from pydu.network import dotted_netmask
|
||||
>>> dotted_netmask('24')
|
||||
'255.255.255.0'
|
||||
>>> dotted_netmask(24)
|
||||
'255.255.255.0'
|
||||
|
||||
|
||||
.. py:function:: pydu.network.is_ipv4_address(ip)
|
||||
|
||||
Judge whether the given ``ip`` is IPV4.
|
||||
|
||||
>>> from pydu.network import is_ipv4_address
|
||||
>>> is_ipv4_address('8.8.8.8')
|
||||
True
|
||||
>>> is_ipv4_address('localhost.localdomain')
|
||||
False
|
|
@ -0,0 +1,70 @@
|
|||
import os
|
||||
import sys
|
||||
import shutil
|
||||
|
||||
|
||||
# todo tests and docs
|
||||
def makedirs(path, mode=0o755, *, ignore_errors=False):
|
||||
try:
|
||||
os.makedirs(path, mode, exist_ok=True)
|
||||
except Exception:
|
||||
if not ignore_errors:
|
||||
raise OSError('Create dir: {} error'.format(path))
|
||||
|
||||
|
||||
def remove(path, *, ignore_errors=False, onerror=None):
|
||||
if ignore_errors:
|
||||
def onerror(*args):
|
||||
pass
|
||||
elif onerror is None:
|
||||
def onerror(*args):
|
||||
raise OSError('Remove path: {} error'.format(path))
|
||||
|
||||
if os.path.isdir(path):
|
||||
shutil.rmtree(path, ignore_errors=ignore_errors, onerror=onerror)
|
||||
else:
|
||||
try:
|
||||
os.remove(path)
|
||||
except Exception:
|
||||
onerror(os.remove, path, sys.exc_info())
|
||||
|
||||
|
||||
def removes(*paths, ignore_errors=False, onerror=None):
|
||||
for path in paths:
|
||||
remove(path, ignore_errors=ignore_errors, onerror=onerror)
|
||||
|
||||
|
||||
def open_file(path, mode='wb+', *, buffer_size=-1, ignore_errors=False):
|
||||
f = None
|
||||
try:
|
||||
if path and not os.path.isdir(path):
|
||||
makedirs(os.path.dirname(path))
|
||||
f = open(path, mode, buffer_size)
|
||||
except Exception:
|
||||
if not ignore_errors:
|
||||
raise OSError('Open file: {} error'.format(path))
|
||||
return f
|
||||
|
||||
|
||||
def link(src, dst, *, overwrite=False, ignore_errors=False):
|
||||
try:
|
||||
if os.path.exists(dst):
|
||||
if overwrite:
|
||||
remove(dst)
|
||||
else:
|
||||
return
|
||||
os.link(src, dst)
|
||||
except Exception:
|
||||
if not ignore_errors:
|
||||
raise OSError('Link {} to {} error'.format(dst, src))
|
||||
|
||||
|
||||
def copy(src, dst, *, ignore_errors=False, follow_symlinks=True):
|
||||
try:
|
||||
if os.path.isdir(src):
|
||||
shutil.copytree(src, dst, symlinks=not follow_symlinks)
|
||||
else:
|
||||
shutil.copy(src, dst, follow_symlinks=follow_symlinks)
|
||||
except Exception:
|
||||
if not ignore_errors:
|
||||
raise OSError('Copy {} to {} error'.format(src, dst))
|
|
@ -2,21 +2,21 @@ import socket
|
|||
import struct
|
||||
|
||||
|
||||
# todo doc
|
||||
# https://github.com/kennethreitz/requests/blob/master/requests/utils.py
|
||||
def dotted_netmask(mask):
|
||||
"""Converts mask from /xx format to xxx.xxx.xxx.xxx
|
||||
"""
|
||||
Converts mask from /xx format to xxx.xxx.xxx.xxx
|
||||
Example: if mask is 24 function returns 255.255.255.0
|
||||
"""
|
||||
mask = int(mask)
|
||||
bits = 0xffffffff ^ (1 << 32 - mask) - 1
|
||||
return socket.inet_ntoa(struct.pack('>I', bits))
|
||||
|
||||
|
||||
# todo doc
|
||||
# https://github.com/kennethreitz/requests/blob/master/requests/utils.py
|
||||
def is_ipv4_address(string_ip):
|
||||
def is_ipv4_address(ip):
|
||||
try:
|
||||
socket.inet_aton(string_ip)
|
||||
socket.inet_aton(ip)
|
||||
except socket.error:
|
||||
return False
|
||||
return True
|
||||
|
|
Loading…
Reference in New Issue