diff options
Diffstat (limited to 'qface')
-rw-r--r-- | qface/cli.py | 40 | ||||
-rw-r--r-- | qface/generator.py | 23 | ||||
-rw-r--r-- | qface/shell.py | 8 | ||||
-rw-r--r-- | qface/templates/qface/qtcpp.j2 | 6 | ||||
-rw-r--r-- | qface/utils.py | 17 | ||||
-rw-r--r-- | qface/watch.py | 4 |
6 files changed, 77 insertions, 21 deletions
diff --git a/qface/cli.py b/qface/cli.py new file mode 100644 index 0000000..f0b06b0 --- /dev/null +++ b/qface/cli.py @@ -0,0 +1,40 @@ +import sys +import click +from path import Path +from qface.generator import FileSystem, RuleGenerator +from qface.watch import monitor + + +def run(config, src, dst, force=False, features=None): + project = Path(dst).name + system = FileSystem.parse(src) + template_dir = config.dirname() / 'templates' + + context = { + 'dst': dst, + 'system': system, + 'project': project, + 'features': features, + } + + generator = RuleGenerator(template_dir, destination=dst, context=context, features=features, force=force) + generator.process_rules(config, system) + + +@click.command() +@click.option('--config', '-c', type=click.Path(exists=True)) +@click.option('--reload/--no-reload', default=False) +@click.option('--features', '-f', multiple=True) +@click.option('--force/--no-force', default=True, help="Force writing of target files, ignores preserve") +@click.argument('src', nargs=-1, type=click.Path(exists=True)) +@click.argument('dst', nargs=1, type=click.Path(exists=False, file_okay=False)) +def app(config, src, dst, features, reload, force): + """Takes several files or directories as src and generates the code + in the given dst directory.""" + config = Path(config) + if reload: + argv = sys.argv.copy() + argv.remove('--reload') + monitor(config.dirname(), src, dst, argv) + else: + run(config, src, dst, force) diff --git a/qface/generator.py b/qface/generator.py index 1e1da41..b19cfa2 100644 --- a/qface/generator.py +++ b/qface/generator.py @@ -5,14 +5,15 @@ from jinja2 import Environment, Template, Undefined, StrictUndefined from jinja2 import FileSystemLoader, PackageLoader, ChoiceLoader from jinja2 import TemplateSyntaxError, TemplateNotFound, TemplateError from path import Path -from antlr4 import FileStream, CommonTokenStream, ParseTreeWalker +from antlr4 import InputStream, FileStream, CommonTokenStream, ParseTreeWalker from antlr4.error import DiagnosticErrorListener, ErrorListener import shelve import logging import hashlib import yaml import click -import sys, os +import sys +import os from .idl.parser.TLexer import TLexer from .idl.parser.TParser import TParser @@ -20,7 +21,6 @@ from .idl.parser.TListener import TListener from .idl.profile import EProfile from .idl.domain import System from .idl.listener import DomainListener -from .utils import merge from .filters import filters from jinja2.debug import make_traceback as _make_traceback @@ -33,6 +33,16 @@ except ImportError: logger = logging.getLogger(__name__) +def merge(a, b): + "merges b into a recursively if a and b are dicts" + for key in b: + if isinstance(a.get(key), dict) and isinstance(b.get(key), dict): + merge(a[key], b[key]) + else: + a[key] = b[key] + return a + + def template_error_handler(traceback): exc_type, exc_obj, exc_tb = traceback.exc_info error = exc_obj @@ -280,6 +290,11 @@ class FileSystem(object): sys.exit(-1) @staticmethod + def parse_text(text: str, system: System = None, profile=EProfile.FULL): + stream = InputStream(text) + return FileSystem._parse_stream(stream, system, "<TEXT>", profile) + + @staticmethod def _parse_document(document: Path, system: System = None, profile=EProfile.FULL): """Parses a document and returns the resulting domain system @@ -293,7 +308,7 @@ class FileSystem(object): return system @staticmethod - def _parse_stream(stream, system: System = None, document=None, profile=EProfile.FULL): + def _parse_stream(stream: InputStream, system: System = None, document=None, profile=EProfile.FULL): logger.debug('parse stream') system = system or System() diff --git a/qface/shell.py b/qface/shell.py index 3d34d89..bf76f7c 100644 --- a/qface/shell.py +++ b/qface/shell.py @@ -6,11 +6,11 @@ API for interacting with the system shell """ -def sh(cmd, **kwargs): +def sh(args, **kwargs): """ runs the given cmd as shell command """ - if not cmd: + if not args: return - click.echo('$ {0}'.format(cmd)) - return call(cmd, shell=True, **kwargs) + click.echo('$ {0}'.format(' '.join(args))) + return call(args, **kwargs) diff --git a/qface/templates/qface/qtcpp.j2 b/qface/templates/qface/qtcpp.j2 index e522233..67b68f8 100644 --- a/qface/templates/qface/qtcpp.j2 +++ b/qface/templates/qface/qtcpp.j2 @@ -37,8 +37,8 @@ void {{symbol}}{{postfix}}(); /*! \qmlproperty {{property.type}} {{class}}::{{property}} {% with doc = property.comment|parse_doc %} - \brief {{doc.brief}} - {{doc.description}} + \brief {{doc.brief|join(" ")}} + {{doc.description|join("\n ")}} {% endwith %} */ void {{class}}::set{{property|upperfirst}}({{ property|parameterType }}) @@ -68,7 +68,7 @@ void {{class}}::set{{property|upperfirst}}({{ property|parameterType }}) \qmlmethod {{operation.type}} {{class}}::{{operation}}({{operation|parameters}}) {% with doc = operation.comment|parse_doc %} \brief {{doc.brief}} - {{doc.description}} + {{doc.description|join("\n ")}} {% endwith %} */ {{operation|returnType}} {{class}}::{{operation}}({{operation|parameters}}) diff --git a/qface/utils.py b/qface/utils.py index 3122d38..f816b58 100644 --- a/qface/utils.py +++ b/qface/utils.py @@ -1,10 +1,11 @@ +from .generator import FileSystem +from .helper import doc -def merge(a, b): - "merges b into a recursively if a and b are dicts" - for key in b: - if isinstance(a.get(key), dict) and isinstance(b.get(key), dict): - merge(a[key], b[key]) - else: - a[key] = b[key] - return a +def module_info(text): + system = FileSystem.parse_text(text) + module = list(system.modules)[0] + return { + 'title': module.name, + 'brief': " ".join(doc.parse_doc(module.comment).brief) + } diff --git a/qface/watch.py b/qface/watch.py index 29bc30f..33fae01 100644 --- a/qface/watch.py +++ b/qface/watch.py @@ -3,7 +3,7 @@ from watchdog.observers import Observer import click from path import Path import time -from subprocess import call +from .shell import sh """ Provides an API to monitor the file system @@ -25,7 +25,7 @@ class RunScriptChangeHandler(FileSystemEventHandler): if self.is_running: return self.is_running = True - call(self.args, cwd=Path.getcwd()) + sh(self.args, cwd=Path.getcwd()) self.is_running = False |