Skip to content

Commit

Permalink
Merge pull request #6 from BurtBiel/burtbiel/FixTests
Browse files Browse the repository at this point in the history
Unblock tests so they all run and pass
  • Loading branch information
johanste committed Feb 24, 2016
2 parents 85a9c17 + 3720c54 commit eb65817
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 51 deletions.
8 changes: 4 additions & 4 deletions src/azure/cli/_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys

from ._locale import get_file as locale_get_file
from ._logging import logging
from ._logging import logger

# Named arguments are prefixed with one of these strings
ARG_PREFIXES = sorted(('-', '--', '/'), key=len, reverse=True)
Expand Down Expand Up @@ -39,7 +39,7 @@ def __getattr__(self, key):
return self[key]
except LookupError:
pass
logging.debug('Argument %s is required', key)
logger.debug('Argument %s is required', key)
raise IncorrectUsageError(_("Argument {0} is required").format(key))

def _read_arg(string):
Expand Down Expand Up @@ -165,7 +165,7 @@ def not_global(a):
expected_kwargs = m['$kwargs']
handler = m['$handler']
except LookupError:
logging.debug('Missing data for noun %s', n)
logger.debug('Missing data for noun %s', n)
show_usage = True

if show_completions:
Expand Down Expand Up @@ -244,7 +244,7 @@ def _display_usage(self, nouns, noun_map, arguments, out=sys.stdout):
# TODO: Behave better when no docs available
print('No documentation available', file=out)
out.flush()
logging.debug('Expected documentation at %s', doc_file)
logger.debug('Expected documentation at %s', doc_file)

def _display_completions(self, nouns, noun_map, arguments, out=sys.stdout):
completions = [k for k in noun_map if not k.startswith('$')]
Expand Down
36 changes: 5 additions & 31 deletions src/azure/cli/_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,7 @@

__all__ = ['logging', 'configure_logging']

_CODE_LEVEL = _logging.INFO + 1

class Logger(_logging.Logger):
def __init__(self, name, level = _logging.NOTSET):
super(Logger, self).__init__(name, level)

def code(self, msg, *args):
self._log(_CODE_LEVEL, msg, args)

logging = Logger('az', _logging.WARNING)

class PyFileFormatter(_logging.Formatter):
def __init__(self):
super(PyFileFormatter, self).__init__('# %(levelname)s: %(message)s')
self.info_style = _logging.PercentStyle('%(message)s')

def format(self, record):
assert isinstance(record, _logging.LogRecord)
if record.levelno == _CODE_LEVEL:
return record.getMessage()
return super(PyFileFormatter, self).format(record)
logger = _logging.Logger('az', _logging.WARNING)

def _arg_name(arg):
a = arg.lstrip('-/')
Expand Down Expand Up @@ -65,18 +45,12 @@ def configure_logging(argv, config):
# Configure the console output handler
stderr_handler = _logging.StreamHandler(sys.stderr)
stderr_handler.formatter = _logging.Formatter('%(levelname)s: %(message)s')
logging.level = stderr_handler.level = level
logging.handlers.append(stderr_handler)
logger.level = stderr_handler.level = level
logger.handlers.append(stderr_handler)

if logfile and logfile.lower().endswith('.py'):
# Configure a handler that logs code to a Python script
py_handler = _logging.StreamHandler(open(logfile, 'w', encoding='utf-8'))
py_handler.formatter = PyFileFormatter()
py_handler.level = level if level == _logging.DEBUG else _logging.INFO
logging.handlers.append(py_handler)
elif logfile:
if logfile:
# Configure the handler that logs code to a text file
log_handler = _logging.StreamHandler(open(logfile, 'w', encoding='utf-8'))
log_handler.formatter = _logging.Formatter('[%(levelname)s:%(asctime)s] %(message)s')
log_handler.level = level if level == _logging.DEBUG else _logging.INFO
logging.handlers.append(log_handler)
logger.handlers.append(log_handler)
8 changes: 4 additions & 4 deletions src/azure/cli/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .._argparse import IncorrectUsageError
from .._logging import logging
from .._logging import logger

# TODO: Alternatively, simply scan the directory for all modules
COMMAND_MODULES = [
Expand All @@ -12,21 +12,21 @@
def command(name):
def add_command(handler):
_COMMANDS.setdefault(handler, {})['name'] = name
logging.debug('Added %s as command "%s"', handler, name)
logger.debug('Added %s as command "%s"', handler, name)
return handler
return add_command

def description(description):
def add_description(handler):
_COMMANDS.setdefault(handler, {})['description'] = description
logging.debug('Added description "%s" to %s', description, handler)
logger.debug('Added description "%s" to %s', description, handler)
return handler
return add_description

def option(spec, description=None):
def add_option(handler):
_COMMANDS.setdefault(handler, {}).setdefault('args', []).append((spec, description))
logging.debug('Added option "%s" to %s', spec, handler)
logger.debug('Added option "%s" to %s', spec, handler)
return handler
return add_option

Expand Down
2 changes: 1 addition & 1 deletion src/azure/cli/commands/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SubscriptionClientConfiguration
from msrestazure.azure_active_directory import UserPassCredentials

from .._logging import logging
from .._logging import logger
from .._profile import Profile
from .._util import TableOutput
from ..commands import command, description, option
Expand Down
10 changes: 2 additions & 8 deletions src/azure/cli/commands/storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..main import CONFIG, SESSION
from .._logging import logging
from .._logging import logger
from .._util import TableOutput
from ..commands import command, description, option
from .._profile import Profile
Expand All @@ -21,10 +21,8 @@ def list_accounts(args, unexpected):

group = args.get('resource-group')
if group:
logging.code('accounts = smc.storage_accounts.list_by_resource_group(%r)', group)
accounts = smc.storage_accounts.list_by_resource_group(group)
else:
logging.code('accounts = smc.storage_accounts.list()')
accounts = smc.storage_accounts.list()

with TableOutput() as to:
Expand All @@ -42,11 +40,7 @@ def list_accounts(args, unexpected):
def checkname(args, unexpected):
from azure.mgmt.storage import StorageManagementClient, StorageManagementClientConfiguration

logging.code('''smc = StorageManagementClient(StorageManagementClientConfiguration())
smc.storage_accounts.check_name_availability({0.account_name!r})
'''.format(args))

smc = StorageManagementClient(StorageManagementClientConfiguration())
logging.warn(smc.storage_accounts.check_name_availability(args.account_name))
logger.warn(smc.storage_accounts.check_name_availability(args.account_name))


4 changes: 2 additions & 2 deletions src/azure/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ._argparse import ArgumentParser
from ._locale import install as locale_install
from ._logging import configure_logging, logging
from ._logging import configure_logging, logger
from ._session import Session

# CONFIG provides external configuration options
Expand Down Expand Up @@ -39,7 +39,7 @@ def main(args):
try:
parser.execute(args)
except RuntimeError as ex:
logging.error(ex.args[0])
logger.error(ex.args[0])
return ex.args[1] if len(ex.args) >= 2 else -1
except KeyboardInterrupt:
return -1
4 changes: 3 additions & 1 deletion src/azure/cli/tests/test_argparse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import unittest

from azure.cli._argparse import ArgumentParser, IncorrectUsageError
from azure.cli._logging import logging
from azure.cli._logging import logger
import logging


class Test_argparse(unittest.TestCase):
@classmethod
Expand Down

0 comments on commit eb65817

Please sign in to comment.