Merge pull request #1 from Prodesire/master

更新上有代码
This commit is contained in:
focalism 2017-11-29 20:46:55 +08:00 committed by GitHub
commit 23f4b049bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 102 additions and 43 deletions

View File

@ -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

View File

@ -27,6 +27,7 @@ Content
dict
inspect
misc
network
request
set
string

View File

@ -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):

24
docs/network.rst Normal file
View File

@ -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

70
pydu/file.py Normal file
View File

@ -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))

View File

@ -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