# Path: phononweb/phononweb.py
# def open_file_phononwebsite(filename,port=8000,
#                             website="http://henriquemiranda.github.io/phononwebsite",
#                             host="localhost"):
#     """
#     take a file, detect the type and open it on the phonon website
#     """
#     import webbrowser
#     import signal
#     import sys
# 
#     #python 2 and 3 http server
#     try:
#         from http.server import HTTPServer, SimpleHTTPRequestHandler
#     except ImportError:
#         from BaseHTTPServer import HTTPServer
#         # python 2 requires internal implementation
#         from SimpleHTTPServer import SimpleHTTPRequestHandler
# 
#     #create threads python
#     from threading import Thread
# 
#     if ".json" in filename:
#         filetype = "json"
#     elif ".yaml" in filename:
#         filetype = "yaml"
#     else: 
#         filetype = "rest"
# 
#     # Add CORS header to the website
#     class CORSRequestHandler (SimpleHTTPRequestHandler):
#         def end_headers (self):
#             self.send_header('Access-Control-Allow-Origin', website)
#             SimpleHTTPRequestHandler.end_headers(self)
#         def log_message(self, format, *args):
#             return
# 
#     # Quit application when SIGINT is received
#     def signal_handler(signal, frame):
#         sys.exit(0)
# 
#     #initialize http server thread
#     print('Starting HTTP server...')
#     try:
#         server = HTTPServer(('', port), CORSRequestHandler)
#     except OSError:
#         server = HTTPServer(('', port+1), CORSRequestHandler)
#         
#     server.url = 'http://{}:{}'.format(host,server.server_port)
#     t = Thread(target=server.serve_forever)
#     t.daemon = True
#     t.start()
# 
#     #open website with the file
#     url_filename = 'http://{}:{}/{}'.format(host,server.server_port,filename)
#     url = '%s/phonon.html?%s=%s'%(website,filetype,url_filename)
#     webbrowser.open_new(url)
# 
#     print('Press Ctrl+C to terminate HTTP server')
#     signal.signal(signal.SIGINT, signal_handler)
#     signal.pause()


import argparse
from phononweb.phononweb import open_file_phononwebsite
#!/usr/bin/env python 
# Copyright (c) 2017, Henrique Miranda
# All rights reserved.
#
# This file is part of the phononwebsite project
#
"""
Read a local file with the phononwebsite
"""

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='open a file in the phononwebsite')
    parser.add_argument('filename', help='name of the file to open')
    parser.add_argument('-w','--website', default="http://henriquemiranda.github.io/phononwebsite",
                                   help='the website to use')
    args = parser.parse_args()


# Path: phononweb/phononweb.py
# def open_file_phononwebsite(filename,port=8000,
#                             website="http://henriquemiranda.github.io/phononwebsite",
#                             host="localhost"):
#     """
#     take a file, detect the type and open it on the phonon website
#     """
#     import webbrowser
#     import signal
#     import sys
# 
#     #python 2 and 3 http server
#     try:
#         from http.server import HTTPServer, SimpleHTTPRequestHandler
#     except ImportError:
#         from BaseHTTPServer import HTTPServer
#         # python 2 requires internal implementation
#         from SimpleHTTPServer import SimpleHTTPRequestHandler
# 
#     #create threads python
#     from threading import Thread
# 
#     if ".json" in filename:
#         filetype = "json"
#     elif ".yaml" in filename:
#         filetype = "yaml"
#     else: 
#         filetype = "rest"
# 
#     # Add CORS header to the website
#     class CORSRequestHandler (SimpleHTTPRequestHandler):
#         def end_headers (self):
#             self.send_header('Access-Control-Allow-Origin', website)
#             SimpleHTTPRequestHandler.end_headers(self)
#         def log_message(self, format, *args):
#             return
# 
#     # Quit application when SIGINT is received
#     def signal_handler(signal, frame):
#         sys.exit(0)
# 
#     #initialize http server thread
#     print('Starting HTTP server...')
#     try:
#         server = HTTPServer(('', port), CORSRequestHandler)
#     except OSError:
#         server = HTTPServer(('', port+1), CORSRequestHandler)
#         
#     server.url = 'http://{}:{}'.format(host,server.server_port)
#     t = Thread(target=server.serve_forever)
#     t.daemon = True
#     t.start()
# 
#     #open website with the file
#     url_filename = 'http://{}:{}/{}'.format(host,server.server_port,filename)
#     url = '%s/phonon.html?%s=%s'%(website,filetype,url_filename)
#     webbrowser.open_new(url)
# 
#     print('Press Ctrl+C to terminate HTTP server')
#     signal.signal(signal.SIGINT, signal_handler)
#     signal.pause()


# Path: scripts/phononwebsite.py
import argparse
from phononweb.phononweb import open_file_phononwebsite

#!/usr/bin/env python 
# Copyright (c) 2017, Henrique Miranda
# All rights reserved.
#
# This file is part of the phononwebsite project
#
"""
Read a local file with the phononwebsite
"""

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='open a file in the phononwebsite')
    parser.add_argument('filename', help='name of the file to open')
    parser.add_argument('-w','--website', default="http://henriquemiranda.github.io/phononwebsite",
                                   help='the website to use')
    args = parser.parse_args()


    open_file_phononwebsite(args.filename,website=args.website)