From 96375a1c1fd3b2e9b17ed4002328090ba61240d4 Mon Sep 17 00:00:00 2001 From: Prodesire Date: Tue, 28 Nov 2017 22:55:51 +0800 Subject: [PATCH 1/5] add file includes several methods include remove, copy and etc. --- pydu/file.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 pydu/file.py diff --git a/pydu/file.py b/pydu/file.py new file mode 100644 index 0000000..a22f99f --- /dev/null +++ b/pydu/file.py @@ -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)) From fb2fa7eb622fedc5fc8d0ae6c4491d96701c3ab1 Mon Sep 17 00:00:00 2001 From: Prodesire Date: Wed, 29 Nov 2017 08:11:02 +0800 Subject: [PATCH 2/5] fix cmd doc replicate --- docs/cmd.rst | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/docs/cmd.rst b/docs/cmd.rst index eaf878a..fb56a81 100644 --- a/docs/cmd.rst +++ b/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) - - - - - - >>> from pydu.cmd import run - >>> execute('echo hello') - (0, b'hello\r\n') # Python 3 - >>> execute('echo hello', wait=False) - - - - - - >>> from pydu.cmd import execute - >>> execute('echo hello') - (0, b'hello\r\n') # Python 3 - >>> run('echo hello', wait=False) - - - - - - >>> from pydu.cmd import execute - >>> execute('echo hello') - (0, b'hello\r\n') # Python 3 - >>> run('echo hello', wait=False) - - - - - >>> from pydu.cmd import execute >>> execute('echo hello') (0, b'hello\r\n') # Python 3 From dcd5584c062e691d1b5185dec36d6e2a67b91baf Mon Sep 17 00:00:00 2001 From: Prodesire Date: Wed, 29 Nov 2017 08:24:53 +0800 Subject: [PATCH 3/5] fix inspect doc error --- docs/inspect.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/inspect.rst b/docs/inspect.rst index 68d2da4..a470ea1 100644 --- a/docs/inspect.rst +++ b/docs/inspect.rst @@ -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): From eaa11e69192416a31622cd3dc4843d82165152b7 Mon Sep 17 00:00:00 2001 From: Prodesire Date: Wed, 29 Nov 2017 08:25:02 +0800 Subject: [PATCH 4/5] add doc for network --- docs/index.rst | 1 + docs/network.rst | 24 ++++++++++++++++++++++++ pydu/network.py | 9 +++++---- 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 docs/network.rst diff --git a/docs/index.rst b/docs/index.rst index 940c1f6..e30938c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -27,6 +27,7 @@ Content dict inspect misc + network request set string diff --git a/docs/network.rst b/docs/network.rst new file mode 100644 index 0000000..6ce74f0 --- /dev/null +++ b/docs/network.rst @@ -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 diff --git a/pydu/network.py b/pydu/network.py index a74f713..265a810 100644 --- a/pydu/network.py +++ b/pydu/network.py @@ -2,21 +2,22 @@ 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 From 1bea626b6dd095a55f9e7c82b5614d2ca2e177b4 Mon Sep 17 00:00:00 2001 From: Prodesire Date: Wed, 29 Nov 2017 08:25:57 +0800 Subject: [PATCH 5/5] remove todo in network --- pydu/network.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pydu/network.py b/pydu/network.py index 265a810..f490a92 100644 --- a/pydu/network.py +++ b/pydu/network.py @@ -13,7 +13,6 @@ def dotted_netmask(mask): return socket.inet_ntoa(struct.pack('>I', bits)) -# todo doc # https://github.com/kennethreitz/requests/blob/master/requests/utils.py def is_ipv4_address(ip): try: