Skip to content

Instantly share code, notes, and snippets.

@DrSensor
Created November 29, 2016 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrSensor/9922d8213cbd378c5f8e1ea0ffd2395e to your computer and use it in GitHub Desktop.
Save DrSensor/9922d8213cbd378c5f8e1ea0ffd2395e to your computer and use it in GitHub Desktop.
Python file module with capability of CLI and REST API
#! /usr/bin/env python
import module1, module2
from bottle import run
run(host='localhost', port='8080')
#! /usr/bin/env python
from bottle import route
from os import listdir
import argparse
@route('/documents/<filename>/<text>', method=['PUT','POST'])
def fill_file(filename, text):
with open('data/'+filename, 'ab') as f:
f.write(text)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='write FAIL at end of all file in folder')
parser.add_argument('folder', type=str,
help='folder of the file that want to be writen')
parser.add_argument('-t', '--text', type=str, default="FAIL",
help='text that want to be writed')
args = parser.parse_args()
files = listdir(args.folder)
for f in files:
fill_file(f, args.text)
#! /usr/bin/env python
from bottle import route
from os import listdir
import argparse
@route('/documents/<filename>', method=['GET'])
def read_file(filename):
with open('data/' + filename, 'rb') as f:
content = f.read()
return {
'filename': filename,
'content': content
}
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='read content of all files')
parser.add_argument('folder', type=str,
help='folder of the file that want to be read')
args = parser.parse_args()
files = listdir(args.folder)
for f in files:
document = read_file(f)
print document['filename'] + ': ' + document['content']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment