Beispiel #1
0
def main():
    parser = argparse.ArgumentParser(description='Manage your environment.')
    parser.add_argument(
        '-f', '--file', metavar='<FILE>', type=str, action='store',
        help='File to load variables from')
    parser.add_argument(
        '-d', '--directory', metavar='<DIR>', type=str, action='store',
        help='Directory to load variables from, works just like `envparse\'')

    subparsers = parser.add_subparsers(title='subcommands', metavar='COMMAND')
    get_parser = subparsers.add_parser(
        'get',
        help='Get and print out the value of the variable <VAR>')
    get_parser.add_argument('cmd_get')

    get_uri_parser = subparsers.add_parser(
        'get-uri', help='Exposes the URI parser API')
    get_uri_parser.add_argument('cmd_get_uri', nargs=2)

    args = parser.parse_args()

    if args.file:
        env = Environment.from_file(args.file)
    if args.directory:
        env = Environment.from_folder(args.directory)

    if hasattr(args, 'cmd_get'):
        return env.get(args.cmd_get)

    if hasattr(args, 'cmd_get_uri'):
        part, variable = args.cmd_get_uri
        return getattr(env.get_uri(variable), part)
Beispiel #2
0
def test_milieu_environment_from_directory_set(_io):
    # Given that I load variables to my env from a folder
    env = Environment.from_folder(
        os.path.join(os.path.dirname(__file__), './fixtures/env'))

    # When I set some stuff
    env.set('CITY', 'NEW-YORK')

    # Then I see that we always try to write the file
    _io.open.return_value.write.assert_called_once_with('NEW-YORK')
Beispiel #3
0
def test_milieu_environment_from_directory_del(_os, _io):
    # Given that I have a folder environment with an item `CITY`
    env = Environment.from_folder('./path')
    env.set('CITY', 'NEW-YORK')

    # We need the path.join function over there, so we need to restore it
    # manually
    _os.path.join.side_effect = os.path.join

    # When I remove that item
    del env['CITY']

    # Then I can see that the unlink function was called properly
    _os.unlink.assert_called_once_with('./path/CITY')
Beispiel #4
0
def test_merge_folder_environment():
    "Environment.update() Should update a folder based environment with data from another one"

    # Given that I load variables to my env from a folder
    path = os.path.join(os.path.dirname(__file__), './fixtures/env')
    environment = Environment.from_folder(path)

    # When I merge another environment into the current one that *does
    # not* have the variable CITY
    environment.update(Environment(storage={'CITY': 'Porto Alegre'}))

    # Then I see that the old value was overwritten
    environment.get('CITY').should.equal('Porto Alegre')

    # And then I just cleanup the file I created above
    del environment['CITY']
Beispiel #5
0
def test_milieu_environment_from_directory_set():
    # Given that I load variables to my env from a folder
    path = os.path.join(os.path.dirname(__file__), './fixtures/env')
    env = Environment.from_folder(path)

    # When I set a new variable to my folder env
    env.set('CITY', 'NEW-YORK')

    # Then I see the file was created with the right content
    target = os.path.join(path, 'CITY')
    os.path.exists(target).should.be.true

    # And then I see that the value is also right
    open(target).read().should.equal('NEW-YORK')

    # And then I see that after removing the item, the file will also go away
    del env['CITY']
    os.path.exists(target).should.be.false
Beispiel #6
0
def test_milieu_environment_from_directory():
    # Given that I load variables to my env from a folder
    env = Environment.from_folder(
        os.path.join(os.path.dirname(__file__), './fixtures/env'))

    # When I try to list all the variables inside of that folder
    sorted(env.items(), key=lambda x: x[0]).should.equal([
        ('ENABLE_SOMETHING', u''),
        ('PI', u'3.14'),
        ('SERVER_URI', u'smtp://[email protected]:[email protected]:25'),
    ])

    # When I try to find the variables, then I see they're there correctly
    env.get_bool('ENABLE_SOMETHING').should.be.false
    env.get_bool('ENABLE_SOMETHING_ELSE', True).should.be.true
    env.get_float('PI').should.equal(3.14)
    env.get_uri('SERVER_URI').host.should.equal('mserver.com')
    env.get_uri('SERVER_URI').user.should.equal('*****@*****.**')
Beispiel #7
0
def test_milieu_environment_from_directory_items(_os, _io):
    # Given that I load variables to my env from a folder
    env = Environment.from_folder(
        os.path.join(os.path.dirname(__file__), './fixtures/env'))

    _os.listdir.return_value = ['ENABLE_SOMETHING', 'PI', 'SERVER_URI']
    _io.open.return_value.read.side_effect = [
        '',
        '3.14',
        'smtp://[email protected]:[email protected]:25',
    ]

    # When I try to list all the variables inside of that folder
    sorted(env.items(), key=lambda x: x[0]).should.equal([
        ('ENABLE_SOMETHING', u''),
        ('PI', u'3.14'),
        ('SERVER_URI', u'smtp://[email protected]:[email protected]:25'),
    ])
Beispiel #8
0
def test_milieu_environment_from_directory_get(_os, _io):
    # Given that I load variables to my env from a folder
    env = Environment.from_folder(
        os.path.join(os.path.dirname(__file__), './fixtures/env'))

    _os.listdir.return_value = ['ENABLE_SOMETHING', 'PI', 'SERVER_URI']
    _io.open.return_value.read.side_effect = [
        '',
        IOError,
        '3.14',
        'smtp://[email protected]:[email protected]:25',
        'smtp://[email protected]:[email protected]:25',
    ]

    # When I try to find the variables, then I see they're there correctly
    env.get_bool('ENABLE_SOMETHING').should.be.false
    env.get_bool('ENABLE_SOMETHING_ELSE', True).should.be.true
    env.get_float('PI').should.equal(3.14)
    env.get_uri('SERVER_URI').host.should.equal('mserver.com')
    env.get_uri('SERVER_URI').user.should.equal('*****@*****.**')