for cred in cred_s:
print "Using Credential {}".format(credential_printable(cred))
-# save methods
-def save_raw_to_file(var, filename, format="text", banner=None):
- if filename == "-":
- # if filename is "-", send it to stdout
- f = sys.stdout
+########## save methods
+
+### raw
+def save_raw_to_file(var, filename, format='text', banner=None):
+ if filename == '-':
+ _save_raw_to_file(var, sys.stdout, format, banner)
else:
- f = open(filename, "w")
- if banner:
- f.write(banner+"\n")
+ with open(filename, w) as fileobj:
+ _save_raw_to_file(var, fileobj, format, banner)
+ print "(Over)wrote {}".format(filename)
+
+def _save_raw_to_file(var, f, format, banner):
if format == "text":
- f.write(str(var))
+ if banner: f.write(banner+"\n")
+ f.write("{}".format(var))
+ if banner: f.write('\n'+banner+"\n")
elif format == "pickled":
f.write(pickle.dumps(var))
elif format == "json":
- if hasattr(json, "dumps"):
- f.write(json.dumps(var)) # python 2.6
- else:
- f.write(json.write(var)) # python 2.5
+ f.write(json.dumps(var)) # python 2.6
else:
# this should never happen
print "unknown output format", format
- if banner:
- f.write('\n'+banner+"\n")
+###
def save_rspec_to_file(rspec, filename):
if not filename.endswith(".rspec"):
filename = filename + ".rspec"
with open(filename, 'w') as f:
f.write("{}".format(rspec))
- return
+ print "(Over)wrote {}".format(filename)
+
+def save_record_to_file(filename, record_dict):
+ record = Record(dict=record_dict)
+ xml = record.save_as_xml()
+ with codecs.open(filename, encoding='utf-8',mode="w") as f:
+ f.write(xml)
+ print "(Over)wrote {}".format(filename)
def save_records_to_file(filename, record_dicts, format="xml"):
if format == "xml":
- index = 0
- for record_dict in record_dicts:
- if index > 0:
- save_record_to_file(filename + "." + str(index), record_dict)
- else:
- save_record_to_file(filename, record_dict)
- index = index + 1
+ for index, record_dict in enumerate(record_dicts):
+ save_record_to_file(filename + "." + str(index), record_dict)
elif format == "xmllist":
- f = open(filename, "w")
- f.write("<recordlist>\n")
- for record_dict in record_dicts:
- record_obj=Record(dict=record_dict)
- f.write('<record hrn="' + record_obj.hrn + '" type="' + record_obj.type + '" />\n')
- f.write("</recordlist>\n")
- f.close()
+ with open(filename, "w") as f:
+ f.write("<recordlist>\n")
+ for record_dict in record_dicts:
+ record_obj = Record(dict=record_dict)
+ f.write('<record hrn="' + record_obj.hrn + '" type="' + record_obj.type + '" />\n')
+ f.write("</recordlist>\n")
+ print "(Over)wrote {}".format(filename)
+
elif format == "hrnlist":
- f = open(filename, "w")
- for record_dict in record_dicts:
- record_obj=Record(dict=record_dict)
- f.write(record_obj.hrn + "\n")
- f.close()
+ with open(filename, "w") as f:
+ for record_dict in record_dicts:
+ record_obj = Record(dict=record_dict)
+ f.write(record_obj.hrn + "\n")
+ print "(Over)wrote {}".format(filename)
+
else:
# this should never happen
print "unknown output format", format
-def save_record_to_file(filename, record_dict):
- record = Record(dict=record_dict)
- xml = record.save_as_xml()
- f=codecs.open(filename, encoding='utf-8',mode="w")
- f.write(xml)
- f.close()
- return
-
# minimally check a key argument
def check_ssh_key (key):
good_ssh_key = r'^.*(?:ssh-dss|ssh-rsa)[ ]+[A-Za-z0-9+/=]+(?: .*)?$'
return 'slice'
elif type.startswith('no'):
return 'node'
+ elif type.startswith('ag'):
+ return 'aggregate'
+ elif type.startswith('al'):
+ return 'all'
else:
+ print 'unknown type {} - should start with one of au|us|sl|no|ag|al'.format(type)
return None
def load_record_from_opts(options):
record_dict = {}
- if hasattr(options, 'type'):
- options.type = normalize_type(options.type)
if hasattr(options, 'xrn') and options.xrn:
if hasattr(options, 'type') and options.type:
xrn = Xrn(options.xrn, options.type)
return Record(dict=record_dict)
def load_record_from_file(filename):
- f=codecs.open(filename, encoding="utf-8", mode="r")
- xml_string = f.read()
- f.close()
- return Record(xml=xml_string)
-
+ with codecs.open(filename, encoding="utf-8", mode="r") as f:
+ xml_str = f.read()
+ return Record(xml=xml_str)
import uuid
def unique_call_id(): return uuid.uuid4().urn
if canonical in ("register", "update"):
parser.add_option('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
- parser.add_option('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
+ parser.add_option('-t', '--type', dest='type', metavar='<type>', help='object type (2 first chars is enough)', default=None)
parser.add_option('-e', '--email', dest='email', default="", help="email (mandatory for users)")
parser.add_option('-n', '--name', dest='name', default="", help="name (optional for authorities)")
parser.add_option('-k', '--key', dest='key', metavar='<key>', help='public key string or file',
help="renew as long as possible")
# registy filter option
if canonical in ("list", "show", "remove"):
- parser.add_option("-t", "--type", dest="type", type="choice",
- help="type filter ([all]|user|slice|authority|node|aggregate)",
- choices=("all", "user", "slice", "authority", "node", "aggregate"),
- default="all")
+ parser.add_option("-t", "--type", dest="type", metavar="<type>",
+ default="all",
+ help="type filter - 2 first chars is enough ([all]|user|slice|authority|node|aggregate)")
if canonical in ("show"):
parser.add_option("-k","--key",dest="keys",action="append",default=[],
help="specify specific keys to be displayed from record")
sys.exit(1)
self.command_options = command_options
+ # allow incoming types on 2 characters only
+ if hasattr(command_options, 'type'):
+ command_options.type = normalize_type(command_options.type)
+ if not command_options.type:
+ sys.exit(1)
+
self.read_config ()
self.bootstrap ()
self.logger.debug("Command={}".format(self.command))
try:
record_filepath = args[0]
rec_file = self.get_record_file(record_filepath)
- record_dict.update(load_record_from_file(rec_file).todict())
+ record_dict.update(load_record_from_file(rec_file).record_to_dict())
except:
print "Cannot load record file {}".format(record_filepath)
sys.exit(1)
if options:
- record_dict.update(load_record_from_opts(options).todict())
+ record_dict.update(load_record_from_opts(options).record_to_dict())
# we should have a type by now
if 'type' not in record_dict :
self.print_help()
if len(args) > 0:
record_filepath = args[0]
rec_file = self.get_record_file(record_filepath)
- record_dict.update(load_record_from_file(rec_file).todict())
+ record_dict.update(load_record_from_file(rec_file).record_to_dict())
if options:
- record_dict.update(load_record_from_opts(options).todict())
+ record_dict.update(load_record_from_opts(options).record_to_dict())
# at the very least we need 'type' here
if 'type' not in record_dict or record_dict['type'] is None:
self.print_help()