Example #1
0
 def platform_default_log_location(self):
     """Returns default log file location for the platform we are running on"""
     if sys.platform == "darwin":
         file = os.path.expanduser("~/Library/Logs/tilecutter.log")
         source = "darwin"
     elif sys.platform == "win32":
         file = os.path.join(getenvvar(u"APPDATA"), "tilecutter\\tilecutter.log")
         #file = os.path.normpath(os.path.expanduser("~/Application Data/tilecutter/tilecutter.log"))
         #file = unicode(file, sys.getfilesystemencoding())
         source = "win32"
     else:
         file = os.path.expanduser("~/.tilecutter/tilecutter.log")
         source = "unix"
     return file, source
Example #2
0
    def __init__(self, parent):
        self.parent = parent
        # Location of save file, by default this is the user's home directory
        # If user has previously selected a place to save project files to, use this as the default save path
        # Whenever the user does a Save-As this config directive should be updated

        # Use userprofile on all platforms as default
        if sys.platform == "darwin":
            location = os.path.expanduser("~")
        elif sys.platform == "win32":
            location = getenvvar("USERPROFILE")
        else:
            location = os.path.expanduser("~")

        # Otherwise use location of program
        # Depending on how/when os.path.expanduser can fail this may not be needed but just in case!
        if location == "~":
            self.save_location = self.test_path(
                self.parent.parent.start_directory)
        else:
            self.save_location = self.test_path(location)

        # As initialised, project is unsaved, so other paths relative to the default value
        self.saved = False

        # Location of .dat file output (relative to save location)
        self.datfile_location = "output.dat"
        self.writedat = True
        # Location of .png file output (relative to dat file)
        self.pngfile_location = os.path.join("images", "output.png")

        # Location of .pak output file (relative to save location)
        # Blank by default so that pak file name is produced by building type/name
        self.pakfile_location = ""

        try:
            logging.info(
                "save_location: %s, datfile_location: %s, pngfile_location: %s, pakfile_location: %s"
                % (self.save_location, self.datfile_location,
                   self.pngfile_location, self.pakfile_location))
        except UnicodeDecodeError:
            logging.error("Unicode Decode Error")
            logging.error(
                "save_location: %s, datfile_location: %s, pngfile_location: %s, pakfile_location: %s"
                % (self.save_location, self.datfile_location,
                   self.pngfile_location, self.pakfile_location))
Example #3
0
    def __init__(self, parent):
        self.parent = parent
        # Location of save file, by default this is the user's home directory
        # If user has previously selected a place to save project files to, use this as the default save path
        # Whenever the user does a Save-As this config directive should be updated

        # Use userprofile on all platforms as default
        if sys.platform == "darwin":
            location = os.path.expanduser(u"~")
        elif sys.platform == "win32":
            location = getenvvar(u"USERPROFILE")
        else:
            location = os.path.expanduser(u"~")

        # Otherwise use location of program
        # Depending on how/when os.path.expanduser can fail this may not be needed but just in case!
        if location == u"~":
            self.save_location = self.test_path(self.parent.parent.start_directory)
        else:
            self.save_location = self.test_path(location)

        # As initialised, project is unsaved, so other paths relative to the default value
        self.saved = False

        # Location of .dat file output (relative to save location)
        self.datfile_location = u"output.dat"
        self.writedat = True
        # Location of .png file output (relative to dat file)
        self.pngfile_location = os.path.join(u"images", u"output.png")

        # Location of .pak output file (relative to save location)
        # Blank by default so that pak file name is produced by building type/name
        self.pakfile_location = u""

        try:
            debug(u"save_location: %s, datfile_location: %s, pngfile_location: %s, pakfile_location: %s" % (self.save_location,
                                                                                                           self.datfile_location,
                                                                                                           self.pngfile_location,
                                                                                                           self.pakfile_location))
        except UnicodeDecodeError:
            debug(u"Unicode Decode Error")
            debug(self.save_location)
            debug(self.datfile_location)
            debug(self.pngfile_location)
            debug(self.pakfile_location)
Example #4
0
    def init_save_location(self):
        """Return our initial save location based on platform-specific settings"""
        # Use userprofile on all platforms as default
        if sys.platform == "darwin":
            save_location = os.path.expanduser("~")
        elif sys.platform == "win32":
            save_location = getenvvar("USERPROFILE")
        else:
            save_location = os.path.expanduser("~")

        # Otherwise use location of program
        # Depending on how/when os.path.expanduser can fail this may not be needed but just in case!
        if save_location == "~":
            save_location = self.test_path(self.parent.parent.start_directory)
        else:
            save_location = self.test_path(save_location)

        ## logging.debug(u"project: init_save_location - as: %s, datfile_location: %s, pngfile_location: %s, pakfile_location: %s" % (
        ##                                   self.save_location, self.datfile_location, self.pngfile_location, self.pakfile_location))
        return save_location
Example #5
0
    def init_save_location(self):
        """Return our initial save location based on platform-specific settings"""
        # Use userprofile on all platforms as default
        if sys.platform == "darwin":
            save_location = os.path.expanduser(u"~")
        elif sys.platform == "win32":
            save_location = getenvvar(u"USERPROFILE")
        else:
            save_location = os.path.expanduser(u"~")

        # Otherwise use location of program
        # Depending on how/when os.path.expanduser can fail this may not be needed but just in case!
        if save_location == u"~":
            save_location = self.test_path(self.parent.parent.start_directory)
        else:
            save_location = self.test_path(save_location)

