Top

art_import module

mport os, traceback
rom art import Art, ART_FILE_EXTENSION, DEFAULT_CHARSET
rom ui_file_chooser_dialog import GenericImportChooserDialog
lass ArtImporter:
   
   """
   Class for creating a new Art from data in non-Playscii format.
   Import logic happens in run_import; importer authors simply extend this
   class, override run_import and the class properties below.
   """
   
   format_name = 'ERROR - ArtImporter.format_name'
   "User-visible name for this format, shown in import chooser."
   format_description = "ERROR - ArtImporter.format_description"
   "String (can be triple-quoted) describing format, shown in import chooser."
   allowed_file_extensions = []
   "List of file extensions for this format - if empty, any file is accepted."
   file_chooser_dialog_class = GenericImportChooserDialog
   """
   BaseFileChooserDialog subclass for picking files. Only needed for things
   like custom preview images.
   """
   options_dialog_class = None
   "UIDialog subclass exposing import options to user."
   generic_error = '%s failed to import %s'
   
   def __init__(self, app, in_filename, options={}):
       self.app = app
       new_filename = '%s.%s' % (os.path.splitext(in_filename)[0],
                                 ART_FILE_EXTENSION)
       self.art = self.app.new_art(new_filename)
       # use charset of existing art
       charset = self.app.ui.active_art.charset if self.app.ui.active_art else self.app.load_charset(DEFAULT_CHARSET)
       self.art.set_charset(charset)
       self.app.set_new_art_for_edit(self.art)
       self.art.clear_frame_layer(0, 0, 1)
       self.success = False
       "Set True on successful import."
       # run_import returns success, log it separately from exceptions
       try:
           if self.run_import(in_filename, options):
               self.success = True
       except:
           for line in traceback.format_exc().split('\n'):
               self.app.log(line)
       if not self.success:
           classname = self.__class__.__name__
           self.app.log(self.generic_error % (classname, in_filename))
       # tidy final result, whether or not it was successful
       # TODO: GROSS! figure out why this works but
       # art.geo_changed=True and art.mark_all_frames_changed() don't!
       self.app.ui.erase_selection_or_art()
       self.app.ui.undo()
       # adjust for new art size and set it active
       self.app.ui.adjust_for_art_resize(self.art)
       self.app.ui.set_active_art(self.art)
   
   def set_art_charset(self, charset_name):
       "Convenience function for setting charset by name from run_import."
       self.art.set_charset_by_name(charset_name)
   
   def set_art_palette(self, palette_name):
       "Convenience function for setting palette by name from run_import."
       self.art.set_palette_by_name(palette_name)
   
   def resize(self, new_width, new_height):
       "Convenience function for resizing art from run_import"
       self.art.resize(new_width, new_height)
       self.app.ui.adjust_for_art_resize(self.art)
   
   def run_import(self, in_filename, options):
       """
       Contains the actual import logic. Read input file, set Art
       size/charset/palette, set tiles from data, return success.
       """
       return False

Module variables

var ART_FILE_EXTENSION

var DEFAULT_CHARSET

Classes

class ArtImporter

Class for creating a new Art from data in non-Playscii format. Import logic happens in run_import; importer authors simply extend this class, override run_import and the class properties below.

class ArtImporter:
    
    """
    Class for creating a new Art from data in non-Playscii format.
    Import logic happens in run_import; importer authors simply extend this
    class, override run_import and the class properties below.
    """
    
    format_name = 'ERROR - ArtImporter.format_name'
    "User-visible name for this format, shown in import chooser."
    format_description = "ERROR - ArtImporter.format_description"
    "String (can be triple-quoted) describing format, shown in import chooser."
    allowed_file_extensions = []
    "List of file extensions for this format - if empty, any file is accepted."
    file_chooser_dialog_class = GenericImportChooserDialog
    """
    BaseFileChooserDialog subclass for picking files. Only needed for things
    like custom preview images.
    """
    options_dialog_class = None
    "UIDialog subclass exposing import options to user."
    generic_error = '%s failed to import %s'
    
    def __init__(self, app, in_filename, options={}):
        self.app = app
        new_filename = '%s.%s' % (os.path.splitext(in_filename)[0],
                                  ART_FILE_EXTENSION)
        self.art = self.app.new_art(new_filename)
        # use charset of existing art
        charset = self.app.ui.active_art.charset if self.app.ui.active_art else self.app.load_charset(DEFAULT_CHARSET)
        self.art.set_charset(charset)
        self.app.set_new_art_for_edit(self.art)
        self.art.clear_frame_layer(0, 0, 1)
        self.success = False
        "Set True on successful import."
        # run_import returns success, log it separately from exceptions
        try:
            if self.run_import(in_filename, options):
                self.success = True
        except:
            for line in traceback.format_exc().split('\n'):
                self.app.log(line)
        if not self.success:
            classname = self.__class__.__name__
            self.app.log(self.generic_error % (classname, in_filename))
        # tidy final result, whether or not it was successful
        # TODO: GROSS! figure out why this works but
        # art.geo_changed=True and art.mark_all_frames_changed() don't!
        self.app.ui.erase_selection_or_art()
        self.app.ui.undo()
        # adjust for new art size and set it active
        self.app.ui.adjust_for_art_resize(self.art)
        self.app.ui.set_active_art(self.art)
    
    def set_art_charset(self, charset_name):
        "Convenience function for setting charset by name from run_import."
        self.art.set_charset_by_name(charset_name)
    
    def set_art_palette(self, palette_name):
        "Convenience function for setting palette by name from run_import."
        self.art.set_palette_by_name(palette_name)
    
    def resize(self, new_width, new_height):
        "Convenience function for resizing art from run_import"
        self.art.resize(new_width, new_height)
        self.app.ui.adjust_for_art_resize(self.art)
    
    def run_import(self, in_filename, options):
        """
        Contains the actual import logic. Read input file, set Art
        size/charset/palette, set tiles from data, return success.
        """
        return False

Ancestors (in MRO)

Class variables

var allowed_file_extensions

List of file extensions for this format - if empty, any file is accepted.

var file_chooser_dialog_class

BaseFileChooserDialog subclass for picking files. Only needed for things like custom preview images.

var format_description

String (can be triple-quoted) describing format, shown in import chooser.

var format_name

User-visible name for this format, shown in import chooser.

var generic_error

var options_dialog_class

UIDialog subclass exposing import options to user.

Static methods

def __init__(

self, app, in_filename, options={})

Initialize self. See help(type(self)) for accurate signature.

def __init__(self, app, in_filename, options={}):
    self.app = app
    new_filename = '%s.%s' % (os.path.splitext(in_filename)[0],
                              ART_FILE_EXTENSION)
    self.art = self.app.new_art(new_filename)
    # use charset of existing art
    charset = self.app.ui.active_art.charset if self.app.ui.active_art else self.app.load_charset(DEFAULT_CHARSET)
    self.art.set_charset(charset)
    self.app.set_new_art_for_edit(self.art)
    self.art.clear_frame_layer(0, 0, 1)
    self.success = False
    "Set True on successful import."
    # run_import returns success, log it separately from exceptions
    try:
        if self.run_import(in_filename, options):
            self.success = True
    except:
        for line in traceback.format_exc().split('\n'):
            self.app.log(line)
    if not self.success:
        classname = self.__class__.__name__
        self.app.log(self.generic_error % (classname, in_filename))
    # tidy final result, whether or not it was successful
    # TODO: GROSS! figure out why this works but
    # art.geo_changed=True and art.mark_all_frames_changed() don't!
    self.app.ui.erase_selection_or_art()
    self.app.ui.undo()
    # adjust for new art size and set it active
    self.app.ui.adjust_for_art_resize(self.art)
    self.app.ui.set_active_art(self.art)

def resize(

self, new_width, new_height)

Convenience function for resizing art from run_import

def resize(self, new_width, new_height):
    "Convenience function for resizing art from run_import"
    self.art.resize(new_width, new_height)
    self.app.ui.adjust_for_art_resize(self.art)

def run_import(

self, in_filename, options)

Contains the actual import logic. Read input file, set Art size/charset/palette, set tiles from data, return success.

def run_import(self, in_filename, options):
    """
    Contains the actual import logic. Read input file, set Art
    size/charset/palette, set tiles from data, return success.
    """
    return False

def set_art_charset(

self, charset_name)

Convenience function for setting charset by name from run_import.

def set_art_charset(self, charset_name):
    "Convenience function for setting charset by name from run_import."
    self.art.set_charset_by_name(charset_name)

def set_art_palette(

self, palette_name)

Convenience function for setting palette by name from run_import.

def set_art_palette(self, palette_name):
    "Convenience function for setting palette by name from run_import."
    self.art.set_palette_by_name(palette_name)

Instance variables

var app

var art

var success

Set True on successful import.