Пример #1
0
def command_line_code_generation(filename, language, out_path=None):
    """Starts a code generator without starting the GUI.

    filename: Name of wxg file to generate code from
    language: Code generator language
    out_path: output file / output directory"""
    from xml_parse import CodeWriter

    try:
        if language not in common.code_writers:
            raise ValueError('Code writer for "%s" is not available.' %
                             language)

        writer = common.code_writers[language]
        CodeWriter(writer=writer, input=filename, out_path=out_path)
    except errors.WxgBaseException as inst:
        logging.error(inst)
        sys.exit(inst)
    except Exception:
        logging.error(
            _("An exception occurred while generating the code for the application.\n"
              "If you think this is a wxGlade bug, please report it."))
        logging.exception(_('Internal Error'))
        sys.exit(1)
    sys.exit(0)
Пример #2
0
    def generate_code(self, *args, **kwds):
        preview = kwds.get('preview', False)
        if not self.output_path:
            return wx.MessageBox(
                _("You must specify an output file\n"
                  "before generating any code"),
                _("Error"),
                wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION,
                self.notebook,
            )
        if not preview and (
            (self.name_prop.is_active() or self.klass_prop.is_active())
                and self.top_win_prop.GetSelection() < 0):
            return wx.MessageBox(
                _("Please select a top window for the application"),
                _("Error"),
                wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION,
                self.notebook,
            )

        # temporary buffer for XML
        tmp_xml = misc.UnicodeStringIO('utf-8')

        from xml_parse import CodeWriter
        try:
            # generate the code from the xml buffer
            codewriter = self.get_language()

            # save and overwrite some code generation settings
            if preview and codewriter == 'python':
                overwrite_save = self.overwrite
                self.overwrite = True

            class_names = common.app_tree.write(tmp_xml)

            out_path = os.path.expanduser(self.output_path.strip())
            if not os.path.isabs(out_path) and self.filename:
                out_path = os.path.join(os.path.dirname(self.filename),
                                        out_path)
                out_path = os.path.normpath(out_path)
            else:
                out_path = None

            CodeWriter(common.code_writers[codewriter],
                       tmp_xml.getvalue(),
                       True,
                       preview=preview,
                       out_path=out_path,
                       class_names=class_names)

            # restore saved settings
            if preview and codewriter == 'python':
                self.overwrite = overwrite_save

        except (errors.WxgBaseException, IOError, OSError), inst:
            wx.MessageBox(
                _("Error generating code:\n%s") % inst,
                _("Error"),
                wx.OK | wx.CENTRE | wx.ICON_ERROR,
            )
Пример #3
0
    def _generate_code(self, language, document, filename):
        """\
        Generate code for the given language.

        @param language: Language to generate code for
        @type language:  str
        @param document: XML document to generate code for
        @type document:  str
        @param filename: Name of the virtual output file
        @type filename:  str
        """
        self.failUnless(language in common.code_writers,
                        "No codewriter loaded for %s" % language)
        self.failUnless(isinstance(document, compat.unicode),
                        'Expected unicode document, got "%s"' % type(document))

        document = self._prepare_wxg(language, document)

        # CodeWrite need UTF-8 like all XML parsers
        document = document.encode('UTF-8')

        # generate code
        CodeWriter(writer=common.code_writers[language],
                   input=document,
                   from_string=True,
                   out_path=filename)

        return
Пример #4
0
 def generate_code(self, *args, **kwds):
     preview = kwds.get('preview', False)
     if not self.output_path:
         return wx.MessageBox(_("You must specify an output file\n"
                             "before generating any code"), _("Error"),
                             wx.OK|wx.CENTRE|wx.ICON_EXCLAMATION,
                             self.notebook)
     if not preview and \
            ((self.name_prop.is_active() or self.klass_prop.is_active()) \
             and self.top_win_prop.GetSelection() < 0):
         return wx.MessageBox(_("Please select a top window "
                             "for the application"), _("Error"), wx.OK |
                             wx.CENTRE | wx.ICON_EXCLAMATION, self.notebook)
             
     from cStringIO import StringIO
     out = StringIO()
     #common.app_tree.write(out) # write the xml onto a temporary buffer
     from xml_parse import CodeWriter
     try:
         # generate the code from the xml buffer
         cw = self.get_language() #self.codewriters_prop.get_str_value()
         if preview and cw == 'python': # of course cw == 'python', but...
             old = common.code_writers[cw].use_new_namespace
             common.code_writers[cw].use_new_namespace = True #False
             overwrite = self.overwrite
             self.overwrite = True
         class_names = common.app_tree.write(out) # write the xml onto a
                                                  # temporary buffer
         if not os.path.isabs(self.output_path) and \
            self.filename is not None:
             out_path = os.path.join(os.path.dirname(self.filename),
                                     self.output_path)
         else:
             out_path = None
         CodeWriter(common.code_writers[cw], out.getvalue(), True,
                    preview=preview, out_path=out_path,
                    class_names=class_names)
         if preview and cw == 'python':
             common.code_writers[cw].use_new_namespace = old
             self.overwrite = overwrite
     except (IOError, OSError), msg:
         wx.MessageBox(_("Error generating code:\n%s") % msg, _("Error"),
                      wx.OK|wx.CENTRE|wx.ICON_ERROR)
