home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- from __future__ import with_statement
- SKIN_FILENAME = 'skin.yaml'
- VARIANT_DIR = 'Variants'
- SKIN_NAME_KEY = 'name'
- VARIANT_NAME_KEY = 'variant'
- alternating_keys = [
- 'buddiespanel.backgrounds.buddy',
- 'filetransfers.backgrounds.normal']
- FONT_MULTIPLY_KEY = 'FontPlatform'
- FONT_MULTIPLY = {
- 'windows_mac': 4 / 3,
- 'mac_windows': 3 / 4 }
- __metaclass__ = type
- from operator import isMappingType
- from util.primitives import Storage, traceguard, dictrecurse
- S = Storage
- from path import path
- from copy import deepcopy
- import syck
- import sys
- import os
- import wx
- from wx import GetTopLevelWindows, GetApp, MessageBox
- from traceback import print_exc
- from gui.toolbox import colorfor
- from types import FunctionType
- from util.merge import merge, merge_keys, tolower
- from util.primitives import syck_error_message
- from util.introspect import funcinfo
- from gui.skin.skintransform import transform
- from gui.skin import SkinException
- from peak.util.plugins import Hook
- from logging import getLogger
- log = getLogger('skin')
- info = log.info
- import stdpaths
- activeTree = None
- resource_paths = [
- path('res').abspath(),
- path('res/skins/default').abspath()]
-
- try:
- userdata = stdpaths.userdata
- except AttributeError:
- log.warning('not including stdpaths.userdata in skin lookup paths')
-
- resource_paths.append(userdata / 'skins')
- del userdata
-
- def get(dottedpath, default = sentinel):
-
- try:
- return lookup(activeTree, dottedpath.lower().split('.'))
- except (KeyError, TypeError):
- if default is sentinel:
- raise SkinException('not found: ' + dottedpath)
- default is sentinel
- return default
- except:
- print_exc()
- print >>sys.stderr, 'exception for "%s"' % dottedpath
-
-
-
- def refresh_wx_tree():
- UMenu = UMenu
- import gui.uberwidgets.umenu
- skip = (UMenu,)
- for window in GetTopLevelWindows():
- window.FrozenQuick().__enter__()
-
- try:
- _updateskin(window, skip)
- finally:
- pass
-
-
- UMenu.CallAll(UMenu.UpdateSkin)
-
-
- def _updateskin(window, skip):
-
- try:
- update = window.UpdateSkin
- except AttributeError:
- pass
-
- if not isinstance(window, skip):
-
- try:
- update()
- print_exc()
-
-
- for c in window.Children:
- _updateskin(c, skip)
-
-
-
- def alternating(tree):
- SkinList = SkinList
- import gui.skin.skinobjects
- for k in alternating_keys:
- spath = k.split('.')
-
- try:
- val = lookup(tree, spath)
- except AttributeError:
- continue
-
- if isinstance(val, list):
- lookup(tree, spath[:-1])[spath[-1]] = SkinList(val)
- continue
-
-
-
- def fontplatform(finaltree):
- skin = skin
- import gui
- platformName = platformName
- import config
- platform_aliases = {
- 'win': 'windows',
- 'mac': 'mac' }
- fontplatform = finaltree.get(FONT_MULTIPLY_KEY.lower(), None)
- fp = '%s_%s' % (fontplatform, platform_aliases.get(platformName))
- factor = FONT_MULTIPLY.get(fp, 1)
- log.info('new font multiply factor: %s -> %s', fp, factor)
- skin.font_multiply_factor = factor
-
- pretransforms = [
- fontplatform]
- posttransforms = [
- alternating]
- _loaded_fonts = set()
-
- def skin_font_files(paths):
- fonts = []
- for p in paths:
- fontsdir = p.parent / 'fonts'
- if fontsdir.isdir():
- for ext in ('ttf', 'fon'):
- fonts.extend(fontsdir.files('*.' + ext))
-
-
- return [ f.abspath().normpath() for f in fonts ]
-
-
- def load_skinfonts(paths):
- global _loaded_fonts
- import config
- if not config.platform == 'win':
- return None
- loadfont = loadfont
- unloadfont = unloadfont
- import gui.native.win.winfonts
- fonts = set(skin_font_files(paths))
- if _loaded_fonts == fonts:
- return None
- for fontfile in _loaded_fonts - fonts:
- traceguard.__enter__()
-
- try:
- res = unloadfont(fontfile, enumerable = True)
- log.debug('unloaded font %r: %r', fontfile, res)
- finally:
- pass
-
-
- for fontfile in fonts - _loaded_fonts:
- traceguard.__enter__()
-
- try:
- res = loadfont(fontfile, enumerable = True)
- log.debug('loading font %r: %r', fontfile, res)
- finally:
- pass
-
-
- _loaded_fonts = fonts
- import wx.webview as wx
-
-
- def set_active(skin, variant = None, update_gui = False, callback = None):
- global activeTree
- log.info('set_active(%r, %r)', skin, variant)
- app = GetApp()
- skinname = skin
- for rp in resource_paths:
- skin = path(rp) / 'skins' / skinname
- skinpath = skin / SKIN_FILENAME
- if skinpath.isfile():
- break
- continue
-
- import hooks
- hooks.notify('skin.set.pre', skin, variant)
- if variant is not None:
- variant = skin / VARIANT_DIR / variant + '.yaml'
-
- default_path = resource_paths[0] / 'skins' / 'default' / SKIN_FILENAME
- paths = [
- default_path]
- insert_position = 1
- if os.path.basename(skin) == 'native':
- paths.append(resource_paths[0] / path('skins') / 'silverblue' / SKIN_FILENAME)
-
- if not skinpath.isfile():
- log.critical('cannot find %r (%r.isfile() == False, defaulting to silverblue', skin, skinpath)
- skin = resource_paths[0] / path('skins') / 'silverblue'
- skinpath = skin / SKIN_FILENAME
- variant = None
-
- if default_path.abspath() != skinpath.abspath():
- paths.append(skinpath)
-
- if variant is not None:
- if variant.isfile():
- paths.append(variant)
- else:
- log.warning('cannot find variant %r for skin %r', variant, skin)
-
- if not update_gui and hasattr(app, 'skin') and app.skin.paths == paths:
- log.info('skin did not change, returning')
-
- traceguard.__enter__()
-
- try:
- load_skinfonts(paths)
- finally:
- pass
-
- log.info('loading YAML from %d path(s):\n %r', len(paths), '\n '.join(paths))
- trees = get_skintrees(skin, paths, insert_position)
- for tree in trees[1:]:
- tree.pop('appdefaults', None)
-
- default_tree = deepcopy(trees[0])
- combined_tree = merge(*trees, **dict(keytransform = (lambda k: getattr(k, 'lower', (lambda k: k))()
- )))
- app.skin.update(path = skinpath.parent, paths = get_image_load_paths(paths))
- for pretransform in pretransforms:
- pretransform(combined_tree)
-
-
- try:
- finaltree = transform(combined_tree)
- except Exception:
- None if not hasattr(app, 'skin') else traceguard
- e = None if not hasattr(app, 'skin') else traceguard
- MessageBox('There was an error processing skin "%s":\n\n%s' % (skin, str(e)), 'Skin Error')
- print_exc()
- finaltree = transform(default_tree)
- except:
- None if not hasattr(app, 'skin') else traceguard
-
- for posttransform in posttransforms:
- posttransform(finaltree)
-
- activeTree = finaltree
- app.skin.update(tree = finaltree)
-
- def done():
- if update_gui:
- refresh_wx_tree()
-
- import gc
- gc.collect()
-
- wx.CallAfter(done)
- return app.skin
-
-
- def list_skins():
- resource_paths = sys.modules['gui.skin.skintree'].resource_paths
- skins = []
- for res in resource_paths:
- skinrootdir = res / 'skins'
- if skinrootdir.isdir():
- for skindir in skinrootdir.dirs():
- if skindir.name.startswith('.'):
- continue
-
- rootfile = skindir / SKIN_FILENAME
- if rootfile.isfile() and skindir.name != 'default':
- skins.append(skindesc(rootfile))
- continue
-
-
- return skins
-
-
- def get_skintrees(skin, paths, insert_position):
- trees = []
- errors = []
- successful = False
- for f in paths:
-
- try:
- trees.append(load_skinfile(f))
- except Exception:
- e = None
- errors.append(e)
- del e
- continue
-
- successful = True
-
- if not successful:
- MessageBox('There was an error loading skin "%s":\n\n%s' % (skin, '\n'.join((lambda .0: for e in .0:
- str(e))(errors))), 'Skin Error')
-
- SkinStorage = SkinStorage
- import gui.skin
- to_skinstorage = dictrecurse(SkinStorage)
- for hook in Hook('digsby.skin.load.trees'):
- hook_trees = hook()
- if hook_trees is not None:
- trees[insert_position:insert_position] = (lambda .0: for t in .0:
- to_skinstorage(deepcopy(t)))(hook_trees)
- continue
- (None,)
-
- return trees
-
-
- def get_image_load_paths(paths):
- image_load_paths = [ p.parent for p in paths ]
- for pathhook in Hook('digsby.skin.load.skinpaths'):
- hook_paths = pathhook()
- if hook_paths is not None:
- image_load_paths.extend(hook_paths)
- continue
- []
-
- return image_load_paths
-
-
- def quick_name_lookup(p, **names):
- names = dict((lambda .0: for key, name in .0:
- (name.lower(), key))(names.iteritems()))
- vals = { }
-
- try:
- f = _[1]
- s = syck.load(f)
- for k, name in getattr(s, ('iteritems',), (lambda : s))():
-
- try:
- k = k.lower()
- except AttributeError:
- p.open('r').__exit__
- p.open('r').__exit__
- p.open('r')
- continue
-
- if k in names and isinstance(name, basestring):
- vals[k] = name
- if len(vals) == len(names):
- break
-
- len(vals) == len(names)
- finally:
- pass
-
- return vals
-
-
- def skindesc(rootfile):
- rootfile = path(rootfile)
- aliases = quick_name_lookup(rootfile, skin_alias = SKIN_NAME_KEY, novariant_alias = VARIANT_NAME_KEY)
- variants = []
- vardir = rootfile.parent / VARIANT_DIR
- if vardir.isdir():
- for variant in vardir.files('*.yaml'):
- if variant != rootfile:
- valias = quick_name_lookup(variant, variant_alias = VARIANT_NAME_KEY).get('variant_alias', variant.namebase)
- variants.append(S(path = variant, alias = valias))
- continue
-
-
- return S(name = rootfile.parent.name, alias = aliases.get(SKIN_NAME_KEY, rootfile.parent.name), novariant_alias = aliases.get(VARIANT_NAME_KEY, _('(none)')), base = rootfile, variants = variants)
-
-
- def lookup(root, pathseq):
- elem = root
- for p in pathseq:
- elem = elem[p]
-
- return elem
-
-
- def load_skinfile(filepath):
- if not filepath.isfile():
- raise ValueError('file %s does not exist' % filepath)
- filepath.isfile()
- return globals()['load_%s' % filepath.ext[1:]](filepath)
-
-
- def load_yaml(str_or_path):
- SkinStorage = SkinStorage
- import gui.skin
- (bytes, fpath) = getcontent(str_or_path)
- if isinstance(bytes, unicode):
- bytes = bytes.encode('utf-8')
-
- if not bytes:
- raise SkinException('no bytes in ' + str_or_path)
- bytes
- content = load_yaml_content(bytes)
-
- try:
- root = syck.load(content)
- except syck.error:
- e = None
- raise SkinException(syck_error_message(e, fpath))
-
- return merge_keys(root, maptype = SkinStorage)
-
-
- def load_yaml_content(bytes, included_paths = None):
- if included_paths is None:
- included_paths = []
-
- lines = []
- for line in bytes.split('\n'):
- if line.lower().startswith('include:'):
- include_path = line[line.index(':') + 1:].strip()
- line = load_yaml_include(include_path, included_paths)
-
- lines.append(line)
-
- return '\n'.join(lines)
-
-
- def load_yaml_include(incpath, included_paths):
- incpath = path(incpath).abspath()
- if incpath not in included_paths:
- bytes = load_yaml_content(incpath.bytes(), included_paths)
- included_paths += [
- incpath]
- return bytes
- return ''
-
- from util.data_importer import zipopen
- from contextlib import closing
-
- def getcontent(str_or_path):
-
- try:
- if str_or_path.isfile():
- return (str_or_path.bytes(), str_or_path)
-
- try:
- f = _[1]
- return (f.read(), str_or_path)
- finally:
- pass
-
- except AttributeError:
- return (str_or_path, '<String Input>')
-
-
- if __name__ == '__main__':
- s = 'common:\n- &mycolor red\n\nmenu:\n color: *mycolor\n\ncommon:\n- &mycolor blue'
- print load_yaml(s)
-
-