Beispiel #1
0
    def update_links(self, source, target):
        Folder.update_links(self, source, target)
        TagsAware.update_links(self, source, target)

        base = self.get_canonical_path()
        resources_new2old = get_context().database.resources_new2old
        base = str(base)
        old_base = resources_new2old.get(base, base)
        old_base = Path(old_base)
        new_base = Path(base)

        # metadata
        schema = self.class_schema
        for key, datatype in schema.iteritems():
            if issubclass(datatype, PathDataType) is False:
                continue
            value = self.get_property(key)
            if not value:
                continue
            ref = get_reference(value)
            if ref.scheme:
                continue
            path, view = get_path_and_view(ref.path)
            path = str(old_base.resolve2(path))
            if path == source:
                # Hit the old name
                # Build the new path with the right path
                new_path = str(new_base.get_pathto(target)) + view
                self.set_property(key, Path(new_path))

        # comments
        comments = self.metadata.get_property('comment') or []
        for comment in comments:
            # XXX hardcoded, not typed
            for key in ('attachment',):
                value = comment.get_parameter(key)
                if not value:
                    continue
                ref = get_reference(value)
                if ref.scheme:
                    continue
                path, view = get_path_and_view(ref.path)
                path = str(old_base.resolve2(path))
                if path == source:
                    # Hit the old name
                    # Build the new path with the right path
                    new_path = str(new_base.get_pathto(target)) + view
                    comment.set_parameter(key, new_path)

        self.set_property('comment', comments)
Beispiel #2
0
    def update_links(self, source, target):
        # FIXME BaseTheme does not take into account 'child'
        BaseTheme.update_links(self, source, target)
        base = self.get_canonical_path()
        resources_new2old = get_context().database.resources_new2old
        base = str(base)
        old_base = resources_new2old.get(base, base)
        old_base = Path(old_base)
        new_base = Path(base)

        # banner_path
        site_root = self.get_site_root()
        available_languages = site_root.get_property("website_languages")

        for language in available_languages:
            value = self.get_property("banner_path", language=language)
            if not value:
                continue
            path = old_base.resolve2(value)
            if path == source:
                # Hit the old name
                # Build the new reference with the right path
                self.set_property("banner_path", new_base.get_pathto(target), language=language)

        get_context().database.change_resource(self)
Beispiel #3
0
    def update_links(self, source, target):
        base = self.get_canonical_path()
        resources_new2old = get_context().database.resources_new2old
        base = str(base)
        old_base = resources_new2old.get(base, base)
        old_base = Path(old_base)
        new_base = Path(base)

        handler = self.handler
        get_value = handler.get_record_value

        for record in handler.get_records_in_order():
            path = get_value(record, 'name')
            if not path:
                continue
            ref = get_reference(path)
            if ref.scheme:
                continue
            # Strip the view
            path = ref.path
            name = path.get_name()
            if name and name[0] == ';':
                view = '/' + name
                path = path[:-1]
            else:
                view = ''
            path = str(old_base.resolve2(path))
            if path == source:
                # Hit the old name
                # Build the new reference with the right path
                new_ref = deepcopy(ref)
                new_ref.path = str(new_base.get_pathto(target)) + view
                handler.update_record(record.id, **{'name': str(new_ref)})

        get_context().server.change_resource(self)
Beispiel #4
0
    def update_links(self, source, target):
        base = self.get_canonical_path()
        resources_new2old = get_context().database.resources_new2old
        base = str(base)
        old_base = resources_new2old.get(base, base)
        old_base = Path(old_base)
        new_base = Path(base)

        handler = self.handler
        get_value = handler.get_record_value

        for record in handler.get_records_in_order():
            path = get_value(record, 'name')
            if not path:
                continue
            ref = get_reference(path)
            if ref.scheme:
                continue
            # Strip the view
            path = ref.path
            name = path.get_name()
            if name and name[0] == ';':
                view = '/' + name
                path = path[:-1]
            else:
                view = ''
            path = str(old_base.resolve2(path))
            if path == source:
                # Hit the old name
                # Build the new reference with the right path
                new_ref = deepcopy(ref)
                new_ref.path = str(new_base.get_pathto(target)) + view
                handler.update_record(record.id, **{'name': str(new_ref)})

        get_context().server.change_resource(self)
Beispiel #5
0
    def update_links(self, source, target):
        WebPage.update_links(self, source, target)

        site_root = self.get_site_root()
        available_languages = site_root.get_property('website_languages')

        base = self.get_canonical_path()
        resources_new2old = get_context().database.resources_new2old
        base = str(base)
        old_base = resources_new2old.get(base, base)
        old_base = Path(old_base)
        new_base = Path(base)

        for lang in available_languages:
            path = self.get_property('thumbnail', language=lang)
            if not path:
                continue
            ref = get_reference(path)
            if ref.scheme:
                continue
            path, view = get_path_and_view(ref.path)
            path = str(old_base.resolve2(path))
            if path == source:
                # Hit the old name
                # Build the new reference with the right path
                new_ref = deepcopy(ref)
                new_ref.path = str(new_base.get_pathto(target)) + view
                self.set_property('thumbnail', str(new_ref), language=lang)

        get_context().database.change_resource(self)
Beispiel #6
0
    def update_links(self, source, target):
        base = self.get_canonical_path()
        resources_new2old = get_context().database.resources_new2old
        base = str(base)
        old_base = resources_new2old.get(base, base)
        old_base = Path(old_base)
        new_base = Path(base)

        site_root = self.get_site_root()
        languages = site_root.get_property('website_languages')
        links = []
        for key, datatype in self.get_metadata_schema().items():
            multilingual = getattr(datatype, 'multilingual', False)
            langs = languages if multilingual is True else [None]
            if issubclass(datatype, XHTMLBody):
                for lang in langs:
                    events = self.get_property(key, language=lang)
                    if not events:
                        continue
                    events = _change_link(source, target, old_base, new_base,
                                          events)
                    events = list(events)
                    self.set_property(key, events, language=lang)
            elif issubclass(datatype, PathDataType):
                # Relative path
                for lang in langs:
                    path = self.get_property(key, language=lang)
                    if path is None:
                        continue
                    path = str(old_base.resolve2(path))
                    if path == source:
                        # Hit the old name
                        new_path = str(new_base.get_pathto(target))
                        self.set_property(key, new_path, language=lang)
            elif issubclass(datatype, AbsolutePathDataTypeEnumerate):
                # Absolute path
                for lang in langs:
                    path = self.get_property(key, language=lang)
                    if path is None:
                        continue
                    path = str(path)
                    path = resources_new2old.get(path, path)
                    if path == source:
                        # Hit the old name
                        self.set_property(key, str(target), language=lang)
        # Tagaware ?
        if isinstance(self, TagsAware):
            TagsAware.update_links(self, source, target)

        # Change resource
        get_context().database.change_resource(self)
Beispiel #7
0
    def update_links(self, source, target):
        super(BoxFeed, self).update_links(source, target)

        container_path = self.get_property('container_path')
        if container_path:
            if container_path == '/':
                # Even if site_root is renammed, '/' is '/'
                pass
            else:
                resources_new2old = get_context().database.resources_new2old
                site_root_abspath = self.get_site_root().abspath
                base = str(site_root_abspath)
                old_base = resources_new2old.get(base, base)
                old_base = Path(old_base)
                # Path is relative to site_root
                path = old_base.resolve2(container_path)

                if path == source:
                    # Hit the old name
                    new_path = site_root_abspath.get_pathto(target)
                    self.set_property('container_path', new_path)
Beispiel #8
0
    def update_links(self, source, target):
        super(DiaporamaTable, self).update_links(source, target)

        # Caution multilingual property
        base = self.get_canonical_path()
        resources_new2old = get_context().database.resources_new2old
        base = str(base)
        old_base = resources_new2old.get(base, base)
        old_base = Path(old_base)
        new_base = Path(base)

        site_root = self.get_site_root()
        available_languages = site_root.get_property('website_languages')
        handler = self.handler
        record_properties = handler.record_properties

        # TODO To improve
        get_value = handler.get_record_value
        for record in handler.get_records():
            for lang in available_languages:
                for key in ('img_path', 'img_link'):
                    path = get_value(record, key, lang)
                    if not path:
                        continue
                    ref = get_reference(path)
                    if ref.scheme:
                        continue
                    path, view = get_path_and_view(ref.path)
                    path = str(old_base.resolve2(path))
                    if path == source:
                        # Hit the old name
                        # Build the new reference with the right path
                        new_ref = deepcopy(ref)
                        new_ref.path = str(new_base.get_pathto(target)) + view
                        datatype = record_properties.get(key, String)
                        new_path = Property(datatype.decode(str(new_ref)),
                                            language=lang)
                        handler.update_record(record.id, **{key: new_path})

        get_context().database.change_resource(self)
Beispiel #9
0
    def update_links(self, source, target):
        source = Path(source)
        site_root = self.get_site_root()
        available_languages = site_root.get_property('website_languages')

        # Tags
        tags_base = site_root.get_abspath().resolve2('tags')
        if tags_base.get_prefix(source) == tags_base:
            tags = list(self.get_property('tags'))
            source_name = source.get_name()
            target_name = Path(target).get_name()
            for tag in tags:
                if tag == source_name:
                    # Hit
                    index = tags.index(source_name)
                    tags[index] = target_name
                    self.set_property('tags', tags)

        # Thumbnail
        base = self.get_canonical_path()
        resources_new2old = get_context().database.resources_new2old
        base = str(base)
        old_base = resources_new2old.get(base, base)
        old_base = Path(old_base)
        new_base = Path(base)

        for lang in available_languages:
            path = self.get_property('thumbnail', lang)
            if not path:
                continue
            ref = get_reference(path)
            if ref.scheme:
                continue
            path = old_base.resolve2(path)
            if path == source:
                # Hit
                self.set_property('thumbnail', new_base.get_pathto(target),
                                  lang)

        get_context().database.change_resource(self)
Beispiel #10
0
    def update_links(self, source, target):
        WebPage.update_links(self, source, target)

        base = self.get_canonical_path()
        resources_new2old = get_context().database.resources_new2old
        base = str(base)
        old_base = resources_new2old.get(base, base)
        old_base = Path(old_base)
        new_base = Path(base)

        path = self.get_property('title_link')
        if path:
            ref = get_reference(path)
            if not ref.scheme:
                path, view = get_path_and_view(ref.path)
                path = str(old_base.resolve2(path))
                if path == source:
                    # Hit the old name
                    # Build the new reference with the right path
                    new_ref = deepcopy(ref)
                    new_ref.path = str(new_base.get_pathto(target)) + view
                    self.set_property('title_link', str(new_ref))

        get_context().database.change_resource(self)
Beispiel #11
0
class LocalFolder(object):
    def __init__(self, path="."):
        if not exists(path):
            raise IOError, "No such directory: '%s'" % path
        if isfile(path):
            raise IOError, "Is a directory: '%s'" % path
        self.path = Path(abspath(path))

    def _resolve_path(self, path):
        path = self.path.resolve2(path)
        return str(path)

    #######################################################################
    # Public API
    #######################################################################
    def exists(self, path):
        path = self._resolve_path(path)
        return exists(path)

    def is_file(self, path):
        path = self._resolve_path(path)
        return isfile(path)

    def is_folder(self, path):
        path = self._resolve_path(path)
        return isdir(path)

    def can_read(self, path):
        path = self._resolve_path(path)
        return access(path, R_OK)

    def can_write(self, path):
        path = self._resolve_path(path)
        return access(path, W_OK)

    def make_file(self, path):
        path = self._resolve_path(path)
        parent_path = dirname(path)
        if exists(parent_path):
            if exists(path):
                raise OSError, "File exists: '%s'" % path
        else:
            makedirs(parent_path)
        return file(path, "wb")

    def make_folder(self, path):
        path = self._resolve_path(path)
        return makedirs(path)

    def get_ctime(self, path):
        path = self._resolve_path(path)
        ctime = getctime(path)
        return datetime.fromtimestamp(ctime)

    def get_mtime(self, path):
        path = self._resolve_path(path)
        mtime = getmtime(path)
        return datetime.fromtimestamp(mtime)

    def get_atime(self, path):
        path = self._resolve_path(path)
        atime = getatime(path)
        return datetime.fromtimestamp(atime)

    def get_mimetype(self, path):
        path = self._resolve_path(path)
        # Not a file ?
        if not isfile(path):
            return "application/x-not-regular-file"
        name = basename(path)
        return get_mimetype(name)

    def get_size(self, path):
        path = self._resolve_path(path)
        return getsize(path)

    def open(self, path, mode=None):
        path = self._resolve_path(path)
        if isdir(path):
            return self.__class__(path)
        mode = MODES.get(mode, "rb")
        return file(path, mode)

    def remove(self, path):
        path = self._resolve_path(path)
        if isdir(path):
            # Remove folder contents
            rmtree(path)
        else:
            os_remove(path)

    def copy(self, source, target):
        source = self._resolve_path(source)
        target = self._resolve_path(target)
        if isdir(source):
            # Copy inside target
            if exists(target):
                target = join(target, basename(source))
            copytree(source, target)
        else:
            # Will overwrite target file
            shutil_copy(source, target)

    def move(self, source, target):
        source = self._resolve_path(source)
        target = self._resolve_path(target)
        return renames(source, target)

    def get_names(self, path="."):
        path = self._resolve_path(path)
        try:
            return listdir(path)
        except OSError, e:
            # Path does not exist or is not a directory
            if e.errno == 2 or e.errno == 20:
                return []
            raise
