umnetdb-utils 0.1.2__py3-none-any.whl → 0.1.4__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.
umnetdb_utils/base.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
|
2
|
+
from typing import Union
|
2
3
|
from os import getenv
|
3
4
|
import re
|
4
5
|
import logging
|
@@ -78,7 +79,7 @@ class UMnetdbBase:
|
|
78
79
|
|
79
80
|
raise AttributeError(self)
|
80
81
|
|
81
|
-
def _build_select(self, select, table, joins=None, where=None, order_by=None, limit=None, group_by=None, distinct=False):
|
82
|
+
def _build_select(self, select, table, joins=None, where=None, order_by=None, limit=None, group_by=None, distinct=False) -> str:
|
82
83
|
'''
|
83
84
|
Generic 'select' query string builder built from standard query components as input.
|
84
85
|
The user is required to generate substrings for the more complex inputs
|
@@ -137,7 +138,7 @@ class UMnetdbBase:
|
|
137
138
|
|
138
139
|
return sql
|
139
140
|
|
140
|
-
def _execute(self, sql, rows_as_dict=True):
|
141
|
+
def _execute(self, sql:str, rows_as_dict:bool=True, fetch_one:bool=False):
|
141
142
|
'''
|
142
143
|
Generic sqlalchemy "open a session, execute this sql command and give me all the results"
|
143
144
|
|
@@ -146,21 +147,23 @@ class UMnetdbBase:
|
|
146
147
|
scripts that import the child class to use the context manager and execute multiple
|
147
148
|
mehtods within the same session.
|
148
149
|
'''
|
149
|
-
with self as
|
150
|
-
r =
|
151
|
-
rows = r.fetchall()
|
150
|
+
with self.engine.begin() as c:
|
151
|
+
r = c.execute(text(sql))
|
152
152
|
|
153
|
+
rows = r.fetchall()
|
153
154
|
if rows and rows_as_dict:
|
154
155
|
return [r._mapping for r in rows]
|
155
156
|
elif rows:
|
156
157
|
return rows
|
157
158
|
else:
|
158
159
|
return []
|
160
|
+
|
159
161
|
|
160
|
-
def execute(self, sql:str, rows_as_dict:bool=True, fetch_one:bool=False):
|
162
|
+
def execute(self, sql:str, rows_as_dict:bool=True, fetch_one:bool=False) -> Union[list[dict],dict]:
|
161
163
|
"""
|
162
|
-
Executes a sqlalchemy command and gives all the results
|
163
|
-
|
164
|
+
Executes a sqlalchemy command and gives all the results as a list of dicts, or as a dict
|
165
|
+
if 'fetch_one' is set to true.
|
166
|
+
Does not open a session - you must open one yourself.
|
164
167
|
"""
|
165
168
|
result = self.session.execute(text(sql))
|
166
169
|
|
@@ -168,6 +171,6 @@ class UMnetdbBase:
|
|
168
171
|
result = result.mappings()
|
169
172
|
|
170
173
|
if fetch_one:
|
171
|
-
return result.fetchone()
|
174
|
+
return dict(result.fetchone())
|
172
175
|
|
173
|
-
return result.fetchall()
|
176
|
+
return [dict(r) for r in result.fetchall()]
|
umnetdb_utils/umnetdb.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
|
2
2
|
from typing import List
|
3
3
|
import logging
|
4
|
+
import re
|
4
5
|
|
5
6
|
|
6
7
|
from sqlalchemy import text
|
@@ -22,7 +23,8 @@ class UMnetdb(UMnetdbBase):
|
|
22
23
|
up in lldp neighbor.
|
23
24
|
|
24
25
|
Setting 'known_devices_only' to true only returns neighbors that are found
|
25
|
-
in umnet_db's device table. Setting it to false will return all lldp neighbors
|
26
|
+
in umnet_db's device table. Setting it to false will return all lldp neighbors
|
27
|
+
and will include things like phones and APs
|
26
28
|
|
27
29
|
Returns results as a list of dictionary entries keyed on column names.
|
28
30
|
"""
|
@@ -56,14 +58,12 @@ class UMnetdb(UMnetdbBase):
|
|
56
58
|
|
57
59
|
table = "neighbor n"
|
58
60
|
|
59
|
-
|
60
|
-
|
61
61
|
where = [f"n.device='{device}'"]
|
62
62
|
|
63
63
|
query = self._build_select(select, table, joins, where)
|
64
|
-
result = self.session.execute(text(query))
|
65
64
|
|
66
|
-
return
|
65
|
+
return self.execute(query)
|
66
|
+
|
67
67
|
|
68
68
|
|
69
69
|
def get_dlzone(self, zone_name:str) -> List[dict]:
|
@@ -95,18 +95,24 @@ class UMnetdb(UMnetdbBase):
|
|
95
95
|
|
96
96
|
# note that by default this method only returns neighbors in the 'device' table,
|
97
97
|
# any others are ignored
|
98
|
-
neighs = self.
|
99
|
-
|
98
|
+
neighs = self.get_neighbors(device)
|
99
|
+
devices_by_name[device]["neighbors"] = {}
|
100
100
|
for neigh in neighs:
|
101
|
-
if neigh["remote_device"].startswith("s-"):
|
102
101
|
|
103
|
-
|
104
|
-
|
102
|
+
# only want 'd- or 'dl-' or 's-' devices
|
103
|
+
if re.match(r"(dl?|s)-", neigh["remote_device"]):
|
105
104
|
|
106
|
-
|
107
|
-
|
105
|
+
# adding neighbor to local device's neighbor list
|
106
|
+
devices_by_name[device]["neighbors"][neigh["port"]] = {k:v for k,v in neigh.items() if k != "port"}
|
108
107
|
|
109
|
-
|
108
|
+
# if we haven't seen this neighbor yet, pull data from our device table for it, and
|
109
|
+
# add it to our 'to do' list to pull its neighbors.
|
110
|
+
if neigh["remote_device"] not in devices_by_name:
|
110
111
|
|
112
|
+
query = self._build_select(select=device_cols, table="device", where=f"name = '{neigh['remote_device']}'")
|
113
|
+
neigh_device = self.execute(query, fetch_one=True)
|
114
|
+
devices_by_name[neigh_device["name"]] = neigh_device
|
115
|
+
|
116
|
+
todo.append(neigh_device["name"])
|
111
117
|
|
112
118
|
return list(devices_by_name.values())
|
@@ -1,10 +1,10 @@
|
|
1
1
|
umnetdb_utils/__init__.py,sha256=QJaytbr4ccKESiwaKjpf1b4b8s2cHNfCDdnCOs1tmoI,131
|
2
|
-
umnetdb_utils/base.py,sha256=
|
3
|
-
umnetdb_utils/umnetdb.py,sha256=
|
2
|
+
umnetdb_utils/base.py,sha256=aC5oKSB5Ox9QtylmHV2uvEUVUx4CRLZTgH6HpQJ6DZo,6064
|
3
|
+
umnetdb_utils/umnetdb.py,sha256=IxqpAcPZn71HVV_oL3ryvbKS7cupUsyM6rJSGk6NLaU,4554
|
4
4
|
umnetdb_utils/umnetdisco.py,sha256=Z2XwT79jKO_avd3w_z99DDEdAikrfKxYm1JYHRWqvG4,7841
|
5
5
|
umnetdb_utils/umnetequip.py,sha256=jOW5kvk0FXtdHv8PA4rYT_PWfLdMiq83Mjwmy-c1DN8,5701
|
6
6
|
umnetdb_utils/umnetinfo.py,sha256=MH1YDW4OWtHD46qfYb5Pv40vPSbL0GrMNW5gAhdlihE,18445
|
7
7
|
umnetdb_utils/utils.py,sha256=wU6QMYfofj7trX3QeqXty0btbGdhP_RUaSqA7QTflFM,991
|
8
|
-
umnetdb_utils-0.1.
|
9
|
-
umnetdb_utils-0.1.
|
10
|
-
umnetdb_utils-0.1.
|
8
|
+
umnetdb_utils-0.1.4.dist-info/METADATA,sha256=JI2hwHkX3B8y-GLwS0yywAiFszcqUlkiWUyga0A2KaY,1555
|
9
|
+
umnetdb_utils-0.1.4.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
10
|
+
umnetdb_utils-0.1.4.dist-info/RECORD,,
|
File without changes
|