Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
# python_elves
Little helpers written in Python
# python_elves: Little helpers written in Python

This repository is meant as a loose collection of python tools for various purposes.

## lsconvert
The script takes ls -R output and turns it into a XML file.
Sample:

ls -R /home/my_dir > input.log
python lsconvert.py input.log output.xml

-------- input.log -------

/home/my_dir:
oneFile.txt
anotherFile.log
someDirectory
pink_elephant.sh

/home/my_dir/someDirectory:
someMoreFiles
someScript.py
someNestedDirectory

/home/my_dir/someDirectory/someMoreFiles:
very_nested_file.txt
very_nested_program.sh

/home/my_dir/someDirectory/someNestedDirectory:

-------- output.xml -------

<home>
<my_dir>
<oneFile.txt />
<anotherFile.log />
<someDirectory>
<someMoreFiles>
<very_nested_file.txt />
<very_nested_program.sh />
</someMoreFiles>
</someDirectory>
<someScript.py />
<someNestedDirectory />
</my_dir>
</home>

## JQL Composer
The script is used to compose complex JQL queries from a JSON-encoded dictionary of simpler sub-queries.

It will enclose the expansion of sub-queries in parentheses while expanding, in order to ensure the original precedence.

In order to use one query inside another, use the sub-query's name in brackets inside the outer query. See sample.json for an example.
In order to use one query inside another, use the sub-query's name in brackets inside the outer query. See sample.json for an example.
50 changes: 50 additions & 0 deletions lsconvert/lsconvert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import sys
import os

def read_subtree(filename):
fileTree = {}
with open(filename, "rb") as readFrom:
dirPath = []
for line in readFrom:
shortline = line.strip()
if len(shortline) == 0:
pass
elif shortline[0] == "/":
dirPath = shortline.split("/")[1:]
dirPath[-1] = dirPath[-1][:-1]
else:
dictionary = fileTree
for directory in dirPath:
if directory not in dictionary.keys():
dictionary[directory] = {}
dictionary = dictionary[directory]
dictionary[shortline] = {}
return fileTree

def toXml(dictionary, lineIndent=""):
text = ""
for tag in dictionary.keys():
if len(dictionary[tag]) > 0:
text = text + "{indent}<{tag}>{sep}{children}{indent}</{tag}>{sep}".format(tag=tag, children=toXml(dictionary[tag], lineIndent + " "), indent=lineIndent, sep=os.linesep)
else:
text = text + "{indent}<{tag} />".format(tag=tag, indent=lineIndent) + os.linesep
return text

def print_subtree(treeDict, filename):
with open(filename, "wb") as writeTo:
writeTo.write(toXml(treeDict))

def format_subtree(infile, outfile):
fileTree = read_subtree(infile)
print_subtree(fileTree, outfile)

if __name__ == "__main__":
if len(sys.argv) < 2 or len(sys.argv) > 3:
print("usage: lsconvert infile [outfile]")
else:
infile = str(os.path.abspath(sys.argv[1]))
outfile = str(os.path.dirname(os.path.abspath(sys.argv[1])))
if len(sys.argv) == 3:
outfile = os.path.abspath(sys.argv[2])
print("Converting from " + infile + " to " + outfile)
format_subtree(infile, outfile)