<commit_before># -*- coding:utf-8 -*-
'''
Function for building whole packed version of highlight.js out of
pre-packed modules.
'''

import os
import sys
import re
import optparse
import subprocess

REPLACES = {
    'defaultMode': 'dM',
    'case_insensitive': 'cI',
    'lexems': 'l',
    'contains': 'c',
    'keywords': 'k',
    'subLanguage': 'sL',
    'modes': 'm',
    'className': 'cN',
    'begin': 'b',
    'end': 'e',
    'endsWithParent': 'eW',
    'illegal': 'i',
    'excludeBegin': 'eB',
    'excludeEnd': 'eE',
    'returnBegin': 'rB',
    'returnEnd': 'rE',
    'noMarkup': 'nM',
    'relevance': 'r',
    'IDENT_RE': 'IR',
    'UNDERSCORE_IDENT_RE': 'UIR',
    'NUMBER_RE': 'NR',
    'C_NUMBER_RE': 'CNR',
    'RE_STARTERS_RE': 'RSR',
    'APOS_STRING_MODE': 'ASM',
    'QUOTE_STRING_MODE': 'QSM',
    'BACKSLASH_ESCAPE': 'BE',
    'C_LINE_COMMENT_MODE': 'CLCM',
    'C_BLOCK_COMMENT_MODE': 'CBLCLM',
    'HASH_COMMENT_MODE': 'HCM',
    'C_NUMBER_MODE': 'CNM',
    'NUMBER_MODE': 'NM',
}

LIBRARY_REPLACES = {
    'beginRe': 'bR',
    'endRe': 'eR',
    'illegalRe': 'iR',
    'lexemsRe': 'lR',
    'sub_modes': 'sm',
    'terminators': 't',
    'keywordGroups': 'kG',
}

def compress_content(tools_path, content):
    cmd = 'java -jar %s --type js' % os.path.join(tools_path, 'yuicompressor.jar')

    def replace(content, s, r):
        return re.sub(r'(?<=[^\w"\'|])%s(?=[^\w"\'|])' % s, r, content)

    for s, r in REPLACES.items():
        content = replace(content, s, r)
    if not parse_header(content): # this is the highlight.js file, not a language file
        content = re.sub(r'(block|parentNode)\.cN', r'\1.className', content)
        for s, r in LIBRARY_REPLACES.items():
            content = replace(content, s, r)
    p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    p.stdin.write(content)
    p.stdin.close()
    content = p.stdout.read()
    p.stdout.close()
    return content

def parse_header(content):
    '''
    Parses possible language description header from a file. If a header is found returns it
    as dict, otherwise returns None.
    '''
    match = re.search(r'^\s*/\*(.*?)\*/', content, re.S)
    if not match:
        return
    headers = match.group(1).split('\n')
    headers = dict(h.strip().split(': ') for h in headers if ': ' in h)
    return headers if 'Language' in headers else None

def language_filenames(src_path, languages):
    '''
    Resolves dependencies and returns the list of actual language filenames
    '''
    lang_path = os.path.join(src_path, 'languages')
    filenames = os.listdir(lang_path)
    infos = [
        (parse_header(open(os.path.join(lang_path, f)).read(1024)), f)
        for f in filenames]
    infos = [(i, f) for i, f in infos if i]

    if languages:
        infos = [(i, f) for i, f in infos if os.path.splitext(f)[0] in languages]

    def append(filename):
        if filename not in filenames:
            filenames.append(filename)

    filenames = []
    for info, filename in infos:
        if 'Requires' in info:
            requires = [r.strip() for r in info['Requires'].split(',')]
            for r in requires:
                append(r)
        append(filename)
    return [os.path.join(lang_path, f) for f in filenames]

def file_content(filename, compressor):
    content = open(filename).read()
    content = compressor(content)
    return content

def build(root, compress, languages):
    src_path = os.path.join(root, 'src')
    tools_path = os.path.join(root, 'tools')
    files = [os.path.join(src_path, 'highlight.js')] + \
            language_filenames(src_path, languages)
    f = open(os.path.join(src_path, 'highlight.pack.js'), 'w')
    for file in files:
        print file
        content = open(file).read()
        if compress:
            content = compress_content(tools_path, content)
        f.write(content)
    f.close()

if __name__ == '__main__':
    parser = optparse.OptionParser()
    parser.add_option(
        '-n', '--no-compress',
        dest = 'compress', action = 'store_false', default = True,
        help = 'don\'t compress source files',
    )
    parser.set_usage('%prog [options] [<language>...]')
    options, args = parser.parse_args()
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    build(root, options.compress, args)
<commit_msg>Removed file_content from build.py<commit_after># -*- coding:utf-8 -*-
'''
Function for building whole packed version of highlight.js out of
pre-packed modules.
'''

import os
import sys
import re
import optparse
import subprocess

REPLACES = {
    'defaultMode': 'dM',
    'case_insensitive': 'cI',
    'lexems': 'l',
    'contains': 'c',
    'keywords': 'k',
    'subLanguage': 'sL',
    'modes': 'm',
    'className': 'cN',
    'begin': 'b',
    'end': 'e',
    'endsWithParent': 'eW',
    'illegal': 'i',
    'excludeBegin': 'eB',
    'excludeEnd': 'eE',
    'returnBegin': 'rB',
    'returnEnd': 'rE',
    'noMarkup': 'nM',
    'relevance': 'r',
    'IDENT_RE': 'IR',
    'UNDERSCORE_IDENT_RE': 'UIR',
    'NUMBER_RE': 'NR',
    'C_NUMBER_RE': 'CNR',
    'RE_STARTERS_RE': 'RSR',
    'APOS_STRING_MODE': 'ASM',
    'QUOTE_STRING_MODE': 'QSM',
    'BACKSLASH_ESCAPE': 'BE',
    'C_LINE_COMMENT_MODE': 'CLCM',
    'C_BLOCK_COMMENT_MODE': 'CBLCLM',
    'HASH_COMMENT_MODE': 'HCM',
    'C_NUMBER_MODE': 'CNM',
    'NUMBER_MODE': 'NM',
}

LIBRARY_REPLACES = {
    'beginRe': 'bR',
    'endRe': 'eR',
    'illegalRe': 'iR',
    'lexemsRe': 'lR',
    'sub_modes': 'sm',
    'terminators': 't',
    'keywordGroups': 'kG',
}

def compress_content(tools_path, content):
    cmd = 'java -jar %s --type js' % os.path.join(tools_path, 'yuicompressor.jar')

    def replace(content, s, r):
        return re.sub(r'(?<=[^\w"\'|])%s(?=[^\w"\'|])' % s, r, content)

    for s, r in REPLACES.items():
        content = replace(content, s, r)
    if not parse_header(content): # this is the highlight.js file, not a language file
        content = re.sub(r'(block|parentNode)\.cN', r'\1.className', content)
        for s, r in LIBRARY_REPLACES.items():
            content = replace(content, s, r)
    p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    p.stdin.write(content)
    p.stdin.close()
    content = p.stdout.read()
    p.stdout.close()
    return content

def parse_header(content):
    '''
    Parses possible language description header from a file. If a header is found returns it
    as dict, otherwise returns None.
    '''
    match = re.search(r'^\s*/\*(.*?)\*/', content, re.S)
    if not match:
        return
    headers = match.group(1).split('\n')
    headers = dict(h.strip().split(': ') for h in headers if ': ' in h)
    return headers if 'Language' in headers else None

def language_filenames(src_path, languages):
    '''
    Resolves dependencies and returns the list of actual language filenames
    '''
    lang_path = os.path.join(src_path, 'languages')
    filenames = os.listdir(lang_path)
    infos = [
        (parse_header(open(os.path.join(lang_path, f)).read(1024)), f)
        for f in filenames]
    infos = [(i, f) for i, f in infos if i]

    if languages:
        infos = [(i, f) for i, f in infos if os.path.splitext(f)[0] in languages]

    def append(filename):
        if filename not in filenames:
            filenames.append(filename)

    filenames = []
    for info, filename in infos:
        if 'Requires' in info:
            requires = [r.strip() for r in info['Requires'].split(',')]
            for r in requires:
                append(r)
        append(filename)
    return [os.path.join(lang_path, f) for f in filenames]

def build(root, compress, languages):
    src_path = os.path.join(root, 'src')
    tools_path = os.path.join(root, 'tools')
    files = [os.path.join(src_path, 'highlight.js')] + \
            language_filenames(src_path, languages)
    f = open(os.path.join(src_path, 'highlight.pack.js'), 'w')
    for file in files:
        print file
        content = open(file).read()
        if compress:
            content = compress_content(tools_path, content)
        f.write(content)
    f.close()

if __name__ == '__main__':
    parser = optparse.OptionParser()
    parser.add_option(
        '-n', '--no-compress',
        dest = 'compress', action = 'store_false', default = True,
        help = 'don\'t compress source files',
    )
    parser.set_usage('%prog [options] [<language>...]')
    options, args = parser.parse_args()
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    build(root, options.compress, args)
<|endoftext|>


<commit_before>import types
import sys
import os
from functools import partial

import libcppyy as cppyy_backend
from cppyy import gbl as gbl_namespace
from cppyy import cppdef, include, load_library
from libROOTPythonizations import gROOT, CreateBufferFromAddress

from ._application import PyROOTApplication
_numba_pyversion = (2, 7, 5)
if sys.version_info[:3] > _numba_pyversion:
    # Python <= 2.7.5 cannot use exec in an inner function
    from ._numbadeclare import _NumbaDeclareDecorator

class PyROOTConfiguration(object):
    """Class for configuring PyROOT"""

    def __init__(self):
        self.IgnoreCommandLineOptions = True
        self.ShutDown = True
        self.DisableRootLogon = False
        self.StartGUIThread = True

class _gROOTWrapper(object):
    """Internal class to manage lookups of gROOT in the facade.
       This wrapper calls _finalSetup on the facade when it
       receives a lookup, unless the lookup is for SetBatch.
       This allows to evaluate the command line parameters
       before checking if batch mode is on in _finalSetup
    """

    def __init__(self, facade):
        self.__dict__['_facade'] = facade
        self.__dict__['_gROOT'] = gROOT

    def __getattr__( self, name ):
        if name != 'SetBatch' and self._facade.__dict__['gROOT'] != self._gROOT:
            self._facade._finalSetup()
        return getattr(self._gROOT, name)

    def __setattr__(self, name, value):
        return setattr(self._gROOT, name, value)

class ROOTFacade(types.ModuleType):
    """Facade class for ROOT module"""

    def __init__(self, module, is_ipython):
        types.ModuleType.__init__(self, module.__name__)

        self.module = module
        # Importing all will be customised later
        self.module.__all__ = []

        self.__doc__  = module.__doc__
        self.__name__ = module.__name__
        self.__file__ = module.__file__

        # Inject gROOT global
        self.gROOT = _gROOTWrapper(self)

        # Expose some functionality from CPyCppyy extension module
        self._cppyy_exports = [ 'nullptr', 'bind_object', 'as_cobject', 'addressof',
                                'SetMemoryPolicy', 'kMemoryHeuristics', 'kMemoryStrict',
                                'SetOwnership' ]
        for name in self._cppyy_exports:
            setattr(self, name, getattr(cppyy_backend, name))
        # For backwards compatibility
        self.MakeNullPointer = partial(self.bind_object, 0)
        self.BindObject = self.bind_object
        self.AsCObject = self.as_cobject

        # Initialize configuration
        self.PyConfig = PyROOTConfiguration()

        self._is_ipython = is_ipython

        # Redirect lookups to temporary helper methods
        # This lets the user do some actions before all the machinery is in place:
        # - Set batch mode in gROOT
        # - Set options in PyConfig
        self.__class__.__getattr__ = self._getattr
        self.__class__.__setattr__ = self._setattr

        # Setup import hook
        self._set_import_hook()

    def AddressOf(self, obj):
        # Return an indexable buffer of length 1, whose only element
        # is the address of the object.
        # The address of the buffer is the same as the address of the
        # address of the object

        # addr is the address of the address of the object
        addr = self.addressof(instance = obj, byref = True)

        # Create a buffer (LowLevelView) from address
        return CreateBufferFromAddress(addr)

    def _set_import_hook(self):
        # This hook allows to write e.g:
        # from ROOT.A import a
        # instead of the longer:
        # from ROOT import A
        # from A import a
        try:
            import __builtin__
        except ImportError:
            import builtins as __builtin__  # name change in p3
        _orig_ihook = __builtin__.__import__
        def _importhook(name, *args, **kwds):
            if name[0:5] == 'ROOT.':
                try:
                    sys.modules[name] = getattr(self, name[5:])
                except Exception:
                    pass
            return _orig_ihook(name, *args, **kwds)
        __builtin__.__import__ = _importhook

    def _handle_import_all(self):
        # Called if "from ROOT import *" is executed in the app.
        # Customises lookup in Python's main module to also
        # check in C++'s global namespace

        # Get caller module (jump over the facade frames)
        num_frame = 2
        frame = sys._getframe(num_frame).f_globals['__name__']
        while frame == 'ROOT._facade':
            num_frame += 1
            frame = sys._getframe(num_frame).f_globals['__name__']
        caller = sys.modules[frame]

        # Install the hook
        cppyy_backend._set_cpp_lazy_lookup(caller.__dict__)

    def _fallback_getattr(self, name):
        # Try:
        # - in the global namespace
        # - in the ROOT namespace
        # - in gROOT (ROOT lists such as list of files,
        #   memory mapped files, functions, geometries ecc.)
        # The first two attempts allow to lookup
        # e.g. ROOT.ROOT.Math as ROOT.Math

        if name == '__all__':
            self._handle_import_all()
            # Make the attributes of the facade be injected in the
            # caller module
            raise AttributeError()
        # Note that hasattr caches the lookup for getattr
        elif hasattr(gbl_namespace, name):
            return getattr(gbl_namespace, name)
        elif hasattr(gbl_namespace.ROOT, name):
            return getattr(gbl_namespace.ROOT, name)
        else:
            res = gROOT.FindObject(name)
            if res:
                return res
        raise AttributeError("Failed to get attribute {} from ROOT".format(name))

    def _finalSetup(self):
        # Prevent this method from being re-entered through the gROOT wrapper
        self.__dict__['gROOT'] = gROOT

        # Setup interactive usage from Python
        self.__dict__['app'] = PyROOTApplication(self.PyConfig, self._is_ipython)
        if not self.gROOT.IsBatch() and self.PyConfig.StartGUIThread:
            self.app.init_graphics()

        # Set memory policy to kUseHeuristics.
        # This restores the default in PyROOT which was changed
        # by new Cppyy
        self.SetMemoryPolicy(self.kMemoryHeuristics)

        # Redirect lookups to cppyy's global namespace
        self.__class__.__getattr__ = self._fallback_getattr
        self.__class__.__setattr__ = lambda self, name, val: setattr(gbl_namespace, name, val)

        # Run rootlogon if exists
        self._run_rootlogon()

    def _getattr(self, name):
        # Special case, to allow "from ROOT import gROOT" w/o starting the graphics
        if name == '__path__':
            raise AttributeError(name)

        self._finalSetup()

        return getattr(self, name)

    def _setattr(self, name, val):
        self._finalSetup()

        return setattr(self, name, val)

    def _run_rootlogon(self):
        # Run custom logon file (must be after creation of ROOT globals)
        hasargv = hasattr(sys, 'argv')
        # -n disables the reading of the logon file, just like with root
        if hasargv and not '-n' in sys.argv and not self.PyConfig.DisableRootLogon:
            file_path = os.path.expanduser('~/.rootlogon.py')
            if os.path.exists(file_path):
                # Could also have used execfile, but import is likely to give fewer surprises
                module_name = 'rootlogon'
                if sys.version_info >= (3,5):
                    import importlib.util
                    spec = importlib.util.spec_from_file_location(module_name, file_path)
                    module = importlib.util.module_from_spec(spec)
                    sys.modules[module_name] = module
                    spec.loader.exec_module(module)
                else:
                    import imp
                    imp.load_module(module_name, open(file_path, 'r'), file_path, ('.py','r',1))
                    del imp
            else:
                # If the .py version of rootlogon exists, the .C is ignored (the user can
                # load the .C from the .py, if so desired).
                # System logon, user logon, and local logon (skip Rint.Logon)
                name = '.rootlogon.C'
                logons = [
                    os.path.join(str(self.TROOT.GetEtcDir()), 'system' + name),
                    os.path.expanduser(os.path.join('~', name))
                    ]
                if logons[-1] != os.path.join(os.getcwd(), name):
                    logons.append(name)
                for rootlogon in logons:
                    if os.path.exists(rootlogon):
                        self.TApplication.ExecuteFile(rootlogon)

    # Inject version as __version__ property in ROOT module
    @property
    def __version__(self):
        return self.gROOT.GetVersion()

    # Overload VecOps namespace
    # The property gets the C++ namespace, adds the pythonizations and
    # eventually deletes itself so that following calls go directly
    # to the C++ namespace. This mechanic ensures that we pythonize the
    # namespace lazily.
    @property
    def VecOps(self):
        ns = self._fallback_getattr('VecOps')
        try:
            from libROOTPythonizations import AsRVec
            ns.AsRVec = AsRVec
        except:
            raise Exception('Failed to pythonize the namespace VecOps')
        del type(self).VecOps
        return ns

    # Overload RDF namespace
    @property
    def RDF(self):
        ns = self._fallback_getattr('RDF')
        try:
            from libROOTPythonizations import MakeNumpyDataFrame
            ns.MakeNumpyDataFrame = MakeNumpyDataFrame
        except:
            raise Exception('Failed to pythonize the namespace RDF')
        del type(self).RDF
        return ns

    # Overload TMVA namespace
    @property
    def TMVA(self):
        ns = self._fallback_getattr('TMVA')
        try:
            from libROOTPythonizations import AsRTensor
            ns.Experimental.AsRTensor = AsRTensor
        except:
            raise Exception('Failed to pythonize the namespace TMVA')
        del type(self).TMVA
        return ns

    # Create and overload Numba namespace
    @property
    def Numba(self):
        if sys.version_info[:3] <= _numba_pyversion:
            raise Exception('ROOT.Numba requires Python above version {}.{}.{}'.format(*_numba_pyversion))
        cppdef('namespace Numba {}')
        ns = self._fallback_getattr('Numba')
        ns.Declare = staticmethod(_NumbaDeclareDecorator)
        del type(self).Numba
        return ns

    # Get TPyDispatcher for programming GUI callbacks
    @property
    def TPyDispatcher(self):
        include('ROOT/TPyDispatcher.h')
        major, minor = sys.version_info[0:2]
        load_library('libROOTPythonizations{}_{}'.format(major, minor))
        tpd = gbl_namespace.TPyDispatcher
        type(self).TPyDispatcher = tpd
        return tpd
<commit_msg>Do not attempt to load_library the ROOT Pythonizations module<commit_after>import types
import sys
import os
from functools import partial

import libcppyy as cppyy_backend
from cppyy import gbl as gbl_namespace
from cppyy import cppdef, include
from libROOTPythonizations import gROOT, CreateBufferFromAddress

from ._application import PyROOTApplication
_numba_pyversion = (2, 7, 5)
if sys.version_info[:3] > _numba_pyversion:
    # Python <= 2.7.5 cannot use exec in an inner function
    from ._numbadeclare import _NumbaDeclareDecorator

class PyROOTConfiguration(object):
    """Class for configuring PyROOT"""

    def __init__(self):
        self.IgnoreCommandLineOptions = True
        self.ShutDown = True
        self.DisableRootLogon = False
        self.StartGUIThread = True

class _gROOTWrapper(object):
    """Internal class to manage lookups of gROOT in the facade.
       This wrapper calls _finalSetup on the facade when it
       receives a lookup, unless the lookup is for SetBatch.
       This allows to evaluate the command line parameters
       before checking if batch mode is on in _finalSetup
    """

    def __init__(self, facade):
        self.__dict__['_facade'] = facade
        self.__dict__['_gROOT'] = gROOT

    def __getattr__( self, name ):
        if name != 'SetBatch' and self._facade.__dict__['gROOT'] != self._gROOT:
            self._facade._finalSetup()
        return getattr(self._gROOT, name)

    def __setattr__(self, name, value):
        return setattr(self._gROOT, name, value)

class ROOTFacade(types.ModuleType):
    """Facade class for ROOT module"""

    def __init__(self, module, is_ipython):
        types.ModuleType.__init__(self, module.__name__)

        self.module = module
        # Importing all will be customised later
        self.module.__all__ = []

        self.__doc__  = module.__doc__
        self.__name__ = module.__name__
        self.__file__ = module.__file__

        # Inject gROOT global
        self.gROOT = _gROOTWrapper(self)

        # Expose some functionality from CPyCppyy extension module
        self._cppyy_exports = [ 'nullptr', 'bind_object', 'as_cobject', 'addressof',
                                'SetMemoryPolicy', 'kMemoryHeuristics', 'kMemoryStrict',
                                'SetOwnership' ]
        for name in self._cppyy_exports:
            setattr(self, name, getattr(cppyy_backend, name))
        # For backwards compatibility
        self.MakeNullPointer = partial(self.bind_object, 0)
        self.BindObject = self.bind_object
        self.AsCObject = self.as_cobject

        # Initialize configuration
        self.PyConfig = PyROOTConfiguration()

        self._is_ipython = is_ipython

        # Redirect lookups to temporary helper methods
        # This lets the user do some actions before all the machinery is in place:
        # - Set batch mode in gROOT
        # - Set options in PyConfig
        self.__class__.__getattr__ = self._getattr
        self.__class__.__setattr__ = self._setattr

        # Setup import hook
        self._set_import_hook()

    def AddressOf(self, obj):
        # Return an indexable buffer of length 1, whose only element
        # is the address of the object.
        # The address of the buffer is the same as the address of the
        # address of the object

        # addr is the address of the address of the object
        addr = self.addressof(instance = obj, byref = True)

        # Create a buffer (LowLevelView) from address
        return CreateBufferFromAddress(addr)

    def _set_import_hook(self):
        # This hook allows to write e.g:
        # from ROOT.A import a
        # instead of the longer:
        # from ROOT import A
        # from A import a
        try:
            import __builtin__
        except ImportError:
            import builtins as __builtin__  # name change in p3
        _orig_ihook = __builtin__.__import__
        def _importhook(name, *args, **kwds):
            if name[0:5] == 'ROOT.':
                try:
                    sys.modules[name] = getattr(self, name[5:])
                except Exception:
                    pass
            return _orig_ihook(name, *args, **kwds)
        __builtin__.__import__ = _importhook

    def _handle_import_all(self):
        # Called if "from ROOT import *" is executed in the app.
        # Customises lookup in Python's main module to also
        # check in C++'s global namespace

        # Get caller module (jump over the facade frames)
        num_frame = 2
        frame = sys._getframe(num_frame).f_globals['__name__']
        while frame == 'ROOT._facade':
            num_frame += 1
            frame = sys._getframe(num_frame).f_globals['__name__']
        caller = sys.modules[frame]

        # Install the hook
        cppyy_backend._set_cpp_lazy_lookup(caller.__dict__)

    def _fallback_getattr(self, name):
        # Try:
        # - in the global namespace
        # - in the ROOT namespace
        # - in gROOT (ROOT lists such as list of files,
        #   memory mapped files, functions, geometries ecc.)
        # The first two attempts allow to lookup
        # e.g. ROOT.ROOT.Math as ROOT.Math

        if name == '__all__':
            self._handle_import_all()
            # Make the attributes of the facade be injected in the
            # caller module
            raise AttributeError()
        # Note that hasattr caches the lookup for getattr
        elif hasattr(gbl_namespace, name):
            return getattr(gbl_namespace, name)
        elif hasattr(gbl_namespace.ROOT, name):
            return getattr(gbl_namespace.ROOT, name)
        else:
            res = gROOT.FindObject(name)
            if res:
                return res
        raise AttributeError("Failed to get attribute {} from ROOT".format(name))

    def _finalSetup(self):
        # Prevent this method from being re-entered through the gROOT wrapper
        self.__dict__['gROOT'] = gROOT

        # Setup interactive usage from Python
        self.__dict__['app'] = PyROOTApplication(self.PyConfig, self._is_ipython)
        if not self.gROOT.IsBatch() and self.PyConfig.StartGUIThread:
            self.app.init_graphics()

        # Set memory policy to kUseHeuristics.
        # This restores the default in PyROOT which was changed
        # by new Cppyy
        self.SetMemoryPolicy(self.kMemoryHeuristics)

        # Redirect lookups to cppyy's global namespace
        self.__class__.__getattr__ = self._fallback_getattr
        self.__class__.__setattr__ = lambda self, name, val: setattr(gbl_namespace, name, val)

        # Run rootlogon if exists
        self._run_rootlogon()

    def _getattr(self, name):
        # Special case, to allow "from ROOT import gROOT" w/o starting the graphics
        if name == '__path__':
            raise AttributeError(name)

        self._finalSetup()

        return getattr(self, name)

    def _setattr(self, name, val):
        self._finalSetup()

        return setattr(self, name, val)

    def _run_rootlogon(self):
        # Run custom logon file (must be after creation of ROOT globals)
        hasargv = hasattr(sys, 'argv')
        # -n disables the reading of the logon file, just like with root
        if hasargv and not '-n' in sys.argv and not self.PyConfig.DisableRootLogon:
            file_path = os.path.expanduser('~/.rootlogon.py')
            if os.path.exists(file_path):
                # Could also have used execfile, but import is likely to give fewer surprises
                module_name = 'rootlogon'
                if sys.version_info >= (3,5):
                    import importlib.util
                    spec = importlib.util.spec_from_file_location(module_name, file_path)
                    module = importlib.util.module_from_spec(spec)
                    sys.modules[module_name] = module
                    spec.loader.exec_module(module)
                else:
                    import imp
                    imp.load_module(module_name, open(file_path, 'r'), file_path, ('.py','r',1))
                    del imp
            else:
                # If the .py version of rootlogon exists, the .C is ignored (the user can
                # load the .C from the .py, if so desired).
                # System logon, user logon, and local logon (skip Rint.Logon)
                name = '.rootlogon.C'
                logons = [
                    os.path.join(str(self.TROOT.GetEtcDir()), 'system' + name),
                    os.path.expanduser(os.path.join('~', name))
                    ]
                if logons[-1] != os.path.join(os.getcwd(), name):
                    logons.append(name)
                for rootlogon in logons:
                    if os.path.exists(rootlogon):
                        self.TApplication.ExecuteFile(rootlogon)

    # Inject version as __version__ property in ROOT module
    @property
    def __version__(self):
        return self.gROOT.GetVersion()

    # Overload VecOps namespace
    # The property gets the C++ namespace, adds the pythonizations and
    # eventually deletes itself so that following calls go directly
    # to the C++ namespace. This mechanic ensures that we pythonize the
    # namespace lazily.
    @property
    def VecOps(self):
        ns = self._fallback_getattr('VecOps')
        try:
            from libROOTPythonizations import AsRVec
            ns.AsRVec = AsRVec
        except:
            raise Exception('Failed to pythonize the namespace VecOps')
        del type(self).VecOps
        return ns

    # Overload RDF namespace
    @property
    def RDF(self):
        ns = self._fallback_getattr('RDF')
        try:
            from libROOTPythonizations import MakeNumpyDataFrame
            ns.MakeNumpyDataFrame = MakeNumpyDataFrame
        except:
            raise Exception('Failed to pythonize the namespace RDF')
        del type(self).RDF
        return ns

    # Overload TMVA namespace
    @property
    def TMVA(self):
        ns = self._fallback_getattr('TMVA')
        try:
            from libROOTPythonizations import AsRTensor
            ns.Experimental.AsRTensor = AsRTensor
        except:
            raise Exception('Failed to pythonize the namespace TMVA')
        del type(self).TMVA
        return ns

    # Create and overload Numba namespace
    @property
    def Numba(self):
        if sys.version_info[:3] <= _numba_pyversion:
            raise Exception('ROOT.Numba requires Python above version {}.{}.{}'.format(*_numba_pyversion))
        cppdef('namespace Numba {}')
        ns = self._fallback_getattr('Numba')
        ns.Declare = staticmethod(_NumbaDeclareDecorator)
        del type(self).Numba
        return ns

    # Get TPyDispatcher for programming GUI callbacks
    @property
    def TPyDispatcher(self):
        include('ROOT/TPyDispatcher.h')
        tpd = gbl_namespace.TPyDispatcher
        type(self).TPyDispatcher = tpd
        return tpd
<|endoftext|>


<commit_before># Copyright 2016 IBM Corp.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import copy

from tempest.lib.api_schema.response.compute.v2_3 import servers

list_servers = copy.deepcopy(servers.list_servers)
get_server = copy.deepcopy(servers.get_server)
list_servers_detail = copy.deepcopy(servers.list_servers_detail)

# NOTE: The consolidated remote console API got introduced with v2.6
# with bp/consolidate-console-api. See Nova commit 578bafeda
get_remote_consoles = {
    'status_code': [200],
    'response_body': {
        'type': 'object',
        'properties': {
            'remote_console': {
                'type': 'object',
                'properties': {
                    'protocol': {'enum': ['vnc', 'rdp', 'serial', 'spice']},
                    'type': {'enum': ['novnc', 'xpvnc', 'rdp-html5',
                                      'spice-html5', 'serial']},
                    'url': {
                        'type': 'string',
                        'format': 'uri'
                    }
                },
                'additionalProperties': False,
                'required': ['protocol', 'type', 'url']
            }
        },
        'additionalProperties': False,
        'required': ['remote_console']
    }
}
<commit_msg>Add server schema for microversion 2.6<commit_after># Copyright 2016 IBM Corp.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import copy

from tempest.lib.api_schema.response.compute.v2_3 import servers

# NOTE: Below are the unchanged schema in this microversion. We need
# to keep this schema in this file to have the generic way to select the
# right schema based on self.schema_versions_info mapping in service client.
# ****** Schemas unchanged since microversion 2.3 ******
list_servers = copy.deepcopy(servers.list_servers)
get_server = copy.deepcopy(servers.get_server)
list_servers_detail = copy.deepcopy(servers.list_servers_detail)
update_server = copy.deepcopy(servers.update_server)
rebuild_server = copy.deepcopy(servers.rebuild_server)
rebuild_server_with_admin_pass = copy.deepcopy(
    servers.rebuild_server_with_admin_pass)
show_server_diagnostics = copy.deepcopy(servers.show_server_diagnostics)

# NOTE: The consolidated remote console API got introduced with v2.6
# with bp/consolidate-console-api. See Nova commit 578bafeda
get_remote_consoles = {
    'status_code': [200],
    'response_body': {
        'type': 'object',
        'properties': {
            'remote_console': {
                'type': 'object',
                'properties': {
                    'protocol': {'enum': ['vnc', 'rdp', 'serial', 'spice']},
                    'type': {'enum': ['novnc', 'xpvnc', 'rdp-html5',
                                      'spice-html5', 'serial']},
                    'url': {
                        'type': 'string',
                        'format': 'uri'
                    }
                },
                'additionalProperties': False,
                'required': ['protocol', 'type', 'url']
            }
        },
        'additionalProperties': False,
        'required': ['remote_console']
    }
}
<|endoftext|>


<commit_before># ----------------------------------------------------------------------------
# pyglet
# Copyright (c) 2006-2008 Alex Holkner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#  * Neither the name of pyglet nor the names of its
#    contributors may be used to endorse or promote products
#    derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

'''pyglet is a cross-platform games and multimedia package.

Detailed documentation is available at http://www.pyglet.org
'''

__docformat__ = 'restructuredtext'
__version__ = '$Id$'

import os
import sys

_is_epydoc = hasattr(sys, 'is_epydoc') and sys.is_epydoc

#: The release version of this pyglet installation.
#:
#: Valid only if pyglet was installed from a source or binary distribution
#: (i.e. not in a checked-out copy from SVN).
#:
#: Use setuptools if you need to check for a specific release version, e.g.::
#:
#:    >>> import pyglet
#:    >>> from pkg_resources import parse_version
#:    >>> parse_version(pyglet.version) >= parse_version('1.1')
#:    True
#:
version = '1.2dev'

def _require_ctypes_version(version):
    # Check ctypes version
    import ctypes
    req = [int(i) for i in version.split('.')]
    have = [int(i) for i in ctypes.__version__.split('.')]
    if not tuple(have) >= tuple(req):
        raise ImportError('pyglet requires ctypes %s or later.' % version)
_require_ctypes_version('1.0.0')

_enable_optimisations = not __debug__
if getattr(sys, 'frozen', None):
    _enable_optimisations = True

#: Global dict of pyglet options.  To change an option from its default, you
#: must import ``pyglet`` before any sub-packages.  For example::
#:
#:      import pyglet
#:      pyglet.options['debug_gl'] = False
#:
#: The default options can be overridden from the OS environment.  The
#: corresponding environment variable for each option key is prefaced by
#: ``PYGLET_``.  For example, in Bash you can set the ``debug_gl`` option with::
#:
#:      PYGLET_DEBUG_GL=True; export PYGLET_DEBUG_GL
#:
#: For options requiring a tuple of values, separate each value with a comma.
#:
#: The non-development options are:
#:
#: audio
#:     A sequence of the names of audio modules to attempt to load, in
#:     order of preference.  Valid driver names are:
#:
#:     * directsound, the Windows DirectSound audio module (Windows only)
#:     * pulse, the PulseAudio module (Linux only)
#:     * openal, the OpenAL audio module
#:     * silent, no audio
#: debug_lib
#:     If True, prints the path of each dynamic library loaded.
#: debug_gl
#:     If True, all calls to OpenGL functions are checked afterwards for
#:     errors using ``glGetError``.  This will severely impact performance,
#:     but provides useful exceptions at the point of failure.  By default,
#:     this option is enabled if ``__debug__`` is (i.e., if Python was not run
#:     with the -O option).  It is disabled by default when pyglet is "frozen"
#:     within a py2exe or py2app library archive.
#: shadow_window
#:     By default, pyglet creates a hidden window with a GL context when
#:     pyglet.gl is imported.  This allows resources to be loaded before
#:     the application window is created, and permits GL objects to be
#:     shared between windows even after they've been closed.  You can
#:     disable the creation of the shadow window by setting this option to
#:     False.  Recommended for advanced devlopers only.
#:
#:     **Since:** pyglet 1.1
#: vsync
#:     If set, the `pyglet.window.Window.vsync` property is ignored, and
#:     this option overrides it (to either force vsync on or off).  If unset,
#:     or set to None, the `pyglet.window.Window.vsync` property behaves
#:     as documented.
#: xsync
#:     If set (the default), pyglet will attempt to synchronise the drawing of
#:     double-buffered windows to the border updates of the X11 window
#:     manager.  This improves the appearance of the window during resize
#:     operations.  This option only affects double-buffered windows on
#:     X11 servers supporting the Xsync extension with a window manager
#:     that implements the _NET_WM_SYNC_REQUEST protocol.
#:
#:     **Since:** pyglet 1.1
#:
options = {
    'audio': ('directsound', 'pulse', 'openal', 'silent'),
    'font': ('gdiplus', 'win32'), # ignored outside win32; win32 is deprecated
    'debug_font': False,
    'debug_gl': not _enable_optimisations,
    'debug_gl_trace': False,
    'debug_gl_trace_args': False,
    'debug_graphics_batch': False,
    'debug_lib': False,
    'debug_media': False,
    'debug_texture': False,
    'debug_trace': False,
    'debug_trace_args': False,
    'debug_trace_depth': 1,
    'debug_trace_flush': True,
    'debug_win32': False,
    'debug_x11': False,
    'graphics_vbo': True,
    'shadow_window': True,
    'vsync': None,
    'xsync': True,
}

_option_types = {
    'audio': tuple,
    'font': tuple,
    'debug_font': bool,
    'debug_gl': bool,
    'debug_gl_trace': bool,
    'debug_gl_trace_args': bool,
    'debug_graphics_batch': bool,
    'debug_lib': bool,
    'debug_media': bool,
    'debug_texture': bool,
    'debug_trace': bool,
    'debug_trace_args': bool,
    'debug_trace_depth': int,
    'debug_trace_flush': bool,
    'debug_win32': bool,
    'debug_x11': bool,
    'graphics_vbo': bool,
    'shadow_window': bool,
    'vsync': bool,
    'xsync': bool,
}

def _read_environment():
    '''Read defaults for options from environment'''
    for key in options:
        env = 'PYGLET_%s' % key.upper()
        try:
            value = os.environ['PYGLET_%s' % key.upper()]
            if _option_types[key] is tuple:
                options[key] = value.split(',')
            elif _option_types[key] is bool:
                options[key] = value in ('true', 'TRUE', 'True', '1')
            elif _option_types[key] is int:
                options[key] = int(value)
        except KeyError:
            pass
_read_environment()

if sys.platform == 'cygwin':
    # This hack pretends that the posix-like ctypes provides windows
    # functionality.  COM does not work with this hack, so there is no
    # DirectSound support.
    import ctypes
    ctypes.windll = ctypes.cdll
    ctypes.oledll = ctypes.cdll
    ctypes.WINFUNCTYPE = ctypes.CFUNCTYPE
    ctypes.HRESULT = ctypes.c_long

# Call tracing
# ------------

_trace_filename_abbreviations = {}

