Skip to content

Instantly share code, notes, and snippets.

@dtranhuusm
Created October 17, 2018 04:35
Show Gist options
  • Save dtranhuusm/7437eeb6a7b291e951765e0e9a0554a7 to your computer and use it in GitHub Desktop.
Save dtranhuusm/7437eeb6a7b291e951765e0e9a0554a7 to your computer and use it in GitHub Desktop.
Make static html site browsable in Sharepoint by renaming the files with extension aspx and also the content of the files
import os
import re
import traceback
import sys
import logging
logger = logging.getLogger(__name__)
def explore_path(path, params):
""" This is the function that walks folder tree looking for files to process.
* path: is the path of the folder tree
* params: is a dictionary requiring the following keys:
- ext: is the file extension
- method: is the function used to process a file
- encoding: is the additional parameter required by method function
"""
if 'max_errors' in params:
max_errors = params['max_errors']
else:
max_errors = 20
ext_length = -1*len(params['ext'])
path = os.path.abspath(path)
logger.info('Processing %s', path)
lst = os.listdir(path)
lst.sort()
for fname in lst:
if os.path.isfile(os.path.join(path, fname)) and fname[ext_length:] == params['ext']:
try:
params['method'](os.path.join(path, fname), params)
except:
tb = traceback.format_exc()
print(tb)
if 'errors' in params:
params['errors'][os.path.join(path, fname)] = tb
else:
errors = dict()
errors[os.path.join(path, fname)] = tb
params['errors'] = errors
if len(params['errors']) > max_errors:
raise SystemError("Too many errors!")
elif os.path.isdir(os.path.join(path, fname)):
explore_path(os.path.join(path, fname), params)
def process_html(fpath, params):
fname = os.path.basename(fpath)
dname = os.path.dirname(fpath)
logger.info("Processing: %s", fpath)
with open(fpath, 'r') as fp:
content = fp.read()
# rename the file and replaces '.html' with '.aspx' in the content
with open(os.path.join(dname, fname[:-4]+'aspx'),'w') as fp:
fp.write(content.replace('.html','.aspx'))
os.remove(fpath)
if __name__=="__main__":
params={'ext': 'html', 'encoding': 'latin1', 'method':process_html, 'max_errors': 5}
if os.path.isdir(sys.argv[1]):
explore_path(sys.argv[1], params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment