umnetdb-utils 0.1.0__py3-none-any.whl → 0.1.1__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
@@ -2,6 +2,7 @@
2
2
  from os import getenv
3
3
  import re
4
4
  import logging
5
+ from decouple import Config, RepositoryEnv
5
6
 
6
7
  from sqlalchemy import create_engine, text
7
8
  from sqlalchemy.orm import Session
@@ -15,13 +16,20 @@ class UMnetdbBase:
15
16
  # set in child classes - you can use environment variables within curly braces here
16
17
  URL = None
17
18
 
18
- def __init__(self):
19
+ def __init__(self, env_file:str=".env"):
19
20
  """
20
- Initiate a umnetdb object. Note that you must provide either a url or a path
21
- to an env file that looks like the 'sample_env' provided in this repo.
22
- If both are provided, the url takes precedence.
21
+ Initiate a umnetdb object. Optionally provide a path to a file with environment variables
22
+ containing the credentials for the database. If no file is provided and there's no ".env",
23
+ the code will look in the user's environment (os.getenv) for these values.
23
24
  """
25
+
26
+ try:
27
+ self._env = Config(RepositoryEnv(env_file))
28
+ except FileNotFoundError:
29
+ self._env = {}
30
+
24
31
  self.url = self._resolve_url()
32
+
25
33
  self.engine = create_engine(self.url)
26
34
  self.session = None
27
35
 
@@ -34,10 +42,11 @@ class UMnetdbBase:
34
42
  url = self.URL
35
43
  for m in re.finditer(r"{(\w+)}", url):
36
44
  var = m.group(1)
45
+ val = self._env.get(var, getenv(var))
37
46
 
38
- if not getenv(var):
39
- raise ValueError(f"Undefined environment variable {var} in {url}")
40
- url = re.sub(r"{" + var + "}", getenv(var), url)
47
+ if not val:
48
+ raise ValueError(f"Undefined environment variable {val} in {url}")
49
+ url = re.sub(r"{" + var + "}", val, url)
41
50
 
42
51
  return url
43
52
 
@@ -126,7 +135,12 @@ class UMnetdbBase:
126
135
 
127
136
  def _execute(self, sql, rows_as_dict=True):
128
137
  '''
129
- Generic sqlalchemy "execute this sql command and give me all the results"
138
+ Generic sqlalchemy "open a session, execute this sql command and give me all the results"
139
+
140
+ NB This function is defined for legacy database classes that came from umnet-scripts.
141
+ It's encouraged to use "self.session.execute" in other child methods, allowing
142
+ scripts that import the child class to use the context manager and execute multiple
143
+ mehtods within the same session.
130
144
  '''
131
145
  with self as session:
132
146
  r = session.execute(text(sql))
@@ -0,0 +1,35 @@
1
+ Metadata-Version: 2.3
2
+ Name: umnetdb-utils
3
+ Version: 0.1.1
4
+ Summary: Helper classes for querying UMnet databases
5
+ License: MIT
6
+ Author: Amy Liebowitz
7
+ Author-email: amylieb@umich.edu
8
+ Requires-Python: >=3.10
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Requires-Dist: oracledb (>=3.1.0,<4.0.0)
16
+ Requires-Dist: psycopg[binary] (>=3.2.9,<4.0.0)
17
+ Requires-Dist: python-decouple (>=3.8,<4.0)
18
+ Requires-Dist: sqlalchemy (>=2.0.41,<3.0.0)
19
+ Description-Content-Type: text/markdown
20
+
21
+ # umnetdb-utils
22
+ Helper classes for gathering data from umnet databases
23
+
24
+ This package is hosted on pypi - you can install it with `pip install umnetdb-utils` and use it in your own code.
25
+
26
+ ## Database Helper Classes
27
+ As of May 2025 this repo defines db wrapper classes for Equipdb, Netinfo, Netdisco and UMnetdb (populated by agador, hosted on wintermute).
28
+ To use these classes you need to set specific environment variables with the credentials for each db respectively:
29
+ * Netinfo: `NETINFO_USERNAME`, `NETINFO_PASSSWORD`
30
+ * Netdisco: `NETDISCO_DB_USER`, `NETDISCO_DB_PASSWORD`
31
+ * Equipdb: `EQUIP_DB_USER`, `EQUIP_DB_PASSWORD`
32
+ * UMnetdb: `UMNETDB_USER`, `UMNETDB_PASSWORD`
33
+
34
+ Netinfo, Netdisco, and Equipdb classes are copied over from `umnet-scripts` which is reaching the end of its life as a package.
35
+
@@ -1,10 +1,10 @@
1
1
  umnetdb_utils/__init__.py,sha256=QJaytbr4ccKESiwaKjpf1b4b8s2cHNfCDdnCOs1tmoI,131
2
- umnetdb_utils/base.py,sha256=u0ZEKb3Vx8aSTrN8r4dbVCSpKHaBVw0Aw4VwTZfMRjc,4609
2
+ umnetdb_utils/base.py,sha256=PCtc08eZ1BwExSY1nm-eGWMTd_V56fsZXhXq2D6rU0Q,5231
3
3
  umnetdb_utils/umnetdb.py,sha256=2jPsZsJWFtwWKIRqww86B8tRWBQ0cbw3iOk9PwAXaxY,1948
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.0.dist-info/METADATA,sha256=JTcsp2paNp2hNZowNqNbfGyZIx23TKym5x3pVtz5GBc,750
9
- umnetdb_utils-0.1.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
10
- umnetdb_utils-0.1.0.dist-info/RECORD,,
8
+ umnetdb_utils-0.1.1.dist-info/METADATA,sha256=T0ycl5URR_p5V8qWkm-IBk1VuDLsvEkwGZbUMYZFm-M,1521
9
+ umnetdb_utils-0.1.1.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
10
+ umnetdb_utils-0.1.1.dist-info/RECORD,,
@@ -1,22 +0,0 @@
1
- Metadata-Version: 2.3
2
- Name: umnetdb-utils
3
- Version: 0.1.0
4
- Summary: Helper classes for querying UMnet databases
5
- License: MIT
6
- Author: Amy Liebowitz
7
- Author-email: amylieb@umich.edu
8
- Requires-Python: >=3.10
9
- Classifier: License :: OSI Approved :: MIT License
10
- Classifier: Programming Language :: Python :: 3
11
- Classifier: Programming Language :: Python :: 3.10
12
- Classifier: Programming Language :: Python :: 3.11
13
- Classifier: Programming Language :: Python :: 3.12
14
- Classifier: Programming Language :: Python :: 3.13
15
- Requires-Dist: oracledb (>=3.1.0,<4.0.0)
16
- Requires-Dist: psycopg[binary] (>=3.2.9,<4.0.0)
17
- Requires-Dist: sqlalchemy (>=2.0.41,<3.0.0)
18
- Description-Content-Type: text/markdown
19
-
20
- # umnetdb-utils
21
- Helper classes for gathering data from umnet databases
22
-