Skip to content

Commit

Permalink
[Python] Bump python to 3.9 (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
liranbg authored Nov 17, 2024
1 parent 9c5283a commit 5d1b2b7
Show file tree
Hide file tree
Showing 24 changed files with 573 additions and 582 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,47 @@ jobs:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.7
python-version: 3.9

- name: Install dependencies
run: make install-ci
run: make install

- name: Run lint
run: make lint

unit_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.7
python-version: 3.9

- name: Install dependencies
run: make install-ci
run: make install

- name: Run unit test
run: make test-unit

integration_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.7
python-version: 3.9

- name: Install dependencies
run: make install-ci
run: make install

- name: Run integration test
run: make test-integ
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pip-selfcheck.json
*.egg-info
.coverage
.ziggy
.python-version

.idea/*
!.idea/runConfigurations/
12 changes: 2 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ lint: venv fmt-check
.PHONY: fmt
fmt:
@echo "Running black fmt..."
$(VENV_PYTHON) -m black --skip-string-normalization --exclude='.*venv.*' .
$(VENV_PYTHON) -m black --exclude='.*venv.*' .

.PHONY: fmt-check
fmt-check:
@echo "Running black fmt check..."
$(VENV_PYTHON) -m black --skip-string-normalization --check --diff .
$(VENV_PYTHON) -m black --check --diff .

.PHONY: test
test: test-unit test-integ
Expand All @@ -33,13 +33,5 @@ test-integ: venv
install: venv
@echo Installed

.PHONY: install-ci
install-ci: install-venv install

venv:
python ./install --dev

.PHONY: install-venv
install-venv:
python -m pip install --upgrade pip
python -m pip install virtualenv
88 changes: 44 additions & 44 deletions clients/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ def __init__(self, name, level=logging.NOTSET):
# a new Logger instance and adds it to his list
# so we need to add the first error to the manager attributes
# so we can keep the first error in the whole application
if not hasattr(self.manager, 'first_error'):
setattr(self.manager, 'first_error', None)
if not hasattr(self.manager, "first_error"):
setattr(self.manager, "first_error", None)

@property
def first_error(self):
return self.manager.first_error

def clear_first_error(self):
if hasattr(self.manager, 'first_error'):
if hasattr(self.manager, "first_error"):
self.manager.first_error = None

def _check_and_log(self, level, msg, args, kw_args):
if self.isEnabledFor(level):
kw_args.update(self._bound_variables)
self._log(level, msg, args, extra={'vars': kw_args})
self._log(level, msg, args, extra={"vars": kw_args})

def error(self, msg, *args, **kw_args):
if self.manager.first_error is None:
self.manager.first_error = {'msg': msg, 'args': args, 'kw_args': kw_args}
self.manager.first_error = {"msg": msg, "args": args, "kw_args": kw_args}

self._check_and_log(helpers.Severity.Error, msg, args, kw_args)

Expand All @@ -70,22 +70,22 @@ def __init__(self, logger_instance):

def __call__(self, event_info):
try:
if event_info['isError'] == 1:
if event_info["isError"] == 1:
try:
self.logger_instance.error(
'Unhandled exception in deferred',
failure=str(event_info['failure']).replace('\n', '\n\r'),
"Unhandled exception in deferred",
failure=str(event_info["failure"]).replace("\n", "\n\r"),
traceback=str(
event_info['failure'].getBriefTraceback()
).replace('\n', '\n\r'),
event_info["failure"].getBriefTraceback()
).replace("\n", "\n\r"),
)
except Exception:
pass

try:
if len(event_info['message']) > 0:
if len(event_info["message"]) > 0:
self.logger_instance.error(
str(event_info['message']).replace('\n', '\n\r')
str(event_info["message"]).replace("\n", "\n\r")
)
except Exception:
pass
Expand All @@ -105,7 +105,7 @@ def __init__(
max_log_size_mb=5,
max_num_log_files=3,
log_file_name=None,
log_colors='on',
log_colors="on",
):

# disabled - this hijacks stdout and adds RESETCOLOR at the end regardless if we are on atty or not
Expand Down Expand Up @@ -134,9 +134,9 @@ def __init__(
# on - disable colors if stdout is not a tty
# always - never disable colors
# off - always disable colors
if log_colors == 'off':
if log_colors == "off":
enable_colors = False
elif log_colors == 'always':
elif log_colors == "always":
enable_colors = True
else: # on - colors when stdout is a tty
enable_colors = sys.stdout.isatty()
Expand All @@ -154,9 +154,9 @@ def __init__(

if output_dir is not None:
log_file_name = (
name.replace('-', '.')
name.replace("-", ".")
if log_file_name is None
else log_file_name.replace('.log', '')
else log_file_name.replace(".log", "")
)
self.enable_log_file_writing(
output_dir,
Expand Down Expand Up @@ -192,12 +192,12 @@ def enable_log_file_writing(
for h in self.logger.handlers
):
helpers.make_dir_recursively(output_dir)
log_path = os.path.join(output_dir, '{0}.log'.format(log_file_name))
log_path = os.path.join(output_dir, "{0}.log".format(log_file_name))

# Creates the log file if it doesn't already exist.
rotating_file_handler = logging.handlers.RotatingFileHandler(
log_path,
mode='a+',
mode="a+",
maxBytes=max_log_size_mb * 1024 * 1024,
backupCount=max_num_log_files,
)
Expand All @@ -217,55 +217,55 @@ def register_arguments(parser):
:param parser: The argparser
"""
parser.add_argument(
'--log-severity',
help='Set log severity',
"--log-severity",
help="Set log severity",
choices=helpers.Severity.string_enum_dict.keys(),
default='debug',
default="debug",
)

# old-style abbreviation log-level for backwards compatibility
parser.add_argument(
'--log-console-severity',
help='Defines severity of logs printed to console',
"--log-console-severity",
help="Defines severity of logs printed to console",
choices=helpers.Severity.string_enum_dict.keys(),
default='debug',
default="debug",
)

# old-style abbreviation log-level for backwards compatibility
parser.add_argument(
'--log-file-severity',
help='Defines severity of logs printed to file',
"--log-file-severity",
help="Defines severity of logs printed to file",
choices=helpers.Severity.string_enum_dict.keys(),
default='debug',
default="debug",
)

parser.add_argument(
'--log-disable-stdout',
help='Disable logging to stdout',
action='store_true',
"--log-disable-stdout",
help="Disable logging to stdout",
action="store_true",
)
parser.add_argument('--log-output-dir', help='Log files directory path')
parser.add_argument("--log-output-dir", help="Log files directory path")
parser.add_argument(
'--log-file-rotate-max-file-size', help='Max log file size', default=5
"--log-file-rotate-max-file-size", help="Max log file size", default=5
)
parser.add_argument(
'--log-file-rotate-num-files', help='Num of log files to keep', default=5
"--log-file-rotate-num-files", help="Num of log files to keep", default=5
)
parser.add_argument(
'--log-file-name',
"--log-file-name",
help=(
'Override to filename (instead of deriving it from the logger name. '
'e.g. [node_name].[service_name].[service_instance].log'
"Override to filename (instead of deriving it from the logger name. "
"e.g. [node_name].[service_name].[service_instance].log"
),
)
parser.add_argument(
'--log-colors',
"--log-colors",
help=(
'CLI friendly color control. default is on (color when stdout+tty). '
'You can also force always/off.'
"CLI friendly color control. default is on (color when stdout+tty). "
"You can also force always/off."
),
choices=['on', 'off', 'always'],
default='on',
choices=["on", "off", "always"],
default="on",
)


Expand All @@ -274,5 +274,5 @@ class TestingClient(Client):
An override of the logging client with defaults suitable for testing
"""

def __init__(self, name='test', initial_severity='debug'):
super(TestingClient, self).__init__(name, initial_severity, log_colors='always')
def __init__(self, name="test", initial_severity="debug"):
super(TestingClient, self).__init__(name, initial_severity, log_colors="always")
30 changes: 15 additions & 15 deletions clients/logging/formatter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ class Severity(object):
Error = logging.ERROR

string_enum_dict = {
'verbose': Verbose,
'debug': Debug,
'info': Info,
'warn': Warning,
'warning': Warning,
"verbose": Verbose,
"debug": Debug,
"info": Info,
"warn": Warning,
"warning": Warning,
# Allow abbreviations
# Also provides backwards compatibility with log-console/file-severity syntax
'V': Verbose,
'D': Debug,
'I': Info,
'W': Warning,
'E': Error,
"V": Verbose,
"D": Debug,
"I": Info,
"W": Warning,
"E": Error,
}

@staticmethod
Expand All @@ -51,15 +51,15 @@ def format_to_json_str(params):

# this is the widest complementary encoding found
return simplejson.dumps(
params, cls=ObjectEncoder, encoding='raw_unicode_escape'
params, cls=ObjectEncoder, encoding="raw_unicode_escape"
)

def format(self, record):
params = {
'datetime': self.formatTime(record, self.datefmt),
'name': record.name,
'level': record.levelname.lower(),
'message': record.getMessage(),
"datetime": self.formatTime(record, self.datefmt),
"name": record.name,
"level": record.levelname.lower(),
"message": record.getMessage(),
}

params.update(record.vars)
Expand Down
Loading

0 comments on commit 5d1b2b7

Please sign in to comment.