-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_instrument.py
48 lines (40 loc) · 1.68 KB
/
new_instrument.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
Create a new temperature controller instrument
Usage:
new_instrument.py INSTRUMENT_NAME
Arguments:
INSTRUMENT_NAME Name of new instrument
Options:
-h --help Show this screen
'''
import os
from chemios_tc.utils import package_path
from shutil import copyfile
from fileinput import FileInput
from docopt import docopt
def new_instrument(instrument_name):
'''Create a new instrument based on the base files'''
#Change to chemios_tc directory and copy base file
working_path = package_path()
old_path = os.getcwd()
os.chdir(working_path)
#Create new filename
if instrument_name.rstrip('.py') == instrument_name:
instrument_filename = '_' + instrument_name.lower().replace(' ','_').replace('-', '_') + '.py'
copyfile('chemios_tc_base.py', instrument_filename)
#Open the file and change the import statement and the name of the class
class_name = instrument_name.title().replace(' ', '') + '(TemperatureControllers)'
with FileInput(instrument_filename, inplace=True) as file:
for line in file:
print(line.replace('TemperatureControllers(ABC)', class_name).replace('from abc import ABC', 'from .chemios_tc_base import TemperatureControllers'),
end='')
#Add class to package level in __init__.py
import_statement = "from .{} import {}".format(instrument_filename.rstrip('.py'),
instrument_name.title().replace(' ', ''))
with open('__init__.py', 'a') as f:
f.write(import_statement)
#Change back to the original directory
os.chdir(old_path)
args = docopt(__doc__)
if args['INSTRUMENT_NAME']:
new_instrument(args['INSTRUMENT_NAME'])