Beispiel #1
0
	def find_section_edit(self, section_type, replacement, verbose = True):
		'''
		Call editor's edit method for specific section type
		@param section_type: section type to be used when calling edit
		@type section_type: __class__
		@param replacement: parameter for edit method
		@type replacement: string
		@param verbose: if True raise an exception if editor is not found
		@type verbose: Boolean
		@return: section used for edit
		@rtype: L{SpecSection} or None
		@raise SpecNotFound: if editor is not found
		@raise SpecNotImplemented: if more than one section matches section type
		'''
		s = self.get_model_reader().find_section(section_type)

		if s is not None:
			if len(s) > 1:
				raise SpecNotImplemented("Cannot edit more then one section")

			SpecDebug.debug("- editing section '%s'" % str(s[0]))
			self.get_editor(s[0]).edit(s[0], replacement)
		elif verbose:
			raise SpecNotFound("Error: section type '%s' not found" % section_type)

		return s[0] if s else None
    def find_section_edit(self, section_type, replacement, verbose=True):
        '''
		Call editor's edit method for specific section type
		@param section_type: section type to be used when calling edit
		@type section_type: __class__
		@param replacement: parameter for edit method
		@type replacement: string
		@param verbose: if True raise an exception if editor is not found
		@type verbose: Boolean
		@return: section used for edit
		@rtype: L{SpecSection} or None
		@raise SpecNotFound: if editor is not found
		@raise SpecNotImplemented: if more than one section matches section type
		'''
        s = self.get_model_reader().find_section(section_type)

        if s is not None:
            if len(s) > 1:
                raise SpecNotImplemented("Cannot edit more then one section")

            SpecDebug.debug("- editing section '%s'" % str(s[0]))
            self.get_editor(s[0]).edit(s[0], replacement)
        elif verbose:
            raise SpecNotFound("Error: section type '%s' not found" %
                               section_type)

        return s[0] if s else None
Beispiel #3
0
	def parse_preamble(self):
		'''
		Parse preamble of a spec file
		@return: parsed sections in preamble
		@rtype: list of L{SpecSection}
		'''
		allowed = [ SpecIfParser, SpecDefinitionParser, SpecGlobalParser ]

		ret = self.parse_loop(self.token_list, None, allowed)
		unparsed = self.token_list.touch()
		SpecDebug.debug("-- preamble finished with token '%s' on line %d" % (str(unparsed), unparsed.line))
		return ret
Beispiel #4
0
	def sections_add(self, sections):
		'''
		Add sections to model
		@param sections: section to be added
		@type : L{SpecSection}
		@return: None
		@rtype: None
		@todo: remove/use only model to add?
		'''
		for section in sections:
			SpecDebug.debug("- adding section '%s'" % type(section))
			self.get_model_writer().add(section)
    def sections_add(self, sections):
        '''
		Add sections to model
		@param sections: section to be added
		@type : L{SpecSection}
		@return: None
		@rtype: None
		@todo: remove/use only model to add?
		'''
        for section in sections:
            SpecDebug.debug("- adding section '%s'" % type(section))
            self.get_model_writer().add(section)
    def register(self, manipulator):
        """
		Register a spec model manipulator
		@param manipulator: an manipulator to be registered
		@type manipulator: L{SpecSectionEditor}/L{SpecSectionParser}/L{SpecSectionRenderer}
		@return: None
		@rtype: None
		@raise SpecNotFound: if provided manipulator cannot be registered e.g. invalid manipulator
		"""
        found = False
        for idx, item in enumerate(self.MANIPULATORS):
            if issubclass(manipulator, item):
                found = True
                SpecDebug.debug("- registered new manipulator '%s'" % str(manipulator))
                self.MANIPULATORS[idx] = manipulator

        if not found:
            raise SpecNotFound("Invalid manipulator '%s' registration" % manipulator.__name__)
Beispiel #7
0
    def render_section(self, s, f):
        '''
		Render a section
		@param s: a section to be rendered
		@type s: L{SpecSection}
		@param f: a file to render to
		@type f: file
		@return: None
		@rtype: None
		@raise SpecNotImplemented: if renderer for the section is not registered
		'''
        found = False
        for renderer in self.MANIPULATORS:
            if issubclass(s.__class__, renderer.obj):
                found = True
                SpecDebug.debug("- rendering section '%s'" % type(s))
                renderer(s).render(f, self)
        if not found:
            raise SpecNotImplemented("Not implemented renderer")
	def render_section(self, s, f):
		'''
		Render a section
		@param s: a section to be rendered
		@type s: L{SpecSection}
		@param f: a file to render to
		@type f: file
		@return: None
		@rtype: None
		@raise SpecNotImplemented: if renderer for the section is not registered
		'''
		found = False
		for renderer in self.MANIPULATORS:
			if issubclass(s.__class__, renderer.obj):
				found = True
				SpecDebug.debug("- rendering section '%s'" % str(s))
				renderer(s).render(f, self)
		if not found:
			raise SpecNotImplemented("Not implemented renderer")
    def register(self, manipulator):
        '''
		Register a spec model manipulator
		@param manipulator: an manipulator to be registered
		@type manipulator: L{SpecSectionEditor}/L{SpecSectionParser}/L{SpecSectionRenderer}
		@return: None
		@rtype: None
		@raise SpecNotFound: if provided manipulator cannot be registered e.g. invalid manipulator
		'''
        found = False
        for idx, item in enumerate(self.MANIPULATORS):
            if issubclass(manipulator, item):
                found = True
                SpecDebug.debug("- registered new manipulator '%s'" %
                                str(manipulator))
                self.MANIPULATORS[idx] = manipulator

        if not found:
            raise SpecNotFound("Invalid manipulator '%s' registration" %
                               manipulator.__name__)
Beispiel #10
0
	def parse_loop(self, token_list, parent, allowed):
		'''
		Main parse loop to get list of parsed sections
		@param token_list: a list of tokens to be used
		@type token_list: L{SpecTokenList}
		@param parent: parent section
		@type parent: L{SpecSection}
		@param allowed: allowed sections to be parsed, section parsers
		@type allowed: list of L{SpecSectionParser}
		@return: list of parsed sections
		@rtype: L{SpecSection}
		'''
		ret = []

		found = True
		while found:
			found = False
			token = token_list.touch()
			SpecDebug.debug("- parsing round: '%s'" % str(token))

			if token.is_eof():
				break

			for t in allowed:
				section = t.parse(token_list, parent, allowed, self)
				if section:
					found = True
					SpecDebug.debug("- adding parsed section '%s'" % type(section))
					ret.append(section)
					break

			if not found:
				SpecDebug.debug("- unparsed token '%s' on line %s" % (str(token), str(token.line)))

		return ret
Beispiel #11
0
	def find_section_add(self, section_type, items, verbose = True):
		'''
		Add items to a section
		@param section_type: section type to be used when calling add
		@type section_type: __class__
		@param items: items to be added to a section of type section type
		@type items: editor specific
		@param verbose: if True raise an exception if editor is not found
		@type verbose: Boolean
		@return: section used for edit
		@rtype: L{SpecSection} or None
		@raise SpecNotFound: if editor is not found
		@raise SpecNotImplemented: if more than one section matches section type
		'''
		s = self.get_model_reader().find_section(section_type)

		if s is not None:
			SpecDebug.debug("- adding section to '%s'", type(s[0]))
			self.get_editor(s[0]).add(s[0], items)
		elif verbose:
			raise SpecNotFound("Error: section '%s' not found" % section_type)

		return s[0] if s else None
    def find_section_add(self, section_type, items, verbose=True):
        '''
		Add items to a section
		@param section_type: section type to be used when calling add
		@type section_type: __class__
		@param items: items to be added to a section of type section type
		@type items: editor specific
		@param verbose: if True raise an exception if editor is not found
		@type verbose: Boolean
		@return: section used for edit
		@rtype: L{SpecSection} or None
		@raise SpecNotFound: if editor is not found
		@raise SpecNotImplemented: if more than one section matches section type
		'''
        s = self.get_model_reader().find_section(section_type)

        if s is not None:
            SpecDebug.debug("- adding section to '%s'", type(s[0]))
            self.get_editor(s[0]).add(s[0], items)
        elif verbose:
            raise SpecNotFound("Error: section '%s' not found" % section_type)

        return s[0] if s else None
Beispiel #13
0
	def add(self, section):
		'''
		Add a section, try to guess the most suitable position for the section
		@param section: section to be added
		@type section: L{SpecSection}
		@return: None
		@rtype: None
		'''
		def get_add_index(idx_find, idx):
			if idx_find == idx:
				ret = idx - 1
				diff = -1
			else:
				diff = idx_find - idx
				if diff < 0:
					ret = idx + (-diff)
				else:
					ret = idx - (diff + 1)
			# check bounds
			if ret < 0:
				ret = idx + (abs(diff) + 1)
				if ret >= len(self.SPEC_SECTION_ORDER):
					return None
			elif ret >= len(self.SPEC_SECTION_ORDER):
				ret = idx - abs(diff)
				if ret < 0:
					return None
			return ret

		if issubclass(section.__class__, SpecStDefinition) \
				or issubclass(section.__class__, SpecStIf):
			raise SpecNotImplemented("Unable to add definitions and ifs")

		found = False
		if not issubclass(section.__class__, SpecStPackage):
			# simple replace
			for idx, sec in enumerate(self.sections):
				if issubclass(sec.__class__, section.__class__):
					SpecDebug.debug("-- replacing section '%s'" % type(section))
					self.sections[idx] = section
					found = True
					break

		if found:
			return

		# replace failed, append section based on section order

		if issubclass(section.__class__, SpecStPackage):
			# This needs special checks based on package, not only package, but
			# description, ... as well
			raise SpecNotImplemented("Adding %package section not implemented") # TODO: implement

		for idx, sec in enumerate(self.SPEC_SECTION_ORDER):
			if issubclass(section.__class__, sec):
				break

		# idx now points to the section in SPEC_SECTION_ORDER
		idx_find = idx
		while not found:
			idx_find = get_add_index(idx_find, idx)

			if idx_find is None:
				raise SpecNotFound("Section '%s' was not found in section order" % type(section))

			for i, sec in enumerate(self.sections):
				if issubclass(sec.__class__, self.SPEC_SECTION_ORDER[idx_find]):
					found = True
					if idx_find > idx:
						SpecDebug.debug("-- addiding section '%s' at position before section '%s'" % (type(section), type(sec)))
						self.sections.insert(i, section)
					else:
						SpecDebug.debug("-- addiding section '%s' at position after section '%s'" % (type(section), type(sec)))
						self.sections.insert(i + 1, section)
					break

		if not found:
			raise SpecNotFound("Section '%s' was not added" % type(section))
Beispiel #14
0
    def add(self, section):
        '''
		Add a section, try to guess the most suitable position for the section
		@param section: section to be added
		@type section: L{SpecSection}
		@return: None
		@rtype: None
		'''
        def get_add_index(idx_find, idx):
            if idx_find == idx:
                ret = idx - 1
                diff = -1
            else:
                diff = idx_find - idx
                if diff < 0:
                    ret = idx + (-diff)
                else:
                    ret = idx - (diff + 1)
            # check bounds
            if ret < 0:
                ret = idx + (abs(diff) + 1)
                if ret >= len(self.SPEC_SECTION_ORDER):
                    return None
            elif ret >= len(self.SPEC_SECTION_ORDER):
                ret = idx - abs(diff)
                if ret < 0:
                    return None
            return ret

        if issubclass(section.__class__, SpecStDefinition) \
          or issubclass(section.__class__, SpecStIf):
            raise SpecNotImplemented("Unable to add definitions and ifs")

        found = False
        if not issubclass(section.__class__, SpecStPackage):
            # simple replace
            for idx, sec in enumerate(self.sections):
                if issubclass(sec.__class__, section.__class__):
                    SpecDebug.debug("-- replacing section '%s'" %
                                    type(section))
                    self.sections[idx] = section
                    found = True
                    break

        if found:
            return

        # replace failed, append section based on section order

        if issubclass(section.__class__, SpecStPackage):
            # This needs special checks based on package, not only package, but
            # description, ... as well
            raise SpecNotImplemented(
                "Adding %package section not implemented")  # TODO: implement

        for idx, sec in enumerate(self.SPEC_SECTION_ORDER):
            if issubclass(section.__class__, sec):
                break

        # idx now points to the section in SPEC_SECTION_ORDER
        idx_find = idx
        while not found:
            idx_find = get_add_index(idx_find, idx)

            if idx_find is None:
                raise SpecNotFound(
                    "Section '%s' was not found in section order" %
                    type(section))

            for i, sec in enumerate(self.sections):
                if issubclass(sec.__class__,
                              self.SPEC_SECTION_ORDER[idx_find]):
                    found = True
                    if idx_find > idx:
                        SpecDebug.debug(
                            "-- addiding section '%s' at position before section '%s'"
                            % (type(section), type(sec)))
                        self.sections.insert(i, section)
                    else:
                        SpecDebug.debug(
                            "-- addiding section '%s' at position after section '%s'"
                            % (type(section), type(sec)))
                        self.sections.insert(i + 1, section)
                    break

        if not found:
            raise SpecNotFound("Section '%s' was not added" % type(section))