예제 #1
0
    def handle(self, *args, **options):
        if len(args) < 1 and not options['all_applications']:
            raise CommandError("need one or more arguments for appname")

        use_pygraphviz = options.get('pygraphviz', False)
        use_pydot = options.get('pydot', False)
        use_json = options.get('json', False)
        if use_json and (use_pydot or use_pygraphviz):
            raise CommandError("Cannot specify --json with --pydot or --pygraphviz")

        cli_options = ' '.join(sys.argv[2:])
        graph_data = generate_graph_data(args, cli_options=cli_options, **options)
        if use_json:
            self.render_output_json(graph_data, **options)
            return

        dotdata = generate_dot(graph_data)
        if not six.PY3:
            dotdata = dotdata.encode('utf-8')
        if options['outputfile']:
            if not use_pygraphviz and not use_pydot:
                if HAS_PYGRAPHVIZ:
                    use_pygraphviz = True
                elif HAS_PYDOT:
                    use_pydot = True
            if use_pygraphviz:
                self.render_output_pygraphviz(dotdata, **options)
            elif use_pydot:
                self.render_output_pydot(dotdata, **options)
            else:
                raise CommandError("Neither pygraphviz nor pydot could be found to generate the image")
        else:
            self.print_output(dotdata)
예제 #2
0
    def handle(self, *args, **options):
        self.options_from_settings(options)

        if len(args) < 1 and not options['all_applications']:
            raise CommandError("need one or more arguments for appname")

        use_pygraphviz = options.get('pygraphviz', False)
        use_pydot = options.get('pydot', False)
        cli_options = ' '.join(sys.argv[2:])
        dotdata = generate_dot(args, cli_options=cli_options, **options)
        dotdata = dotdata.encode('utf-8')
        if options['outputfile']:
            if not use_pygraphviz and not use_pydot:
                if HAS_PYGRAPHVIZ:
                    use_pygraphviz = True
                elif HAS_PYDOT:
                    use_pydot = True
            if use_pygraphviz:
                self.render_output_pygraphviz(dotdata, **options)
            elif use_pydot:
                self.render_output_pydot(dotdata, **options)
            else:
                raise CommandError("Neither pygraphviz nor pydot could be found to generate the image")
        else:
            self.print_output(dotdata)
예제 #3
0
    def handle(self, *args, **options):
        if len(args) < 1 and not options['all_applications']:
            raise CommandError("need one or more arguments for appname")

        use_pygraphviz = options.get('pygraphviz', False)
        use_pydot = options.get('pydot', False)
        use_json = options.get('json', False)
        if use_json and (use_pydot or use_pygraphviz):
            raise CommandError("Cannot specify --json with --pydot or --pygraphviz")

        cli_options = ' '.join(sys.argv[2:])
        graph_data = generate_graph_data(args, cli_options=cli_options, **options)
        if use_json:
            self.render_output_json(graph_data, **options)
            return

        dotdata = generate_dot(graph_data)
        if not six.PY3:
            dotdata = dotdata.encode('utf-8')
        if options['outputfile']:
            if not use_pygraphviz and not use_pydot:
                if HAS_PYGRAPHVIZ:
                    use_pygraphviz = True
                elif HAS_PYDOT:
                    use_pydot = True
            if use_pygraphviz:
                self.render_output_pygraphviz(dotdata, **options)
            elif use_pydot:
                self.render_output_pydot(dotdata, **options)
            else:
                raise CommandError("Neither pygraphviz nor pydot could be found to generate the image")
        else:
            self.print_output(dotdata)
예제 #4
0
    def handle(self, *args, **options):
        self.options_from_settings(options)

        if len(args) < 1 and not options['all_applications']:
            raise CommandError("need one or more arguments for appname")

        use_pygraphviz = options.get('pygraphviz', False)
        use_pydot = options.get('pydot', False)
        cli_options = ' '.join(sys.argv[2:])
        dotdata = generate_dot(args, cli_options=cli_options, **options)
        dotdata = dotdata.encode('utf-8')
        if options['outputfile']:
            if not use_pygraphviz and not use_pydot:
                if HAS_PYGRAPHVIZ:
                    use_pygraphviz = True
                elif HAS_PYDOT:
                    use_pydot = True
            if use_pygraphviz:
                self.render_output_pygraphviz(dotdata, **options)
            elif use_pydot:
                self.render_output_pydot(dotdata, **options)
            else:
                raise CommandError(
                    "Neither pygraphviz nor pydot could be found to generate the image"
                )
        else:
            self.print_output(dotdata)
    def handle(self, *args, **options):
        args = options['app_label']
        if len(args) < 1 and not options['all_applications']:
            raise CommandError("need one or more arguments for appname")

        # determine output format based on options, file extension, and library
        # availability
        outputfile = options.get("outputfile") or ""
        _, outputfile_ext = os.path.splitext(outputfile)
        outputfile_ext = outputfile_ext.lower()
        output_opts_names = ['pydot', 'pygraphviz', 'json', 'dot']
        output_opts = {k: v for k, v in options.items() if k in output_opts_names}
        output_opts_count = sum(output_opts.values())
        if output_opts_count > 1:
            raise CommandError(
                "Only one of %s can be set." % ", ".join(["--%s" % opt for opt in output_opts_names]))
        elif output_opts_count == 1:
            output = next(key for key, val in output_opts.items() if val)
        elif not outputfile:
            # When neither outputfile nor a output format option are set,
            # default to printing .dot format to stdout. Kept for backward
            # compatibility.
            output = "dot"
        elif outputfile_ext == ".dot":
            output = "dot"
        elif outputfile_ext == ".json":
            output = "json"
        elif HAS_PYGRAPHVIZ:
            output = "pygraphviz"
        elif HAS_PYDOT:
            output = "pydot"
        else:
            raise CommandError("Neither pygraphviz nor pydotplus could be found to generate the image. To generate text output, use the --json or --dot options.")

        # Consistency check: Abort if --pygraphviz or --pydot options are set
        # but no outputfile is specified. Before 2.1.4 this silently fell back
        # to printind .dot format to stdout.
        if output in ["pydot", "pygraphiviz"] and not outputfile:
            raise CommandError("An output file (--output) must be specified when --pydot or --pygraphviz are set.")

        cli_options = ' '.join(sys.argv[2:])
        graph_models = ModelGraph(args, cli_options=cli_options, **options)
        graph_models.generate_graph_data()

        if output == "json":
            graph_data = graph_models.get_graph_data(as_json=True)
            return self.render_output_json(graph_data, outputfile)

        graph_data = graph_models.get_graph_data(as_json=False)
        dotdata = generate_dot(graph_data)
        if not six.PY3:
            dotdata = dotdata.encode("utf-8")

        if output == "pygraphviz":
            return self.render_output_pygraphviz(dotdata, **options)
        if output == "pydot":
            return self.render_output_pydot(dotdata, **options)
        else:
            self.print_output(dotdata, outputfile)
예제 #6
0
    def handle(self, *args, **options):
        args = options['app_label']
        if len(args) < 1 and not options['all_applications']:
            raise CommandError("need one or more arguments for appname")

        # determine output format based on options, file extension, and library
        # availability
        outputfile = options.get("outputfile") or ""
        _, outputfile_ext = os.path.splitext(outputfile)
        outputfile_ext = outputfile_ext.lower()
        output_opts_names = ['pydot', 'pygraphviz', 'json', 'dot']
        output_opts = {k: v for k, v in options.items() if k in output_opts_names}
        output_opts_count = sum(output_opts.values())
        if output_opts_count > 1:
            raise CommandError(
                "Only one of %s can be set." % ", ".join(["--%s" % opt for opt in output_opts_names]))
        elif output_opts_count == 1:
            output = next(key for key, val in output_opts.items() if val)
        elif not outputfile:
            # When neither outputfile nor a output format option are set,
            # default to printing .dot format to stdout. Kept for backward
            # compatibility.
            output = "dot"
        elif outputfile_ext == ".dot":
            output = "dot"
        elif outputfile_ext == ".json":
            output = "json"
        elif HAS_PYGRAPHVIZ:
            output = "pygraphviz"
        elif HAS_PYDOT:
            output = "pydot"
        else:
            raise CommandError("Neither pygraphviz nor pydotplus could be found to generate the image. To generate text output, use the --json or --dot options.")

        # Consistency check: Abort if --pygraphviz or --pydot options are set
        # but no outputfile is specified. Before 2.1.4 this silently fell back
        # to printind .dot format to stdout.
        if output in ["pydot", "pygraphviz"] and not outputfile:
            raise CommandError("An output file (--output) must be specified when --pydot or --pygraphviz are set.")

        cli_options = ' '.join(sys.argv[2:])
        graph_models = ModelGraph(args, cli_options=cli_options, **options)
        graph_models.generate_graph_data()

        if output == "json":
            graph_data = graph_models.get_graph_data(as_json=True)
            return self.render_output_json(graph_data, outputfile)

        graph_data = graph_models.get_graph_data(as_json=False)
        dotdata = generate_dot(graph_data)
        if not six.PY3:
            dotdata = dotdata.encode("utf-8")

        if output == "pygraphviz":
            return self.render_output_pygraphviz(dotdata, **options)
        if output == "pydot":
            return self.render_output_pydot(dotdata, **options)
        self.print_output(dotdata, outputfile)
예제 #7
0
    def handle(self, *args, **options):
        if len(args) < 1 and not options['all_applications']:
            raise CommandError('need one or more arguments for appname')

        dotdata = generate_dot(args, **options)
        if options['outputfile']:
            self.render_output(dotdata, **options)
        else:
            self.print_output(dotdata)
    def handle(self, *args, **options):
        if len(args) < 1 and not options['all_applications']:
            raise CommandError("need one or more arguments for appname")

        dotdata = generate_dot(args, **options)
        if options['outputfile']:
            self.render_output(dotdata, **options)
        else:
            self.print_output(dotdata)
