Exemple #1
0
    def iteritems(self, fallback=True):
        done = set()

        for key, value in iteritems(self.original_data):
            done.add(key)
            if key in implied_keys:
                continue
            if key in self._changed:
                value = self._data[key]
            if value is not None:
                yield key, value

        if fallback and self.fallback_data:
            for key, value in iteritems(self.fallback_data):
                if key in implied_keys or key in done:
                    continue
                done.add(key)
                if key in self._changed:
                    value = self._data[key]
                if value is not None:
                    yield key, value

        for key in sorted(self._data):
            if key in done:
                continue
            value = self._data.get(key)
            if value is not None:
                yield key, value
Exemple #2
0
    def iteritems(self, fallback=True):
        done = set()

        for key, value in iteritems(self.original_data):
            done.add(key)
            if key in implied_keys:
                continue
            if key in self._changed:
                value = self._data[key]
            if value is not None:
                yield key, value

        if fallback and self.fallback_data:
            for key, value in iteritems(self.fallback_data):
                if key in implied_keys or key in done:
                    continue
                done.add(key)
                if key in self._changed:
                    value = self._data[key]
                if value is not None:
                    yield key, value

        for key in sorted(self._data):
            if key in done:
                continue
            value = self._data.get(key)
            if value is not None:
                yield key, value
def update_cache(package_root,
                 remote_packages,
                 local_package_path,
                 refresh=False):
    """Updates the package cache at package_root for the given dictionary
    of packages as well as packages in the given local package path.
    """
    requires_wipe = False
    if refresh:
        click.echo("Force package cache refresh.")
        requires_wipe = True

    manifest_file = os.path.join(package_root, "lektor-packages.manifest")
    local_packages = list_local_packages(local_package_path)

    old_manifest = load_manifest(manifest_file)
    to_install = []

    all_packages = dict(remote_packages)
    all_packages.update((x, None) for x in local_packages)

    # step 1: figure out which remote packages to install.
    for package, version in iteritems(remote_packages):
        old_version = old_manifest.pop(package, None)
        if old_version is None:
            to_install.append((package, version))
        elif old_version != version:
            requires_wipe = True

    # step 2: figure out which local packages to install
    for package in local_packages:
        if old_manifest.pop(package, False) is False:
            to_install.append((package, None))

    # Bad news, we need to wipe everything
    if requires_wipe or old_manifest:
        try:
            shutil.rmtree(package_root)
        except OSError:
            pass
        to_install = iteritems(all_packages)

    if to_install:
        click.echo("Updating packages in %s for project" % package_root)
        try:
            os.makedirs(package_root)
        except OSError:
            pass
        for package, version in to_install:
            if package[:1] == "@":
                install_local_package(
                    package_root, os.path.join(local_package_path,
                                               package[1:]))
            else:
                download_and_install_package(package_root, package, version)
        write_manifest(manifest_file, all_packages)
Exemple #4
0
def update_cache(package_root, remote_packages, local_package_path,
                 refresh=False):
    """Updates the package cache at package_root for the given dictionary
    of packages as well as packages in the given local package path.
    """
    requires_wipe = False
    if refresh:
        click.echo('Force package cache refresh.')
        requires_wipe = True

    manifest_file = os.path.join(package_root, 'lektor-packages.manifest')
    local_packages = list_local_packages(local_package_path)

    old_manifest = load_manifest(manifest_file)
    to_install = []

    all_packages = dict(remote_packages)
    all_packages.update((x, None) for x in local_packages)

    # step 1: figure out which remote packages to install.
    for package, version in iteritems(remote_packages):
        old_version = old_manifest.pop(package, None)
        if old_version is None:
            to_install.append((package, version))
        elif old_version != version:
            requires_wipe = True

    # step 2: figure out which local packages to install
    for package in local_packages:
        if old_manifest.pop(package, False) is False:
            to_install.append((package, None))

    # Bad news, we need to wipe everything
    if requires_wipe or old_manifest:
        try:
            shutil.rmtree(package_root)
        except OSError:
            pass
        to_install = iteritems(all_packages)

    if to_install:
        click.echo('Updating packages in %s for project' % package_root)
        try:
            os.makedirs(package_root)
        except OSError:
            pass
        for package, version in to_install:
            if package[:1] == '@':
                install_local_package(package_root,
                    os.path.join(local_package_path, package[1:]))
            else:
                download_and_install_package(package_root, package, version)
        write_manifest(manifest_file, all_packages)
Exemple #5
0
 def get_alternative_url_prefixes(self):
     """Returns a list of alternative url prefixes by length."""
     items = [(v["url_prefix"].lstrip("/"), k)
              for k, v in iteritems(self.values["ALTERNATIVES"])
              if v["url_prefix"]]
     items.sort(key=lambda x: -len(x[0]))
     return items
