[pycopia] r757 committed - Add feature to export and import agent list.

3 views
Skip to first unread message

pyc...@googlecode.com

unread,
Mar 16, 2015, 12:05:27 AM3/16/15
to pyc...@googlegroups.com
Revision: 757
Author: keith.dart
Date: Mon Mar 16 04:05:09 2015 UTC
Log: Add feature to export and import agent list.

https://code.google.com/p/pycopia/source/detail?r=757

Modified:
/trunk/QA/pycopia/remote/nsc.py

=======================================
--- /trunk/QA/pycopia/remote/nsc.py Wed May 9 19:18:23 2012 UTC
+++ /trunk/QA/pycopia/remote/nsc.py Mon Mar 16 04:05:09 2015 UTC
@@ -19,7 +19,6 @@
"""
from __future__ import absolute_import
from __future__ import print_function
-#from __future__ import unicode_literals
from __future__ import division


@@ -28,10 +27,25 @@

def print_listing(listing):
for name, uri in sorted(listing.items()):
- if len(uri) > 45:
- print("{:>35.35s} --> \n{:>79.79s}".format(name, uri))
- else:
- print("{:>35.35s} --> {}".format(name, uri))
+ print("{} -->\n {}".format(name, uri))
+
+
+def nsexport(nameserver, fname):
+ with open(fname, "wb") as fo:
+ for name, uri in nameserver.list().items():
+ fo.write("{}\t{}\n".format(name, uri))
+
+
+def nsimport(nameserver, fname):
+ with open(fname, "rb") as fo:
+ for line in fo:
+ name, uri = line.split("\t", 1)
+ try:
+ nameserver.register(name.strip(), uri.strip(), safe=True)
+ except pyro.NamingError:
+ print("Warning: {} already registered.".format(name))
+ else:
+ print("Registered: {}".format(name))


_DOC = """nsc [-h?]
@@ -42,7 +56,8 @@
list - show current objects.
ping - No error if server is reachable.
remove <name> - remove the named agent entry.
-
+ export <filename> - export agent entries to a file.
+ import <filename> - import agent entries from a file.
"""

def nsc(argv):
@@ -74,12 +89,22 @@
elif subcmd.startswith("pi"):
nameserver.ping()
print("Name server is alive.")
- if subcmd.startswith("rem"):
+ elif subcmd.startswith("rem"):
if args:
nameserver.remove(name=args[0])
else:
print(_DOC)
return 2
+ elif subcmd.startswith("imp"):
+ fname = args[0] if len(args) > 0 else "nsentries.txt"
+ nsimport(nameserver, fname)
+ elif subcmd.startswith("exp"):
+ fname = args[0] if len(args) > 0 else "nsentries.txt"
+ nsexport(nameserver, fname)
+ print("exported to {}.".format(fname))
+ else:
+ print(_DOC)
+ return 2


if __name__ == "__main__":
Reply all
Reply to author
Forward
0 new messages