Пример #1
0
def _get_asset_hash(path):
    if ':' in path:
        package, path = path.split(':')
        resolver = AssetResolver(package)
    else:
        resolver = AssetResolver()
    fullpath = resolver.resolve(path).abspath()
    md5 = hashlib.md5()
    with open(fullpath, 'rb') as f:
        for chunk in iter(lambda: f.read(128 * md5.block_size), b''):
            md5.update(chunk)
    return md5.hexdigest()
Пример #2
0
def _create_standard_yaml_config_(name='pyramid_oereb_standard.yml',
                                  database='postgresql://*****:*****@localhost/pyramid_oereb'):
    """
    Creates the specified YAML file using a template. This YAML file contains the standard
    configuration to run a oereb server out of the box.

    Args:
        (str): The name of the new file. Default
        database (str): The database connection string.Default:
            'postgresql://*****:*****@localhost/pyramid_oereb'
    """

    # File names
    logo_oereb_name = 'logo_oereb.png'
    logo_confederation_name = 'logo_confederation.png'
    logo_canton_name = 'logo_canton.png'

    # Create pyramid_oereb.yml from template
    template = Template(
        filename=AssetResolver('pyramid_oereb').resolve('standard/pyramid_oereb.yml.mako').abspath(),
        input_encoding='utf-8',
        output_encoding='utf-8'
    )
    config = template.render(sqlalchemy_url=database, png_root_dir='')
    pyramid_oereb_yml = open(name, 'wb+')
    pyramid_oereb_yml.write(config)
    pyramid_oereb_yml.close()

    # Copy static files
    logo_oereb_path = AssetResolver('pyramid_oereb').resolve(
        'standard/{name}'.format(name=logo_oereb_name)
    ).abspath()
    logo_confederation_path = AssetResolver('pyramid_oereb').resolve(
        'standard/{name}'.format(name=logo_confederation_name)
    ).abspath()
    logo_sample_path = AssetResolver('pyramid_oereb').resolve(
        'standard/{name}'.format(name=logo_canton_name)
    ).abspath()
    target_path = os.path.abspath('{path}{sep}{name}'.format(
        path=os.getcwd(), name=logo_oereb_name, sep=os.sep)
    )
    copyfile(logo_oereb_path, target_path)
    target_path = os.path.abspath('{path}{sep}{name}'.format(
        path=os.getcwd(), name=logo_confederation_name, sep=os.sep)
    )
    copyfile(logo_confederation_path, target_path)
    target_path = os.path.abspath('{path}{sep}{name}'.format(
        path=os.getcwd(), name=logo_canton_name, sep=os.sep)
    )
    copyfile(logo_sample_path, target_path)
Пример #3
0
def create_app(settings):
    from horus import groupfinder
    from pyramid.config import Configurator
    from pyramid.authentication import AuthTktAuthenticationPolicy
    from pyramid.authorization import ACLAuthorizationPolicy
    from pyramid.path import AssetResolver
    from pyramid.response import FileResponse

    authn_policy = AuthTktAuthenticationPolicy(settings['auth.secret'],
                                               callback=groupfinder)
    authz_policy = ACLAuthorizationPolicy()

    config = Configurator(settings=settings,
                          authentication_policy=authn_policy,
                          authorization_policy=authz_policy,
                          root_factory='h.resources.RootFactory')

    favicon = AssetResolver().resolve('h:favicon.ico')
    config.add_route('favicon', '/favicon.ico')
    config.add_view(
        lambda request: FileResponse(favicon.abspath(), request=request),
        route_name='favicon')

    config.include(includeme)
    return config.make_wsgi_app()
Пример #4
0
def _generate_md5(spec):
    asset = AssetResolver(None).resolve(spec)
    md5 = hashlib.md5()
    with asset.stream() as stream:
        for block in iter(lambda: stream.read(4096), b''):
            md5.update(block)
    return md5.hexdigest()
Пример #5
0
def template_exists(config, relpath):
    asset_resolver = AssetResolver()
    for md in config.registry.settings['mako.directories']:
        asset_descriptor = asset_resolver.resolve('/'.join([md, relpath]))
        if os.path.exists(asset_descriptor.abspath()):
            return True
    return False