Beispiel #12
0
 def resolve2(base, path):
     if type(base) is not Path:
         base = Path(base)
     path = base.resolve2(path)
     return str(path)
Beispiel #13
0
    def get_namespace(self, resource, context):
        root = context.root
        here = context.resource
        site_root = here.get_site_root()
        site_root_abspath = site_root.get_abspath()
        shop = site_root.get_resource('shop')
        categories_abspath = str(site_root_abspath.resolve2('categories'))
        show_nb_products = resource.get_property('show_nb_products')
        show_first_category = resource.get_property('show_first_category')
        show_second_level = resource.get_property('show_second_level')
        here_abspath = here.get_abspath()
        here_real_abspath = str(here_abspath)
        here_parent_abspath = here_abspath.resolve2('..')
        current_level = here_real_abspath.count('/')

        if here.metadata.format == 'category':
            here_abspath = str(here.get_abspath())
            # Max level deploy = count '/' + 1
            # here_abspath at level 1 does not contain '/'
            here_abspath_level = here_abspath.count('/')
            max_level_deploy = here_abspath_level + 1
        else:
            if here.metadata.format == 'product':
                # Special case for the product, here_* values are compute
                # with here = parent category
                parent_category = here.parent
                parent_category_abspath = parent_category.get_abspath()
                here_abspath = str(parent_category_abspath)
                here_abspath_level = here_abspath.count('/')
                max_level_deploy = here_abspath_level + 1
                here_parent_abspath = parent_category_abspath.resolve2('..')
            else:
                # Tweak here_abspath
                here_abspath = categories_abspath
                here_abspath_level = here_abspath.count('/')
                max_level_deploy = categories_abspath.count('/') + 1

        here_abspath_p = Path(here_abspath)

        # Get search with all publics products
        all_products = root.search(
            AndQuery(PhraseQuery('format', shop.product_class.class_id),
                     PhraseQuery('workflow_state', 'public')))
        # Get search with all categories
        all_categories = root.search(
            AndQuery(PhraseQuery('parent_paths', categories_abspath),
                     PhraseQuery('format', 'category')))

        # Build a dict with brains by level
        cat_per_level = {}
        for index, cat in enumerate(
                all_categories.get_documents(sort_by='abspath')):
            # Skip first category --> /categories
            if index == 0 and show_first_category is False:
                continue

            level = cat.abspath.count('/')

            # Skip second level (if we are not on level /categories/')
            if (show_second_level is False and current_level > 2 and level == 3
                    and not here_real_abspath == cat.abspath
                    and not here_parent_abspath == cat.abspath):
                continue

            # Skip bad level
            if level > max_level_deploy:
                continue

            diff_level = here_abspath_level - level
            path_to_resolve = ['..' for x in range(diff_level)]
            path_to_resolve = '/'.join(path_to_resolve)
            # Path uses to compute the prefix with the current category
            here_prefix = here_abspath_p.resolve2(path_to_resolve)
            # Compute the prefix
            prefix = here_prefix.get_prefix(cat.abspath)

            if prefix == here_prefix:
                # special case when prefix equals here_prefix
                pass
            elif len(prefix) != level - 1:
                # bad, not in the same arborescence
                continue

            # Get the product number in the category
            sub_results = all_products.search(
                PhraseQuery('parent_paths', cat.abspath))
            cats = cat_per_level.setdefault(level, [])
            cats.append({'doc': cat, 'nb_products': len(sub_results)})

        # Build the tree starting with the higher level
        tree_template = resource.get_resource(self.tree_template)
        levels = sorted(cat_per_level.keys(), reverse=True)
        tree = None
        for level in levels:
            items = []
            for data in cat_per_level[level]:
                doc = data['doc']
                if here_abspath.startswith(doc.abspath):
                    sub_tree = tree
                    css = 'in-path '
                else:
                    sub_tree = None
                    css = ''
                css = 'active ' if here_abspath == doc.abspath else css
                # Href (get_link emulation)
                href = str(site_root_abspath.get_pathto(doc.abspath))
                css += checkid(href)
                css = css.replace('.', '-dot-')
                if resource.get_property('use_small_title'):
                    title = doc.m_breadcrumb_title or doc.m_title or doc.name
                else:
                    title = doc.m_title or doc.name
                d = {
                    'title': title,
                    'href': '/%s' % href,
                    'sub_tree': sub_tree,
                    'nb_products': data['nb_products'],
                    'css': css
                }
                items.append(d)
            tree = stl(tree_template, {
                'items': items,
                'show_nb_products': show_nb_products,
                'css': None
            })

        return {'title': resource.get_title(), 'tree': tree}