def _trace_repr(value, size=40):
    value = repr(value)
    if len(value) > size:
        value = value[:size//2-2] + '...' + value[-size//2-1:]
    return value

def _trace_frame(frame, indent):
    from pyglet import lib
    import os
    if frame.f_code is lib._TraceFunction.__call__.func_code:
        is_ctypes = True
        func = frame.f_locals['self']._func
        name = func.__name__
        location = '[ctypes]'
    else:
        is_ctypes = False
        code = frame.f_code
        name = code.co_name
        path = code.co_filename
        line = code.co_firstlineno

        try:
            filename = _trace_filename_abbreviations[path]
        except KeyError:
            # Trim path down
            dir = ''
            path, filename = os.path.split(path)
            while len(dir + filename) < 30:
                filename = os.path.join(dir, filename)
                path, dir = os.path.split(path)
                if not dir:
                    filename = os.path.join('', filename)
                    break
            else:
                filename = os.path.join('...', filename)
            _trace_filename_abbreviations[path] = filename

        location = '(%s:%d)' % (filename, line)

    if indent:
        name = 'Called from %s' % name
    print '%s%s %s' % (indent, name, location)

    if _trace_args:
        if is_ctypes:
            args = [_trace_repr(arg) for arg in frame.f_locals['args']]
            print '  %sargs=(%s)' % (indent, ', '.join(args))
        else:
            for argname in code.co_varnames[:code.co_argcount]:
                try:
                    argvalue = _trace_repr(frame.f_locals[argname])
                    print '  %s%s=%s' % (indent, argname, argvalue)
                except:
                    pass

    if _trace_flush:
        sys.stdout.flush()

def _trace_func(frame, event, arg):
    if event == 'call':
        indent = ''
        for i in range(_trace_depth):
            _trace_frame(frame, indent)
            indent += '  '
            frame = frame.f_back
            if not frame:
                break

    elif event == 'exception':
        (exception, value, traceback) = arg
        print 'First chance exception raised:', repr(exception)

def _install_trace():
    sys.setprofile(_trace_func)

_trace_args = options['debug_trace_args']
_trace_depth = options['debug_trace_depth']
_trace_flush = options['debug_trace_flush']
if options['debug_trace']:
    _install_trace()

# Lazy loading
# ------------

class _ModuleProxy(object):
    _module = None

    def __init__(self, name):
        self.__dict__['_module_name'] = name

    def __getattr__(self, name):
        try:
            return getattr(self._module, name)
        except AttributeError:
            if self._module is not None:
                raise

            import_name = 'pyglet.%s' % self._module_name
            __import__(import_name)
            module = sys.modules[import_name]
            object.__setattr__(self, '_module', module)
            globals()[self._module_name] = module
            return getattr(module, name)

    def __setattr__(self, name, value):
        try:
            setattr(self._module, name, value)
        except AttributeError:
            if self._module is not None:
                raise

            import_name = 'pyglet.%s' % self._module_name
            __import__(import_name)
            module = sys.modules[import_name]
            object.__setattr__(self, '_module', module)
            globals()[self._module_name] = module
            setattr(module, name, value)

if not _is_epydoc:
    app = _ModuleProxy('app')
    clock = _ModuleProxy('clock')
    com = _ModuleProxy('com')
    event = _ModuleProxy('event')
    font = _ModuleProxy('font')
    gl = _ModuleProxy('gl')
    graphics = _ModuleProxy('graphics')
    image = _ModuleProxy('image')
    lib = _ModuleProxy('lib')
    media = _ModuleProxy('media')
    resource = _ModuleProxy('resource')
    sprite = _ModuleProxy('sprite')
    text = _ModuleProxy('text')
    window = _ModuleProxy('window')

# Fool py2exe, py2app into including all top-level modules (doesn't understand
# lazy loading)
if False:
    import app
    import clock
    import com
    import event
    import font
    import gl
    import graphics
    import image
    import lib
    import media
    import resource
    import sprite
    import text
    import window

# Hack around some epydoc bug that causes it to think pyglet.window is None.
if _is_epydoc:
    import window
<commit_msg>Per-thread debug tracing<commit_after># ----------------------------------------------------------------------------
# pyglet
# Copyright (c) 2006-2008 Alex Holkner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#  * Neither the name of pyglet nor the names of its
#    contributors may be used to endorse or promote products
#    derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

'''pyglet is a cross-platform games and multimedia package.

Detailed documentation is available at http://www.pyglet.org
'''

__docformat__ = 'restructuredtext'
__version__ = '$Id$'

import os
import sys

_is_epydoc = hasattr(sys, 'is_epydoc') and sys.is_epydoc

#: The release version of this pyglet installation.
#:
#: Valid only if pyglet was installed from a source or binary distribution
#: (i.e. not in a checked-out copy from SVN).
#:
#: Use setuptools if you need to check for a specific release version, e.g.::
#:
#:    >>> import pyglet
#:    >>> from pkg_resources import parse_version
#:    >>> parse_version(pyglet.version) >= parse_version('1.1')
#:    True
#:
version = '1.2dev'

def _require_ctypes_version(version):
    # Check ctypes version
    import ctypes
    req = [int(i) for i in version.split('.')]
    have = [int(i) for i in ctypes.__version__.split('.')]
    if not tuple(have) >= tuple(req):
        raise ImportError('pyglet requires ctypes %s or later.' % version)
_require_ctypes_version('1.0.0')

_enable_optimisations = not __debug__
if getattr(sys, 'frozen', None):
    _enable_optimisations = True

#: Global dict of pyglet options.  To change an option from its default, you
#: must import ``pyglet`` before any sub-packages.  For example::
#:
#:      import pyglet
#:      pyglet.options['debug_gl'] = False
#:
#: The default options can be overridden from the OS environment.  The
#: corresponding environment variable for each option key is prefaced by
#: ``PYGLET_``.  For example, in Bash you can set the ``debug_gl`` option with::
#:
#:      PYGLET_DEBUG_GL=True; export PYGLET_DEBUG_GL
#:
#: For options requiring a tuple of values, separate each value with a comma.
#:
#: The non-development options are:
#:
#: audio
#:     A sequence of the names of audio modules to attempt to load, in
#:     order of preference.  Valid driver names are:
#:
#:     * directsound, the Windows DirectSound audio module (Windows only)
#:     * pulse, the PulseAudio module (Linux only)
#:     * openal, the OpenAL audio module
#:     * silent, no audio
#: debug_lib
#:     If True, prints the path of each dynamic library loaded.
#: debug_gl
#:     If True, all calls to OpenGL functions are checked afterwards for
#:     errors using ``glGetError``.  This will severely impact performance,
#:     but provides useful exceptions at the point of failure.  By default,
#:     this option is enabled if ``__debug__`` is (i.e., if Python was not run
#:     with the -O option).  It is disabled by default when pyglet is "frozen"
#:     within a py2exe or py2app library archive.
#: shadow_window
#:     By default, pyglet creates a hidden window with a GL context when
#:     pyglet.gl is imported.  This allows resources to be loaded before
#:     the application window is created, and permits GL objects to be
#:     shared between windows even after they've been closed.  You can
#:     disable the creation of the shadow window by setting this option to
#:     False.  Recommended for advanced devlopers only.
#:
#:     **Since:** pyglet 1.1
#: vsync
#:     If set, the `pyglet.window.Window.vsync` property is ignored, and
#:     this option overrides it (to either force vsync on or off).  If unset,
#:     or set to None, the `pyglet.window.Window.vsync` property behaves
#:     as documented.
#: xsync
#:     If set (the default), pyglet will attempt to synchronise the drawing of
#:     double-buffered windows to the border updates of the X11 window
#:     manager.  This improves the appearance of the window during resize
#:     operations.  This option only affects double-buffered windows on
#:     X11 servers supporting the Xsync extension with a window manager
#:     that implements the _NET_WM_SYNC_REQUEST protocol.
#:
#:     **Since:** pyglet 1.1
#:
options = {
    'audio': ('directsound', 'pulse', 'openal', 'silent'),
    'font': ('gdiplus', 'win32'), # ignored outside win32; win32 is deprecated
    'debug_font': False,
    'debug_gl': not _enable_optimisations,
    'debug_gl_trace': False,
    'debug_gl_trace_args': False,
    'debug_graphics_batch': False,
    'debug_lib': False,
    'debug_media': False,
    'debug_texture': False,
    'debug_trace': False,
    'debug_trace_args': False,
    'debug_trace_depth': 1,
    'debug_trace_flush': True,
    'debug_win32': False,
    'debug_x11': False,
    'graphics_vbo': True,
    'shadow_window': True,
    'vsync': None,
    'xsync': True,
}

_option_types = {
    'audio': tuple,
    'font': tuple,
    'debug_font': bool,
    'debug_gl': bool,
    'debug_gl_trace': bool,
    'debug_gl_trace_args': bool,
    'debug_graphics_batch': bool,
    'debug_lib': bool,
    'debug_media': bool,
    'debug_texture': bool,
    'debug_trace': bool,
    'debug_trace_args': bool,
    'debug_trace_depth': int,
    'debug_trace_flush': bool,
    'debug_win32': bool,
    'debug_x11': bool,
    'graphics_vbo': bool,
    'shadow_window': bool,
    'vsync': bool,
    'xsync': bool,
}

def _read_environment():
    '''Read defaults for options from environment'''
    for key in options:
        env = 'PYGLET_%s' % key.upper()
        try:
            value = os.environ['PYGLET_%s' % key.upper()]
            if _option_types[key] is tuple:
                options[key] = value.split(',')
            elif _option_types[key] is bool:
                options[key] = value in ('true', 'TRUE', 'True', '1')
            elif _option_types[key] is int:
                options[key] = int(value)
        except KeyError:
            pass
_read_environment()

if sys.platform == 'cygwin':
    # This hack pretends that the posix-like ctypes provides windows
    # functionality.  COM does not work with this hack, so there is no
    # DirectSound support.
    import ctypes
    ctypes.windll = ctypes.cdll
    ctypes.oledll = ctypes.cdll
    ctypes.WINFUNCTYPE = ctypes.CFUNCTYPE
    ctypes.HRESULT = ctypes.c_long

# Call tracing
# ------------

_trace_filename_abbreviations = {}

def _trace_repr(value, size=40):
    value = repr(value)
    if len(value) > size:
        value = value[:size//2-2] + '...' + value[-size//2-1:]
    return value

def _trace_frame(thread, frame, indent):
    from pyglet import lib
    import os
    if frame.f_code is lib._TraceFunction.__call__.func_code:
        is_ctypes = True
        func = frame.f_locals['self']._func
        name = func.__name__
        location = '[ctypes]'
    else:
        is_ctypes = False
        code = frame.f_code
        name = code.co_name
        path = code.co_filename
        line = code.co_firstlineno

        try:
            filename = _trace_filename_abbreviations[path]
        except KeyError:
            # Trim path down
            dir = ''
            path, filename = os.path.split(path)
            while len(dir + filename) < 30:
                filename = os.path.join(dir, filename)
                path, dir = os.path.split(path)
                if not dir:
                    filename = os.path.join('', filename)
                    break
            else:
                filename = os.path.join('...', filename)
            _trace_filename_abbreviations[path] = filename

        location = '(%s:%d)' % (filename, line)

    if indent:
        name = 'Called from %s' % name
    print '[%d] %s%s %s' % (thread, indent, name, location)

    if _trace_args:
        if is_ctypes:
            args = [_trace_repr(arg) for arg in frame.f_locals['args']]
            print '  %sargs=(%s)' % (indent, ', '.join(args))
        else:
            for argname in code.co_varnames[:code.co_argcount]:
                try:
                    argvalue = _trace_repr(frame.f_locals[argname])
                    print '  %s%s=%s' % (indent, argname, argvalue)
                except:
                    pass

    if _trace_flush:
        sys.stdout.flush()

def _thread_trace_func(thread):
    def _trace_func(frame, event, arg):
        if event == 'call':
            indent = ''
            for i in range(_trace_depth):
                _trace_frame(thread, frame, indent)
                indent += '  '
                frame = frame.f_back
                if not frame:
                    break

        elif event == 'exception':
            (exception, value, traceback) = arg
            print 'First chance exception raised:', repr(exception)
    return _trace_func

def _install_trace():
    global _trace_thread_count
    sys.setprofile(_thread_trace_func(_trace_thread_count))
    _trace_thread_count += 1

_trace_thread_count = 0
_trace_args = options['debug_trace_args']
_trace_depth = options['debug_trace_depth']
_trace_flush = options['debug_trace_flush']
if options['debug_trace']:
    _install_trace()

# Lazy loading
# ------------

class _ModuleProxy(object):
    _module = None

    def __init__(self, name):
        self.__dict__['_module_name'] = name

    def __getattr__(self, name):
        try:
            return getattr(self._module, name)
        except AttributeError:
            if self._module is not None:
                raise

            import_name = 'pyglet.%s' % self._module_name
            __import__(import_name)
            module = sys.modules[import_name]
            object.__setattr__(self, '_module', module)
            globals()[self._module_name] = module
            return getattr(module, name)

    def __setattr__(self, name, value):
        try:
            setattr(self._module, name, value)
        except AttributeError:
            if self._module is not None:
                raise

            import_name = 'pyglet.%s' % self._module_name
            __import__(import_name)
            module = sys.modules[import_name]
            object.__setattr__(self, '_module', module)
            globals()[self._module_name] = module
            setattr(module, name, value)

if not _is_epydoc:
    app = _ModuleProxy('app')
    clock = _ModuleProxy('clock')
    com = _ModuleProxy('com')
    event = _ModuleProxy('event')
    font = _ModuleProxy('font')
    gl = _ModuleProxy('gl')
    graphics = _ModuleProxy('graphics')
    image = _ModuleProxy('image')
    lib = _ModuleProxy('lib')
    media = _ModuleProxy('media')
    resource = _ModuleProxy('resource')
    sprite = _ModuleProxy('sprite')
    text = _ModuleProxy('text')
    window = _ModuleProxy('window')

# Fool py2exe, py2app into including all top-level modules (doesn't understand
# lazy loading)
if False:
    import app
    import clock
    import com
    import event
    import font
    import gl
    import graphics
    import image
    import lib
    import media
    import resource
    import sprite
    import text
    import window

# Hack around some epydoc bug that causes it to think pyglet.window is None.
if _is_epydoc:
    import window
<|endoftext|>


<commit_before>
from operator import attrgetter
import logging
import blocks
import sys
logger = logging.getLogger(__name__)

class HashChainTask(object):
    """
    - get hashes chain until we see a known block hash
    """

    NUM_HASHES_PER_REQUEST = 2000

    def __init__(self, chain_manager, peer, block_hash):
        self.chain_manager = chain_manager
        self.peer = peer
        self.hash_chain = []  # [youngest, ..., oldest]
        self.request(block_hash)

    def request(self, block_hash):
        logger.debug('%r requesting block_hashes starting from %r', self.peer, block_hash.encode('hex'))
        self.peer.send_GetBlockHashes(block_hash, self.NUM_HASHES_PER_REQUEST)

    def received_block_hashes(self, block_hashes):
        logger.debug('HashChainTask.received_block_hashes %d', len(block_hashes))
        if block_hashes and self.chain_manager.genesis.hash == block_hashes[-1]:
            logger.debug('%r has different chain starting from genesis', self.peer)
        for bh in block_hashes:
            if bh in self.chain_manager or bh == self.chain_manager.genesis.hash:
                logger.debug('%r matching block hash found %r, %d blocks to fetch',
                             self.peer, bh.encode('hex'), len(self.hash_chain))
                return list(reversed(self.hash_chain))
            self.hash_chain.append(bh)
        if len(block_hashes) == 0:
            return list(reversed(self.hash_chain))
            # logger.debug('hash_chain.append(%r) %d', bh.encode('hex'), len(self.hash_chain))
        self.request(bh)

class SynchronizationTask(object):
    """
    Created if we receive a unknown block w/o known parent. Possibly from a different branch.

    - get hashes chain until we see a known block hash
    - request missing blocks

    - once synced
        - rerequest blocks that lacked a reference before syncing
    """
    NUM_BLOCKS_PER_REQUEST = 200

    def __init__(self, chain_manager, peer, block_hash):
        self.chain_manager = chain_manager
        self.peer = peer
        self.hash_chain = []  # [oldest to youngest]
        logger.debug('%r syncing %r', self.peer, block_hash.encode('hex'))
        self.hash_chain_task = HashChainTask(self.chain_manager, self.peer, block_hash)

    def received_block_hashes(self, block_hashes):
        res = self.hash_chain_task.received_block_hashes(block_hashes)
        if res:
            self.hash_chain = res
            logger.debug('%r hash chain with %d hashes for missing blocks', self.peer, len(self.hash_chain))
            self.request_blocks()

    def received_blocks(self, transient_blocks):
        logger.debug('%r received %d blocks. %d missing', self.peer, len(transient_blocks), len(self.hash_chain))
        for tb in transient_blocks:
            if len(self.hash_chain) and self.hash_chain[0] == tb.hash:
                self.hash_chain.pop(0)
            else:
                logger.debug('%r received unexpected block %r', self.peer, tb)
                return False
        if self.hash_chain:
            # still blocks to fetch
            logger.debug('%r still missing %d blocks', self.peer, len(self.hash_chain))
            self.request_blocks()
        else:  # done
            return True

    def request_blocks(self):
        logger.debug('%r requesting %d of %d missing blocks', self.peer, self.NUM_BLOCKS_PER_REQUEST, len(self.hash_chain))
        self.peer.send_GetBlocks(self.hash_chain[:self.NUM_BLOCKS_PER_REQUEST])

class Synchronizer(object):
    """"
    Cases:
        on "recv_Status": received unknown head_hash w/ sufficient difficulty
        on "recv_Blocks": received block w/o parent (new block mined, competing chain discovered)

    Naive Strategy:
        assert we see a block for which we have no parent
        assume that the sending peer knows the parent
        if we have not yet syncer for this unknown block:
            create new syncer
            sync direction genesis until we see known block_hash
            sync also (re)requests the block we missed, so it can be added on top of the synced chain
        else
            do nothing
            syncing (if finished) will be started with the next broadcasted block w/ missing parent
    """

    def __init__(self, chain_manager):
        self.chain_manager = chain_manager
        self.synchronization_tasks = {}  # peer > syncer # syncer.unknown_hash as marker for task

    def stop_synchronization(self, peer):
        logger.debug('%r sync stopped', peer)
        if peer in self.synchronization_tasks:
            del self.synchronization_tasks[peer]

    def synchronize_unknown_block(self, peer, block_hash, force=False):
        "Case: block with unknown parent. Fetches unknown ancestors and this block"
        logger.debug('%r sync %r', peer, block_hash.encode('hex'))
        if block_hash == self.chain_manager.genesis.hash or block_hash in self.chain_manager:
            logger.debug('%r known_hash %r, skipping', peer, block_hash.encode('hex'))
            return

        if peer and (not peer in self.synchronization_tasks) or force:
            logger.debug('%r new sync task', peer)
            self.synchronization_tasks[peer] = SynchronizationTask(self.chain_manager, peer, block_hash)
        else:
            logger.debug('%r already has a synctask, sorry', peer)

    def synchronize_status(self, peer, block_hash, total_difficulty):
        "Case: unknown head with sufficient difficulty"
        logger.debug('%r status  with %r %d', peer,  block_hash.encode('hex'), total_difficulty)

        # guesstimate the max difficulty difference possible for a sucessfully competing chain
        # worst case if skip it: we are on a stale chain until the other catched up
        # assume difficulty is constant
        num_blocks_behind = 7
        avg_uncles_per_block = 4
        max_diff = self.chain_manager.head.difficulty * num_blocks_behind * (1 + avg_uncles_per_block)
        if total_difficulty + max_diff > self.chain_manager.head.difficulty:
            logger.debug('%r sufficient difficulty, syncing', peer)
            self.synchronize_unknown_block(peer, block_hash)
        else:
            logger.debug('%r insufficient difficulty, not syncing', peer)

    def received_block_hashes(self, peer, block_hashes):
        if peer in self.synchronization_tasks:
            logger.debug("Synchronizer.received_block_hashes %d for: %r", len(block_hashes), peer)
            self.synchronization_tasks[peer].received_block_hashes(block_hashes)

    def received_blocks(self, peer, transient_blocks):
        if peer in self.synchronization_tasks:
            res = self.synchronization_tasks[peer].received_blocks(transient_blocks)
            if res is True:
                logger.debug("Synchronizer.received_blocks: chain w %r synced", peer)
                del self.synchronization_tasks[peer]
<commit_msg>new loging infrastructure<commit_after>
from operator import attrgetter
import sys
import blocks
from pyethereum.tlogging import log_synchronizer as log_debug

class HashChainTask(object):
    """
    - get hashes chain until we see a known block hash
    """

    NUM_HASHES_PER_REQUEST = 2000

    def __init__(self, chain_manager, peer, block_hash):
        self.chain_manager = chain_manager
        self.peer = peer
        self.hash_chain = []  # [youngest, ..., oldest]
        self.request(block_hash)

    def request(self, block_hash):
        log_debug('requesting block_hashes', peer=self.peer, from=block_hash.encode('hex'))
        self.peer.send_GetBlockHashes(block_hash, self.NUM_HASHES_PER_REQUEST)

    def received_block_hashes(self, block_hashes):
        log_debug('HashChainTask.received_block_hashes', num=len(block_hashes))
        if block_hashes and self.chain_manager.genesis.hash == block_hashes[-1]:
            log_debug('has different chain starting from genesis', peer=self.peer)
        for bh in block_hashes:
            if bh in self.chain_manager or bh == self.chain_manager.genesis.hash:
                log_debug('matching block hash found', peer=self.peer,
                    hash=bh.encode('hex'), num_to_fetch=len(self.hash_chain))
                return list(reversed(self.hash_chain))
            self.hash_chain.append(bh)
        if len(block_hashes) == 0:
            return list(reversed(self.hash_chain))
        self.request(bh)

class SynchronizationTask(object):
    """
    Created if we receive a unknown block w/o known parent. Possibly from a different branch.

    - get hashes chain until we see a known block hash
    - request missing blocks

    - once synced
        - rerequest blocks that lacked a reference before syncing
    """
    NUM_BLOCKS_PER_REQUEST = 200

    def __init__(self, chain_manager, peer, block_hash):
        self.chain_manager = chain_manager
        self.peer = peer
        self.hash_chain = []  # [oldest to youngest]
        log_debug('syncing', peer=self.peer, hash=block_hash.encode('hex'))
        self.hash_chain_task = HashChainTask(self.chain_manager, self.peer, block_hash)

    def received_block_hashes(self, block_hashes):
        res = self.hash_chain_task.received_block_hashes(block_hashes)
        if res:
            self.hash_chain = res
            log_debug('receieved hash chain', peer=self.peer, num=len(self.hash_chain))
            self.request_blocks()

    def received_blocks(self, transient_blocks):
        log_debug('blocks received', peer=self.peer, num=len(transient_blocks), missing=len(self.hash_chain))
        for tb in transient_blocks:
            if len(self.hash_chain) and self.hash_chain[0] == tb.hash:
                self.hash_chain.pop(0)
            else:
                log_debug('received unexpected block', peer=self.peer, hash=tb.hex_hash())
                return False
        if self.hash_chain:
            # still blocks to fetch
            log_debug('still missing blocks', peer=self.peer, num=len(self.hash_chain))
            self.request_blocks()
        else:  # done
            return True

    def request_blocks(self):
        log_debug('requesting missing blocks', peer=self.peer,
                    requested=self.NUM_BLOCKS_PER_REQUEST, missing=len(self.hash_chain))
        self.peer.send_GetBlocks(self.hash_chain[:self.NUM_BLOCKS_PER_REQUEST])

class Synchronizer(object):
    """"
    Cases:
        on "recv_Status": received unknown head_hash w/ sufficient difficulty
        on "recv_Blocks": received block w/o parent (new block mined, competing chain discovered)

    Naive Strategy:
        assert we see a block for which we have no parent
        assume that the sending peer knows the parent
        if we have not yet syncer for this unknown block:
            create new syncer
            sync direction genesis until we see known block_hash
            sync also (re)requests the block we missed, so it can be added on top of the synced chain
        else
            do nothing
            syncing (if finished) will be started with the next broadcasted block w/ missing parent
    """

    def __init__(self, chain_manager):
        self.chain_manager = chain_manager
        self.synchronization_tasks = {}  # peer > syncer # syncer.unknown_hash as marker for task

    def stop_synchronization(self, peer):
        log_debug('sync stopped', peer=peer)
        if peer in self.synchronization_tasks:
            del self.synchronization_tasks[peer]

    def synchronize_unknown_block(self, peer, block_hash, force=False):
        "Case: block with unknown parent. Fetches unknown ancestors and this block"
        log_debug('sync unknown', peer=peer, block=block_hash.encode('hex'))
        if block_hash == self.chain_manager.genesis.hash or block_hash in self.chain_manager:
            log_debug('known_hash, skipping', peer=peer, hash=block_hash.encode('hex'))
            return

        if peer and (not peer in self.synchronization_tasks) or force:
            log_debug('new sync task', peer=peer)
            self.synchronization_tasks[peer] = SynchronizationTask(self.chain_manager, peer, block_hash)
        else:
            log_debug('existing synctask', peer=peer)

    def synchronize_status(self, peer, block_hash, total_difficulty):
        "Case: unknown head with sufficient difficulty"
        log_debug('sync status', peer=peer,  hash=block_hash.encode('hex'), total_difficulty=total_difficulty)

        # guesstimate the max difficulty difference possible for a sucessfully competing chain
        # worst case if skip it: we are on a stale chain until the other catched up
        # assume difficulty is constant
        num_blocks_behind = 7
        avg_uncles_per_block = 4
        max_diff = self.chain_manager.head.difficulty * num_blocks_behind * (1 + avg_uncles_per_block)
        if total_difficulty + max_diff > self.chain_manager.head.difficulty:
            log_debug('sufficient difficulty, syncing', peer=peer)
            self.synchronize_unknown_block(peer, block_hash)
        else:
            log_debug('insufficient difficulty, not syncing', peer=peer)

    def received_block_hashes(self, peer, block_hashes):
        if peer in self.synchronization_tasks:
            log_debug("Synchronizer.received_block_hashes", peer=peer, num=len(block_hashes))
            self.synchronization_tasks[peer].received_block_hashes(block_hashes)

    def received_blocks(self, peer, transient_blocks):
        if peer in self.synchronization_tasks:
            res = self.synchronization_tasks[peer].received_blocks(transient_blocks)
            if res is True:
                log_debug("Synchronizer.received_blocks: chain synced", peer=peer)
                del self.synchronization_tasks[peer]
<|endoftext|>


<commit_before># encoding: utf8

__all__ = [
    'Builder',
    'TkApplication',
    'BuilderObject',
    'register_widget',
    'register_property',
    'register_custom_property',
    'remove_binding',
    'ApplicationLevelBindManager']

from pygubu.binding import ApplicationLevelBindManager, remove_binding
from pygubu.builder import Builder
from pygubu.builder.builderobject import (BuilderObject, register_widget,
                                          register_property,
                                          register_custom_property)

__version__ = '0.19'

class TkApplication:
    def __init__(self, master=None):
        self.master = master
        self.toplevel = master.winfo_toplevel()

        self.toplevel.withdraw()
        self._init_before()
        self._create_ui()
        self._init_after()
        self.toplevel.deiconify()

    def _init_before(self):
        pass

    def _create_ui(self):
        pass

    def _init_after(self):
        pass

    def run(self):
        """Ejecute the main loop."""

        self.toplevel.protocol("WM_DELETE_WINDOW", self.__on_window_close)
        self.toplevel.mainloop()

    def set_resizable(self):
        self.toplevel.rowconfigure(0, weight=1)
        self.toplevel.columnconfigure(0, weight=1)

    def set_title(self, title):
        """Set the window title."""
        self.toplevel.title(title)

    def set_menu(self, menu):
        """Set the main menu."""
        self.toplevel.config(menu=menu)

    def __on_window_close(self):
        """Manage WM_DELETE_WINDOW protocol."""
        if self.on_close_execute():
            self.toplevel.destroy()

    def on_close_execute(self):
        """Determine if if the application is ready for quit,
           return boolean."""
        return True

    def quit(self):
        """Exit the app if it is ready for quit."""
        self.__on_window_close()

    def set_size(self, geom):
        self.toplevel.geometry(geom)
<commit_msg>Update version to sync with pypi.<commit_after># encoding: utf8

__all__ = [
    'Builder',
    'TkApplication',
    'BuilderObject',
    'register_widget',
    'register_property',
    'register_custom_property',
    'remove_binding',
    'ApplicationLevelBindManager']

from pygubu.binding import ApplicationLevelBindManager, remove_binding
from pygubu.builder import Builder
from pygubu.builder.builderobject import (BuilderObject, register_widget,
                                          register_property,
                                          register_custom_property)

__version__ = '0.20'

class TkApplication:
    def __init__(self, master=None):
        self.master = master
        self.toplevel = master.winfo_toplevel()

        self.toplevel.withdraw()
        self._init_before()
        self._create_ui()
        self._init_after()
        self.toplevel.deiconify()

    def _init_before(self):
        pass

    def _create_ui(self):
        pass

    def _init_after(self):
        pass

    def run(self):
        """Ejecute the main loop."""

        self.toplevel.protocol("WM_DELETE_WINDOW", self.__on_window_close)
        self.toplevel.mainloop()

    def set_resizable(self):
        self.toplevel.rowconfigure(0, weight=1)
        self.toplevel.columnconfigure(0, weight=1)

    def set_title(self, title):
        """Set the window title."""
        self.toplevel.title(title)

    def set_menu(self, menu):
        """Set the main menu."""
        self.toplevel.config(menu=menu)

    def __on_window_close(self):
        """Manage WM_DELETE_WINDOW protocol."""
        if self.on_close_execute():
            self.toplevel.destroy()

    def on_close_execute(self):
        """Determine if if the application is ready for quit,
           return boolean."""
        return True

    def quit(self):
        """Exit the app if it is ready for quit."""
        self.__on_window_close()

    def set_size(self, geom):
        self.toplevel.geometry(geom)
<|endoftext|>


<commit_before>"""Cloud browser views."""
import os
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.importlib import import_module

from cloud_browser.app_settings import settings
from cloud_browser.cloud import get_connection, get_connection_cls, errors
from cloud_browser.common import get_int, \
    path_parts, path_join, path_yield, relpath

MAX_LIMIT = get_connection_cls().cont_cls.max_list

def settings_view_decorator(function):
    """Insert decorator from settings, if any.

    .. note:: Decorator in ``CLOUD_BROWSER_VIEW_DECORATOR`` can be either a
        callable or a fully-qualified string path (the latter, which we'll
        lazy import).
    """

    dec = settings.CLOUD_BROWSER_VIEW_DECORATOR

    # Trade-up string to real decorator.
    if isinstance(dec, basestring):
        # Split into module and decorator strings.
        mod_str, _, dec_str = dec.rpartition('.')
        if not (mod_str and dec_str):
            raise ImportError("Unable to import module: %s" % mod_str)

        # Import and try to get decorator function.
        mod = import_module(mod_str)
        if not hasattr(mod, dec_str):
            raise ImportError("Unable to import decorator: %s" % dec)

        dec = getattr(mod, dec_str)

    if dec and callable(dec):
        return dec(function)

    return function

def _breadcrumbs(path):
    """Return breadcrumb dict from path."""

    full = None
    crumbs = []
    for part in path_yield(path):
        full = path_join(full, part) if full else part
        crumbs.append((full, part))

    return crumbs

@settings_view_decorator
def browser(request, repo_name, revision='latest', path='', template='cloud_browser/browser.html'):
    """View files in a file path.

    :param request: The request.
    :param path: Path to resource, including container as first part of path.
    :param template: Template to render.
    """

    # Inputs.
    path = '/' + path if not os.path.isabs(path) else path
    container_path, object_path = path_parts(path)
    incoming = request.POST or request.GET or {}
    page = incoming['page'] if 'page' in incoming else '0'

    # check the validity of the parameters
    if repo_name in settings.CLOUD_BROWSER_CVMFS_FQRN_WHITELIST:
        domain_pos = repo_name.find('.')
        domain = repo_name[domain_pos:]
        url = settings.CLOUD_BROWSER_CVMFS_URL_MAPPING[domain] \
            + repo_name[:domain_pos]
    else:
        return Http404('The repository %s is not allowed', repo_name)

    # Q1: Get all containers.
    #     We optimize here by not individually looking up containers later,
    #     instead going through this in-memory list.
    # TODO: Should page listed containers with a ``limit`` and ``marker``.
    try:
        params = {'url': url, 'revision': revision}
        conn = get_connection(params)
        if path == '/':
            containers = [conn.cont_cls.from_path(conn, path)]
        else:
            containers = conn.get_containers(container_path)
        # Q2: Get objects for instant list, plus one to check "next".
        container = [c for c in containers if c.base_path == path][0]
        objects = container.get_objects(object_path, None, 1000000)
    except Exception:
        raise Http404("Revision or folder not found")

    tag_list = conn.get_tag_list()
    limit = settings.CLOUD_BROWSER_DEFAULT_LIST_LIMIT
    page_int = int(page)
    init_page = page_int * limit
    end_page = ((page_int + 1) * limit)
    if init_page >= len(tag_list):
        init_page -= limit
        end_page -= limit
    tag_list = tag_list[init_page:end_page]
    statistics = conn.get_statistics()
    closest_catalog_path = container.get_closest_catalog_path()[1:]

    return render_to_response(template,
                              {'path': path,
                               'revision': revision,
                               'revision_number': conn.revision,
                               'tag_list': tag_list,
                               'fqrn': repo_name,
                               'closest_catalog_path': closest_catalog_path,
                               'breadcrumbs': _breadcrumbs(path),
                               'page': page,
                               'statistics': statistics,
                               'container_path': container_path,
                               'containers': containers,
                               'container': container,
                               'object_path': object_path,
                               'objects': objects},
                              context_instance=RequestContext(request))

@settings_view_decorator
def document(_, repo_name, revision, path):
    """View single document from path.

    :param path: Path to resource, including container as first part of path.
    """
    container_path, object_path = path_parts(path)
    # check the validity of the parameters
    if repo_name in settings.CLOUD_BROWSER_CVMFS_FQRN_WHITELIST:
        domain_pos = repo_name.find('.')
        domain = repo_name[domain_pos:]
        url = settings.CLOUD_BROWSER_CVMFS_URL_MAPPING[domain] \
              + repo_name[:domain_pos]
    else:
        return Http404('The repository %s is not allowed', repo_name)
    params = {'url': url, 'revision': revision}
    conn = get_connection(params)
    try:
        container = conn.get_container(container_path)
    except errors.NoContainerException:
        raise Http404("No container at: %s" % container_path)
    except errors.NotPermittedException:
        raise Http404("Access denied for container at: %s" % container_path)

    try:
        storage_obj = container.get_object(object_path)
    except errors.NoObjectException:
        raise Http404("No object at: %s" % object_path)

    # Get content-type and encoding.
    content_type = storage_obj.smart_content_type
    encoding = storage_obj.smart_content_encoding
    response = HttpResponse(content=storage_obj.read(),
                            content_type=content_type)
    if encoding not in (None, ''):
        response['Content-Encoding'] = encoding

    return response
<commit_msg>Get current tag and associated data from the revision<commit_after>"""Cloud browser views."""
import os
import time
import datetime
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.importlib import import_module

from cloud_browser.app_settings import settings
from cloud_browser.cloud import get_connection, get_connection_cls, errors
from cloud_browser.common import get_int, \
    path_parts, path_join, path_yield, relpath

MAX_LIMIT = get_connection_cls().cont_cls.max_list

def settings_view_decorator(function):
    """Insert decorator from settings, if any.

    .. note:: Decorator in ``CLOUD_BROWSER_VIEW_DECORATOR`` can be either a
        callable or a fully-qualified string path (the latter, which we'll
        lazy import).
    """

    dec = settings.CLOUD_BROWSER_VIEW_DECORATOR

    # Trade-up string to real decorator.
    if isinstance(dec, basestring):
        # Split into module and decorator strings.
        mod_str, _, dec_str = dec.rpartition('.')
        if not (mod_str and dec_str):
            raise ImportError("Unable to import module: %s" % mod_str)

        # Import and try to get decorator function.
        mod = import_module(mod_str)
        if not hasattr(mod, dec_str):
            raise ImportError("Unable to import decorator: %s" % dec)

        dec = getattr(mod, dec_str)

    if dec and callable(dec):
        return dec(function)

    return function

def _breadcrumbs(path):
    """Return breadcrumb dict from path."""

    full = None
    crumbs = []
    for part in path_yield(path):
        full = path_join(full, part) if full else part
        crumbs.append((full, part))

    return crumbs

@settings_view_decorator
def browser(request, repo_name, revision='latest', path='',
            template='cloud_browser/browser.html'):
    """View files in a file path.

    :param request: The request.
    :param path: Path to resource, including container as first part of path.
    :param template: Template to render.
    """

    # Inputs.
    path = '/' + path if not os.path.isabs(path) else path
    container_path, object_path = path_parts(path)
    incoming = request.POST or request.GET or {}
    page = incoming['page'] if 'page' in incoming else '0'
    revision_date_str = incoming['revision_date'] \
        if 'revision_date' in incoming else None

    revision_tmstamp = None
    if revision_date_str:
        revision_tmstamp = int(time.
                               mktime(datetime.datetime
                                       .strptime(revision_date_str, "%Y-%m-%d")
                                       .timetuple()))

    # check the validity of the parameters
    if repo_name in settings.CLOUD_BROWSER_CVMFS_FQRN_WHITELIST:
        domain_pos = repo_name.find('.')
        domain = repo_name[domain_pos:]
        url = settings.CLOUD_BROWSER_CVMFS_URL_MAPPING[domain] \
            + repo_name[:domain_pos]
    else:
        return Http404('The repository %s is not allowed', repo_name)

    # Q1: Get all containers.
    #     We optimize here by not individually looking up containers later,
    #     instead going through this in-memory list.
    try:
        params = {'url': url, 'revision': revision, 'date': revision_tmstamp}
        conn = get_connection(params)
        if revision_tmstamp:
            new_url = '/'.join(
                ['/cb/browser', repo_name, str(conn.revision), path])
            return HttpResponseRedirect(new_url)
        if path == '/':
            containers = [conn.cont_cls.from_path(conn, path)]
        else:
            containers = conn.get_containers(container_path)
        # Q2: Get objects for instant list, plus one to check "next".
        container = [c for c in containers if c.base_path == path][0]
        objects = container.get_objects(object_path, None, 1000000)
    except Exception:
        raise Http404("Revision or folder not found")

    tag_list = conn.get_tag_list()
    limit = settings.CLOUD_BROWSER_DEFAULT_LIST_LIMIT
    page_int = int(page)
    init_page = page_int * limit
    end_page = ((page_int + 1) * limit)
    if init_page >= len(tag_list):
        init_page -= limit
        end_page -= limit
    tag_list = tag_list[init_page:end_page]
    statistics = conn.get_statistics()
    closest_catalog_path = container.get_closest_catalog_path()[1:]
    current_tag = conn.get_tag_by_revision()
    revision_date = str(current_tag.timestamp.date())

    return render_to_response(template,
                              {'path': path,
                               'revision': revision,
                               'tag_list': tag_list,
                               'revision_date': revision_date,
                               'current_tag': current_tag,
                               'fqrn': repo_name,
                               'closest_catalog_path': closest_catalog_path,
                               'breadcrumbs': _breadcrumbs(path),
                               'page': page,
                               'statistics': statistics,
                               'container_path': container_path,
                               'containers': containers,
                               'container': container,
                               'object_path': object_path,
                               'objects': objects},
                              context_instance=RequestContext(request))

@settings_view_decorator
def document(_, repo_name, revision, path):
    """View single document from path.

    :param path: Path to resource, including container as first part of path.
    """
    container_path, object_path = path_parts(path)
    # check the validity of the parameters
    if repo_name in settings.CLOUD_BROWSER_CVMFS_FQRN_WHITELIST:
        domain_pos = repo_name.find('.')
        domain = repo_name[domain_pos:]
        url = settings.CLOUD_BROWSER_CVMFS_URL_MAPPING[domain] \
              + repo_name[:domain_pos]
    else:
        return Http404('The repository %s is not allowed', repo_name)
    params = {'url': url, 'revision': revision}
    conn = get_connection(params)
    try:
        container = conn.get_container(container_path)
    except errors.NoContainerException:
        raise Http404("No container at: %s" % container_path)
    except errors.NotPermittedException:
        raise Http404("Access denied for container at: %s" % container_path)

    try:
        storage_obj = container.get_object(object_path)
    except errors.NoObjectException:
        raise Http404("No object at: %s" % object_path)

    # Get content-type and encoding.
    content_type = storage_obj.smart_content_type
    encoding = storage_obj.smart_content_encoding
    response = HttpResponse(content=storage_obj.read(),
                            content_type=content_type)
    if encoding not in (None, ''):
        response['Content-Encoding'] = encoding

    return response
<|endoftext|>


<commit_before>try:
	from browser import window, document
	from javascript import JSObject, JSConstructor
	from ggame import *

	jq = window.jQuery

	if not jq(".inputScreen").length:
		jq("body").append('

<input type = "text" id = "toInput"><input type = "submit" id = "toSubmit">

')
		jq(".inputScreen").hide()

	input_queue = []
	winput_queue = []
	#App1 = App
	winput_started = False
	def input_callback():
		input_value = jq('#toInput').val()
		jq('#toInput').val('')

		input_t = jq('#toPrompt').text()
		jq('#toPrompt').text('')

		global input_queue

		print(input_t + " " + input_value)
		input_queue[0][1](input_t, input_value)
		input_queue.pop(0)
		if len(input_queue) > 0:
			inputStart(input_queue[0][0])

	def input_fade(ev):
		jq('.inputScreen').fadeOut(300, input_callback)
		jq('#toSubmit').off('click')

	def inputStart(text):
		jq('.inputScreen').fadeIn(300)
		jq('#toPrompt').text(text)
		jq('#toSubmit').on('click', input_fade)

	def input(text, callback):
		global input_queue
		input_queue.append([text, callback])
		if len(input_queue) == 1:
			inputStart(text)

	def winput_callback():
		winput_value = App._win._w.document.getElementById('toInput').value
		App._win._w.document.getElementById('toInput').value = ''

		winput_t = App._win._w.document.getElementById('toPrompt').textContent
		App._win._w.document.getElementById('toPrompt').textContent = ""

		global winput_queue

		print(winput_t + " " + winput_value)
		winput_queue[0][1](winput_t, winput_value)
		winput_queue.pop(0)
		if len(winput_queue) > 0:
			winputStart(winput_queue[0][0])

	def winput_fade(ev):
		App._win._w.document.getElementById('inputScreen').style.display = "none"
		winput_callback()
		App._win._w.document.getElementById('toSubmit').removeEventListener('click', winput_fade)

	def winputStart(text):
		App._win._w.document.getElementById('inputScreen').style.display = "block"
		App._win._w.document.getElementById('toPrompt').textContent = text
		App._win._w.document.getElementById('toSubmit').addEventListener('click', winput_fade)

	def winput(text, callback):
		global winput_queue
		global winput_started
		winput_queue.append([text, callback])
		if winput_started:
			winputStart(text)
			winput_started = False
		print("winning")

	def winput_init():
		global winput_started
		winput_started = True
		try:
			winputStart(winput_queue[0][0])
		except:
			pass
		print("started")

except:
	underInput = input
	def input(text, callback):
		x = underInput(text+" ")
		callback(text, x)

#def first(text, result):
#    print("Hi " + result)
#    input("What's your favorite color?", second)

#def second(text, result):
#    print("Nice! I like " + result + " too.")

<commit_msg>Update pyinput.py<commit_after>try:
	from browser import window, document
	from javascript import JSObject, JSConstructor
	from ggame import *

	jq = window.jQuery

	if not jq(".inputScreen").length:
		jq("body").append('

<input type = "text" id = "toInput"><input type = "submit" id = "toSubmit">

')
		jq(".inputScreen").hide()

	input_queue = []
	winput_queue = []
	#App1 = App
	winput_started = False
	def input_callback():
		input_value = jq('#toInput').val()
		jq('#toInput').val('')

		input_t = jq('#toPrompt').text()
		jq('#toPrompt').text('')

		global input_queue

		print(input_t + " " + input_value)
		input_queue[0][1](input_t, input_value)
		input_queue.pop(0)
		if len(input_queue) > 0:
			inputStart(input_queue[0][0])

	def input_fade(ev):
		jq('.inputScreen').fadeOut(300, input_callback)
		jq('#toSubmit').off('click')

	def inputStart(text):
		jq('.inputScreen').fadeIn(300)
		jq('#toPrompt').text(text)
		jq('#toSubmit').on('click', input_fade)

	def input(text, callback):
		global input_queue
		input_queue.append([text, callback])
		if len(input_queue) == 1:
			inputStart(text)

	def winput_callback():
		winput_value = App._win._w.document.getElementById('toInput').value
		App._win._w.document.getElementById('toInput').value = ''

		winput_t = App._win._w.document.getElementById('toPrompt').textContent
		App._win._w.document.getElementById('toPrompt').textContent = ""

		global winput_queue

		print(winput_t + " " + winput_value)
		winput_queue[0][1](winput_t, winput_value)
		winput_queue.pop(0)
		if len(winput_queue) > 0:
			winputStart(winput_queue[0][0])

	def winput_fade(ev):
		App._win._w.document.getElementById('inputScreen').style.display = "none"
		winput_callback()
		App._win._w.document.getElementById('toSubmit').removeEventListener('click', winput_fade)

	def winputStart(text):
		App._win._w.document.getElementById('inputScreen').style.display = "block"
		App._win._w.document.getElementById('toPrompt').textContent = text
		App._win._w.document.getElementById('toSubmit').addEventListener('click', winput_fade)

	def winput(text, callback):
		global winput_queue
		global winput_started
		winput_queue.append([text, callback])
		if winput_started:
			winputStart(text)
			winput_started = False
			print("superWin")
		print("winning")

	def winput_init():
		global winput_started
		winput_started = True
		try:
			winputStart(winput_queue[0][0])
			print("doubleStart")
		except:
			pass
		print("started")

except:
	underInput = input
	def input(text, callback):
		x = underInput(text+" ")
		callback(text, x)

#def first(text, result):
#    print("Hi " + result)
#    input("What's your favorite color?", second)

#def second(text, result):
#    print("Nice! I like " + result + " too.")

<|endoftext|>