Skip to content

Commit

Permalink
Commandex v0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
smartlegionlab committed May 8, 2024
1 parent b21b311 commit cec2e8f
Show file tree
Hide file tree
Showing 27 changed files with 855 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
9 changes: 9 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include LICENSE
include README.md
include requirements.txt
include setup.py
include setup.cfg
include tox.ini
global-exclude tests/*
global-exclude data/*
global-exclude requirements/*
186 changes: 185 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,186 @@
# commandex
A cross-platform library for creation, storage, management of commands and command packages. Execution of commands, parsing of files with command packages.

***

![GitHub top language](https://img.shields.io/github/languages/top/smartlegionlab/commandex)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/commandex?label=pypi%20downloads)](https://pypi.org/project/commandex/)
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/smartlegionlab/commandex)](https://github.com/smartlegionlab/commandex/)
[![GitHub](https://img.shields.io/github/license/smartlegionlab/commandex)](https://github.com/smartlegionlab/commandex/blob/master/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/commandex)](https://pypi.org/project/commandex)
[![PyPI - Format](https://img.shields.io/pypi/format/commandex)](https://pypi.org/project/commandex)
[![GitHub Repo stars](https://img.shields.io/github/stars/smartlegionlab/commandex?style=social)](https://github.com/smartlegionlab/commandex/)
[![GitHub watchers](https://img.shields.io/github/watchers/smartlegionlab/commandex?style=social)](https://github.com/smartlegionlab/commandex/)
[![GitHub forks](https://img.shields.io/github/forks/smartlegionlab/commandex?style=social)](https://github.com/smartlegionlab/commandex/)

***

## Short Description:

___commandex___ - A cross-platform library for creation, storage, management of commands and command packages. Execution of commands, parsing of files with command packages.

***

Author and developer: ___A.A Suvorov___

[![[email protected]](https://img.shields.io/static/v1?label=email&[email protected]&color=blue)](mailto:[email protected])

***

## Supported:

- Linux: All.
- Windows: 7/8/10.
- Termux (Android).

***

## What's new?

___commandex v0.2.2___

***

## Description:

A cross-platform library for creation, storage, management of commands and command packages.
Execution of commands, parsing of files with command packages.

- Store the required commands under a given name in a simple file with a clear structure.
- Read named command packages from a file.
- Use only the required command packages from a file using filtering.
- Execute command packages cross-platform.

### How it works?

1. Create a file with any name, with the extension * .cfg
2. Use # as a comment, line break for convenience or separation.
3. Use [package name] to specify the name of the command package.
4. After the name, write the commands that you decided to combine under this name, one per line.
5. To create the next batch of commands, use points 2, 3 again.
6. Between commands, you can insert blank lines or comments, see point 2.

Used to create utilities for working with commands (execution, launch, autorun, storage).

You can keep your commands in simple and understandable files, collect them in one place,
split into named categories (packages) and execute at any time:

The files must have the extension *.cfg, and have the correct structure:


### commands.cfg:

```ini
# Comments

[package name 1]
command 1
command 2
command N

[package name 2]
command 1
command 2
command N
```

***


## Help:

### Install and Use:

- `pip install commandex`

Available tools:

- Pack - storing commands.
- Command executors - command execution.
- Pack makers - create a list with objects of command packages.
- Parsers - parsers for files with command packages.
- Filters - filtering commands.
- Factories - Fabric for creating objects.

Principle of operation:

- Reading command packages from a file.
- Filter packages if needed.
- We create a list of package objects for future use.
- We execute commands from each package.


### Simplest implementation:

```python
from commandex import Commander

commander = Commander()
# Reading command packages from a file.
pack_dict = commander.parsers.cfg_parser.parse('file.cfg')
# Filter packages if needed.
packs = commander.filters.pack_filter.filter_pack_dict(pack_dict, add_list=[], exc_list=[])
# We create a list of package objects for future use.
pack_list = commander.makers.pack_maker.make_pack_list(packs)

# We execute commands from each package.
for pack in pack_list:
print(pack.name)
for command in pack.commands:
print(command)
commander.executors.os.execute(command)

```

### Termux, Windows support:

Utilities created with use work "commandex", without problems in Termux, Windows.

For example: [commandman](https://github.com/smartlegionlab/commandman).

***

### Test coverage:

#### Run tests:
- `pip install pytest`
- `pytest -v`


#### __Test coverage 100%__

- `pip install pytest-coverage`
- `pytest --cov`

![commandex image](https://github.com/smartlegionlab/commandex/raw/master/data/images/commandex.png)


#### Report html:

- `pytest --cov --cov-report=html`

***

## Disclaimer of liability:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

***

## Copyright:
--------------------------------------------------------
Licensed under the terms of the BSD 3-Clause License
(see LICENSE for details).
Copyright © 2018-2024, A.A Suvorov
All rights reserved.
--------------------------------------------------------
https://github.com/smartlegionlab
--------------------------------------------------------
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-architect
18 changes: 18 additions & 0 deletions commandex/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# --------------------------------------------------------
# Licensed under the terms of the BSD 3-Clause License
# (see LICENSE for details).
# Copyright © 2018-2024, A.A Suvorov
# All rights reserved.
# --------------------------------------------------------
# https://github.com/smartlegionlab
# --------------------------------------------------------
"""
Command executors' library.
- A cross-platform library for creation, storage, management
of commands and command packages. Execution of commands,
parsing of files with command packages.
"""
from commandex.commander import Commander
__version__ = '0.2.2'
21 changes: 21 additions & 0 deletions commandex/commander.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# --------------------------------------------------------
# Licensed under the terms of the BSD 3-Clause License
# (see LICENSE for details).
# Copyright © 2018-2024, A.A Suvorov
# All rights reserved.
# --------------------------------------------------------
# https://github.com/smartlegionlab
# --------------------------------------------------------
from commandex.filters import Filters
from commandex.makers import Makers
from commandex.parsers import Parsers
from commandex.executors import Executors
from commandex.factories import Factories


class Commander:
factories = Factories()
executors = Executors()
filters = Filters()
makers = Makers()
parsers = Parsers()
37 changes: 37 additions & 0 deletions commandex/commandpack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# --------------------------------------------------------
# Licensed under the terms of the BSD 3-Clause License
# (see LICENSE for details).
# Copyright © 2018-2024, A.A Suvorov
# All rights reserved.
# --------------------------------------------------------
# https://github.com/smartlegionlab
# --------------------------------------------------------


class Pack:
def __init__(self, name, commands=None):
self._commands = [] if commands is None or not isinstance(commands, list) else commands
self._name = str(name)

def add(self, command):
self._commands.append(str(command))

def remove(self, command):
if command in self._commands:
index = self._commands.index(command)
del self._commands[index]

@property
def commands(self):
return self._commands

@property
def name(self):
return self._name

@property
def command_count(self):
return len(self._commands)

def __repr__(self):
return f'<Pack {self._name}>'
37 changes: 37 additions & 0 deletions commandex/executors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# --------------------------------------------------------
# Licensed under the terms of the BSD 3-Clause License
# (see LICENSE for details).
# Copyright © 2018-2024, A.A Suvorov
# All rights reserved.
# --------------------------------------------------------
# https://github.com/smartlegionlab
# --------------------------------------------------------
import os
import subprocess
from abc import ABC, abstractmethod


class ExecutorBase(ABC):
@abstractmethod
def execute(self, command):
""""""

def __call__(self, command):
return self.execute(command)


class OsExecutor(ExecutorBase):
def execute(self, command):
return not bool(os.system(command))


class SubExecutor(ExecutorBase):
def execute(self, command):
p = subprocess.Popen(command, shell=True, stderr=subprocess.DEVNULL)
status = p.wait()
return not bool(status)


class Executors:
os = OsExecutor()
sub = SubExecutor()
63 changes: 63 additions & 0 deletions commandex/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# --------------------------------------------------------
# Licensed under the terms of the BSD 3-Clause License
# (see LICENSE for details).
# Copyright © 2018-2024, A.A Suvorov
# All rights reserved.
# --------------------------------------------------------
# https://github.com/smartlegionlab
# --------------------------------------------------------
from commandex.executors import OsExecutor, SubExecutor, Executors
from commandex.filters import PackFilter, Filters
from commandex.makers import PackMaker, Makers
from commandex.parsers import CfgParser, Parsers


class ExecutorsFactory:
@classmethod
def get_os_executor(cls):
return OsExecutor()

@classmethod
def get_sub_executor(cls):
return SubExecutor()

@classmethod
def get_executors(cls):
return Executors()


class FiltersFactory:
@classmethod
def get_pack_filter(cls):
return PackFilter()

@classmethod
def get_filters(cls):
return Filters()


class MakersFactory:
@classmethod
def get_pack_maker(cls):
return PackMaker()

@classmethod
def get_makers(cls):
return Makers()


class ParsersFactory:
@classmethod
def get_cfg_parser(cls):
return CfgParser()

@classmethod
def get_parsers(cls):
return Parsers()


class Factories:
executors = ExecutorsFactory()
filters = FiltersFactory()
makers = MakersFactory()
parsers = ParsersFactory()
Loading

0 comments on commit cec2e8f

Please sign in to comment.