Пример #6
0
def set_up_content_mgmt_app(config, potfile_asset_spec, dictionary_factory):
    """
        provide static content backend for hnc projects, text-keys are provided by babel message extractors into the domain.pot file, this is usually located in the root/locale folder

        config: is pyramid configuration instance
        potfile_asset_spec: describing path to potfile message catalog
        render_ctxt_info: is a function with request as sole parameter, when returns truthy value, additional HTML comments are rendered before the output text, normally only admins are interested in this
    """
    def add_renderer_variables(event):
        if event['renderer_name'] != 'json':
            request = event['request']
            event.update({'_': request._})
        return event

    a = AssetResolver()
    asset_path = a.resolve(potfile_asset_spec)
    if not asset_path.exists():
        raise ValueError(
            "POTFILE does not exist in specified location: {}".format(
                potfile_asset_spec))
    config.add_subscriber(add_content(dictionary_factory, potfile_asset_spec),
                          'pyramid.events.ContextFound')
    config.add_subscriber(add_renderer_variables,
                          'pyramid.events.BeforeRender')

    return lambda: polib.pofile(asset_path.abspath())
Пример #7
0
    def __call__(self, info):
        defname = None
        asset, ext = info.name.rsplit('.', 1)
        if '#' in asset:
            asset, defname = asset.rsplit('#', 1)

        spec = '%s.%s' % (asset, ext)

        isabspath = os.path.isabs(spec)
        colon_in_name = ':' in spec
        isabsspec = colon_in_name and (not isabspath)
        isrelspec = (not isabsspec) and (not isabspath)

        try:
            # try to find the template using default search paths
            self.lookup.get_template(spec)
        except TemplateLookupException:
            if isrelspec:
                # convert relative asset spec to absolute asset spec
                resolver = AssetResolver(info.package)
                asset = resolver.resolve(spec)
                spec = asset.absspec()
            else:
                raise

        return self.renderer_factory(spec, defname, self.lookup)
Пример #8
0
def view_file(request):
    form = get_form(AddSchema)
    if request.POST:
        if 'simpan' in request.POST:
            settings = get_settings()
            input_file = request.POST['upload'].file
            filename = request.POST['upload'].filename
            ext = get_ext(filename)
            
            if ext.lower()!='.png':
                request.session.flash('File harus format png','error')
                return dict(form=form.render())	

            resolver = AssetResolver()
            static_path = resolver.resolve('sipkd:static').abspath()
            fullpath = os.path.join( static_path, 'img/logo.png')
            print '------------------>',fullpath
            output_file = open(fullpath, 'wb')
            input_file.seek(0)
            while True:
                data = input_file.read(2<<16)
                if not data:
                    break
                output_file.write(data)
        
        return route_list(request)
    return dict(form=form.render())        
Пример #9
0
    def _get_hash(self, package, path):
        import hashlib
        from pyramid.path import AssetResolver

        abspath = AssetResolver(package).resolve(path).abspath()
        with open(abspath, "rb") as f:
            return hashlib.md5(f.read()).hexdigest()[:8]
Пример #10
0
def includeme(config):
    """pyramid include. declare the add_thumb_view"""
    settings = config.get_settings()

    storage_dir = settings.get('garasu_wsgithumb.path', 'storage')

    storage_dir = AssetResolver(None).resolve(storage_dir).abspath()
    config.add_directive('add_thumb_view', add_thumb_view)
    config.add_directive('add_file_view', add_file_view)

    sizes = {
        'icon': (16, 16),
        'small': (24, 24),
        'thumb': (80, 80),
        'card': (167, 200),
        'gallery': (320, 480),
        'bloglist': (400, 259),
        'blogview': (1366, 768),
        'featuredimage': (840, 381),
        'landscape': (1024, 780),
        'mobile': 20,
        'web': 35,
        'original': None
    }
    config.add_thumb_view('thumbs',
                          sizes=sizes,
                          document_root=storage_dir,
                          cache_directory='/'.join([storage_dir, 'cache']))
Пример #11
0
def get_location_file_wizard(location):
    """
    Render a wizard file for ``location`` and return the rendered HTML.
    """
    asset_spec = 'webgnome:location_files/%s' % location
    wizard_file = '%s/wizard.mak' % asset_spec
    lookup = PkgResourceTemplateLookup(
        directories=[wizard_file, 'webgnome:templates'])

    try:
        template = lookup.get_template(wizard_file)
        html = template.render(h=helpers)
    except (IOError, TopLevelLookupException):
        html = ''

    json_path = AssetResolver().resolve('%s/wizard.json' % location).abspath()

    try:
        with open(json_path) as f:
            json_data = json.loads(f.read())
    except (OSError, IOError, TypeError):
        json_data = {}
        if os.path.exists(json_path):
            logger.exception('Could not open wizard file: %s' % json_path)

    return {'html': html, 'json': json_data}
