Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli #12

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open

Cli #12

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5dd09fc
Add basic files
francozappa Jul 26, 2017
30aa917
added cli command group init
aporan Aug 2, 2017
aacf164
added Init class for generating scaffold
aporan Aug 2, 2017
49c7148
template file for generating scaffold
aporan Aug 2, 2017
544613f
common interface for creating scaffold in binary
aporan Aug 3, 2017
9aa859c
template factory generates appropriate template; device class provide…
aporan Aug 3, 2017
60cef08
added exception to provide user feedback
aporan Aug 3, 2017
e1c4ba1
DRY'd code
aporan Aug 3, 2017
ac8d18d
added custom path cli optional arg
aporan Aug 3, 2017
6f7e078
added test point for makefile
aporan Aug 7, 2017
7d47997
checks permission for creating folders; type raises NotImplementedError
aporan Aug 7, 2017
b5fff7b
devices template method filters type based on argument
aporan Aug 7, 2017
05cc1b6
ui test cases
aporan Aug 7, 2017
e229e5a
added test for travis
aporan Aug 7, 2017
b3ca354
Merge pull request #11 from remmihsorp/cli
francozappa Aug 8, 2017
0e2aa06
Add skeleton files for the cli doc
francozappa Sep 11, 2017
e4bbc5e
Draft new version
francozappa Sep 11, 2017
97404e6
fixed simple string formatting for clarity
aporan Sep 12, 2017
904927b
added docs for CLI
aporan Sep 12, 2017
4fa8053
added new version release notes
aporan Sep 12, 2017
f2ea9e5
removed duplicate entry in setup file
aporan Sep 12, 2017
2724659
Use a separate rst file to generate mcps man page
francozappa Sep 12, 2017
e82ee1a
edited man for mcps to follow man structure
aporan Sep 13, 2017
f20556d
man is now generated in the docs dir when `make man` is executed; set…
aporan Sep 13, 2017
9501058
man for mcps bin
aporan Sep 13, 2017
b6148da
formatted man page and corrected man location to be copied to
aporan Sep 13, 2017
97c2355
added argument for specifying folder name
aporan Sep 15, 2017
97e92ab
updated docs for the addition of argument for mcps
aporan Sep 15, 2017
3c00cb5
add subcommand description and update default name
aporan Sep 18, 2017
236d94c
improved man page for mcps
aporan Sep 18, 2017
2bfbe4f
update man page placement location
aporan Sep 18, 2017
0ac9b77
Add click dependency
francozappa Sep 18, 2017
3ece104
update help message for subcommand `init`
aporan Sep 18, 2017
09ecbfe
updated release date for version 1.2.0
aporan Sep 18, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# VARIABLES {{{1

LATEST_VERSION = 1.1.3
LATEST_VERSION = 1.2.0
MININET = sudo mn

PYTHON = sudo python
Expand Down Expand Up @@ -66,6 +66,7 @@ tests-travis:
$(TESTER_TRAVIS) $(TESTER_OPTS) tests/protocols_tests.py
$(TESTER_TRAVIS) $(TESTER_OPTS) tests/devices_tests.py
$(TESTER_TRAVIS) $(TESTER_OPTS) tests/states_tests.py
$(TESTER_TRAVIS) $(TESTER_OPTS) tests/ui_tests.py

tests:
$(TESTER) $(TESTER_OPTS) tests
Expand Down Expand Up @@ -111,6 +112,11 @@ test-devices:

test-device:
$(TESTER) $(TESTER_OPTS) tests/devices_tests.py:TestDevice


test-ui:
$(TESTER) $(TESTER_OPTS) tests/ui_tests.py

# }}}

# }}}
Expand Down
7 changes: 7 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Releases and Changelog

## Version 1.2.0 (2017-09-18)

### UI

* Added CLI tool `mcps`.
* Generate default files for simulation using a single command.

## Version 1.1.3 (2017-05-14)

### Misc
Expand Down
45 changes: 45 additions & 0 deletions bin/mcps
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/python2

import click

import errno
import traceback

import minicps.ui.commands as mcps

# vim: ft=python

@click.group()
def main():
""" A command line tool for MINICPS."""
pass

@main.command('init', short_help="Initialize a directory with default files for MINICPS simulation.")
@click.option('--config', help="Configuration file for generating template.")
@click.option('--path', help="Path to the scaffold directory.")
@click.argument('name', default="myproject")
def init(config, path, name):
"""
Initialize directory NAME with optional custom configuration.

Default dir name: `myproject`.
"""
init = mcps.Init()

path = path if (path is not None) else "."
full_path = "{path}/{folder}".format(path=path.rstrip('/'), folder=name)

try:
init.make(path=full_path)
click.echo(click.style("\n... Success: directory generated.", fg="green", bold=True))
except OSError as e:
if e.errno == errno.EEXIST:
click.echo(click.style("Warning: Directory ('{}') already exists.".format(path), fg="yellow"))
elif e.errno == errno.EACCES:
click.echo(click.style("Error: Permission Denied. Cannot write to {path}.".format(path), fg="red"))
else:
click.echo(click.style(traceback.format_exc(), fg="red"))
click.echo("... init aborted.")

if __name__=="__main__":
main()
1 change: 1 addition & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ text:

man:
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
cp $(BUILDDIR)/man/mcps.1 .
@echo
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."

Expand Down
14 changes: 11 additions & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
# built documents.
#
# The short X.Y version.
version = '1.1'
version = '1.2'
# The full version, including alpha/beta/rc tags.
release = '1.1.3'
release = '1.2.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down Expand Up @@ -264,8 +264,16 @@

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).

# NOTE: we don't want one man page for each document in the toc
# man_pages = [
# (master_doc, 'minicps', u'minicps Documentation',
# [author], 1)
# ]

# NOTE: mcps-man is not included in the TOC
man_pages = [
(master_doc, 'minicps', u'minicps Documentation',
('mcps-man', 'mcps', u'mcps man page',
[author], 1)
]

Expand Down
3 changes: 1 addition & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ Contents:
swat-tutorial
contributing
tests
ui
misc



Indices and tables
==================

Expand Down
49 changes: 49 additions & 0 deletions docs/mcps-man.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.. MCPS-MAN {{{1
.. _mcps-man:

****************************************
mcps man page (not included in the docs)
****************************************

========
Synopsis
========

``mcps`` [``--help``]

``mcps init`` [NAME] [``--path`` path] [``--config`` file]

===========
Description
===========

The command line utility is called ``mcps``. It generates a scaffold directory which has
the minimum files necessary to set-up a simulation environment.

====================
Command Line Options
====================

``init``
--------

This generates a default scaffold directory (``myproject``) using two PLCs in the current working directory.

``init [NAME] [OPTIONS]``
-------------------------
``[NAME]``
Custom name of the directory to be generated.

``--path``
Provide a custom path for generating the scaffold.

``--config``
Provide a configuration file to generate template with custom devices and values.

Note: This is **NOT** implemented yet.

``--help``
----------

This lists the available commands.

68 changes: 68 additions & 0 deletions docs/mcps.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
.\" Man page generated from reStructuredText.
.
.TH "MCPS" "1" "Sep 18, 2017" "1.2" "minicps"
.SH NAME
mcps \- mcps man page
.
.nr rst2man-indent-level 0
.
.de1 rstReportMargin
\\$1 \\n[an-margin]
level \\n[rst2man-indent-level]
level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
-
\\n[rst2man-indent0]
\\n[rst2man-indent1]
\\n[rst2man-indent2]
..
.de1 INDENT
.\" .rstReportMargin pre:
. RS \\$1
. nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
. nr rst2man-indent-level +1
.\" .rstReportMargin post:
..
.de UNINDENT
. RE
.\" indent \\n[an-margin]
.\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
.nr rst2man-indent-level -1
.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
.in \\n[rst2man-indent\\n[rst2man-indent-level]]u
..
.SH SYNOPSIS
.sp
\fBmcps\fP [\fB\-\-help\fP]
.sp
\fBmcps init\fP [NAME] [\fB\-\-path\fP path] [\fB\-\-config\fP file]
.SH DESCRIPTION
.sp
The command line utility is called \fBmcps\fP\&. It generates a scaffold directory which has
the minimum files necessary to set\-up a simulation environment.
.SH COMMAND LINE OPTIONS
.SS \fBinit\fP
.sp
This generates a default scaffold directory (\fBmyproject\fP) using two PLCs in the current working directory.
.SS \fBinit [NAME] [OPTIONS]\fP
.INDENT 0.0
.TP
.B \fB[NAME]\fP
Custom name of the directory to be generated.
.TP
.B \fB\-\-path\fP
Provide a custom path for generating the scaffold.
.TP
.B \fB\-\-config\fP
Provide a configuration file to generate template with custom devices and values.
.sp
Note: This is \fBNOT\fP implemented yet.
.UNINDENT
.SS \fB\-\-help\fP
.sp
This lists the available commands.
.SH AUTHOR
scy-phy
.SH COPYRIGHT
2017, Daniele Antonioli
.\" Generated by docutils manpage writer.
.
51 changes: 51 additions & 0 deletions docs/ui.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
.. UI {{{1
.. _ui:

***************
User Interface
***************

.. CLI {{{2

This section documents the CLI for minicps.

=======================
Command Line Interface
=======================

The command line utility is called ``mcps``. To get more information on the usage:

.. code-block:: console

mcps --help

Default
--------

The base command generates a default scaffold directory which has the minimum files necessary
to set-up a simulation environment with two ``PLC``'s as the default device.

.. code-block:: console

mcps init

Custom
-------

The command has additional options.

.. code-block:: console

mcps init [NAME] [OPTIONS]

``[NAME]``
Custom name of the directory to be generated.

``path``
Provide a custom path for generating the scaffold.

``config``
Provide a configuration file to generate template with custom devices and values.

Note: This is **NOT** implemented yet.

Empty file added minicps/ui/__init__.py
Empty file.
47 changes: 47 additions & 0 deletions minicps/ui/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
This file contains the commands that can be used (with parameters) both by cli
and gui packages. For example the mcps click cli is using them as subcommands.

This is the list of supported commands:

- init
"""
import os
from template import TemplateFactory

class Init(object):

"""Docstring for Init. """

def __init__(self):
self._template = None

def make(self, path, _type="default"):
os.mkdir(path)

self._template = TemplateFactory.get_template(_type)
if _type == "default":
self._default(path)
else:
pass

def _default(self, path):
devices = self._template.devices(number=2)
for idx in range(len(devices)):
self._create('{}/plc{}.py'.format(path, (idx+1)), devices[idx])
self._display("{folder}/plc{idx}.py".format(folder=path, idx=idx+1))

self._create('{}/run.py'.format(path), self._template.run())
self._create('{}/state.py'.format(path), self._template.state())
self._create('{}/topo.py'.format(path), self._template.topology())

self._display("{folder}/state.py".format(folder=path))
self._display("{folder}/run.py".format(folder=path))
self._display("{folder}/topo.py".format(folder=path))

def _create(self, path, message):
with open(path, 'w') as f:
f.write(message)

def _display(self, message):
print "{:<5} create {}".format("", message)
Loading