imio.pyutils 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- imio/pyutils/__init__.py +0 -0
- imio/pyutils/batching.py +41 -0
- imio/pyutils/bs.py +118 -0
- imio/pyutils/postgres.py +173 -0
- imio/pyutils/system.py +503 -0
- imio/pyutils/tests.py +258 -0
- imio/pyutils/utils.py +361 -0
- imio.pyutils-1.1.0-py3.11-nspkg.pth +1 -0
- imio.pyutils-1.1.0.dist-info/METADATA +370 -0
- imio.pyutils-1.1.0.dist-info/RECORD +13 -0
- imio.pyutils-1.1.0.dist-info/WHEEL +5 -0
- imio.pyutils-1.1.0.dist-info/namespace_packages.txt +1 -0
- imio.pyutils-1.1.0.dist-info/top_level.txt +1 -0
imio/pyutils/__init__.py
ADDED
|
File without changes
|
imio/pyutils/batching.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
# IMIO <support@imio.be>
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
"""
|
|
6
|
+
This module is just a part of imio.helpers.batching module.
|
|
7
|
+
https://github.com/IMIO/imio.helpers/blob/master/src/imio/helpers/batching.py
|
|
8
|
+
|
|
9
|
+
Following function has been put here because imio.pyutils is common to imio.helpers and imio.updates.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from datetime import datetime
|
|
13
|
+
|
|
14
|
+
import logging
|
|
15
|
+
import os
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
logger = logging.getLogger("imio.pyutils")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# 7) when all the items are treated, we can delete the dictionary file
|
|
22
|
+
def batch_delete_files(batch_keys, config, rename=True):
|
|
23
|
+
"""Deletes the file containing the batched keys.
|
|
24
|
+
|
|
25
|
+
:param batch_keys: the treated keys set
|
|
26
|
+
:param config: a config dict {'bn': batch_number, 'bl': batch_last, 'cn': commit_number, 'll': loop_length,
|
|
27
|
+
'lc': loop_count, 'pf': infile, 'cf': config_file, 'kc': keys_count, 'lk': last_key,
|
|
28
|
+
'ldk': last_dump_key, 'fr'; first_run_bool}
|
|
29
|
+
:param rename: do not delete but rename
|
|
30
|
+
"""
|
|
31
|
+
if batch_keys is None:
|
|
32
|
+
return
|
|
33
|
+
try:
|
|
34
|
+
for key in ("pf", "cf"):
|
|
35
|
+
if config[key] and os.path.exists(config[key]):
|
|
36
|
+
if rename:
|
|
37
|
+
os.rename(config[key], "{}.{}".format(config[key], datetime.now().strftime("%Y%m%d-%H%M%S")))
|
|
38
|
+
else:
|
|
39
|
+
os.remove(config[key])
|
|
40
|
+
except Exception as error:
|
|
41
|
+
logger.exception("Error while renaming/deleting the file %s: %s", config["pf"], error)
|
imio/pyutils/bs.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
#!/usr/bin/python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
#
|
|
4
|
+
# bs4 utility methods
|
|
5
|
+
# IMIO <support@imio.be>
|
|
6
|
+
#
|
|
7
|
+
from bs4 import BeautifulSoup
|
|
8
|
+
from bs4.element import Comment
|
|
9
|
+
|
|
10
|
+
import re
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def is_empty(element):
|
|
14
|
+
"""Check if element has no text content nor children.
|
|
15
|
+
|
|
16
|
+
:param element: bs item
|
|
17
|
+
:return: Bool value (True is empty)
|
|
18
|
+
"""
|
|
19
|
+
return len(element.get_text(strip=True)) == 0 and not len([ch for ch in element.children if str(ch).strip()])
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def remove_attributes(element, attributes=[], recursive=True):
|
|
23
|
+
"""Removes attributes on given element or recursively.
|
|
24
|
+
|
|
25
|
+
:param element: bs item
|
|
26
|
+
:param attributes: attributes to remove
|
|
27
|
+
:param recursive: consider sub items attributes
|
|
28
|
+
"""
|
|
29
|
+
elements = [element]
|
|
30
|
+
if recursive:
|
|
31
|
+
elements += element.find_all()
|
|
32
|
+
for tag in elements:
|
|
33
|
+
for attr in attributes:
|
|
34
|
+
try:
|
|
35
|
+
del tag[attr]
|
|
36
|
+
except KeyError:
|
|
37
|
+
pass
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def remove_childrens_by_pattern(parent, child_text_pat, name=None, recursive=False, keep=()):
|
|
41
|
+
"""Remove children with matched text.
|
|
42
|
+
|
|
43
|
+
:param parent: bs parent to consider
|
|
44
|
+
:param child_text_pat: text pattern to search
|
|
45
|
+
:param name: optional children's tag
|
|
46
|
+
:param recursive: search children recursively
|
|
47
|
+
:param keep: keep children at this position (starting at 1)
|
|
48
|
+
:return: list of removed items
|
|
49
|
+
"""
|
|
50
|
+
childrens = list(parent.find_all(name=name, recursive=recursive))
|
|
51
|
+
if isinstance(parent, BeautifulSoup):
|
|
52
|
+
childrens.pop(0)
|
|
53
|
+
removed = []
|
|
54
|
+
count = 0
|
|
55
|
+
for ch_t in childrens:
|
|
56
|
+
if found := re.search(child_text_pat, ch_t.text, re.I):
|
|
57
|
+
count += 1
|
|
58
|
+
if count not in keep:
|
|
59
|
+
removed.append(str(ch_t))
|
|
60
|
+
ch_t.decompose()
|
|
61
|
+
return removed
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def remove_comments(element):
|
|
65
|
+
"""Removes html comments"""
|
|
66
|
+
for comment in element.find_all(text=lambda text: isinstance(text, Comment)):
|
|
67
|
+
comment.extract()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def remove_elements(element, to_remove=[]):
|
|
71
|
+
"""Removes sub tags and all their content"""
|
|
72
|
+
for tagtr in to_remove:
|
|
73
|
+
for tag in element.find_all(tagtr):
|
|
74
|
+
tag.decompose()
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def replace_entire_strings(element, replace="\n", by=""):
|
|
78
|
+
"""Replaces an entire string by another value. With default params, removes newlines.
|
|
79
|
+
|
|
80
|
+
:param element: bs item
|
|
81
|
+
:param replace: value to replace
|
|
82
|
+
:param by: replacement value
|
|
83
|
+
"""
|
|
84
|
+
# reversed needed to avoid internal strings error
|
|
85
|
+
strings = reversed([s for s in element.strings])
|
|
86
|
+
for string in strings:
|
|
87
|
+
if string == replace:
|
|
88
|
+
string.replace_with(by)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def replace_strings_by_pattern(element, find_pat, replace_pat):
|
|
92
|
+
"""Replace element strings.
|
|
93
|
+
|
|
94
|
+
:param element: bs item
|
|
95
|
+
:param find_pat: pattern to find
|
|
96
|
+
:param replace_pat: replacement pattern
|
|
97
|
+
:return: replaced strings
|
|
98
|
+
"""
|
|
99
|
+
replaced = []
|
|
100
|
+
# reversed needed to avoid internal strings error
|
|
101
|
+
strings = list(reversed([s for s in element.strings]))
|
|
102
|
+
for string in strings:
|
|
103
|
+
if found := re.search(find_pat, string, re.I):
|
|
104
|
+
new_val = re.sub(find_pat, replace_pat, string, re.I)
|
|
105
|
+
replaced.append("'{}' => '{}'".format(string, new_val))
|
|
106
|
+
string.replace_with(new_val)
|
|
107
|
+
return replaced
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def unwrap_tags(element, tags=[]):
|
|
111
|
+
"""Unwrap tags on content (<a ...>lien</a> => lien).
|
|
112
|
+
|
|
113
|
+
:param element: bs item
|
|
114
|
+
:param tags: tags to search
|
|
115
|
+
"""
|
|
116
|
+
for tagtu in tags:
|
|
117
|
+
for tag in element.find_all(tagtu):
|
|
118
|
+
tag.unwrap()
|
imio/pyutils/postgres.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#!/usr/bin/python
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
#
|
|
4
|
+
# postgresql utility methods
|
|
5
|
+
# IMIO <support@imio.be>
|
|
6
|
+
#
|
|
7
|
+
|
|
8
|
+
from .system import error
|
|
9
|
+
from .system import trace
|
|
10
|
+
|
|
11
|
+
import psycopg2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# ------------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# dsn="host=localhost port=5432 dbname= user= password="
|
|
18
|
+
def openConnection(dsn):
|
|
19
|
+
"""open a postgres connection"""
|
|
20
|
+
conn = None
|
|
21
|
+
try:
|
|
22
|
+
conn = psycopg2.connect(dsn)
|
|
23
|
+
except Exception as message:
|
|
24
|
+
msg = "Cannot connect to database with dsn '%s': %s" % (dsn, message)
|
|
25
|
+
error(msg)
|
|
26
|
+
raise Exception(msg)
|
|
27
|
+
return conn
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# ------------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def insertInTable(dsn, table, columns, vals, TRACE=False):
|
|
34
|
+
"""insert values in a table"""
|
|
35
|
+
conn = openConnection(dsn)
|
|
36
|
+
cursor = conn.cursor()
|
|
37
|
+
req = "insert into %s(%s) values(%s)" % (table, columns, vals)
|
|
38
|
+
trace(TRACE, "Insertion: %s" % req)
|
|
39
|
+
try:
|
|
40
|
+
cursor.execute(req)
|
|
41
|
+
cursor.close()
|
|
42
|
+
except Exception as message:
|
|
43
|
+
conn.rollback()
|
|
44
|
+
error("Cannot insert in database : %s" % message)
|
|
45
|
+
error("Request was : '%s'" % req)
|
|
46
|
+
conn.close()
|
|
47
|
+
return False
|
|
48
|
+
conn.commit()
|
|
49
|
+
conn.close()
|
|
50
|
+
return True
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
# ------------------------------------------------------------------------------
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def updateTable(dsn, table, updates, condition="", TRACE=False):
|
|
57
|
+
"""update columns in a table"""
|
|
58
|
+
conn = openConnection(dsn)
|
|
59
|
+
cursor = conn.cursor()
|
|
60
|
+
req = "update %s set %s" % (table, updates)
|
|
61
|
+
if condition:
|
|
62
|
+
req += " where %s" % condition
|
|
63
|
+
trace(TRACE, "Update: %s" % req)
|
|
64
|
+
try:
|
|
65
|
+
cursor.execute(req)
|
|
66
|
+
cursor.close()
|
|
67
|
+
except Exception as message:
|
|
68
|
+
conn.rollback()
|
|
69
|
+
error("Cannot update in database : %s" % message)
|
|
70
|
+
error("Request was : '%s'" % req)
|
|
71
|
+
conn.close()
|
|
72
|
+
return False
|
|
73
|
+
conn.commit()
|
|
74
|
+
conn.close()
|
|
75
|
+
return True
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# ------------------------------------------------------------------------------
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def selectWithSQLRequest(dsn, sql, TRACE=False):
|
|
82
|
+
"""select multiple lines in a table with a complete sql"""
|
|
83
|
+
conn = openConnection(dsn)
|
|
84
|
+
cursor = conn.cursor()
|
|
85
|
+
req = sql
|
|
86
|
+
trace(TRACE, "Selection: %s" % req)
|
|
87
|
+
try:
|
|
88
|
+
cursor.execute(req)
|
|
89
|
+
data = cursor.fetchall()
|
|
90
|
+
cursor.close()
|
|
91
|
+
except Exception as message:
|
|
92
|
+
error("Cannot select from database : %s" % message)
|
|
93
|
+
error("Request was : '%s'" % req)
|
|
94
|
+
conn.close()
|
|
95
|
+
return None
|
|
96
|
+
conn.close()
|
|
97
|
+
return data
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
# ------------------------------------------------------------------------------
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def selectAllInTable(dsn, table, selection, condition="", TRACE=False):
|
|
104
|
+
"""select multiple lines in a table"""
|
|
105
|
+
conn = openConnection(dsn)
|
|
106
|
+
cursor = conn.cursor()
|
|
107
|
+
req = "select %s from %s" % (selection, table)
|
|
108
|
+
if condition:
|
|
109
|
+
req += " where %s" % condition
|
|
110
|
+
trace(TRACE, "Selection: %s" % req)
|
|
111
|
+
try:
|
|
112
|
+
cursor.execute(req)
|
|
113
|
+
data = cursor.fetchall()
|
|
114
|
+
cursor.close()
|
|
115
|
+
except Exception as message:
|
|
116
|
+
error("Cannot select from database : %s" % message)
|
|
117
|
+
error("Request was : '%s'" % req)
|
|
118
|
+
conn.close()
|
|
119
|
+
return None
|
|
120
|
+
conn.close()
|
|
121
|
+
return data
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
# ------------------------------------------------------------------------------
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def selectOneInTable(dsn, table, selection, condition="", TRACE=False):
|
|
128
|
+
"""select a single line in a table"""
|
|
129
|
+
conn = openConnection(dsn)
|
|
130
|
+
cursor = conn.cursor()
|
|
131
|
+
req = "select %s from %s" % (selection, table)
|
|
132
|
+
if condition:
|
|
133
|
+
req += " where %s" % condition
|
|
134
|
+
trace(TRACE, "Selection: %s" % req)
|
|
135
|
+
try:
|
|
136
|
+
cursor.execute(req)
|
|
137
|
+
data = cursor.fetchone()
|
|
138
|
+
cursor.close()
|
|
139
|
+
except Exception as message:
|
|
140
|
+
error("Cannot select from database : %s" % message)
|
|
141
|
+
error("Request was : '%s'" % req)
|
|
142
|
+
conn.close()
|
|
143
|
+
return None
|
|
144
|
+
conn.close()
|
|
145
|
+
return data
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
# ------------------------------------------------------------------------------
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def deleteTable(dsn, table, condition="", TRACE=False):
|
|
152
|
+
"""delete a table"""
|
|
153
|
+
conn = openConnection(dsn)
|
|
154
|
+
cursor = conn.cursor()
|
|
155
|
+
req = "delete from %s" % table
|
|
156
|
+
if condition:
|
|
157
|
+
req += " where %s" % condition
|
|
158
|
+
trace(TRACE, "Deletion : %s" % req)
|
|
159
|
+
try:
|
|
160
|
+
cursor.execute(req)
|
|
161
|
+
cursor.close()
|
|
162
|
+
except Exception as message:
|
|
163
|
+
conn.rollback()
|
|
164
|
+
error("Cannot delete from database : %s" % message)
|
|
165
|
+
error("Request was : '%s'" % req)
|
|
166
|
+
conn.close()
|
|
167
|
+
return False
|
|
168
|
+
conn.commit()
|
|
169
|
+
conn.close()
|
|
170
|
+
return True
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
# ------------------------------------------------------------------------------
|