Skip to content

Commit

Permalink
Merge 96f16e7 into 1d9c615
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-maier committed Aug 16, 2020
2 parents 1d9c615 + 96f16e7 commit ad063a8
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/appendix.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ This documentation uses a few special terms to refer to Python types:
This allows connection definitions to be persisted and referenced by name
in pywbemcli commands, via the :ref:`--name general option`.

By default, the connections file is ``pywbemcli_connection_definitions.yaml``
By default, the connections file is ``.pywbemcli_connections.yaml``
in the user's home directory. The user's home directory depends on the
operating system used. It is determined with ``os.path.expanduser("~")``,
which works on all operating systems including Windows.
Expand Down
6 changes: 6 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ Released: not yet
* Resolved remaining Pylint issues and enforced clean pylint checks.
(See issue #668)

* Renamed the default connections file in the user's home directory from
`pywbemcli_connection_definitions.yaml` to `.pywbemcli_connections.yaml`,
because it is really an internal file not meant for being edited.
An existing file with the old name is migrated automatically.
(See issue #716)

**Known issues:**

* See `list of open issues`_.
Expand Down
10 changes: 5 additions & 5 deletions docs/pywbemcli/cmdshelp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ Help text for ``pywbemcli``:
Enable deprecation warnings. Default: EnvVar PYWBEMCLI_DEPRECATION_WARNINGS, or true.
-C, --connections-file FILE PATH
Path name of the connections file to be used. Default: EnvVar
PYWBEMCLI_CONNECTIONS_FILE, or "pywbemcli_connection_definitions.yaml" in the user's
home directory (as determined using Python's os.path.expanduser("~"). See there for
PYWBEMCLI_CONNECTIONS_FILE, or ".pywbemcli_connections.yaml" in the user's home
directory (as determined using Python's os.path.expanduser("~"). See there for
details, particularly for Windows).

--pdb Pause execution in the built-in pdb debugger just before executing the command within
Expand Down Expand Up @@ -622,9 +622,9 @@ Help text for ``pywbemcli connection`` (see :ref:`connection command group`):
general option.

The connection definitions are stored in a connections file. By default, the connections file is
'pywbemcli_connection_definitions.yaml' in the user's home directory. The location of the user's home directory
depends on the operating system used. It is determined with Python's 'os.path.expanduser("~")', which works on all
operating systems including Windows. The default path name of the connections file can be overwritten using the
'.pywbemcli_connections.yaml' in the user's home directory. The location of the user's home directory depends on the
operating system used. It is determined with Python's 'os.path.expanduser("~")', which works on all operating
systems including Windows. The default path name of the connections file can be overwritten using the
'PYWBEMCLI_CONNECTIONS_FILE' environment variable, or with the '--connections-file' general option.

In addition to the command-specific options shown in this help text, the general options (see 'pywbemcli --help')
Expand Down
2 changes: 1 addition & 1 deletion docs/pywbemcli/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,7 @@ current connections file.
pywbemcli> --server http://blahblah connection list
WBEM server connections(brief): (#: default, *: current)
file: /home/johndoe/pywbemcli_connection_definitions.yaml
file: /home/johndoe/.pywbemcli_connections.yaml
+--------------+------------------+----------------------------------------+
| name | server | mock-server |
|--------------+------------------+----------------------------------------|
Expand Down
6 changes: 3 additions & 3 deletions docs/pywbemcli/generaloptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ a path name for the :term:`connections file`.

By default, the path name of the connections file is the value of the
``PYWBEMCLI_CONNECTIONS_FILE`` environment variable or if not set, the file
``pywbemcli_connection_definitions.yaml`` in the user's home directory.
``.pywbemcli_connections.yaml`` in the user's home directory.
The user's home directory depends on the operating system used and is
determined with ``os.path.expanduser("~")``, which works on all operating
systems including Windows. See :func:`~py3:os.path.expanduser` for details.
Expand Down Expand Up @@ -1352,8 +1352,8 @@ Pywbemcli persisted connection definitions

Pywbemcli can manage persisted connection definitions via the
:ref:`Connection command group`. These connection definitions are persisted in
a :term:`connections file` named ``pywbemcli_connection_definitions.yaml`` in
the current directory. A connection definition has a name
a :term:`connections file` named ``.pywbemcli_connections.yaml`` in
the user's home directory. A connection definition has a name
and defines all parameters necessary to connect to a WBEM server. Once defined
these connection definitions can be used with the :ref:`--name general option`
or in the interactive mode by defining a current connection with the
Expand Down
2 changes: 1 addition & 1 deletion pywbemtools/pywbemcli/_cmd_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def connection_group():
'--name' general option.
The connection definitions are stored in a connections file. By default,
the connections file is 'pywbemcli_connection_definitions.yaml' in the
the connections file is '.pywbemcli_connections.yaml' in the
user's home directory. The location of the user's home directory depends on
the operating system used. It is determined with Python's
'os.path.expanduser("~")', which works on all operating systems including
Expand Down
61 changes: 47 additions & 14 deletions pywbemtools/pywbemcli/_connection_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,30 @@
# with the use of the base file name in several other .rst and .py files.

# Base file name of the connections file
CONNECTIONS_FILENAME = 'pywbemcli_connection_definitions.yaml'
# The B08_* file name was used before pywbemcli 0.8.
CONNECTIONS_FILENAME = '.pywbemcli_connections.yaml'
B08_CONNECTIONS_FILENAME = 'pywbemcli_connection_definitions.yaml'

# Path name of default connections file directory.
DEFAULT_CONNECTIONS_DIR = os.path.expanduser("~")

# Path name of default connections file
# The B08_* path name was used before pywbemcli 0.8.
DEFAULT_CONNECTIONS_FILE = os.path.join(DEFAULT_CONNECTIONS_DIR,
CONNECTIONS_FILENAME)
B08_DEFAULT_CONNECTIONS_FILE = os.path.join(DEFAULT_CONNECTIONS_DIR,
B08_CONNECTIONS_FILENAME)

BAK_FILE_SUFFIX = 'bak'


class ConnectionsFileNotFoundError(Exception):
"""
Exception indicating that the connections file was not found.
"""
pass


class ConnectionRepository(object):
# pylint: disable=useless-object-inheritance
"""
Expand Down Expand Up @@ -125,22 +137,29 @@ def default_connection_name(self):

def file_exists(self):
"""
Test if the connection file exists
Test if the connection file exists.
An old connections file is migrated, if needed.
The connections file is loaded as part of the test.
Returns:
(:class:`py:bool`) True if the connections_file exists and False if
it does not exist
"""
return os.path.isfile(self.connections_file)
try:
self._load_connections_file()
return True
except ConnectionsFileNotFoundError:
return False

def __str__(self):
"""
Return a string containing connections file name and count of items in
connections file
"""
if self.file_exists():
# Has loaded the file
status = 'exists'
self._load_connections_file()
length = len(self)
else:
status = "does not exist"
Expand Down Expand Up @@ -210,17 +229,32 @@ def iterkeys(self):

def _load_connections_file(self):
"""
If there is a file, read it in and install into the dictionary.
This file is read only once.
If the connections file exists, read it in and install into the
dictionary.
Raises:
IOError: repository does not exist
An old connections file is migrated, if needed.
Repeated calls to this method will load the file only once.
ValueError: Error in reading repository
Raises:
ConnectionsFileNotFoundError: Connections file not found
IOError: Cannot rename old connections file
ValueError,KeyError,TypeError: Error in connections file
"""
if self._loaded:
return

# Migrate an old connections file
if self._connections_file == DEFAULT_CONNECTIONS_FILE and \
not os.path.isfile(self._connections_file) and \
os.path.isfile(B08_DEFAULT_CONNECTIONS_FILE):

# May raise IOError:
os.rename(B08_DEFAULT_CONNECTIONS_FILE, DEFAULT_CONNECTIONS_FILE)

click.echo("Migrated old connections file {!r} to {!r}".
format(B08_DEFAULT_CONNECTIONS_FILE,
DEFAULT_CONNECTIONS_FILE))

if os.path.isfile(self._connections_file):
with self._open_file(self._connections_file, 'r') as _fp:
try:
Expand Down Expand Up @@ -252,11 +286,10 @@ def _load_connections_file(self):
raise ValueError("Invalid YAML in connections file {0}. "
"Exception {1}".format
(self._connections_file, ve))
# Apply IO error for non-existent file
# TODO. IOError can also mean disk full in general
else:
raise IOError("Connections file {} does not exist.".
format(self.connections_file))
raise ConnectionsFileNotFoundError(
"Connections file does not exist: {0}".
format(self.connections_file))

def add(self, name, svr_definition):
"""
Expand Down Expand Up @@ -440,6 +473,6 @@ def get_default_connection_name(self):
"""
if self.file_exists():
self._load_connections_file()
# Has loaded the file
return self.default_connection_name
return None
2 changes: 1 addition & 1 deletion pywbemtools/pywbemcli/pywbemcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def validate_connections_file(connections_repo):
Abort click if file does not exist
"""
if not connections_repo.file_exists():
click.echo('Connections file: "{}" does not exist.'.format(
click.echo('Connections file does not exist: {}'.format(
connections_repo.connections_file), err=True)
raise click.Abort()

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_general_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ class Command group for CIM classes.
'./filenotfound.yaml'],
'cmdgrp': 'connection',
'args': ['show']},
{'stderr': ['Connections file: "./filenotfound.yaml" does not exist.',
{'stderr': ['Connections file does not exist: ./filenotfound.yaml',
'Aborted!'],
'rc': 1,
'test': 'innows'},
Expand All @@ -485,7 +485,7 @@ class Command group for CIM classes.
'./filenotfound.yaml'],
'cmdgrp': 'connection',
'args': ['show']},
{'stderr': ['Connections file: "./filenotfound.yaml" does not exist.',
{'stderr': ['Connections file does not exist: ./filenotfound.yaml',
'Aborted!'],
'rc': 1,
'test': 'innows'},
Expand Down

0 comments on commit ad063a8

Please sign in to comment.