Exemple #6
0
def discover_relevant_flowblock_models(flow, pad, record, alt):
    """Returns a dictionary of all relevant flow blocks.  If no list of
    flow block names is provided all flow blocks are returned.  Otherwise
    only flow blocks that are in the list or are children of flowblocks
    in the list are returned.
    """
    flow_blocks = flow.flow_blocks

    all_blocks = pad.db.flowblocks
    if flow_blocks is None:
        return dict((k, v.to_json(pad, record, alt))
                    for k, v in iteritems(all_blocks))

    wanted_blocks = set()
    to_process = flow_blocks[:]

    while to_process:
        block_name = to_process.pop()
        flowblock = all_blocks.get(block_name)
        if block_name in wanted_blocks or flowblock is None:
            continue
        wanted_blocks.add(block_name)
        for field in flowblock.fields:
            if isinstance(field.type, FlowType):
                if field.type.flow_blocks is None:
                    raise RuntimeError('Nested flow-blocks require explicit '
                                       'list of involved blocks.')
                to_process.extend(field.type.flow_blocks)

    rv = {}
    for block_name in wanted_blocks:
        rv[block_name] = all_blocks[block_name].to_json(pad, record, alt)

    return rv
Exemple #7
0
def _process_search_results(builder, cur, alt, lang, limit):
    mapping = _mapping_from_cursor(cur)
    rv = []

    files_needed = set()

    for path, infos in iteritems(mapping):
        info = _find_best_info(infos, alt, lang)
        if info is None:
            continue

        for parent in _iter_parents(path):
            if parent not in mapping:
                files_needed.add(parent)

        rv.append(info)
        if len(rv) == limit:
            break

    if files_needed:
        cur.execute('''
            select path, alt, lang, type, title
              from source_info
             where path in (%s)
        ''' % ', '.join(['?'] * len(files_needed)), list(files_needed))
        mapping.update(_mapping_from_cursor(cur))

    for info in rv:
        info['parents'] = _build_parent_path(info['path'], mapping, alt, lang)

    return rv
Exemple #8
0
def get_new_record_info():
    # XXX: convert to tree usage
    pad = g.admin_context.pad
    alt = request.args.get('alt') or PRIMARY_ALT
    ts = g.admin_context.tree.edit(request.args['path'], alt=alt)
    if ts.is_attachment:
        can_have_children = False
    elif ts.datamodel.child_config.replaced_with is not None:
        can_have_children = False
    else:
        can_have_children = True
    implied = ts.datamodel.child_config.model

    def describe_model(model):
        primary_field = None
        if model.primary_field is not None:
            f = model.field_map.get(model.primary_field)
            if f is not None:
                primary_field = f.to_json(pad)
        return {
            'id': model.id,
            'name': model.name,
            'name_i18n': model.name_i18n,
            'primary_field': primary_field
        }

    return jsonify({
        'label': ts.record and ts.record.record_label or ts.id,
        'can_have_children': can_have_children,
        'implied_model': implied,
        'available_models': dict(
            (k, describe_model(v)) for k, v in iteritems(pad.db.datamodels)
            if not v.hidden or k == implied)
    })
Exemple #9
0
 def get_alternative_url_suffixes(self):
     """Returns a list of alternative url suffixes by length."""
     items = [(v['url_suffix'].rstrip('/'), k)
              for k, v in iteritems(self.values['ALTERNATIVES'])
              if v['url_suffix']]
     items.sort(key=lambda x: -len(x[0]))
     return items
Exemple #10
0
 def get_alternative_url_suffixes(self):
     """Returns a list of alternative url suffixes by length."""
     items = [(v['url_suffix'].rstrip('/'), k)
              for k, v in iteritems(self.values['ALTERNATIVES'])
              if v['url_suffix']]
     items.sort(key=lambda x: -len(x[0]))
     return items
Exemple #11
0
def discover_relevant_flowblock_models(flow, pad, record, alt):
    """Returns a dictionary of all relevant flow blocks.  If no list of
    flow block names is provided all flow blocks are returned.  Otherwise
    only flow blocks that are in the list or are children of flowblocks
    in the list are returned.
    """
    flow_blocks = flow.flow_blocks

    all_blocks = pad.db.flowblocks
    if flow_blocks is None:
        return dict(
            (k, v.to_json(pad, record, alt)) for k, v in iteritems(all_blocks))

    wanted_blocks = set()
    to_process = flow_blocks[:]

    while to_process:
        block_name = to_process.pop()
        flowblock = all_blocks.get(block_name)
        if block_name in wanted_blocks or flowblock is None:
            continue
        wanted_blocks.add(block_name)
        for field in flowblock.fields:
            if isinstance(field.type, FlowType):
                if field.type.flow_blocks is None:
                    raise RuntimeError('Nested flow-blocks require explicit '
                                       'list of involved blocks.')
                to_process.extend(field.type.flow_blocks)

    rv = {}
    for block_name in wanted_blocks:
        rv[block_name] = all_blocks[block_name].to_json(pad, record, alt)

    return rv
