Revision 7dd0bc9b
Added by David Sorber over 12 years ago
| software/file_share/main.py | ||
|---|---|---|
|
import os
|
||
|
import sys
|
||
|
|
||
|
import lxml.etree as ET
|
||
|
|
||
|
class Directory(object):
|
||
|
|
||
|
def __init__(self, path):
|
||
|
self.path = path
|
||
|
self.dirname = os.path.basename(path)
|
||
|
|
||
|
def __repr__(self):
|
||
|
return '{:s} - DIRECTORY'.format(self.dirname)
|
||
|
|
||
|
def make_row(self):
|
||
|
row = ET.Element('tr')
|
||
|
name_element = ET.SubElement(row, 'td')
|
||
|
name_element.text = self.dirname
|
||
|
size_element = ET.SubElement(row, 'td')
|
||
|
size_element.text = '--'
|
||
|
return row
|
||
|
|
||
|
|
||
|
class File(object):
|
||
|
|
||
|
def __init__(self, path):
|
||
|
self.path = path
|
||
|
self.filename = os.path.basename(path)
|
||
|
self.filesize = os.path.getsize(path)
|
||
|
|
||
|
def __repr__(self):
|
||
|
return '{:s} - {:d}'.format(self.path, self.filesize)
|
||
|
|
||
|
def make_row(self):
|
||
|
row = ET.Element('tr')
|
||
|
name_element = ET.SubElement(row, 'td')
|
||
|
name_element.text = self.filename
|
||
|
size_element = ET.SubElement(row, 'td')
|
||
|
size_element.text = '{:d}'.format(self.filesize)
|
||
|
return row
|
||
|
|
||
|
|
||
|
def main():
|
||
|
|
||
|
if len(sys.argv) < 2:
|
||
|
print 'ERROR: not enough arguments specified'
|
||
|
return 1
|
||
|
|
||
|
contents = []
|
||
|
|
||
|
base_path = sys.argv[1]
|
||
|
for item in os.listdir(base_path):
|
||
|
full_path = os.path.join(base_path, item)
|
||
|
|
||
|
if os.path.isfile(full_path):
|
||
|
contents.append(File(full_path))
|
||
|
elif os.path.isdir(full_path):
|
||
|
contents.append(Directory(full_path))
|
||
|
|
||
|
root = ET.Element('html')
|
||
|
body_tag = ET.SubElement(root, 'body')
|
||
|
table_tag = ET.SubElement(root, 'table')
|
||
|
|
||
|
row = ET.Element('tr')
|
||
|
head1 = ET.SubElement(row, 'th')
|
||
|
head1.text = 'Name'
|
||
|
head2 = ET.SubElement(row, 'th')
|
||
|
head2.text = 'Size'
|
||
|
table_tag.append(row)
|
||
|
|
||
|
for item in contents:
|
||
|
# poly freakin morphism
|
||
|
table_tag.append(item.make_row())
|
||
|
|
||
|
ET.dump(root)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
sys.exit(main())
|
||
Adding file share code that I started working on. It depends on the lxml package.