Beispiel #14
0
 def resolve2(base, path):
     if type(base) is not Path:
         base = Path(base)
     path = base.resolve2(path)
     return str(path)
Beispiel #15
0
    def get_namespace(self, resource, context):
        root = context.root
        here = context.resource
        site_root = here.get_site_root()
        site_root_abspath = site_root.get_abspath()
        shop = site_root.get_resource('shop')
        categories_abspath = str(site_root_abspath.resolve2('categories'))
        show_nb_products = resource.get_property('show_nb_products')
        show_first_category = resource.get_property('show_first_category')
        show_second_level = resource.get_property('show_second_level')
        here_abspath = here.get_abspath()
        here_real_abspath = str(here_abspath)
        here_parent_abspath = here_abspath.resolve2('..')
        current_level = here_real_abspath.count('/')

        if here.metadata.format == 'category':
            here_abspath = str(here.get_abspath())
            # Max level deploy = count '/' + 1
            # here_abspath at level 1 does not contain '/'
            here_abspath_level = here_abspath.count('/')
            max_level_deploy = here_abspath_level + 1
        else:
            if here.metadata.format == 'product':
                # Special case for the product, here_* values are compute
                # with here = parent category
                parent_category = here.parent
                parent_category_abspath = parent_category.get_abspath()
                here_abspath = str(parent_category_abspath)
                here_abspath_level = here_abspath.count('/')
                max_level_deploy = here_abspath_level + 1
                here_parent_abspath = parent_category_abspath.resolve2('..')
            else:
                # Tweak here_abspath
                here_abspath = categories_abspath
                here_abspath_level = here_abspath.count('/')
                max_level_deploy = categories_abspath.count('/') + 1

        here_abspath_p = Path(here_abspath)

        # Get search with all publics products
        all_products = root.search(AndQuery(
                          PhraseQuery('format', shop.product_class.class_id),
                          PhraseQuery('workflow_state', 'public')))
        # Get search with all categories
        all_categories = root.search(AndQuery(
                            PhraseQuery('parent_paths', categories_abspath),
                            PhraseQuery('format', 'category')))

        # Build a dict with brains by level
        cat_per_level = {}
        for index, cat in enumerate(all_categories.get_documents(
                                        sort_by='abspath')):
            # Skip first category --> /categories
            if index == 0 and show_first_category is False:
                continue

            level = cat.abspath.count('/')

            # Skip second level (if we are not on level /categories/')
            if (show_second_level is False and current_level > 2 and
                level == 3 and
                not here_real_abspath == cat.abspath and
                not here_parent_abspath == cat.abspath):
                continue

            # Skip bad level
            if level > max_level_deploy:
                continue

            diff_level = here_abspath_level - level
            path_to_resolve = [ '..' for x in range(diff_level) ]
            path_to_resolve = '/'.join(path_to_resolve)
            # Path uses to compute the prefix with the current category
            here_prefix = here_abspath_p.resolve2(path_to_resolve)
            # Compute the prefix
            prefix = here_prefix.get_prefix(cat.abspath)

            if prefix == here_prefix:
                # special case when prefix equals here_prefix
                pass
            elif len(prefix) != level-1:
                # bad, not in the same arborescence
                continue

            # Get the product number in the category
            sub_results = all_products.search(PhraseQuery('parent_paths',
                                                          cat.abspath))
            cats = cat_per_level.setdefault(level, [])
            cats.append({'doc': cat, 'nb_products': len(sub_results)})

        # Build the tree starting with the higher level
        tree_template = resource.get_resource(self.tree_template)
        levels = sorted(cat_per_level.keys(), reverse=True)
        tree = None
        for level in levels:
            items = []
            for data in cat_per_level[level]:
                doc = data['doc']
                if here_abspath.startswith(doc.abspath):
                    sub_tree = tree
                    css = 'in-path '
                else:
                    sub_tree = None
                    css = ''
                css = 'active ' if here_abspath == doc.abspath else css
                # Href (get_link emulation)
                href = str(site_root_abspath.get_pathto(doc.abspath))
                css += checkid(href)
                css = css.replace('.', '-dot-')
                if resource.get_property('use_small_title'):
                    title = doc.m_breadcrumb_title or doc.m_title or doc.name
                else:
                    title = doc.m_title or doc.name
                d = {'title': title,
                     'href': '/%s' % href,
                     'sub_tree': sub_tree,
                     'nb_products': data['nb_products'],
                     'css': css}
                items.append(d)
            tree = stl(tree_template, {'items': items,
                                       'show_nb_products': show_nb_products,
                                       'css': None})

        return {'title': resource.get_title(),
                'tree': tree}
Beispiel #16
0
class LocalFolder(object):

    def __init__(self, path='.'):
        if not exists(path):
            raise IOError, "No such directory: '%s'" % path
        if isfile(path):
            raise IOError, "Is a directory: '%s'" % path
        self.path = Path(abspath(path))


    def _resolve_path(self, path):
        path = self.path.resolve2(path)
        return str(path)


    #######################################################################
    # Public API
    #######################################################################
    def exists(self, path):
        path = self._resolve_path(path)
        return exists(path)


    def is_file(self, path):
        path = self._resolve_path(path)
        return isfile(path)


    def is_folder(self, path):
        path = self._resolve_path(path)
        return isdir(path)


    def can_read(self, path):
        path = self._resolve_path(path)
        return access(path, R_OK)


    def can_write(self, path):
        path = self._resolve_path(path)
        return access(path, W_OK)


    def make_file(self, path):
        path = self._resolve_path(path)
        parent_path = dirname(path)
        if exists(parent_path):
            if exists(path):
                raise OSError, "File exists: '%s'" % path
        else:
            makedirs(parent_path)
        return file(path, 'wb')


    def make_folder(self, path):
        path = self._resolve_path(path)
        return makedirs(path)


    def get_ctime(self, path):
        path = self._resolve_path(path)
        ctime = getctime(path)
        return datetime.fromtimestamp(ctime)


    def get_mtime(self, path):
        path = self._resolve_path(path)
        mtime = getmtime(path)
        return datetime.fromtimestamp(mtime)


    def get_atime(self, path):
        path = self._resolve_path(path)
        atime = getatime(path)
        return datetime.fromtimestamp(atime)


    def get_mimetype(self, path):
        path = self._resolve_path(path)
        # Not a file ?
        if not isfile(path):
            return 'application/x-not-regular-file'
        name = basename(path)
        return get_mimetype(name)


    def get_size(self, path):
        path = self._resolve_path(path)
        return getsize(path)


    def open(self, path, mode=None):
        path = self._resolve_path(path)
        if isdir(path):
            return self.__class__(path)
        mode = MODES.get(mode, 'rb')
        return file(path, mode)


    def remove(self, path):
        path = self._resolve_path(path)
        if isdir(path):
            # Remove folder contents
            rmtree(path)
        else:
            os_remove(path)


    def copy(self, source, target):
        source = self._resolve_path(source)
        target = self._resolve_path(target)
        if isdir(source):
            # Copy inside target
            if exists(target):
                target = join(target, basename(source))
            copytree(source, target)
        else:
            # Will overwrite target file
            shutil_copy(source, target)


    def move(self, source, target):
        source = self._resolve_path(source)
        target = self._resolve_path(target)
        # If target is a folder, move inside it
        return shutil_move(source, target)


    def get_names(self, path='.'):
        path = self._resolve_path(path)
        return listdir(path)


    def traverse(self, path='.'):
        path = self._resolve_path(path)
        yield path
        if isdir(path):
            for root, folders, files in walk(path, topdown=True):
                for name in folders:
                    yield join(root, name)
                for name in files:
                    yield join(root, name)


    def get_absolute_path(self, path='.'):
        path = self._resolve_path(path)
        return abspath(path)


    def get_relative_path(self, path):
        path = self._resolve_path(path)
        return relpath(path)


    #######################################################################
    # Used by itools.handlers
    #######################################################################
    @staticmethod
    def get_basename(path):
        if type(path) is not Path:
            path = Path(path)
        return path.get_name()


    @staticmethod
    def get_path(path):
        if type(path) is not Path:
            path = Path(path)
        return str(path)


    @staticmethod
    def resolve(base, path):
        if type(base) is not Path:
            base = Path(base)
        path = base.resolve(path)
        return str(path)


    @staticmethod
    def resolve2(base, path):
        if type(base) is not Path:
            base = Path(base)
        path = base.resolve2(path)
        return str(path)


    # Resolution method for handler database keys
    resolve_key = get_absolute_path