예제 #9
0
 def _render_svg(self, renderapps=None):
     if not renderapps:
         renderapps = [a.label for a in self._get_all_apps()]
     graph_models = ModelGraph(all_applications=False,
                               app_labels=renderapps,
                               arrow_shape="diamond")
     graph_models.generate_graph_data()
     return (pygraphviz.AGraph(
         generate_dot(
             graph_models.get_graph_data(),
             template="django_extensions/graph_models/original/digraph.dot",
         )).draw(format="svg", prog="dot").decode())
예제 #10
0
'''

out = open(dest, 'w')
out.write(HTMLHEAD)

for name, models, stoplist in [
    ('Projects & Authorisation', ('ProjectRole', 'Privilege'), ()),
    ('Articles', ['Sentence', 'Articleset'], ('Project', )),
    ('Coding Jobs', ['CodingJob', 'CodingSchemaField', 'ArticleSet'],
     ('CodingJob', 'Project', 'User', 'Codebook')),
    ('Codebook', ['Codebook', 'CodebookCode', 'Code', 'Label',
                  'CodebookBase'], ['Project']),
    ('Codings', ['CodingValue'], ('Project', 'User', 'Article', 'CodingJob',
                                  'CodingSchemaField')),
]:

    out.write("<h1>Model graph for %s</h1>" % name)

    models = djangotoolkit.get_related_models(models, stoplist)
    modelnames = [m.__name__ for m in models]

    dot = generate_dot(['amcat'], include_models=",".join(modelnames))
    dot = dot.replace("digraph name {", "digraph name {\ngraph [rankdir=LR];")

    imgfn = name.lower().replace("&", "").replace(" ", "_") + ".png"
    out.write(
        "<img src='img/{imgfn}' title='{name}' alt='Model graph for {name}' />"
        .format(**locals()))

    open(os.path.join(imgdir, imgfn), 'w').write(dot2img(dot, format='png'))
예제 #11
0
    def handle(self, *args, **options):
        args = options['app_label']
        if not args and not options['all_applications']:
            default_app_labels = getattr(settings, 'GRAPH_MODELS',
                                         {}).get("app_labels")
            if default_app_labels:
                args = default_app_labels
            else:
                raise CommandError("need one or more arguments for appname")

        # Determine output format based on options, file extension, and library
        # availability.
        outputfile = options.get("outputfile") or ""
        _, outputfile_ext = os.path.splitext(outputfile)
        outputfile_ext = outputfile_ext.lower()
        output_opts_names = ['pydot', 'pygraphviz', 'json', 'dot']
        output_opts = {
            k: v
            for k, v in options.items() if k in output_opts_names
        }
        output_opts_count = sum(output_opts.values())
        if output_opts_count > 1:
            raise CommandError(
                "Only one of %s can be set." %
                ", ".join(["--%s" % opt for opt in output_opts_names]))

        if output_opts_count == 1:
            output = next(key for key, val in output_opts.items() if val)
        elif not outputfile:
            # When neither outputfile nor a output format option are set,
            # default to printing .dot format to stdout. Kept for backward
            # compatibility.
            output = "dot"
        elif outputfile_ext == ".dot":
            output = "dot"
        elif outputfile_ext == ".json":
            output = "json"
        elif HAS_PYGRAPHVIZ:
            output = "pygraphviz"
        elif HAS_PYDOT:
            output = "pydot"
        else:
            raise CommandError(
                "Neither pygraphviz nor pydotplus could be found to generate the image. To generate text output, use the --json or --dot options."
            )

        if options.get('rankdir') != 'TB' and output not in [
                "pydot", "pygraphviz", "dot"
        ]:
            raise CommandError(
                "--rankdir is not supported for the chosen output format")

        # Consistency check: Abort if --pygraphviz or --pydot options are set
        # but no outputfile is specified. Before 2.1.4 this silently fell back
        # to printind .dot format to stdout.
        if output in ["pydot", "pygraphviz"] and not outputfile:
            raise CommandError(
                "An output file (--output) must be specified when --pydot or --pygraphviz are set."
            )

        cli_options = ' '.join(sys.argv[2:])
        graph_models = ModelGraph(args, cli_options=cli_options, **options)
        graph_models.generate_graph_data()

        if output == "json":
            graph_data = graph_models.get_graph_data(as_json=True)
            return self.render_output_json(graph_data, outputfile)

        graph_data = graph_models.get_graph_data(as_json=False)

        theme = options['theme']
        template_name = os.path.join('django_extensions', 'graph_models',
                                     theme, 'digraph.dot')
        template = loader.get_template(template_name)

        dotdata = generate_dot(graph_data, template=template)

        if output == "pygraphviz":
            return self.render_output_pygraphviz(dotdata, **options)
        if output == "pydot":
            return self.render_output_pydot(dotdata, **options)
        self.print_output(dotdata, outputfile)