Пример #12
0
    def collect_static(self):
        """Collect all static files from all static views for the manifest."""

        def recurse(collector, path):

            for entry in scandir(path):

                if entry.name.startswith("."):
                    # Dot files are usually backups or other no no files
                    continue

                # Don't process our internal cache folder
                if MARKER_FOLDER in entry.path:
                    continue

                relative = os.path.relpath(entry.path, collector.root)

                if entry.is_file():
                    collector.collect(path, name, entry, relative)
                elif entry.is_dir():
                    recurse(collector, entry.path)

        r = AssetResolver()
        for name, asset_spec in self.views.items():

            root = r.resolve(asset_spec).abspath()
            collector = CopyAndHashCollector(root, self.settings)
            recurse(collector, root)
            results = collector.finish()

        # Expose for testing
        return results
Пример #13
0
    def get_template(self, uri):
        """Fetch a template from the cache, or check the filesystem
        for it

        In addition to the basic filesystem lookup, this subclass will
        use pkg_resource to load a file using the asset
        specification syntax.

        """
        isabs = os.path.isabs(uri)
        if (not isabs) and (':' in uri):
            # Windows can't cope with colons in filenames, so we replace the
            # colon with a dollar sign in the filename mako uses to actually
            # store the generated python code in the mako module_directory or
            # in the temporary location of mako's modules
            adjusted = uri.replace(':', '$')
            try:
                if self.filesystem_checks:
                    return self._check(adjusted, self._collection[adjusted])
                else:
                    return self._collection[adjusted]
            except KeyError:
                asset = AssetResolver().resolve(uri)
                if asset.exists():
                    srcfile = asset.abspath()
                    return self._load(srcfile, adjusted)
                raise TopLevelLookupException(
                    "Can not locate template for uri %r" % uri)
        try:
            return TemplateLookup.get_template(self, uri)
        except TemplateLookupException:
            if isabs:
                return self._load(uri, uri)
            else:
                raise
Пример #14
0
def extract_static_assets_from_components(compo_list):
    ar = AssetResolver()

    js_paths = []
    js_name = []
    css_paths = []
    css_name = []

    # The Page needs to be in the webassets first, then all other pages, then all components.
    for cls in compo_list:
        for js in cls.js_name:
            if type(js) is not tuple:
                js = (cls.asset_spec, js)
            if js in js_name:
                continue
            js_name.append(js)
            js_paths.append(ar.resolve('/'.join(js)).abspath())
        cls.js_name = cls.js_name + getattr(cls, 'js_name_no_bundle', [])

        for css in cls.css_name:
            if type(css) is not tuple:
                css = (cls.asset_spec, css)
            if css in css_name:
                continue
            css_name.append(css)
            css_paths.append(ar.resolve('/'.join(css)).abspath())
        cls.css_name = cls.css_name + getattr(cls, 'css_name_no_bundle', [])

    return js_paths, js_name, css_paths, css_name
Пример #15
0
def create_app(settings):
    from pyramid.config import Configurator
    from pyramid.authorization import ACLAuthorizationPolicy
    from pyramid.path import AssetResolver
    from pyramid.response import FileResponse

    from h.auth import HybridAuthenticationPolicy
    from h.models import groupfinder

    authn_policy = HybridAuthenticationPolicy(callback=groupfinder)
    authz_policy = ACLAuthorizationPolicy()

    config = Configurator(
        settings=settings,
        authentication_policy=authn_policy,
        authorization_policy=authz_policy,
    )

    favicon = AssetResolver().resolve('h:favicon.ico')
    config.add_route('favicon', '/favicon.ico')
    config.add_view(
        lambda request: FileResponse(favicon.abspath(), request=request),
        route_name='favicon'
    )

    config.add_route('ok', '/ruok')
    config.add_view(lambda request: 'imok', renderer='string', route_name='ok')

    # Include all the pyramid subcomponents
    config.include(includeme)

    return config.make_wsgi_app()
Пример #16
0
def add_layer(cfg, layer, name='', path='', description=''):
    """ add new layer

    :param layer: layer id
    :param name: name
    :param path: asset path
    :param description: module description
    """
    if not path:
        raise ConfigurationError('Path is required')

    discr = (ID_LAYER, name, layer)

    resolver = AssetResolver()
    directory = resolver.resolve(path).abspath()

    intr = Introspectable(ID_LAYER, discr, name, ID_LAYER)
    intr['name'] = name
    intr['layer'] = layer
    intr['path'] = directory
    intr['asset'] = path
    intr['description'] = description
    intr['filters'] = {}

    storage = cfg.registry.setdefault(ID_LAYER, {})
    layers = storage.setdefault(layer, [])
    layers.insert(0, intr)

    cfg.action(discr, introspectables=(intr, ))
    log.info("Add layer: %s path:%s" % (layer, path))
Пример #17
0
        def register():
            directories = []
            resolver = AssetResolver(self.package_name)

            # defer spec resolution until register to allow for asset
            # overrides to take place in an earlier config phase
            for spec in specs:
                # the trailing slash helps match asset overrides for folders
                if not spec.endswith('/'):
                    spec += '/'
                asset = resolver.resolve(spec)
                directory = asset.abspath()
                if not asset.isdir():
                    raise ConfigurationError('"%s" is not a directory' %
                                             directory)
                intr = self.introspectable(
                    'translation directories',
                    directory,
                    spec,
                    'translation directory',
                )
                intr['directory'] = directory
                intr['spec'] = spec
                introspectables.append(intr)
                directories.append(directory)

            tdirs = self.registry.queryUtility(ITranslationDirectories)
            if tdirs is None:
                tdirs = []
                self.registry.registerUtility(tdirs, ITranslationDirectories)
            if override:
                tdirs.extend(directories)
            else:
                for directory in reversed(directories):
                    tdirs.insert(0, directory)
Пример #18
0
    def pserve_file_config(self, filename, global_conf=None):
        here = os.path.abspath(os.path.dirname(filename))
        defaults = {}
        if global_conf:
            defaults.update(global_conf)
        defaults['here'] = here

        config = self.ConfigParser(defaults=defaults)
        config.optionxform = str
        config.read(filename)
        try:
            items = dict(config.items('pserve'))
        except configparser.NoSectionError:
            return

        watch_files = aslist(items.get('watch_files', ''), flatten=False)

        # track file paths relative to the ini file
        resolver = AssetResolver(package=None)
        for file in watch_files:
            if ':' in file:
                file = resolver.resolve(file).abspath()
            elif not os.path.isabs(file):
                file = os.path.join(here, file)
            self.watch_files.append(os.path.abspath(file))
Пример #19
0
def _get_asset_hash(path):
    """Returns an MD5 hash of the given assets path.

    :param path: An asset specification to the asset file.
    """
    if ":" in path:
        package, path = path.split(":")
        resolver = AssetResolver(package)
    else:
        resolver = AssetResolver()
    fullpath = resolver.resolve(path).abspath()
    md5 = hashlib.md5()
    with open(fullpath, "rb") as f:
        for chunk in iter(lambda: f.read(128 * md5.block_size), b""):
            md5.update(chunk)
    return md5.hexdigest()
Пример #20
0
def register_less_bundle(cfg, name, path, description='', watchdir=None):
    """ Register less bundle;

    :param name: module name
    :param path: asset path
    :param description:
    """
    resolver = AssetResolver()
    abs_path = resolver.resolve(path).abspath()

    if not os.path.isfile(abs_path):
        raise ConfigurationError("less file is required: %s" % path)

    discr = (ID_BUNDLE, name)

    intr = Introspectable(ID_BUNDLE, discr, name, ID_BUNDLE)
    intr['name'] = name
    intr['path'] = path
    intr['abs_path'] = abs_path
    intr['watchdir'] = watchdir or os.path.dirname(abs_path)
    intr['description'] = description

    storage = cfg.registry.setdefault(ID_BUNDLE, {})
    storage[name] = intr

    cfg.action(discr, introspectables=(intr, ))
