Skip to content

Commit

Permalink
Added support for exporting a tree. Still a bit dodgy
Browse files Browse the repository at this point in the history
  • Loading branch information
emilerl committed Nov 21, 2011
1 parent f9903e5 commit 9884ee2
Showing 1 changed file with 61 additions and 13 deletions.
74 changes: 61 additions & 13 deletions pyplcli/pyplcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import subprocess
import getopt
import socket

from operator import attrgetter

try:
import packetlogic2
Expand Down Expand Up @@ -459,13 +459,13 @@ def tree(*args):
else:
print c.white("Listing the contents of ") + c.green(path)
objs = rs.object_list(path, recursive=True)
objs = sorted(objs, key=attrgetter("path"))
for obj in objs:
print c.green(obj.path) + "/" +c.blue(obj.name)
if obj is not None:
for i in obj.items:
print c.green(obj.path) + "/" +c.purple(i.value1) + "/" + c.purple(i.value2)


def lsl(*args):
if pl is None:
error("Not connected to any PacketLogic")
Expand Down Expand Up @@ -520,15 +520,19 @@ def mkdir(*args):
else:
if len(args[0]) > 0:
no_name = " ".join(args[0])
what = os.path.join(path, no_name)
print c.white("Creating NetObject path: ") + c.green("%s" % what)
o = rs.object_find(what)
if o is None:
oid = rs.add(what)
print c.white("New object id: %d" % oid)
rs.commit()
else:
error("NetObject %s already exists! Skipping" % what)
parts = no_name.split("/")
cpwd = path
for part in parts:
what = os.path.join(cpwd, part)
o = rs.object_find(what)
if o is None:
oid = rs.add(what)
print c.white("New object '%s' with id: %d" % (what, oid))
else:
warning("NetObject %s already exists! Skipping" % what)
cpwd = what
rs.commit()
print c.white("Created NetObject path: ") + c.green("%s" % cpwd)
else:
error("Usage: mkdir name")

Expand Down Expand Up @@ -1100,6 +1104,47 @@ def goto(*args):
def simplepmt(*args):
global simpleprompt
simpleprompt = not simpleprompt

def exporttree(*args):
if pl is None:
error("Not connected to any PacketLogic")
else:
script = None
if len(args[0]) == 1:
script = args[0][0]

lines = []
lines.append("# Generated by pyplcli version %s" % SCRIPT_VERSION)

lines.append("cd /")

objs = rs.object_list(path, recursive=True)
for obj in objs:
pth = os.path.join(obj.path, obj.name).replace("/NetObjects/", "")
lines.append("mkdir %s" % pth)
if len(obj.items) > 0:
lines.append("cd %s" % pth)
for i in obj.items:
if i.value2 == "":
lines.append("add %s" % i.value1)
else:
lines.append("add %s/%s" % (i.value1, i.value2))
lines.append("cd /")

if script is not None:
print c.white("Writing tree to file %s" % script)
f = open(script, "w")
for line in lines:
f.write(line + "\n")
f.close()
print c.white("Wrote %d lines to %s" % (len(lines), script))
else:
for line in lines:
if line.startswith("#"):
print c.brown(line)
else:
parts = line.split(" ")
print c.green(parts[0]) + " " + c.yellow(" ".join(parts[1:]))

# Mapping between the text names and the python methods
# First item in list is a method handle and second is a help string used by the
Expand Down Expand Up @@ -1149,8 +1194,9 @@ def simplepmt(*args):
'rmalias' : [rmalias, "Remove an alias.\n\tUsage: rmalias NAME"],
'bookmark' : [bookmark, "Create a bookmark at pwd (use goto to go back later)\n\tUsage: bookmark MAME"],
'rmbookmark' : [rmbookmark, "Removes a bookmark.\n\tUsage: rmbookmark BOOKMARK"],
'goto' : [goto, "Go to a bookmarked location. If used with no arguments it lists the bookmarks.\n\tUsage: goto BOOKMARK"],
'simpleprompt' : [simplepmt, "Toggle simple or advanced prompt."]
'goto' : [goto, "Go to a bookmarked location. If used with no arguments it lists the bookmarks.\n\tUsage: goto [BOOKMARK]"],
'simpleprompt' : [simplepmt, "Toggle simple or advanced prompt."],
'exporttree' : [exporttree, "Export the NetObject tree as a pli script.\n\tUsage: exporttree [SCRIPTFILE]"]
}

#############################################################################
Expand Down Expand Up @@ -1589,6 +1635,8 @@ def main():
assert False, "unhandled option"

if script is not None:
if packetlogic is not None:
connect([packetlogic])
run_script(script)
elif execute is not None:
for line in execute:
Expand Down

0 comments on commit 9884ee2

Please sign in to comment.