def configure_environ(dsn_env_name='PROM_DSN', connection_class=DsnConnection): """ configure interfaces based on environment variables by default, when prom is imported, it will look for PROM_DSN, and PROM_DSN_N (where N is 1 through infinity) in the environment, if it finds them, it will assume they are dsn urls that prom understands and will configure db connections with them. If you don't want this behavior (ie, you want to configure prom manually) then just make sure you don't have any environment variables with matching names The num checks (eg PROM_DSN_1, PROM_DSN_2) go in order, so you can't do PROM_DSN_1, PROM_DSN_3, because it will fail on _2 and move on, so make sure your num dsns are in order (eg, 1, 2, 3, ...) example -- export PROM_DSN_1=some.Interface://host:port/dbname#i1 export PROM_DSN_2=some.Interface://host2:port/dbname2#i2 $ python >>> import prom >>> print prom.interfaces # prints a dict with interfaces i1 and i2 keys :param dsn_env_name: string, the name of the environment variables """ inters = [] cs = dsnparse.parse_environs(dsn_env_name, parse_class=connection_class) for c in cs: inter = c.interface set_interface(inter, c.name) inters.append(inter) return inters
def test_parse_environs(self): os.environ['ENVIRONS_DSN_1'] = 'scheme://*****:*****@host:1234/foo' os.environ['ENVIRONS_DSN_2'] = 'scheme://*****:*****@host:1234/bar' os.environ['ENVIRONS_DSN_3'] = 'scheme://*****:*****@host:1234/che' rs = dsnparse.parse_environs('ENVIRONS_DSN') self.assertEqual(3, len(rs)) for x in range(1, 4): self.assertEqual(os.environ['ENVIRONS_DSN_{x}'.format(x=x)], rs[x - 1].geturl())
def configure_environ(dsn_env_name='MORP_DSN', connection_class=DsnConnection): """ configure interfaces based on environment variables by default, when morp is imported, it will look for MORP_DSN, and MORP_DSN_N (where N is 1 through infinity) in the environment, if it finds them, it will assume they are dsn urls that morp understands and will configure connections with them. If you don't want this behavior (ie, you want to configure morp manually) then just make sure you don't have any environment variables with matching names The num checks (eg MORP_DSN_1, MORP_DSN_2) go in order, so you can't do MORP_DSN_1, MORP_DSN_3, because it will fail on _2 and move on, so make sure your num dsns are in order (eg, 1, 2, 3, ...) dsn_env_name -- string -- the name of the environment variables prefix """ cs = dsnparse.parse_environs(dsn_env_name, parse_class=connection_class) for c in cs: set_interface(c.interface, c.name)