Exemple #12
0
def update_config_from_ini(config, inifile):
    def set_simple(target, source_path):
        rv = config.get(source_path)
        if rv is not None:
            config[target] = rv

    set_simple(target='IMAGEMAGICK_EXECUTABLE',
               source_path='env.imagemagick_executable')
    set_simple(target='LESSC_EXECUTABLE', source_path='env.lessc_executable')

    for section_name in ('ATTACHMENT_TYPES', 'PROJECT', 'PACKAGES'):
        section_config = inifile.section_as_dict(section_name.lower())
        config[section_name].update(section_config)

    for sect in inifile.sections():
        if sect.startswith('servers.'):
            server_id = sect.split('.')[1]
            config['SERVERS'][server_id] = inifile.section_as_dict(sect)
        elif sect.startswith('alternatives.'):
            alt = sect.split('.')[1]
            config['ALTERNATIVES'][alt] = {
                'name': get_i18n_block(inifile, 'alternatives.%s.name' % alt),
                'url_prefix': inifile.get('alternatives.%s.url_prefix' % alt),
                'url_suffix': inifile.get('alternatives.%s.url_suffix' % alt),
                'primary': inifile.get_bool('alternatives.%s.primary' % alt),
                'locale': inifile.get('alternatives.%s.locale' % alt, 'en_US'),
            }

    for alt, alt_data in iteritems(config['ALTERNATIVES']):
        if alt_data['primary']:
            config['PRIMARY_ALTERNATIVE'] = alt
            break
    else:
        if config['ALTERNATIVES']:
            raise RuntimeError('Alternatives defined but no primary set.')
Exemple #13
0
def test_exif(pad):
    image = pad.root.attachments.images.get('test.jpg')
    assert image is not None

    assert image.exif

    assert almost_equal(image.exif.altitude, 779.0293)
    assert almost_equal(image.exif.aperture, 2.275)
    assert image.exif.artist is None
    assert image.exif.camera == 'Apple iPhone 6'
    assert image.exif.camera_make == 'Apple'
    assert image.exif.camera_model == 'iPhone 6'
    assert image.exif.copyright is None
    assert image.exif.created_at == datetime(2015, 12, 6, 11, 37, 34)
    assert image.exif.modified_at == datetime(2015, 12, 6, 11, 37, 34)
    assert image.exif.exposure_time == '1/33'
    assert image.exif.f == u'\u0192/2.2'
    assert almost_equal(image.exif.f_num, 2.2)
    assert image.exif.flash_info == 'Flash did not fire, compulsory flash mode'
    assert image.exif.focal_length == '4.15mm'
    assert image.exif.focal_length_35mm == '29mm'
    assert image.exif.iso == 160
    assert almost_equal(image.exif.latitude, 46.6338333)
    assert image.exif.lens == 'Apple iPhone 6 back camera 4.15mm f/2.2'
    assert image.exif.lens_make == 'Apple'
    assert image.exif.lens_model == 'iPhone 6 back camera 4.15mm f/2.2'
    assert almost_equal(image.exif.longitude, 13.4048333)
    assert image.exif.location == (image.exif.latitude, image.exif.longitude)
    assert image.exif.shutter_speed == '1/33'

    assert isinstance(image.exif.to_dict(), dict)

    for key, value in iteritems(image.exif.to_dict()):
        assert getattr(image.exif, key) == value
Exemple #14
0
def test_exif(pad):
    image = pad.root.attachments.images.get('test.jpg')
    assert image is not None

    assert image.exif

    assert almost_equal(image.exif.altitude, 779.0293)
    assert almost_equal(image.exif.aperture, 2.275)
    assert image.exif.artist is None
    assert image.exif.camera == 'Apple iPhone 6'
    assert image.exif.camera_make == 'Apple'
    assert image.exif.camera_model == 'iPhone 6'
    assert image.exif.copyright is None
    assert image.exif.created_at == datetime(2015, 12, 6, 11, 37, 34)
    assert image.exif.exposure_time == '1/33'
    assert image.exif.f == u'\u0192/2.2'
    assert almost_equal(image.exif.f_num, 2.2)
    assert image.exif.flash_info == 'Flash did not fire, compulsory flash mode'
    assert image.exif.focal_length == '4.15mm'
    assert image.exif.focal_length_35mm == '29mm'
    assert image.exif.iso == 160
    assert almost_equal(image.exif.latitude, 46.6338333)
    assert image.exif.lens == 'Apple iPhone 6 back camera 4.15mm f/2.2'
    assert image.exif.lens_make == 'Apple'
    assert image.exif.lens_model == 'iPhone 6 back camera 4.15mm f/2.2'
    assert almost_equal(image.exif.longitude, 13.4048333)
    assert image.exif.location == (image.exif.latitude, image.exif.longitude)
    assert image.exif.shutter_speed == '1/33'

    assert isinstance(image.exif.to_dict(), dict)

    for key, value in iteritems(image.exif.to_dict()):
        assert getattr(image.exif, key) == value
Exemple #15
0
def remove_package_from_project(project, name):
    cfg = project.open_config()
    choices = (name.lower(), "lektor-" + name.lower())
    for pkg, version in iteritems(cfg.section_as_dict("packages")):
        if pkg.lower() in choices:
            del cfg["packages.%s" % pkg]
            cfg.save()
            return {"name": pkg, "version": version}
Exemple #16
0
def _get_package_version_from_project(cfg, name):
    choices = (name.lower(), 'lektor-' + name.lower())
    for pkg, version in iteritems(cfg.section_as_dict('packages')):
        if pkg.lower() in choices:
            return {
                'name': pkg,
                'version': version
            }
Exemple #17
0
def _get_package_version_from_project(cfg, name):
    choices = (name.lower(), 'lektor-' + name.lower())
    for pkg, version in iteritems(cfg.section_as_dict('packages')):
        if pkg.lower() in choices:
            return {
                'name': pkg,
                'version': version
            }
Exemple #18
0
def add_system_field(name, **opts):
    for key, value in opts.items():
        if key.endswith('_i18n'):
            base_key = key[:-5]
            for lang, trans in iteritems(load_i18n_block(value)):
                opts['%s[%s]' % (base_key, lang)] = trans

    ty = types.builtin_types[opts.pop('type')]
    system_fields[name] = (ty, opts)
Exemple #19
0
def remove_package_from_project(project, name):
    cfg = project.open_config()
    choices = (name.lower(), "lektor-" + name.lower())
    for pkg, version in iteritems(cfg.section_as_dict("packages")):
        if pkg.lower() in choices:
            del cfg["packages.%s" % pkg]
            cfg.save()
            return {"name": pkg, "version": version}
    return None
Exemple #20
0
    def __init__(
        self,
        env,
        id,
        name_i18n,
        label_i18n=None,
        filename=None,
        hidden=None,
        protected=None,
        child_config=None,
        attachment_config=None,
        pagination_config=None,
        fields=None,
        primary_field=None,
        parent=None,
    ):
        self.env = env
        self.filename = filename
        self.id = id
        self.name_i18n = name_i18n
        self.label_i18n = label_i18n
        if hidden is None:
            hidden = False
        self.hidden = hidden
        if protected is None:
            protected = False
        self.protected = protected
        if child_config is None:
            child_config = ChildConfig()
        self.child_config = child_config
        if attachment_config is None:
            attachment_config = AttachmentConfig()
        self.attachment_config = attachment_config
        if pagination_config is None:
            pagination_config = PaginationConfig(env)
        self.pagination_config = pagination_config
        if fields is None:
            fields = []
        self.fields = fields
        if primary_field is None and fields:
            primary_field = fields[0].name
        self.primary_field = primary_field
        self.parent = parent

        # This is a mapping of the key names to the actual field which
        # also includes the system fields.  This is primarily used for
        # fast internal operations but also the admin.
        self.field_map = dict((x.name, x) for x in fields)
        for key, (ty, opts) in iteritems(system_fields):
            self.field_map[key] = Field(env, name=key, type=ty, options=opts)

        self._child_slug_tmpl = None
        self._child_replacements = None
        self._label_tmpls = {}
Exemple #21
0
def _reflow_and_split_labels(labels):
    rv = []
    for lang, string in iteritems(labels):
        for idx, item in enumerate(string.split(",")):
            try:
                d = rv[idx]
            except LookupError:
                d = {}
                rv.append(d)
            d[lang] = item.strip()
    return rv
Exemple #22
0
def remove_package_from_project(project, name):
    cfg = project.open_config()
    choices = (name.lower(), 'lektor-' + name.lower())
    for pkg, version in iteritems(cfg.section_as_dict('packages')):
        if pkg.lower() in choices:
            del cfg['packages.%s' % pkg]
            cfg.save()
            return {
                'name': pkg,
                'version': version
            }
Exemple #23
0
def remove_package_from_project(project, name):
    cfg = project.open_config()
    choices = (name.lower(), 'lektor-' + name.lower())
    for pkg, version in iteritems(cfg.section_as_dict('packages')):
        if pkg.lower() in choices:
            del cfg['packages.%s' % pkg]
            cfg.save()
            return {
                'name': pkg,
                'version': version
            }
Exemple #24
0
def _reflow_and_split_labels(labels):
    rv = []
    for lang, string in iteritems(labels):
        for idx, item in enumerate(string.split(',')):
            try:
                d = rv[idx]
            except LookupError:
                d = {}
                rv.append(d)
            d[lang] = item.strip()
    return rv
Exemple #25
0
    def consolidate_listing(self, con, current_artifacts):
        server_artifacts, duplicates = self.read_existing_artifacts(con)
        known_folders = set()
        for artifact_name in iterkeys(current_artifacts):
            known_folders.add(posixpath.dirname(artifact_name))

        for artifact_name, checksum in iteritems(server_artifacts):
            if artifact_name not in current_artifacts:
                con.log_buffer.append('000 Deleting %s' % artifact_name)
                con.delete_file(artifact_name)
                folder = posixpath.dirname(artifact_name)
                if folder not in known_folders:
                    con.log_buffer.append('000 Deleting %s' % folder)
                    con.delete_folder(folder)

        if duplicates or server_artifacts != current_artifacts:
            listing = []
            for artifact_name, checksum in iteritems(current_artifacts):
                listing.append('%s|%s\n' % (artifact_name, checksum))
            listing.sort()
            con.upload_file('.lektor/.listing.tmp', ''.join(listing))
            con.rename_file('.lektor/.listing.tmp', '.lektor/listing')
Exemple #26
0
def generate_i18n_kvs(**opts):
    """Generates key-value pairs based on the kwargs passed into this function.
    For every key ending in "_i18n", its corresponding value will be translated
    and returned once for every language that has a known translation.
    """
    for key, value in opts.items():
        if key.endswith('_i18n'):
            base_key = key[:-5]
            for lang, trans in iteritems(load_i18n_block(value)):
                lang_key = '%s[%s]' % (base_key, lang)
                yield lang_key, trans
        else:
            yield key, value
Exemple #27
0
    def to_json(self, pad, record=None, alt=PRIMARY_ALT):
        rv = Type.to_json(self, pad, record, alt)

        rv['flowblocks'] = discover_relevant_flowblock_models(
            self, pad, record, alt)

        block_order = self.flow_blocks
        if block_order is None:
            block_order = [k for k, v in sorted(iteritems(pad.db.flowblocks),
                                                key=lambda x: x[1].order)]
        rv['flowblock_order'] = block_order

        return rv
Exemple #28
0
def generate_i18n_kvs(**opts):
    """Generates key-value pairs based on the kwargs passed into this function.
    For every key ending in "_i18n", its corresponding value will be translated
    and returned once for every language that has a known translation.
    """
    for key, value in opts.items():
        if key.endswith('_i18n'):
            base_key = key[:-5]
            for lang, trans in iteritems(load_i18n_block(value)):
                lang_key = '%s[%s]' % (base_key, lang)
                yield lang_key, trans
        else:
            yield key, value
Exemple #29
0
    def consolidate_listing(self, con, current_artifacts):
        server_artifacts, duplicates = self.read_existing_artifacts(con)
        known_folders = set()
        for artifact_name in iterkeys(current_artifacts):
            known_folders.add(posixpath.dirname(artifact_name))

        for artifact_name, checksum in iteritems(server_artifacts):
            if artifact_name not in current_artifacts:
                con.log_buffer.append('000 Deleting %s' % artifact_name)
                con.delete_file(artifact_name)
                folder = posixpath.dirname(artifact_name)
                if folder not in known_folders:
                    con.log_buffer.append('000 Deleting %s' % folder)
                    con.delete_folder(folder)

        if duplicates or server_artifacts != current_artifacts:
            listing = []
            for artifact_name, checksum in iteritems(current_artifacts):
                listing.append('%s|%s\n' % (artifact_name, checksum))
            listing.sort()
            con.upload_file('.lektor/.listing.tmp', ''.join(listing))
            con.rename_file('.lektor/.listing.tmp', '.lektor/listing')
Exemple #30
0
def merge(a, b):
    """Merges two values together."""
    if b is None and a is not None:
        return a
    if a is None:
        return b
    if isinstance(a, list) and isinstance(b, list):
        for idx, (item_1, item_2) in enumerate(zip(a, b)):
            a[idx] = merge(item_1, item_2)
    if isinstance(a, dict) and isinstance(b, dict):
        for key, value in iteritems(b):
            a[key] = merge(a.get(key), value)
        return a
    return a
Exemple #31
0
def merge(a, b):
    """Merges two values together."""
    if b is None and a is not None:
        return a
    if a is None:
        return b
    if isinstance(a, list) and isinstance(b, list):
        for idx, (item_1, item_2) in enumerate(zip(a, b)):
            a[idx] = merge(item_1, item_2)
    if isinstance(a, dict) and isinstance(b, dict):
        for key, value in iteritems(b):
            a[key] = merge(a.get(key), value)
        return a
    return a
Exemple #32
0
    def to_json(self, pad, record=None, alt=PRIMARY_ALT):
        rv = Type.to_json(self, pad, record, alt)

        rv['flowblocks'] = discover_relevant_flowblock_models(
            self, pad, record, alt)

        block_order = self.flow_blocks
        if block_order is None:
            block_order = [
                k for k, v in sorted(iteritems(pad.db.flowblocks),
                                     key=lambda x: x[1].order)
            ]
        rv['flowblock_order'] = block_order

        return rv
Exemple #33
0
    def _convert(container):
        if _value_marker in container:
            force_list = False
            values = container.pop(_value_marker)
            if container.pop(_list_marker, False):
                force_list = True
                values.extend(_convert(x[1]) for x in sorted(container.items()))
            if not force_list and len(values) == 1:
                values = values[0]

            if not container:
                return values
            return _convert(container)
        elif container.pop(_list_marker, False):
            return [_convert(x[1]) for x in sorted(container.items())]
        return dict_cls((k, _convert(v)) for k, v in iteritems(container))
Exemple #34
0
    def __init__(self, path, filename, alt=PRIMARY_ALT,
                 type='unknown', title_i18n=None):
        self.path = path
        self.alt = alt
        self.filename = filename
        self.type = type
        self.title_i18n = {}

        en_title = self.path
        if 'en' in title_i18n:
            en_title = title_i18n['en']
        for key, value in iteritems(title_i18n):
            if key == 'en':
                continue
            if value != en_title:
                self.title_i18n[key] = value
        self.title_i18n['en'] = en_title