Пример #5
0
def command_line_code_generation(options, args):
    """\
    starts a code generator without starting the GUI.
    """
    import common
    if not options: usage()
    if not options[0]:
        usage() # a language for code generation must be provided
    if len(args) != 1: usage() # an input file name must be provided
    
    common.use_gui = False # don't import wxPython.wx
    # use_gui has to be set before importing config
    import config
    config.init_preferences()
    common.load_code_writers()
    common.load_widgets()
    common.load_sizers()
    try:
        from xml_parse import CodeWriter
        out_path = None
        language = ''
        for option, arg in options:
            if option == '-g' or option == '--generate-code':
                language = arg
            elif option == '-o' or option == '--output':
                out_path = _fix_path(arg)
        writer = common.code_writers[language]
        CodeWriter(writer, _fix_path(args[0]), out_path=out_path)
    except KeyError:
        print >> sys.stderr, \
              _('Error: no writer for language "%s" available') % language
        sys.exit(1)
    except Exception, e:
        print >> sys.stderr, _("Error: %s") % e
        import traceback; traceback.print_exc()
        sys.exit(1)
Пример #6
0
    def generate_code(self, *args, **kwds):
        preview = kwds.get('preview', False)
        if not self.output_path:
            return wx.MessageBox(
                _("You must specify an output file\nbefore generating any code"
                  ), _("Error"), wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION,
                self.notebook)
        name_p = self.properties["name"]
        class_p = self.properties["class"]
        if not preview and (name_p.is_active()
                            or class_p.is_active()) and not self.top_window:
            return wx.MessageBox(
                _("Please select a top window for the application"),
                _("Error"), wx.OK | wx.CENTRE | wx.ICON_EXCLAMATION,
                self.notebook)

        # temporary buffer for XML
        tmp_xml = misc.UnicodeStringIO('utf-8')

        from xml_parse import CodeWriter
        try:
            # generate the code from the xml buffer
            language = self.language

            # save and overwrite some code generation settings; to be restored below
            if preview and language == 'python':
                overwrite_save = self.overwrite
                self.properties["overwrite"].set(True)

            class_names = common.app_tree.write(tmp_xml)

            out_path = os.path.expanduser(self.output_path.strip())
            if not os.path.isabs(out_path) and self.filename:
                out_path = os.path.join(os.path.dirname(self.filename),
                                        out_path)
                out_path = os.path.normpath(out_path)
            else:
                out_path = None

            CodeWriter(common.code_writers[language],
                       tmp_xml.getvalue(),
                       True,
                       preview=preview,
                       out_path=out_path,
                       class_names=class_names)

            # restore saved settings
            if preview and language == 'python':
                self.properties["overwrite"].set(overwrite_save)

        except errors.WxgBaseException as inst:
            wx.MessageBox(
                _("Error generating code:\n%s") % inst, _("Error"),
                wx.OK | wx.CENTRE | wx.ICON_ERROR)
        except EnvironmentError as inst:
            bugdialog.ShowEnvironmentError(
                _('An IO/OS related error is occurred:'), inst)
            bugdialog.Show(_('Generate Code'), inst)
        else:
            if not preview:
                if config.preferences.show_completion:
                    # Show informational dialog
                    wx.MessageBox(_("Code generation completed successfully"),
                                  _("Information"),
                                  wx.OK | wx.CENTRE | wx.ICON_INFORMATION)
                else:
                    # Show message in application status bar
                    app = wx.GetApp()
                    frame = app.GetTopWindow()
                    frame.user_message(_('Code generated'))