Пример #21
0
def _create_yaml_config_(
        name='pyramid_oereb.yml',
        database='postgresql://*****:*****@localhost/pyramid_oereb',
        print_backend='MapFishPrint',
        print_url='http://*****:*****@localhost/pyramid_oereb'
    """

    # Create pyramid_oereb.yml from template
    template = Template(filename=AssetResolver('dev').resolve(
        'config/pyramid_oereb.yml.mako').abspath(),
                        input_encoding='utf-8',
                        output_encoding='utf-8')
    config = template.render(sqlalchemy_url=database,
                             print_backend=print_backend,
                             print_url=print_url)
    pyramid_oereb_yml = open(name, 'wb+')
    pyramid_oereb_yml.write(config)
    pyramid_oereb_yml.close()
Пример #22
0
def xml_templates():
    a = AssetResolver('pyramid_oereb')
    resolver = a.resolve('lib/renderer/extract/templates/xml')
    templates = TemplateLookup(directories=[resolver.abspath()],
                               output_encoding='utf-8',
                               input_encoding='utf-8')
    return templates
Пример #23
0
def register_handlebars_bundle(cfg,
                               name,
                               path='',
                               description='',
                               i18n_domain=''):
    """ Register handlebars bundle;

    :param name: module name
    :param path: asset path
    :param description:
    :param i18n_domain: i18n domain

    """
    resolver = AssetResolver()
    abs_path = resolver.resolve(path).abspath()

    if not path or not os.path.isdir(abs_path):
        raise ConfigurationError("Directory is required: %s" % path)

    discr = (ID_AMD_MODULE, name)

    intr = Introspectable(ID_AMD_MODULE, discr, name, ID_AMD_MODULE)
    intr['name'] = name
    intr['path'] = path
    intr['abs_path'] = abs_path
    intr['description'] = description
    intr['i18n_domain'] = i18n_domain

    storage = cfg.registry.setdefault(ID_BUNDLE, {})
    storage[name] = intr

    cfg.action(discr, introspectables=(intr, ))
Пример #24
0
 def get_parser(self, ini_filename='test.ini'):
     medici = AssetResolver('pyramid_analytics')
     app_path = medici.resolve('').abspath()
     project_path = path.split(app_path)[0]
     ini_path = path.join(project_path, 'pyramid_analytics', ini_filename)
     parser = configparser.SafeConfigParser()
     parser.read(ini_path)
     return parser
Пример #25
0
 def __call__(self, info):
     resolver = AssetResolver(info.package)
     tmpl_path = resolver.resolve(info.name).abspath()
     return GenshiTemplateRenderer(
         path=tmpl_path,
         settings=info.settings,
         package=info.package,
     )
Пример #26
0
def add_amd_dir(cfg, path):
    data = cfg.get_settings()['amd.debug.data']
    paths = data.setdefault('paths', [])

    resolver = AssetResolver()
    directory = resolver.resolve(path).abspath()

    paths.append((path, directory))
    log.info("Add resource dir: %s" % path)
Пример #27
0
    def search_for_source(self, item):
        try:
            item = AssetResolver(None).resolve(item).abspath()
        except ImportError as e:
            raise BundleError(e)
        except ValueError as e:
            return super(PyramidResolver, self).search_for_source(item)

        return item
Пример #28
0
def _create_standard_configuration_models_py_(code, geometry_type, absolute_path, schema=None,
                                              primary_key_is_string=False):
    """
    The simplest way to get a python file containing a database definition in sqlalchemy orm way. It will
     contain all necessary definitions to produce an extract as the specification defines for the new topic.

    Args:
        code (str): The unique Code for the new model (see oereb specification for more details)
        geometry_type (str): A valid geometry type.
        absolute_path (str): The absolute Path where the genderated python file will be placed. It
            must bewriteable by the user running this command.
        schema (str): The schema name. If not specified, "name" will be used.
        primary_key_is_string (bool): The type of the primary key. You can use this to switch between STRING
            type or INTEGER type. Standard is to INTEGER => False
    """
    if primary_key_is_string:
        template = Template(
            filename=AssetResolver('pyramid_oereb').resolve(
                'standard/templates/plr_string_primary_keys.py.mako'
            ).abspath()
        )
    else:
        template = Template(
            filename=AssetResolver('pyramid_oereb').resolve(
                'standard/templates/plr_integer_primary_keys.py.mako'
            ).abspath()
        )
    name = convert_camel_case_to_snake_case(code)
    content = template.render(**{
        'topic': convert_camel_case_to_text_form(code),
        'schema_name': schema or name,
        'geometry_type': geometry_type
    })
    models_path = '{path}/{name}.py'.format(
        path=absolute_path,
        name=name
    )
    models_path = os.path.abspath(models_path)
    if os.path.exists(models_path):
        os.remove(models_path)
    models_file = open(models_path, 'w+')
    models_file.write(content)
    models_file.close()
Пример #29
0
 def __init__(self, request):
     self.request = request
     resolver = AssetResolver()
     file_path = resolver.resolve('nadamusic:integrations.json').abspath()
     with open(file_path) as f:
         creds = json.load(f)
     self.client_id = creds['client_id']
     self.client_secret = creds['client_secret']
     self.redirect_uri = creds['redirect_uri']
     self.scope = creds['scope']
Пример #30
0
    def _get_abspath(self, filename):
        """Get the absolute path of the file

        :param filename: name of file
        :type filename: str

        :return: abspath to file
        :rtype: str
        """
        return AssetResolver().resolve(filename).abspath()