def report_item(remote, otype, item=None, name=None): if item is None: if otype == "setting": cur_settings = remote.get_settings() try: item = {'name': name, 'value': cur_settings[name]} except: print "Setting not found: %s" % name sys.exit(1) elif otype == "signature": items = remote.get_signatures() total_sigs = 0 if "breeds" in items: print "Currently loaded signatures:" if name in items["breeds"]: print "%s:" % name oskeys = items["breeds"][name].keys() oskeys.sort() if len(oskeys) > 0: total_sigs += len(oskeys) for osversion in oskeys: print "\t%s" % osversion else: print "\t(none)" print "\nBreed '%s' has %d total signatures" % (name, total_sigs) else: print "No breed named '%s' found" % name sys.exit(1) else: print "No breeds found in the signature, a signature update is recommended" sys.exit(1) return else: item = remote.get_item(otype, name) if item == "~": print "No %s found: %s" % (otype, name) sys.exit(1) if otype == "distro": data = utils.to_string_from_fields(item, item_distro.FIELDS) elif otype == "profile": data = utils.to_string_from_fields(item, item_profile.FIELDS) elif otype == "system": data = utils.to_string_from_fields( item, item_system.FIELDS, item_system.NETWORK_INTERFACE_FIELDS) elif otype == "repo": data = utils.to_string_from_fields(item, item_repo.FIELDS) elif otype == "image": data = utils.to_string_from_fields(item, item_image.FIELDS) elif otype == "mgmtclass": data = utils.to_string_from_fields(item, item_mgmtclass.FIELDS) elif otype == "package": data = utils.to_string_from_fields(item, item_package.FIELDS) elif otype == "file": data = utils.to_string_from_fields(item, item_file.FIELDS) elif otype == "setting": data = "%-40s: %s" % (item['name'], item['value']) print data
def report_item(remote, otype, item=None, name=None): if item is None: if otype == "setting": cur_settings = remote.get_settings() try: item = {'name': name, 'value': cur_settings[name]} except: print("Setting not found: %s" % name) sys.exit(1) elif otype == "signature": items = remote.get_signatures() total_sigs = 0 if "breeds" in items: print("Currently loaded signatures:") if name in items["breeds"]: print("%s:" % name) oskeys = list(items["breeds"][name].keys()) oskeys.sort() if len(oskeys) > 0: total_sigs += len(oskeys) for osversion in oskeys: print("\t%s" % osversion) else: print("\t(none)") print("\nBreed '%s' has %d total signatures" % (name, total_sigs)) else: print("No breed named '%s' found" % name) sys.exit(1) else: print("No breeds found in the signature, a signature update is recommended") sys.exit(1) return else: item = remote.get_item(otype, name) if item == "~": print("No %s found: %s" % (otype, name)) sys.exit(1) if otype == "distro": data = utils.to_string_from_fields(item, item_distro.FIELDS) elif otype == "profile": data = utils.to_string_from_fields(item, item_profile.FIELDS) elif otype == "system": data = utils.to_string_from_fields(item, item_system.FIELDS, item_system.NETWORK_INTERFACE_FIELDS) elif otype == "repo": data = utils.to_string_from_fields(item, item_repo.FIELDS) elif otype == "image": data = utils.to_string_from_fields(item, item_image.FIELDS) elif otype == "mgmtclass": data = utils.to_string_from_fields(item, item_mgmtclass.FIELDS) elif otype == "package": data = utils.to_string_from_fields(item, item_package.FIELDS) elif otype == "file": data = utils.to_string_from_fields(item, item_file.FIELDS) elif otype == "setting": data = "%-40s: %s" % (item['name'], item['value']) print(data)
def to_string(self): """ Convert an item into a string. :return: The string representation of the object. """ return utils.to_string_from_fields(self, self.get_fields())
def test_to_string_from_fields(): # Arrange test_api = CobblerAPI() test_manager = CollectionManager(test_api) test_distro = Distro(test_manager) # Act result = utils.to_string_from_fields(test_distro.__dict__, test_distro.get_fields()) # Assert - This test is specific to a Distro object assert len(result.splitlines()) == 19
def to_string(self): return utils.to_string_from_fields(self, self.get_fields())
def report_item(remote, otype, item=None, name=None): """ Return a single item in a given collection. Either this is an item object or this method searches for a name. :param remote: The remote to use as the query-source. :param otype: The object type to query. :param item: The item to display :param name: The name to search for and display. """ if item is None: if otype == "setting": cur_settings = remote.get_settings() try: item = {'name': name, 'value': cur_settings[name]} except: print("Setting not found: %s" % name) return 1 elif otype == "signature": items = remote.get_signatures() total_sigs = 0 if "breeds" in items: print("Currently loaded signatures:") if name in items["breeds"]: print("%s:" % name) oskeys = list(items["breeds"][name].keys()) oskeys.sort() if len(oskeys) > 0: total_sigs += len(oskeys) for osversion in oskeys: print("\t%s" % osversion) else: print("\t(none)") print("\nBreed '%s' has %d total signatures" % (name, total_sigs)) else: print("No breed named '%s' found" % name) return 1 else: print( "No breeds found in the signature, a signature update is recommended" ) return 1 return else: item = remote.get_item(otype, name) if item == "~": print("No %s found: %s" % (otype, name)) return 1 if otype == "distro": data = utils.to_string_from_fields(item, distro.FIELDS) elif otype == "profile": data = utils.to_string_from_fields(item, profile.FIELDS) elif otype == "system": data = utils.to_string_from_fields(item, system.FIELDS, system.NETWORK_INTERFACE_FIELDS) elif otype == "repo": data = utils.to_string_from_fields(item, repo.FIELDS) elif otype == "image": data = utils.to_string_from_fields(item, image.FIELDS) elif otype == "mgmtclass": data = utils.to_string_from_fields(item, mgmtclass.FIELDS) elif otype == "package": data = utils.to_string_from_fields(item, package.FIELDS) elif otype == "file": data = utils.to_string_from_fields(item, file.FIELDS) elif otype == "setting": data = "%-40s: %s" % (item['name'], item['value']) print(data)