From 1ee33264315c1bd3946f73dea2194553409b04fe Mon Sep 17 00:00:00 2001 From: root Date: Wed, 27 Jan 2021 10:47:48 +0300 Subject: [PATCH] Adding newline character in raw format was fixed. --- calculate/server/server.py | 10 +++++++--- calculate/templates/format/base_format.py | 1 + calculate/templates/format/raw_format.py | 14 ++++++++------ calculate/utils/files.py | 6 +++--- calculate/utils/package.py | 2 +- tests/templates/test_directory_processor.py | 6 +++--- .../db/pkg/test-category/test-package-1.0/CONTENTS | 1 + 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/calculate/server/server.py b/calculate/server/server.py index 9e3b6af..b2733eb 100644 --- a/calculate/server/server.py +++ b/calculate/server/server.py @@ -2,8 +2,12 @@ from ..variables.loader import Datavars from ..commands.commands import Command from ..logging import dictLogConfig from logging.config import dictConfig +from typing import ( + Callable, + Optional, + NoReturn + ) from logging import getLogger -from typing import Callable from fastapi import FastAPI from .worker import Worker import importlib @@ -119,13 +123,13 @@ class Server: # Обработчики сообщений воркеров. # Вспомогательные методы. - def _add_routes(self, method: Callable, routes: dict) -> None: + def _add_routes(self, method: Callable, routes: dict) -> NoReturn: '''Метод для добавления методов.''' for path, handler in routes.items(): router = method(path) router(handler) - def _make_worker(self, wid: int = None): + def _make_worker(self, wid: Optional[int] = None): '''Метод для создания воркера для команды.''' if wid is not None: self._workers[wid] = Worker(wid, self._event_loop) diff --git a/calculate/templates/format/base_format.py b/calculate/templates/format/base_format.py index ba249cc..3630fa7 100644 --- a/calculate/templates/format/base_format.py +++ b/calculate/templates/format/base_format.py @@ -387,6 +387,7 @@ class Format: template_paths.append(template_path) header = self._make_header(template_paths) + print(header) document_text = re.sub(header_pattern, '', input_text) if check_shebang: diff --git a/calculate/templates/format/raw_format.py b/calculate/templates/format/raw_format.py index 335c96c..b6b335e 100644 --- a/calculate/templates/format/raw_format.py +++ b/calculate/templates/format/raw_format.py @@ -38,17 +38,19 @@ class RawFormat(Format): self._document_text) def join_template(self, template): + sep = "" if self._before: + if (template._document_text + and not template._document_text.endswith("\n")): + sep = "\n" self._document_text = '{0}{1}{2}'.format( - template._document_text, - "" if template._document_text. - endswith("\n") else "\n", + template._document_text, sep, self._document_text) else: + if self._document_text and not self._document_text.endswith("\n"): + sep = "\n" self._document_text = '{0}{1}{2}'.format( - self._document_text, - "" if self._document_text. - endswith("\n") else "\n", + self._document_text, sep, template._document_text) def __bool__(self): diff --git a/calculate/utils/files.py b/calculate/utils/files.py index e96b94a..afd9ebc 100644 --- a/calculate/utils/files.py +++ b/calculate/utils/files.py @@ -71,7 +71,7 @@ class Process: self._process = None self._iterator = iter([]) - # Flags. + # Флаги. self._opened = False self._is_read = False @@ -79,12 +79,12 @@ class Process: self._writable = False self._readable_errors = False - # I/O handlers. + # I/O обработчики. self.stdin_handler = None self.stdout_handler = None self.stderr_handler = None - # Caches. + # Кэши. self._output_cache = '' self._error_cache = '' diff --git a/calculate/utils/package.py b/calculate/utils/package.py index bbcdbc7..f8829f9 100644 --- a/calculate/utils/package.py +++ b/calculate/utils/package.py @@ -87,10 +87,10 @@ class PackageCreator(type): class Version: + '''Класс объектов представляющих значения версий.''' _suffix_order = {'alpha': 0, 'beta': 1, 'pre': 2, 'rc': 3, 'no': 4, 'p': 5} - '''Класс для работы со значениями версий.''' def __init__(self, version_value: Union["Version", None, int, float, str] = None): if version_value is None: diff --git a/tests/templates/test_directory_processor.py b/tests/templates/test_directory_processor.py index 9cc5fd4..9566f31 100644 --- a/tests/templates/test_directory_processor.py +++ b/tests/templates/test_directory_processor.py @@ -1506,7 +1506,7 @@ class TestDirectoryProcessor: '/etc/dir_61/._cfg0000_file_0'), 'r') as new_file: new_text = new_file.read() - assert new_text == '\nnew content\n' + assert new_text == 'new content\n' def test_current_template_variable(self): datavars.main['cl_template_path'] = os.path.join(CHROOT_PATH, @@ -1518,7 +1518,7 @@ class TestDirectoryProcessor: '# Processed template files:\n' + f'# {template_path}\n' + '#' + '-' * 79 + - f'\n\ntemplate = {template_path}\n') + f'\ntemplate = {template_path}\n') directory_processor = DirectoryProcessor( 'install', datavars_module=datavars, @@ -1803,7 +1803,7 @@ class TestDirectoryProcessor: with open(join_paths(CHROOT_PATH, '/etc/file_23'), 'r') as target_file: target_text = target_file.read() - assert target_text == "joy division -- new dawn fades" + assert target_text == "joy division -- new dawn fades\n" mode = os.stat(join_paths(CHROOT_PATH, '/etc/file_23')).st_mode assert int(mode) == 0o100645 diff --git a/tests/templates/testfiles/test_dir_processor_root/var.backup/db/pkg/test-category/test-package-1.0/CONTENTS b/tests/templates/testfiles/test_dir_processor_root/var.backup/db/pkg/test-category/test-package-1.0/CONTENTS index 7887488..0dbd5fe 100644 --- a/tests/templates/testfiles/test_dir_processor_root/var.backup/db/pkg/test-category/test-package-1.0/CONTENTS +++ b/tests/templates/testfiles/test_dir_processor_root/var.backup/db/pkg/test-category/test-package-1.0/CONTENTS @@ -8,6 +8,7 @@ dir /etc/dir_60 obj /etc/dir_60/file_0 c9a9459e4266ea35a612b90dc3653112 1593525253 obj /etc/file_19 3fd436479300b04370b97f4bcfdc90f7 1592574626 obj /etc/file_20 3fd436479300b04370b97f4bcfdc90f7 1592574626 +obj /etc/file_23 c18d75dcdbe03b64a461dc708b5b1a80 1592574626 dir /etc/dir_76 obj /etc/dir_76/file_0 3fd436479300b04370b97f4bcfdc90f7 1592574626 dir /etc/dir_77