#        debug(u"project: init_save_location - as: %s, datfile_location: %s, pngfile_location: %s, pakfile_location: %s" % (self.save_location,
#                                                                                                       self.datfile_location,
#                                                                                                       self.pngfile_location,
#                                                                                                       self.pakfile_location))
        return save_location
Example #6
0
class Config(object):
    """Program configuration object"""
    # The first time this object is instantiated, config is populated
    config = {}

    # If a tc.config or tilecutter.config file exists in the program directory use that to load config from
    # (to permit setting of global config, or for backwards compatibility with existing installs)
    if os.path.isfile("tc.config"):
        main_path = os.path.abspath(".")
        logs_path = os.path.join(main_path, "tilecutter.log")
        conf_path = os.path.join(main_path, "tc.config")
        source = "legacy: tc.config in program directory"
    else:
        if os.path.isfile("tilecutter.config"):
            main_path = os.path.abspath(".")
            source = "override: tilecutter.config in program directory"
        elif sys.platform == "win32":
            main_path = os.path.join(getenvvar("APPDATA"), "tilecutter\\")
            source = "win32 auto location"
        else:
            main_path = os.path.expanduser("~/.tilecutter/")
            source = "unix auto location"

        conf_path = os.path.join(main_path, "tilecutter.config")
        logs_path = os.path.join(main_path, "tilecutter.log")

        if sys.platform == "darwin":
            logs_path = os.path.expanduser("~/Library/Logs/tilecutter.log")
            source = "darwin auto location"

    # All externally settable variables must be included in defaults, or they won't be read
    # by Config on loading. Non-externally settable variables can be placed in internals
    # Internals will always be checked before config
    defaults = {
        "debug_on": True,
        "debug_level": 2,
        "logfile": logs_path,
        "logfile_platform_default": True,
        "transparent":     [231, 255, 255],
        "transparent_bg": [[153, 153, 153], [103, 103, 103]],
        "default_paksize": 64,
        "valid_image_extensions": [".png"],
        "OFFSET_NEGATIVE_ALLOWED": False,
        "window_size":     [-1, -1],
        "window_position": [-1, -1],
        "window_maximised": False,

        "last_save_path": "",
        "last_image_path": "",

        "negative_offset_allowed": False,
        "default_image_path": "",

        "path_to_makeobj": "",
        "write_dat": True,

        "default_language": "English",
    }
    internals = {
        "version": "1.2.1",
        "TCPversion": "1.1",
        "choicelist_paksize": [16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 256, 320, 512],
        "choicelist_views":   [1, 2, 4],
        "choicelist_dims":    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
        "choicelist_dims_z":  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
    }

    def __init__(self):
        """First time Config is intialised, call the read_in function to init all the settings
        from file. Subsequent instances of Config all refer to the same in-memory copy of
        the settings, can add a setting by name, set a setting's value, remove a setting
        re-read settings from file etc."""
        if Config.config == {}:
            try:
                f = codecs.open(self.conf_path, "r", "UTF-8")
                file_config = json.loads(f.read())
                f.close()
            except IOError:
                # If unable to open config file, abort and use defaults
                file_config = {}
            # Now merge this into the defaults, value from default is always overrided if there
            # is a value in the file, also means only keys with a default set are loaded in
            for k in list(Config.defaults.keys()):
                if k in file_config:
                    Config.config[k] = file_config.pop(k)
                else:
                    Config.config[k] = Config.defaults[k]

    def __str__(self):
        """Return a string representing this object"""
        return str(Config.config)

    def __getattr__(self, name):
        """Lookup method by . access e.g. z = x.y"""
        if name in Config.internals:
            return Config.internals[name]
        elif name in Config.config:
            return Config.config[name]
        else:
            raise AttributeError(name)

    def __setattr__(self, name, value):
        """Set method by . access e.g. x.y = z"""
        # Internal attributes are immutable
        if name in Config.internals:
            pass
        elif name in Config.defaults:
            Config.config[name] = value
            self.save()
        else:
            object.__setattr__(self, name, value)

    def __getitem__(self, key):
        """Lookup method by dict access e.g. z = x['y']"""
        if key in Config.internals:
            return Config.internals[key]
        elif key in Config.config:
            return Config.config[key]
        else:
            raise KeyError(key)

    def __setitem__(self, key, value):
        """Set method by dict access e.g. x['y'] = z"""
        # Again, internals are immutable
        if key in Config.internals:
            pass
        elif key in Config.defaults:
            Config.config[key] = value
            self.save()
        else:
            raise KeyError(key)

    def save(self):
        """Save the current config out to the config file"""
        # Confirm if path exists, create directories if needed
        if not os.path.isdir(os.path.split(self.conf_path)[0]):
            os.makedirs(os.path.split(self.conf_path)[0])

        try:
            f = codecs.open(self.conf_path, "w", "UTF-8")
            f.write(json.dumps(Config.config, ensure_ascii=False, sort_keys=True, indent=4))
            f.close()
        except IOError:
            logging.error("IOError working with file %s, likely this path doesn't exist or the user doesn't have permission to write to it" % self.conf_path)
            return False

        return True