Skip to content

Instantly share code, notes, and snippets.

@apetro
Created January 15, 2014 15:49
Show Gist options
  • Save apetro/8438640 to your computer and use it in GitHub Desktop.
Save apetro/8438640 to your computer and use it in GitHub Desktop.
Lists portlet fnames
import os
import re
import sys
'''
Given the path to a directory containing uPortal portlet-definition.xml entity files, prints the title element from each.
Assumes . if target directory not specified.
Example of running:
$ python list_portlets.py /Users/apetro/code/code_doit/MUM-overlay/my-prod-overlay/entities/tags/entities-51/src/main/resources/portlet-definition
Academic Advisors List of Advisees - with Notes
Academic Planning and Analysis
Academic Resources
Access Denied Records
Activate Services
Advisee Lookup - with Notes
Advisee Lookup
...
'''
def main():
# Get the directory of portlets from the command line, using '.' as a fallback.
target_directory = "."
if len(sys.argv) >= 2:
target_directory = sys.argv[1]
if target_directory == "-h":
print("usage: [path to target directory]")
sys.exit(1)
# TODO: filter down to portlet-definition.xml files
# TODO: limit file size so as to not open illegitimately large files
portlet_definition_file_names = [name for name in os.listdir(target_directory) if os.path.isfile(os.path.join(target_directory, name))]
for portlet_definition_file_name in portlet_definition_file_names:
full_path_file_name = os.path.join(target_directory, portlet_definition_file_name)
with open(full_path_file_name, 'rt') as f:
file_contents = f.read()
match = re.search('(<fname>)(.*)(</fname>)', file_contents)
if match:
print match.group(2)
if __name__ == '__main__':
main()
@dima767
Copy link

dima767 commented Jan 15, 2014

Or could could add a "shebang" at the top: #!/usr/bin/env python so then invoking it would be just a matter of ./list_portlets (if you drop the .py extension). Just a matter of taste and nothing else :-)

Cheers,
D.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment