Browse Source

v2.5 improvement localenv && added CC to sendmail

Josue Gomez 4 months ago
parent
commit
8b150c14ff

BIN
dist/pyutl-2.1.tar.gz View File


BIN
dist/pyutl-2.5.tar.gz View File


+ 15
- 0
pyutl.egg-info/PKG-INFO View File

@@ -0,0 +1,15 @@
1
+Metadata-Version: 2.1
2
+Name: pyutl
3
+Version: 2.5
4
+Summary: functions and utilities to recycle code
5
+Home-page: https://git.binkfe.com/jesrat/pyutl
6
+Author: Josue Gomez <jgomez@jesrat.com>
7
+Author-email: jgomez@binkfe.com
8
+License: UNKNOWN
9
+Description: pyutils
10
+        
11
+Platform: UNKNOWN
12
+Classifier: Programming Language :: Python :: 3
13
+Classifier: License :: OSI Approved :: MIT License
14
+Classifier: Operating System :: OS Independent
15
+Description-Content-Type: text/markdown

+ 12
- 0
pyutl.egg-info/SOURCES.txt View File

@@ -0,0 +1,12 @@
1
+README.md
2
+setup.cfg
3
+setup.py
4
+pyutl/__init__.py
5
+pyutl/localenv.py
6
+pyutl/oracle.py
7
+pyutl/remote.py
8
+pyutl/sendmail.py
9
+pyutl.egg-info/PKG-INFO
10
+pyutl.egg-info/SOURCES.txt
11
+pyutl.egg-info/dependency_links.txt
12
+pyutl.egg-info/top_level.txt

+ 1
- 0
pyutl.egg-info/dependency_links.txt View File

@@ -0,0 +1 @@
1
+

+ 1
- 0
pyutl.egg-info/top_level.txt View File

@@ -0,0 +1 @@
1
+pyutl

+ 28
- 4
pyutl/__init__.py View File

@@ -1,5 +1,3 @@
1
-# functions to recycle code
2
-
3 1
 r"""
4 2
 To use, simply 'import pyutils' 
5 3
 
@@ -10,14 +8,18 @@ shellExecute can receive a cmd as str or arr example
10 8
     (b"'hello world'\n", b'')
11 9
 """
12 10
 
11
+__title__ = 'pyutl'
12
+__description__ = 'functions and utilities to recycle code'
13
+__url__ = 'https://git.binkfe.com/jesrat/pyutl'
14
+__version__ = '2.5'
13 15
 __author__ = 'Josue Gomez <jgomez@jesrat.com>'
14
-__maintainer__ = "Josue Gomez"
15 16
 __email__ = "jgomez@binkfe.com"
17
+__maintainer__ = "Josue Gomez"
16 18
 __license__ = "MIT"
17
-__version__ = '2.0'
18 19
 __all__ = ['', ]
19 20
 __status__ = "production"
20 21
 __date__ = "30 January 2019"
22
+__copyright__ = 'Copyright 2019 Josue Gomez'
21 23
 
22 24
 
23 25
 import sys
@@ -43,3 +45,25 @@ def progress_bar(progress, total, status=''):
43 45
     bar = '■' * fill_len + '-' * (bar_len - fill_len)
44 46
     sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percent, '%', status))
45 47
     sys.stdout.flush()
48
+
49
+
50
+def read_streamed_file(file, chunk_size=10):
51
+    """
52
+    Reads a hugh file by chunks (10 lines default)
53
+    :param file
54
+    :param chunk_size (10 lines default)
55
+    :return: chunk by chunk
56
+    """
57
+    counter = 0
58
+    ret_lines = []
59
+    with open(file) as f:
60
+        while True:
61
+            counter += 1
62
+            line = f.readline()
63
+            if line:
64
+                ret_lines.append((counter, line))
65
+            if (counter/chunk_size).is_integer() or not line:
66
+                yield ret_lines
67
+                ret_lines = []
68
+            if not line:
69
+                break

+ 36
- 21
pyutl/localenv.py View File

@@ -1,14 +1,22 @@
1 1
 import os
2 2
 import sys
3
+import json
3 4
 
4 5
 
5 6
 class NoEnvironmentFile(Exception):
6 7
     pass
7 8
 
8 9
 
10
+class KeyNotFound(Exception):
11
+    pass
12
+
13
+
14
+DEFAULT = object()
15
+
16
+
9 17
 class LocalEnv:
10 18
     def __init__(self):
11
-        self.file = None
19
+        self.files = []
12 20
         self.data = {}
13 21
 
14 22
     def load(self, file=None):
@@ -17,29 +25,35 @@ class LocalEnv:
17 25
         in invoker module's directory
18 26
         """
19 27
         if file is not None:
20
-            self.file = file
28
+            self.files.append({'file': file, 'exists': '', 'loaded': False})
21 29
         else:
22
-            self.file = self._invoker()
23
-
24
-        if not os.path.isfile(self.file):
25
-            raise NoEnvironmentFile(f'for file {self.file}')
26
-
27
-        with open(self.file) as f:
28
-            for line in f:
29
-                line = line.strip()
30
-                if not line or line.startswith('#') or '=' not in line:
31
-                    continue
32
-                key, value = line.split('=', 1)
33
-                key = key.replace('export', '')
34
-                key = key.strip()
35
-                value = value.strip().strip('\'"')
36
-                self.data[key] = value
30
+            self.files.append({'file': self._invoker(), 'exists': '', 'loaded': False})
37 31
 
38
-    def get(self, key, cast=None):
39
-        if cast is None:
40
-            return self.data[key]
32
+        # search all files given and load them
33
+        for file_dict in self.files:
34
+            file_dict['exists'] = os.path.isfile(file_dict['file'])
35
+            if file_dict['exists'] and not file_dict['loaded']:
36
+                with open(file_dict['file']) as f:
37
+                    for line in f:
38
+                        line = line.strip()
39
+                        if not line or line.startswith('#') or '=' not in line:
40
+                            continue
41
+                        key, value = line.split('=', 1)
42
+                        key = key.replace('export', '')
43
+                        key = key.strip()
44
+                        value = value.strip().strip('\'"')
45
+                        self.data[key] = value
46
+                    file_dict['loaded'] = True
41 47
 
42
-        return cast(self.data[key])
48
+    def get(self, key, default=DEFAULT, cast=None):
49
+        try:
50
+            ret_val = self.data[key] if cast is None else cast(self.data[key])
51
+        except KeyError:
52
+            if default != DEFAULT:
53
+                ret_val = default if cast is None else cast(default)
54
+            else:
55
+                raise KeyNotFound(f'value not found in files: \n{json.dumps(self.files, indent=4)}')
56
+        return ret_val
43 57
 
44 58
     @staticmethod
45 59
     def _invoker():
@@ -53,3 +67,4 @@ class LocalEnv:
53 67
 
54 68
 
55 69
 localenv = LocalEnv()
70
+localenv.load()

+ 7
- 1
pyutl/sendmail.py View File

@@ -33,13 +33,19 @@ class SendMail:
33 33
             self.conn.starttls()
34 34
         self.conn.login(self.user, self.pssw)
35 35
 
36
-    def content(self, from_address, to_address, subject, msg):
36
+    def content(self, from_address, to_address, subject, msg, cc=None):
37 37
         if not isinstance(to_address, list):
38 38
             raise AssertionError('destination address should be a list []')
39
+        if cc:
40
+            if not isinstance(cc, list):
41
+                raise AssertionError('cc address should be a list []')
42
+
39 43
         self.msg = MIMEMultipart()
40 44
         self.msg['Subject'] = subject
41 45
         self.msg['From'] = from_address
42 46
         self.msg['To'] = COMMASPACE.join(to_address)
47
+        if cc:
48
+            self.msg['Cc'] = COMMASPACE.join(cc)
43 49
         self.msg.attach(MIMEText(msg, 'html'))
44 50
 
45 51
     def attach(self, files):

+ 10
- 9
setup.py View File

@@ -1,18 +1,19 @@
1
-import setuptools
1
+import pyutl as pkg
2
+from setuptools import setup, find_packages
2 3
 
3 4
 with open("README.md", "r") as fh:
4 5
     long_description = fh.read()
5 6
 
6
-setuptools.setup(
7
-    name="pyutl",
8
-    version="2.1",
9
-    author="Josue Gomez",
10
-    author_email="jgomez@binkfe.com",
11
-    description="A package of utilities for python",
7
+setup(
8
+    name=pkg.__title__,
9
+    version=pkg.__version__,
10
+    author=pkg.__author__,
11
+    author_email=pkg.__email__,
12
+    description=pkg.__description__,
12 13
     long_description=long_description,
13 14
     long_description_content_type="text/markdown",
14
-    url="https://git.binkfe.com/jesrat/pyutils",
15
-    packages=['pyutl'],
15
+    url=pkg.__url__,
16
+    packages=find_packages(),
16 17
     classifiers=[
17 18
         "Programming Language :: Python :: 3",
18 19
         "License :: OSI Approved :: MIT License",