Exemple #35
0
    def _convert(container):
        if _value_marker in container:
            force_list = False
            values = container.pop(_value_marker)
            if container.pop(_list_marker, False):
                force_list = True
                values.extend(_convert(x[1]) for x in
                              sorted(container.items()))
            if not force_list and len(values) == 1:
                values = values[0]

            if not container:
                return values
            return _convert(container)
        elif container.pop(_list_marker, False):
            return [_convert(x[1]) for x in sorted(container.items())]
        return dict_cls((k, _convert(v)) for k, v in iteritems(container))
Exemple #36
0
 def write_source_info(self, info):
     """Writes the source info into the database.  The source info is
     an instance of :class:`lektor.build_programs.SourceInfo`.
     """
     reporter.report_write_source_info(info)
     source = self.to_source_filename(info.filename)
     con = self.connect_to_database()
     try:
         cur = con.cursor()
         for lang, title in iteritems(info.title_i18n):
             cur.execute('''
                 insert or replace into source_info
                     (path, alt, lang, type, source, title)
                     values (?, ?, ?, ?, ?, ?)
             ''', [info.path, info.alt, lang, info.type, source, title])
         con.commit()
     finally:
         con.close()
Exemple #37
0
 def write_source_info(self, info):
     """Writes the source info into the database.  The source info is
     an instance of :class:`lektor.build_programs.SourceInfo`.
     """
     reporter.report_write_source_info(info)
     source = self.to_source_filename(info.filename)
     con = self.connect_to_database()
     try:
         cur = con.cursor()
         for lang, title in iteritems(info.title_i18n):
             cur.execute('''
                 insert or replace into source_info
                     (path, alt, lang, type, source, title)
                     values (?, ?, ?, ?, ?, ?)
             ''', [info.path, info.alt, lang, info.type, source, title])
         con.commit()
     finally:
         con.close()
    def __init__(
        self, path, filename, alt=PRIMARY_ALT, type="unknown", title_i18n=None
    ):
        self.path = path
        self.alt = alt
        self.filename = filename
        self.type = type
        self.title_i18n = {}

        en_title = self.path
        if "en" in title_i18n:
            en_title = title_i18n["en"]
        for key, value in iteritems(title_i18n):
            if key == "en":
                continue
            if value != en_title:
                self.title_i18n[key] = value
        self.title_i18n["en"] = en_title
Exemple #39
0
    def __init__(self, env, id, name_i18n, label_i18n=None,
                 filename=None, hidden=None, protected=None,
                 child_config=None, attachment_config=None,
                 pagination_config=None, fields=None,
                 primary_field=None, parent=None):
        self.env = env
        self.filename = filename
        self.id = id
        self.name_i18n = name_i18n
        self.label_i18n = label_i18n
        if hidden is None:
            hidden = False
        self.hidden = hidden
        if protected is None:
            protected = False
        self.protected = protected
        if child_config is None:
            child_config = ChildConfig()
        self.child_config = child_config
        if attachment_config is None:
            attachment_config = AttachmentConfig()
        self.attachment_config = attachment_config
        if pagination_config is None:
            pagination_config = PaginationConfig(env)
        self.pagination_config = pagination_config
        if fields is None:
            fields = []
        self.fields = fields
        if primary_field is None and fields:
            primary_field = fields[0].name
        self.primary_field = primary_field
        self.parent = parent

        # This is a mapping of the key names to the actual field which
        # also includes the system fields.  This is primarily used for
        # fast internal operations but also the admin.
        self.field_map = dict((x.name, x) for x in fields)
        for key, (ty, opts) in iteritems(system_fields):
            self.field_map[key] = Field(env, name=key, type=ty, options=opts)

        self._child_slug_tmpl = None
        self._child_replacements = None
        self._label_tmpls = {}
Exemple #40
0
def update_config_from_ini(config, inifile):
    def set_simple(target, source_path):
        rv = config.get(source_path)
        if rv is not None:
            config[target] = rv

    set_simple(target='IMAGEMAGICK_EXECUTABLE',
               source_path='env.imagemagick_executable')
    set_simple(target='LESSC_EXECUTABLE',
               source_path='env.lessc_executable')

    config['ATTACHMENT_TYPES'].update(
        (k.encode('ascii', 'replace'), v.encode('ascii', 'replace'))
        for k, v in inifile.section_as_dict('attachment_types'))

    config['PROJECT'].update(inifile.section_as_dict('project'))
    config['PACKAGES'].update(inifile.section_as_dict('packages'))

    for sect in inifile.sections():
        if sect.startswith('servers.'):
            server_id = sect.split('.')[1]
            config['SERVERS'][server_id] = inifile.section_as_dict(sect)
        elif sect.startswith('alternatives.'):
            alt = sect.split('.')[1]
            config['ALTERNATIVES'][alt] = {
                'name': get_i18n_block(inifile, 'alternatives.%s.name' % alt),
                'url_prefix': inifile.get('alternatives.%s.url_prefix' % alt),
                'url_suffix': inifile.get('alternatives.%s.url_suffix' % alt),
                'primary': inifile.get_bool('alternatives.%s.primary' % alt),
                'locale': inifile.get('alternatives.%s.locale' % alt, 'en_US'),
            }

    for alt, alt_data in iteritems(config['ALTERNATIVES']):
        if alt_data['primary']:
            config['PRIMARY_ALTERNATIVE'] = alt
            break
    else:
        if config['ALTERNATIVES']:
            raise RuntimeError('Alternatives defined but no primary set.')
Exemple #41
0
def update_config_from_ini(config, inifile):
    def set_simple(target, source_path):
        rv = config.get(source_path)
        if rv is not None:
            config[target] = rv

    set_simple(target="IMAGEMAGICK_EXECUTABLE", source_path="env.imagemagick_executable")
    set_simple(target="LESSC_EXECUTABLE", source_path="env.lessc_executable")

    config["ATTACHMENT_TYPES"].update(
        (k.encode("ascii", "replace"), v.encode("ascii", "replace"))
        for k, v in inifile.section_as_dict("attachment_types")
    )

    config["PROJECT"].update(inifile.section_as_dict("project"))
    config["PACKAGES"].update(inifile.section_as_dict("packages"))

    for sect in inifile.sections():
        if sect.startswith("servers."):
            server_id = sect.split(".")[1]
            config["SERVERS"][server_id] = inifile.section_as_dict(sect)
        elif sect.startswith("alternatives."):
            alt = sect.split(".")[1]
            config["ALTERNATIVES"][alt] = {
                "name": get_i18n_block(inifile, "alternatives.%s.name" % alt),
                "url_prefix": inifile.get("alternatives.%s.url_prefix" % alt),
                "url_suffix": inifile.get("alternatives.%s.url_suffix" % alt),
                "primary": inifile.get_bool("alternatives.%s.primary" % alt),
                "locale": inifile.get("alternatives.%s.locale" % alt, "en_US"),
            }

    for alt, alt_data in iteritems(config["ALTERNATIVES"]):
        if alt_data["primary"]:
            config["PRIMARY_ALTERNATIVE"] = alt
            break
    else:
        if config["ALTERNATIVES"]:
            raise RuntimeError("Alternatives defined but no primary set.")
Exemple #42
0
def get_new_record_info():
    # XXX: convert to tree usage
    pad = g.admin_context.pad
    alt = request.args.get('alt') or PRIMARY_ALT
    ts = g.admin_context.tree.edit(request.args['path'], alt=alt)
    if ts.is_attachment:
        can_have_children = False
    elif ts.datamodel.child_config.replaced_with is not None:
        can_have_children = False
    else:
        can_have_children = True
    implied = ts.datamodel.child_config.model

    def describe_model(model):
        primary_field = None
        if model.primary_field is not None:
            f = model.field_map.get(model.primary_field)
            if f is not None:
                primary_field = f.to_json(pad)
        return {
            'id': model.id,
            'name': model.name,
            'name_i18n': model.name_i18n,
            'primary_field': primary_field
        }

    return jsonify({
        'label':
        ts.record and ts.record.record_label or ts.id,
        'can_have_children':
        can_have_children,
        'implied_model':
        implied,
        'available_models':
        dict((k, describe_model(v)) for k, v in iteritems(pad.db.datamodels)
             if not v.hidden or k == implied)
    })
Exemple #43
0
def get_new_record_info():
    # XXX: convert to tree usage
    pad = g.admin_context.pad
    alt = request.args.get("alt") or PRIMARY_ALT
    ts = g.admin_context.tree.edit(request.args["path"], alt=alt)
    if ts.is_attachment:
        can_have_children = False
    elif ts.datamodel.child_config.replaced_with is not None:
        can_have_children = False
    else:
        can_have_children = True
    implied = ts.datamodel.child_config.model

    def describe_model(model):
        primary_field = None
        if model.primary_field is not None:
            f = model.field_map.get(model.primary_field)
            if f is not None:
                primary_field = f.to_json(pad)
        return {
            "id": model.id,
            "name": model.name,
            "name_i18n": model.name_i18n,
            "primary_field": primary_field,
        }

    return jsonify({
        "label":
        ts.record and ts.record.record_label or ts.id,
        "can_have_children":
        can_have_children,
        "implied_model":
        implied,
        "available_models":
        dict((k, describe_model(v)) for k, v in iteritems(pad.db.datamodels)
             if not v.hidden or k == implied),
    })
Exemple #44
0
def test_exif(pad):
    image = pad.root.attachments.images.get("test.jpg")
    assert image is not None

    assert image.exif

    assert almost_equal(image.exif.altitude, 779.0293)
    assert almost_equal(image.exif.aperture, 2.275)
    assert image.exif.artist is None
    assert image.exif.camera == "Apple iPhone 6"
    assert image.exif.camera_make == "Apple"
    assert image.exif.camera_model == "iPhone 6"
    assert image.exif.copyright is None
    assert image.exif.created_at == datetime(2015, 12, 6, 11, 37, 38)
    assert image.exif.exposure_time == "1/33"
    assert image.exif.f == u"\u0192/2.2"
    assert almost_equal(image.exif.f_num, 2.2)
    assert image.exif.flash_info == "Flash did not fire, compulsory flash mode"
    assert image.exif.focal_length == "4.2mm"
    assert image.exif.focal_length_35mm == "29mm"
    assert image.exif.iso == 160
    assert almost_equal(image.exif.latitude, 46.6338333)
    assert image.exif.lens == "Apple iPhone 6 back camera 4.15mm f/2.2"
    assert image.exif.lens_make == "Apple"
    assert image.exif.lens_model == "iPhone 6 back camera 4.15mm f/2.2"
    assert almost_equal(image.exif.longitude, 13.4048333)
    assert image.exif.location == (image.exif.latitude, image.exif.longitude)
    assert image.exif.shutter_speed == "1/33"

    assert image.exif.documentname == "testName"
    assert image.exif.description == "testDescription"
    assert image.exif.is_rotated

    assert isinstance(image.exif.to_dict(), dict)

    for key, value in iteritems(image.exif.to_dict()):
        assert getattr(image.exif, key) == value
Exemple #45
0
def update_config_from_ini(config, inifile):
    def set_simple(target, source_path):
        rv = config.get(source_path)
        if rv is not None:
            config[target] = rv

    set_simple(target="IMAGEMAGICK_EXECUTABLE",
               source_path="env.imagemagick_executable")
    set_simple(target="LESSC_EXECUTABLE", source_path="env.lessc_executable")

    for section_name in ("ATTACHMENT_TYPES", "PROJECT", "PACKAGES",
                         "THEME_SETTINGS"):
        section_config = inifile.section_as_dict(section_name.lower())
        config[section_name].update(section_config)

    for sect in inifile.sections():
        if sect.startswith("servers."):
            server_id = sect.split(".")[1]
            config["SERVERS"][server_id] = inifile.section_as_dict(sect)
        elif sect.startswith("alternatives."):
            alt = sect.split(".")[1]
            config["ALTERNATIVES"][alt] = {
                "name": get_i18n_block(inifile, "alternatives.%s.name" % alt),
                "url_prefix": inifile.get("alternatives.%s.url_prefix" % alt),
                "url_suffix": inifile.get("alternatives.%s.url_suffix" % alt),
                "primary": inifile.get_bool("alternatives.%s.primary" % alt),
                "locale": inifile.get("alternatives.%s.locale" % alt, "en_US"),
            }

    for alt, alt_data in iteritems(config["ALTERNATIVES"]):
        if alt_data["primary"]:
            config["PRIMARY_ALTERNATIVE"] = alt
            break
    else:
        if config["ALTERNATIVES"]:
            raise RuntimeError("Alternatives defined but no primary set.")
Exemple #46
0
 def update(self, *args, **kwargs):
     for key, value in iteritems(dict(*args, **kwargs)):
         self[key] = value
Exemple #47
0
def _get_package_version_from_project(cfg, name):
    choices = (name.lower(), "lektor-" + name.lower())
    for pkg, version in iteritems(cfg.section_as_dict("packages")):
        if pkg.lower() in choices:
            return {"name": pkg, "version": version}
    return None
Exemple #48
0
 def to_dict(self):
     rv = {}
     for key, value in iteritems(self.__class__.__dict__):
         if key[:1] != '_' and isinstance(value, property):
             rv[key] = getattr(self, key)
     return rv
Exemple #49
0
 def get_alternative_url_prefixes(self):
     """Returns a list of alternative url prefixes by length."""
     items = [(v["url_prefix"].lstrip("/"), k) for k, v in iteritems(self.values["ALTERNATIVES"]) if v["url_prefix"]]
     items.sort(key=lambda x: -len(x[0]))
     return items
Exemple #50
0
 def to_dict(self):
     rv = {}
     for key, value in iteritems(self.__class__.__dict__):
         if key[:1] != '_' and isinstance(value, property):
             rv[key] = getattr(self, key)
     return rv
Exemple #51
0
 def update(self, *args, **kwargs):
     for key, value in iteritems(dict(*args, **kwargs)):
         self[key] = value
Exemple #52
0
def initialize_plugins(env):
    """Initializes the plugins for the environment."""
    plugins = load_plugins()
    for plugin_id, plugin_cls in iteritems(plugins):
        env.plugin_controller.instanciate_plugin(plugin_id, plugin_cls)
    env.plugin_controller.emit('setup-env')
Exemple #53
0
def initialize_plugins(env):
    """Initializes the plugins for the environment."""
    plugins = load_plugins()
    for plugin_id, plugin_cls in iteritems(plugins):
        env.plugin_controller.instanciate_plugin(plugin_id, plugin_cls)
    env.plugin_controller.emit('setup-env')
Exemple #54
0
def _get_package_version_from_project(cfg, name):
    choices = (name.lower(), "lektor-" + name.lower())
    for pkg, version in iteritems(cfg.section_as_dict("packages")):
        if pkg.lower() in choices:
            return {"name": pkg, "version": version}