Exemplo n.º 1
0
def initialize(app_attrs): #out_path, multi_files):
    """\
    Code generator initialization function.
    """
    out_path = app_attrs['path']
    multi_files = app_attrs['option']
    
    global output_file, curr_tab, xrc_objects, output_file_name, app_encoding
    # first, set the app encoding
    if 'encoding' in app_attrs:
        app_encoding = app_attrs['encoding']
        # wx doesn't like latin-1
        if app_encoding == 'latin-1': app_encoding = 'ISO-8859-1'
    if multi_files:
        # for now we handle only single-file code generation
        raise IOError("XRC code cannot be split into multiple files")
    output_file_name = out_path
    output_file = cStringIO.StringIO() #open(out_path, 'w')
    from time import asctime
    header_lines = ['<?xml version="1.0" encoding="%s"?>' % app_encoding, 
                    '<!-- generated by wxGlade %s on %s%s -->' % \
                    (common.version, asctime(), common.generated_from())]
    if not config.preferences.write_timestamp:
        header_lines[1] = '<!-- generated by wxGlade %s%s -->' % \
                          (common.version, common.generated_from())

    for line in header_lines:
        output_file.write(line + '\n')
    output_file.write('\n<resource version="2.3.0.1">\n')
    curr_tab = 1
    xrc_objects = {}
Exemplo n.º 2
0
def initialize(app_attrs):  #out_path, multi_files):
    """\
    Code generator initialization function.
    """
    out_path = app_attrs['path']
    multi_files = app_attrs['option']

    global output_file, curr_tab, xrc_objects, output_file_name, app_encoding
    # first, set the app encoding
    if 'encoding' in app_attrs:
        app_encoding = app_attrs['encoding']
        # wx doesn't like latin-1
        if app_encoding == 'latin-1': app_encoding = 'ISO-8859-1'
    if multi_files:
        # for now we handle only single-file code generation
        raise IOError("XRC code cannot be split into multiple files")
    output_file_name = out_path
    output_file = cStringIO.StringIO()  #open(out_path, 'w')
    from time import asctime
    header_lines = ['<?xml version="1.0" encoding="%s"?>' % app_encoding,
                    '<!-- generated by wxGlade %s on %s%s -->' % \
                    (common.version, asctime(), common.generated_from())]
    if not config.preferences.write_timestamp:
        header_lines[1] = '<!-- generated by wxGlade %s%s -->' % \
                          (common.version, common.generated_from())

    for line in header_lines:
        output_file.write(line + '\n')
    output_file.write('\n<resource version="2.3.0.1">\n')
    curr_tab = 1
    xrc_objects = {}
Exemplo n.º 3
0
def initialize(app_attrs):
    """\
    Writer initialization function.
    - app_attrs: dict of attributes of the application. The following two
                 are always present:
           path: output path for the generated code (a file if multi_files is
                 False, a dir otherwise)
         option: if True, generate a separate file for each custom class
    """

    out_path = app_attrs["path"]
    multi_files = app_attrs["option"]

    global classes, header_lines, multiple_files, previous_source, nonce, _current_extra_modules, _use_gettext, _overwrite
    import time, random

    ##     # scan widgets.txt for widgets, load perl_codegen's
    ##     _widgets_dir = os.path.join(common.wxglade_path, 'widgets')
    ##     widgets_file = os.path.join(_widgets_dir, 'widgets.txt')
    ##     if not os.path.isfile(widgets_file):
    ##         print >> sys.stderr, "widgets file (%s) doesn't exist" % widgets_file
    ##         return
    ##     import sys
    ##     sys.path.append(_widgets_dir)
    ##     modules = open(widgets_file)
    ##     for line in modules:
    ##         module_name = line.strip()
    ##         if not module_name or module_name.startswith('#'): continue
    ##         module_name = module_name.split('#')[0].strip()
    ##         try:
    ##             m = __import__(
    ##                 module_name + '.perl_codegen', {}, {}, ['initialize'])
    ##             m.initialize()
    ##         except (ImportError, AttributeError):
    ##             print 'ERROR loading "%s"' % module_name
    ##             import traceback;
    ##             traceback.print_exc()
    ## ##        else:
    ## ##            print 'initialized perl generator for ', module_name
    ##     modules.close()

    ##     # ...then, the sizers
    ##     import edit_sizers.perl_sizers_codegen
    ##     edit_sizers.perl_sizers_codegen.initialize()

    try:
        _use_gettext = int(app_attrs["use_gettext"])
    except (KeyError, ValueError):
        _use_gettext = False

    try:
        _overwrite = int(app_attrs["overwrite"])
    except (KeyError, ValueError):
        _overwrite = False

    # this is to be more sure to replace the right tags
    nonce = "%s%s" % (str(time.time()).replace(".", ""), random.randrange(10 ** 6, 10 ** 7))

    # ALB 2004-12-05
    global for_version
    try:
        for_version = tuple([int(t) for t in app_attrs["for_version"].split(".")[:2]])
    except (KeyError, ValueError):
        if common.app_tree is not None:
            for_version = common.app_tree.app.for_version
        else:
            for_version = (2, 4)  # default...

    classes = {}
    _current_extra_modules = {}
    header_lines = [
        "# generated by wxGlade %s on %s%s\n" % (common.version, time.asctime(), common.generated_from()),
        "# To get wxPerl visit http://wxPerl.sourceforge.net/\n\n",
        "use Wx 0.15 qw[:allclasses];\nuse strict;\n",
    ]
    if not config.preferences.write_timestamp:
        header_lines[0] = "# generated by wxGlade %s%s\n" % (common.version, common.generated_from())

    multiple_files = multi_files
    if not multiple_files:
        global output_file, output_file_name
        if not _overwrite and os.path.isfile(out_path):
            # the file exists, we must keep all the lines not inside a wxGlade
            # block. NOTE: this may cause troubles if out_path is not a valid
            # perl file, so be careful!
            previous_source = SourceFileContent(out_path)
        else:
            # if the file doesn't exist, create it and write the ``intro''
            previous_source = None
            output_file = cStringIO.StringIO()
            output_file_name = out_path
            output_file.write("#!/usr/bin/perl -w -- \n")
            for line in header_lines:
                output_file.write(line)
            output_file.write("#<%swxGlade extra_modules>\n" % nonce)
            output_file.write("\n")
    else:
        previous_source = None
        global out_dir
        if not os.path.isdir(out_path):
            raise XmlParsingError("'path' must be a directory when generating" " multiple output files")
        out_dir = out_path