Example #1
0
 def __init__(self, goto_dir=STORAGE_DIR, label_filename=LABEL_STORAGE_PATH,
     labels_section=LABELS_SECTION, stack_filename=STACK_STORAGE_PATH,
     stack_section=STACK_SECTION):
     """__init__"""
     self.goto_dir = goto_dir
     self.parser = UnicodeConfigParser()
     self.label_filename = label_filename
     self.labels_section = labels_section
     self.stack_filename = stack_filename
     self.stack_section = stack_section
Example #2
0
 def __init__(self, file_name=LABEL_STORAGE_PATH, labels_section=LABELS_SECTION):
     self.parser = UnicodeConfigParser()
     self.file_name = file_name
     self.labels_section = labels_section
Example #3
0
class Storage(object):
    def __init__(self, goto_dir=STORAGE_DIR, label_filename=LABEL_STORAGE_PATH,
        labels_section=LABELS_SECTION, stack_filename=STACK_STORAGE_PATH,
        stack_section=STACK_SECTION):
        """__init__"""
        self.goto_dir = goto_dir
        self.parser = UnicodeConfigParser()
        self.label_filename = label_filename
        self.labels_section = labels_section
        self.stack_filename = stack_filename
        self.stack_section = stack_section


    ########## FILE MANIPULATION ##########

    def _persist(self, filename):
        """Refreshs the file with the last changes."""
        if not os.path.exists(self.goto_dir):
            os.makedirs(self.goto_dir)
        with codecs.open(filename, 'w', encoding='utf-8') as f:
            self.parser.write(f)


    def _create(self, filename, section):
        """
        Creates the label file and put a default session where the labels will
        be stored.
        """
        self.parser.add_section(section)
        self._persist(filename)


    def open(self, filename):
        """Loads the parser with data from the label file."""
        with codecs.open(filename, 'r', encoding='utf-8') as f:
            self.parser.readfp(f)


    def open_or_create(self):
        """
        Tries to open label and stack files, if an error occurs, create the file.
        """
        # label file
        try:
            self.open(self.label_filename)
        except IOError:
            self._create(self.label_filename, self.labels_section)
            self.open(self.label_filename)

        # stack file
        # try:
        #     self.open(self.stack_filename)
        # except IOError:
        #     self._create(self.stack_filename, self.stack_section)
        #     self.open(self.stack_filename)


    ########## LABEL MANIPULATION ##########

    def get(self, label):
        """Returns the path of a label."""
        return self.parser.get(self.labels_section, label)


    def get_all(self):
        """Returns a dictionary with all labels and paths."""
        return {
            label:path for label, path
                in self.parser.items(self.labels_section)
        }


    def replace(self, label, path):
        """
        Replaces the path from a label. The label is created if it not exists.
        """
        if len(label) > LABEL_SIZE:
            raise LabelTooLongError()

        if not LABEL_RE.match(label):
            raise LabelInvalidFormatError()

        self.parser.set(self.labels_section, label, path)
        self._persist(self.label_filename)


    def add(self, label, path):
        """
        Adds a label-path entry in the label file. If the label exists, an
        Exception is raised.
        """
        if self.parser.has_option(self.labels_section, label):
            raise LabelAlreadyExistsError()

        self.replace(label, path)


    def remove(self, label):
        """Removes a label and it's path."""
        self.parser.remove_option(self.labels_section, label)
        self._persist(self.label_filename)


    ########## STACK MANIPULATION ##########

    def push(self, label):
        print 'PUSHED label', label

    def pop(self, ntimes=1):
        pass
Example #4
0
class Storage(object):
    def __init__(self, file_name=LABEL_STORAGE_PATH, labels_section=LABELS_SECTION):
        self.parser = UnicodeConfigParser()
        self.file_name = file_name
        self.labels_section = labels_section


    ########## FILE MANIPULATION ##########

    def _persist(self):
        """Refreshs the file with the last changes."""
        with codecs.open(self.file_name, 'w', encoding='utf-8') as f:
            self.parser.write(f)


    def _create(self):
        """
        Creates the label file and put a default session where the labels will
        be stored.
        """
        self.parser.add_section(self.labels_section)
        self._persist()


    def open(self):
        """Loads the parser with data from the label file."""
        with codecs.open(self.file_name, 'r', encoding='utf-8') as f:
            self.parser.readfp(f)


    def open_or_create(self):
        """
        Tries to open the label file, if an error occurs, create the file.
        """
        try:
            self.open()
        except IOError:
            self._create()
            self.open()


    ########## LABEL MANIPULATION ##########

    def get(self, label):
        """Returns the path of a label."""
        return self.parser.get(self.labels_section, label)


    def get_all(self):
        """Returns a dictionary with all labels and paths."""
        return {
            label:path for label, path
                in self.parser.items(self.labels_section)
        }


    def replace(self, label, path):
        """
        Replaces the path from a label. The label is created if it not exists.
        """
        if len(label) > LABEL_SIZE:
            raise LabelTooLongError()

        if not LABEL_RE.match(label):
            raise LabelInvalidFormatError()

        self.parser.set(self.labels_section, label, path)
        self._persist()


    def add(self, label, path):
        """
        Adds a label-path entry in the label file. If the label exists, an
        Exception is raised.
        """
        if self.parser.has_option(self.labels_section, label):
            raise LabelAlreadyExistsError()

        self.replace(label, path)


    def remove(self, label):
        """Removes a label and it's path."""
        self.parser.remove_option(self.labels_section, label)
        self._persist()