def __add(self, stanza, lineno, ret, config): """Private method for gathering stanzas.""" if not stanza.is_empty(): ret.append(stanza.finish(lineno)) if config: config.add(stanza) stanza = ConfigStanza() return stanza
def _create_mock_config(self, hashes): stanzas = [] for hash in hashes: stanza = ConfigStanza() stanza.set('package', 'apackage', 1) stanza.set('accept-origin', hash['accepted_origin'], 2) stanza.set('track-origin', hash['track_origin'], 3) stanza.set('track-version', hash['track_version'], 4) stanza.finish(4) stanzas.append(stanza) mock_config = self.struct() mock_config.package = lambda name: name == 'apackage' and stanzas or [] return mock_config
def _create_default_stanza(self, status, base=False): """Creates a stanza used when there is no configuration for a given package. Such Stanza has a matching Package tag, Accept-Origin that matches anything, Track-Origin equal to the distributor ID according to facter, and Track-Version equal to the candidate version (or the base of the candidate version, if base argument is True). """ s = ConfigStanza() s.set('package', status.package_name, 1) s.set('accept-origin', '*', 1) s.set('track-origin', self.facter.distributors_id, 1) if base: v = self.base(status.candidate_version.string) else: v = status.candidate_version.string s.set('track-version', v, 1) return s.finish(1)
def load(self, file, config=None): """Parses an open file object, and returns a list of stanzas found. If an optional config argument (a Config object) is provided, it also adds the stanzas to it. Parsing errors are raised as exceptions. """ ret = [] stanza = ConfigStanza() lineno = 0 for line in file: lineno += 1 line = line.strip() if line == '': stanza = self.__add(stanza, lineno, ret, config) continue colon = line.find(':') if colon != -1: key, val = line.split(':', 1) stanza.set(key.strip(), val.strip(), lineno) else: raise ValueError('invalid line %d: %s' % (lineno, repr(line))) stanza = self.__add(stanza, lineno, ret, config) return ret
def setUp(self): super(Test_ConfigStanza, self).setUp() self.cs = ConfigStanza() self.assert_(self.cs.is_empty())