Created
November 29, 2016 11:53
-
-
Save DrSensor/9922d8213cbd378c5f8e1ea0ffd2395e to your computer and use it in GitHub Desktop.
Python file module with capability of CLI and REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import module1, module2 | |
from bottle import run | |
run(host='localhost', port='8080') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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