512
  X   ˆl–Ü4ý( ©”u( 	•íÀà™S�ÿ�þDä¤Y¹DGÃ Â�Ï¿ƒ}÷�Ä_ŠI|¥�+Wé3ž¯a–Ãa¾”Ò~êq@3p/\”Å£Ã &ü    # Copyright (C) 2008 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

__docformat__ = "google"

"""View management.

Views are contained within a working tree and normally constructed
when first accessed.  Clients should do, for example, ...

  tree.views.lookup_view()
"""

import re

from . import (
    errors,
    osutils,
    transport,
    )


_VIEWS_FORMAT_MARKER_RE = re.compile(b'Bazaar views format (\\d+)')
_VIEWS_FORMAT1_MARKER = b"Bazaar views format 1\n"


class NoSuchView(errors.BzrError):
    """A view does not exist.
    """

    _fmt = "No such view: %(view_name)s."

    def __init__(self, view_name):
        self.view_name = view_name


class ViewsNotSupported(errors.BzrError):
    """Views are not supported by a tree format.
    """

    _fmt = ("Views are not supported by %(tree)s;"
            " use 'brz upgrade' to change your tree to a later format.")

    def __init__(self, tree):
        self.tree = tree


class FileOutsideView(errors.BzrError):

    _fmt = ('Specified file "%(file_name)s" is outside the current view: '
            '%(view_str)s')

    def __init__(self, file_name, view_files):
        self.file_name = file_name
        self.view_str = ", ".join(view_files)


class _Views:
    """Base class for View managers."""

    def supports_views(self):
        raise NotImplementedError(self.supports_views)


class PathBasedViews(_Views):
    """View storage in an unversioned tree control file.

    Views are stored in terms of paths relative to the tree root.

    The top line of the control file is a format marker in the format:

      Bazaar views format X

    where X is an integer number. After this top line, version 1 format is
    stored as follows:

     * optional name-values pairs in the format 'name=value'

     * optional view definitions, one per line in the format

       views:
       name file1 file2 ...
       name file1 file2 ...

    where the fields are separated by a nul character (\0). The views file
    is encoded in utf-8. The only supported keyword in version 1 is
    'current' which stores the name of the current view, if any.
    """

    def __init__(self, tree):
        self.tree = tree
        self._loaded = False
        self._current = None
        self._views = {}

    def supports_views(self):
        return True

    def get_view_info(self):
        """Get the current view and dictionary of views.

        Returns: current, views where
          current = the name of the current view or None if no view is enabled
          views = a map from view name to list of files/directories
        """
        self._load_view_info()
        return self._current, self._views

    def set_view_info(self, current, views):
        """Set the current view and dictionary of views.

        Args:
          current: the name of the current view or None if no view is
              enabled
          views: a map from view name to list of files/directories
        """
        if current is not None and current not in views:
            raise NoSuchView(current)
        with self.tree.lock_write():
            self._current = current
            self._views = views
            self._save_view_info()

    def lookup_view(self, view_name=None):
        """Return the contents of a view.

        Args:
          view_Na