Beispiel #17
0
class LocalFolder(object):
    def __init__(self, path='.'):
        if not exists(path):
            raise IOError, "No such directory: '%s'" % path
        if isfile(path):
            raise IOError, "Is a directory: '%s'" % path
        self.path = Path(abspath(path))

    def _resolve_path(self, path):
        path = self.path.resolve2(path)
        return str(path)

    #######################################################################
    # Public API
    #######################################################################
    def exists(self, path):
        path = self._resolve_path(path)
        return exists(path)

    def is_file(self, path):
        path = self._resolve_path(path)
        return isfile(path)

    def is_folder(self, path):
        path = self._resolve_path(path)
        return isdir(path)

    def can_read(self, path):
        path = self._resolve_path(path)
        return access(path, R_OK)

    def can_write(self, path):
        path = self._resolve_path(path)
        return access(path, W_OK)

    def make_file(self, path):
        path = self._resolve_path(path)
        parent_path = dirname(path)
        if exists(parent_path):
            if exists(path):
                raise OSError, "File exists: '%s'" % path
        else:
            makedirs(parent_path)
        return file(path, 'wb')

    def make_folder(self, path):
        path = self._resolve_path(path)
        return makedirs(path)

    def get_ctime(self, path):
        path = self._resolve_path(path)
        ctime = getctime(path)
        return datetime.fromtimestamp(ctime)

    def get_mtime(self, path):
        path = self._resolve_path(path)
        mtime = getmtime(path)
        return datetime.fromtimestamp(mtime)

    def get_atime(self, path):
        path = self._resolve_path(path)
        atime = getatime(path)
        return datetime.fromtimestamp(atime)

    def get_mimetype(self, path):
        path = self._resolve_path(path)
        # Not a file ?
        if not isfile(path):
            return 'application/x-not-regular-file'
        name = basename(path)
        return get_mimetype(name)

    def get_size(self, path):
        path = self._resolve_path(path)
        return getsize(path)

    def open(self, path, mode=None):
        path = self._resolve_path(path)
        if isdir(path):
            return self.__class__(path)
        mode = MODES.get(mode, 'rb')
        return file(path, mode)

    def remove(self, path):
        path = self._resolve_path(path)
        if isdir(path):
            # Remove folder contents
            rmtree(path)
        else:
            os_remove(path)

    def copy(self, source, target):
        source = self._resolve_path(source)
        target = self._resolve_path(target)
        if isdir(source):
            # Copy inside target
            if exists(target):
                target = join(target, basename(source))
            copytree(source, target)
        else:
            # Will overwrite target file
            shutil_copy(source, target)

    def move(self, source, target):
        source = self._resolve_path(source)
        target = self._resolve_path(target)
        return renames(source, target)

    def get_names(self, path='.'):
        path = self._resolve_path(path)
        try:
            return listdir(path)
        except OSError, e:
            # Path does not exist or is not a directory
            if e.errno == 2 or e.errno == 20:
                return []
            raise
Beispiel #18
0
class LocalFolder(object):
    def __init__(self, path='.'):
        if not exists(path):
            raise IOError, "No such directory: '%s'" % path
        if isfile(path):
            raise IOError, "Is a directory: '%s'" % path
        self.path = Path(abspath(path))

    def _resolve_path(self, path):
        path = self.path.resolve2(path)
        return str(path)

    #######################################################################
    # Public API
    #######################################################################
    def exists(self, path):
        path = self._resolve_path(path)
        return exists(path)

    def is_file(self, path):
        path = self._resolve_path(path)
        return isfile(path)

    def is_folder(self, path):
        path = self._resolve_path(path)
        return isdir(path)

    def can_read(self, path):
        path = self._resolve_path(path)
        return access(path, R_OK)

    def can_write(self, path):
        path = self._resolve_path(path)
        return access(path, W_OK)

    def make_file(self, path):
        path = self._resolve_path(path)
        parent_path = dirname(path)
        if exists(parent_path):
            if exists(path):
                raise OSError, "File exists: '%s'" % path
        else:
            makedirs(parent_path)
        return file(path, 'wb')

    def make_folder(self, path):
        path = self._resolve_path(path)
        return makedirs(path)

    def get_ctime(self, path):
        path = self._resolve_path(path)
        ctime = getctime(path)
        return datetime.fromtimestamp(ctime)

    def get_mtime(self, path):
        path = self._resolve_path(path)
        mtime = getmtime(path)
        return datetime.fromtimestamp(mtime)

    def get_atime(self, path):
        path = self._resolve_path(path)
        atime = getatime(path)
        return datetime.fromtimestamp(atime)

    def get_mimetype(self, path):
        path = self._resolve_path(path)
        # Not a file ?
        if not isfile(path):
            return 'application/x-not-regular-file'
        name = basename(path)
        return get_mimetype(name)

    def get_size(self, path):
        path = self._resolve_path(path)
        return getsize(path)

    def open(self, path, mode=None):
        path = self._resolve_path(path)
        if isdir(path):
            return self.__class__(path)
        mode = MODES.get(mode, 'rb')
        return file(path, mode)

    def remove(self, path):
        path = self._resolve_path(path)
        if isdir(path):
            # Remove folder contents
            rmtree(path)
        else:
            os_remove(path)

    def copy(self, source, target):
        source = self._resolve_path(source)
        target = self._resolve_path(target)
        if isdir(source):
            # Copy inside target
            if exists(target):
                target = join(target, basename(source))
            copytree(source, target)
        else:
            # Will overwrite target file
            shutil_copy(source, target)

    def move(self, source, target):
        source = self._resolve_path(source)
        target = self._resolve_path(target)
        # If target is a folder, move inside it
        return shutil_move(source, target)

    def get_names(self, path='.'):
        path = self._resolve_path(path)
        return listdir(path)

    def traverse(self, path='.'):
        path = self._resolve_path(path)
        yield path
        if isdir(path):
            for root, folders, files in walk(path, topdown=True):
                for name in folders:
                    yield join(root, name)
                for name in files:
                    yield join(root, name)

    def get_absolute_path(self, path='.'):
        path = self._resolve_path(path)
        return abspath(path)

    def get_relative_path(self, path):
        path = self._resolve_path(path)
        return relpath(path)

    #######################################################################
    # Used by itools.handlers
    #######################################################################
    @staticmethod
    def get_basename(path):
        if type(path) is not Path:
            path = Path(path)
        return path.get_name()

    @staticmethod
    def get_path(path):
        if type(path) is not Path:
            path = Path(path)
        return str(path)

    @staticmethod
    def resolve(base, path):
        if type(base) is not Path:
            base = Path(base)
        path = base.resolve(path)
        return str(path)

    @staticmethod
    def resolve2(base, path):
        if type(base) is not Path:
            base = Path(base)
        path = base.resolve2(path)
        return str(path)

    # Resolution method for handler database keys
    resolve_key = get_absolute_path