Now that I uploaded version 1.1 of calcpkg, I can post "FolderTrees.py", the script I wrote earlier.
Even though it would probably be called from another Python script or module as a module, rather than a script, I made it executable for debugging purposes. It should print out some categories and files.
Here are the notes I had at the top of the file as comments:
#Todo here- exclude /os/, or do we want users to be able to see /os/ and just not send .8xus?
#Should it be rewritten to use subprocess rather than os.system? Probably, but I didn't feel like it.
Code: #!/usr/bin/env python
import os
class Category:
"""Category object, can contain subcategories and files"""
def __init__(self, path):
self.path = path
self.name = path[path.rfind('/') + 1:]
self.categories = []
self.files = []
def __str__(self):
return self.path
def __repr__(self):
return self.path
def __cmp__(self, other):
if self.path < other.path:
return -1
elif self.path > other.path:
return 1
else:
return 0
class File:
"""File object, contained by categories"""
def __init__(self, fileName, category):
self.fileName = fileName
self.category = category
def __str__(self):
return self.fileName
def __repr__(self):
return self.fileName
def produceTreeStructure():
"""Return an array containing the top-level Categories in the /83plus/ directory"""
#First, get all files in 83+ folder in order to find categories
os.system("calcpkg list -f -c 83plus '' > calcpkg.out")
listOutput = open("calcpkg.out", "rt")
categories = []
for line in listOutput:
if not "83plus" in line:
continue
categoryPath = line[:line.find(" ")]
category = Category(categoryPath)
if not category in categories:
categories.append(category)
listOutput.close()
#Now, for all categories, run a list, create Files for files that are in them, Categories for categories that aren't
for category in categories:
os.system("calcpkg list -f -c " + category.path[:-1] + " '' > calcpkg.out")
listOutput = open("calcpkg.out", "rt")
for line in listOutput:
if not "83plus" in line:
continue
categoryPath = line[:line.find(" ")]
if categoryPath == category.path:
fileName = line[line.find(" "):].strip(" ")[:-1]
newFile = File(fileName, category)
category.files.append(newFile)
elif category.path in categoryPath:
for searchCategory in categories:
if searchCategory.path == categoryPath:
category.categories.append(searchCategory)
categories.remove(searchCategory)
break
listOutput.close()
os.remove("calcpkg.out")
#Now, clean up the root "categories" array... this is a rather exhaustive process
newCategories = []
for category in categories:
if ("os" in category.path or "bbcbasic" in category.path):
newCategories.append(category)
continue
rootStr = category.path[:category.path.rindex("/")]
try:
rootStr = rootStr[:rootStr.rindex("/") + 1]
except:
continue
root = Category(rootStr)
if root in newCategories or rootStr == "83plus":
continue
for loopCategory in categories:
if rootStr in loopCategory.path:
root.categories.append(loopCategory)
newCategories.append(root)
#And one last pass to clean up the really annoying sub directories
categories = []
for category in newCategories:
valid = False
for loopCategory in newCategories:
if category.path in loopCategory.path and category != loopCategory:
valid = True
category.categories.append(loopCategory)
for subCategories in loopCategory.categories:
try:
category.categories.remove(subCategories)
except:
pass
if ("bbcbasic" in category.path or "os" in category.path):
valid = True
if valid:
categories.append(category)
#Debugging code to verify the script is working
print categories
print categories[1].categories
print categories[2].files
return categories
if __name__ == '__main__':
produceTreeStructure()