Skip to content

Commit

Permalink
Merge pull request #7 from vroncevic/dev
Browse files Browse the repository at this point in the history
[gen_coap_service] updated README.md
  • Loading branch information
vroncevic authored Jul 1, 2021
2 parents ced9c90 + c328c4d commit 2ac4019
Show file tree
Hide file tree
Showing 13 changed files with 678 additions and 78 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<img align="right" src="https://raw.githubusercontent.com/vroncevic/gen_coap_service/dev/docs/gen_coap_service_logo.png" width="25%">

# Generate Unnamed Pipe
# Generate CoAP modules

**gen_coap_service** is tool for generation of CoAP modules.

Expand Down
Empty file modified gen_coap_service/.editorconfig
100755 → 100644
Empty file.
114 changes: 64 additions & 50 deletions gen_coap_service/__init__.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# gen_coap_service.py
# Copyright (C) 2018 Vladimir Roncevic <elektron.ronca@gmail.com>
#
# gen_coap_service is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# gen_coap_service is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

"""
Module
__init__.py
Copyright
Copyright (C) 2020 Vladimir Roncevic <elektron.ronca@gmail.com>
gen_coap_service is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
gen_coap_service is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
Info
Define class GenCoAPService with attribute(s) and method(s).
Load a settings, create a CL interface and run operation(s).
"""

import sys

try:
from pathlib import Path

from gen_coap_service.pro.gen_pro import GenPro
from ats_utilities.cfg_base import CfgBase
from ats_utilities.console_io.error import error_message
from ats_utilities.console_io.verbose import verbose_message
from ats_utilities.console_io.success import success_message
except ImportError as e:
msg = "\n{0}\n{1}\n".format(__file__, e)
sys.exit(msg) # Force close python ATS ##################################
except ImportError as error:
MESSAGE = "\n{0}\n{1}\n".format(__file__, error)
sys.exit(MESSAGE) # Force close python ATS ##############################

__author__ = 'Vladimir Roncevic'
__copyright__ = 'Copyright 2018, Free software to use and distributed it.'
__copyright__ = 'Copyright 2020, Free software to use and distributed it.'
__credits__ = ['Vladimir Roncevic']
__license__ = 'GNU General Public License (GPL)'
__version__ = '1.0.0'
Expand All @@ -40,52 +43,55 @@
__status__ = 'Updated'


class gen_coap_service(CfgBase):
class GenCoAPService(CfgBase):
"""
Define class gen_coap_service with attribute(s) and method(s).
Define class GenCoAPService with attribute(s) and method(s).
Load a settings, create a CL interface and run operation(s).
It defines:
attribute:
__slots__ - Setting class slots
VERBOSE - Console text indicator for current process-phase
__CONFIG - Configuration file path
__OPS - Tool options (list)
method:
__init__ - Initial constructor
process - Process and run tool option(s)
:attributes:
| __slots__ - Setting class slots
| VERBOSE - Console text indicator for current process-phase
| __CONFIG - Configuration file path
| __OPS - Tool options (list)
:methods:
| __init__ - Initial constructor
| process - Process and run tool option(s)
"""

__slots__ = (
'VERBOSE',
'__CONFIG',
'__OPS'
)
VERBOSE = 'gen_coap_service'
__slots__ = ('VERBOSE', '__CONFIG', '__OPS')
VERBOSE = 'GEN_COAP_SERVICE'
__CONFIG = '/../conf/gen_coap_service.cfg'
__OPS = ['-g', '--gen', '-h', '--version', '--verbose']

def __init__(self, verbose=False):
"""
Loading configuration and setting argument options.
:param verbose: Enable/disable verbose option
:type verbose: <bool>
:exceptions: None
"""
verbose_message(gen_coap_service.VERBOSE, verbose, 'Initial configuration')
verbose_message(
GenCoAPService.VERBOSE, verbose, 'Initial configuration'
)
current_dir = Path(__file__).resolve().parent
base_config_file = "{0}{1}".format(current_dir, gen_coap_service.__CONFIG)
base_config_file = "{0}{1}".format(
current_dir, GenCoAPService.__CONFIG
)
CfgBase.__init__(self, base_config_file, verbose=verbose)
if self.tool_status:
self.add_new_option(
gen_coap_service.__OPS[0],
gen_coap_service.__OPS[1],
GenCoAPService.__OPS[0],
GenCoAPService.__OPS[1],
dest='option',
help='Option tool'
)

def process(self, verbose=False):
"""
Process and run operation.
:param verbose: Enable/disable verbose option
:type verbose: <bool>
:return: True (success) | False
Expand All @@ -97,18 +103,26 @@ def process(self, verbose=False):
num_of_args_sys = len(sys.argv)
if num_of_args_sys > 1:
option = sys.argv[1]
if option not in gen_coap_service.__OPS:
if option not in GenCoAPService.__OPS:
sys.argv = []
sys.argv.append('-h')
else:
sys.argv.append('-h')
opts, args = self.parse_args(sys.argv)
#
# Code goes here
#
success_message(self.name, 'Done\n')
status = True
opts, script = self.parse_args(sys.argv)
if all([bool(script), bool(opts.gen), not exists(opts.gen)]):
success_message(
GenCoAPService.VERBOSE, 'Generating project', opts.gen
)
generator, gen_status = GenPro(opts.gen), False
gen_status = generator.gen_project()
if gen_status:
success_message(GenCoAPService.VERBOSE, 'Done\n')
status = True
else:
error_message(
GenCoAPService.VERBOSE, 'Failed to generate project'
)
else:
error_message('gen_coap_service', 'Tool is not operational')
error_message(GenCoAPService.VERBOSE, 'Tool is not operational')
return True if status else False

Empty file modified gen_coap_service/conf/.editorconfig
100755 → 100644
Empty file.
Empty file modified gen_coap_service/conf/gen_coap_service.cfg
100755 → 100644
Empty file.
4 changes: 0 additions & 4 deletions gen_coap_service/conf/gen_coap_service_utils.cfg
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
ats_name = gen_coap_service
ats_version = 1.0
ats_build_date = 2020-01-31
ats_license = Vladimir Roncevic GPLv3
3 changes: 3 additions & 0 deletions gen_coap_service/pro/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[**.py]
indent_style = tab
tab_width = 4
5 changes: 5 additions & 0 deletions gen_coap_service/pro/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: UTF-8 -*-

"""
Generator project structure.
"""
165 changes: 165 additions & 0 deletions gen_coap_service/pro/gen_pro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# -*- coding: UTF-8 -*-

"""
Module
gen_pro.py
Copyright
Copyright (C) 2020 Vladimir Roncevic <elektron.ronca@gmail.com>
gen_coap_service is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
gen_coap_service is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
Info
Define class GenPro with attribute(s) and method(s).
Generate project by template and parameters.
"""

import sys
from inspect import stack

try:
from pathlib import Path
from gen_coap_service.pro.read_template import ReadTemplate
from gen_coap_service.pro.write_template import WriteTemplate
from ats_utilities.config.cfg.cfg2object import Cfg2Object
from ats_utilities.console_io.success import success_message
from ats_utilities.console_io.verbose import verbose_message
from ats_utilities.exceptions.ats_type_error import ATSTypeError
from ats_utilities.exceptions.ats_bad_call_error import ATSBadCallError
except ImportError as error:
MESSAGE = "\n{0}\n{1}\n".format(__file__, error)
sys.exit(MESSAGE) # Force close python ATS ##############################

__author__ = 'Vladimir Roncevic'
__copyright__ = 'Copyright 2020, Free software to use and distributed it.'
__credits__ = ['Vladimir Roncevic']
__license__ = 'GNU General Public License (GPL)'
__version__ = '1.0.0'
__maintainer__ = 'Vladimir Roncevic'
__email__ = 'elektron.ronca@gmail.com'
__status__ = 'Updated'


class GenPro(object):
"""
Define class GenPro with attribute(s) and method(s).
Generate project by template and parameters.
It defines:
:attributes:
| __slots__ - Setting class slots
| VERBOSE - Console text indicator for current process-phase
| __PRO_STRUCTURE - Project structure
| __config - Configuration dictionary
| __current_dir - Current directory
| __reader - Reader API
| __writer - Writer API
:methods:
| __init__ - Initial constructor
| get_reader - Getter for reader object
| get_writer - Getter for writer object
| gen_project - Generate python tool
"""

__slots__ = (
'VERBOSE',
'__PRO_SUB_STRUCTURE',
'__config',
'__current_dir',
'__reader',
'__writer'
)
VERBOSE = 'GEN_COAP_SERVICE::PRO::GEN_PRO'
__PRO_STRUCTURE = '../conf/gen_coap_service_util.cfg'

def __init__(self, project_name, verbose=False):
"""
Initial constructor.
:param project_name: Parameter tool name
:type project_name: <str>
:param verbose: Enable/disable verbose option
:type verbose: <bool>
:exceptions: None
"""
func = stack()[0][3]
pro_name_txt = 'Argument: expected project_name <str> object'
pro_name_msg = "{0} {1} {2}".format('def', func, pro_name_txt)
if project_name is None or not project_name:
raise ATSBadCallError(pro_name_msg)
if not isinstance(project_name, str):
raise ATSTypeError(pro_name_msg)
verbose_message(GenPro.VERBOSE, verbose, 'Initial generator')
self.__reader = ReadTemplate(verbose=verbose)
self.__writer = WriteTemplate(project_name, verbose=verbose)
self.__current_dir = Path(__file__).parent
self.__config = None
pro_structure = Cfg2Object(
"{0}/{1}".format(self.__current_dir, GenPro.__PRO_STRUCTURE),
verbose=verbose
)
if pro_structure:
self.__config = pro_structure.read_configuration(verbose=verbose)

def get_reader(self):
"""
Getter for reader object.
:return: Read template object
:rtype: <ReadTemplate>
:exceptions: None
"""
return self.__reader

def get_writer(self):
"""
Getter for writer object.
:return: Write template object
:rtype: <WriteTemplate>
:exceptions: None
"""
return self.__writer

def gen_project(self, verbose=False):
"""
Generate project structure.
:param verbose: Enable/disable verbose option
:type verbose: <bool>
:return: Boolean status True (success) | False
:rtype: <bool>
:exceptions: ATSBadCallError | ATSTypeError
"""
status = False
statuses, expected_num_files = [], len(self.__config.keys())
if bool(self.__config):
for template_file in self.__config.itervalues():
template = self.__reader.read(
template_file, verbose=verbose
)
if template:
status_write = self.__writer.write(
template, template_file, verbose=verbose
)
if status_write:
statuses.append(status_write)
else:
break
else:
break
if all([len(statuses) == expected_num_files, all(statuses)]):
success_message(
GenPro.VERBOSE, 'Successfully loaded templates'
)
success_message(
GenPro.VERBOSE, 'Successfully written modules'
)
status = True
return True if status else False
Loading

0 comments on commit 2ac4019

Please sign in to comment.