-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlauncher.py
executable file
·141 lines (106 loc) · 5.17 KB
/
launcher.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# TadawulStocks v 0.1 - launcher.py
# Copyright (C) <2014> mad_dev(A'mmer Almadani)
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL A'MMER ALMADANI BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Report any issues with this script to <[email protected]>
import npyscreen
import curses
from subprocess import Popen
from sys import exit
import threading
from collections import defaultdict
import ConfigParser
import time
from market.TadawulStocks import *
import re
__author__ = 'Amer Almadani'
__email__ = '[email protected]'
class ActionControllerSearch(npyscreen.ActionControllerSimple):
def create(self):
npyscreen.setTheme(npyscreen.Themes.TransparentThemeLightText)
self.add_action('^/.*', self.write2main, True)
def action(self, command, msg):
current = time.strftime('%c')
value = [current]
self.ThreadOpen(command).start()
value.append(msg)
self.parent.wMain.values = value
def write2main(self, command_line, prox, live):
self.parent.value.set_filter(command_line[1:])
self.parent.wMain.values = ['Command must start with /']
emulator = Conf().get('SYS', 'emulator')
command = Conf().get('SYS', 'command')
action_geo = Conf().get('ACTION', 'emulator_geo_title')
main_geo = Conf().get('MAIN', 'emulator_geo_title')
view_geo = Conf().get('VIEW', 'emulator_geo_title')
feed_geo = Conf().get('FEED', 'emulator_geo_title')
if str(command_line) == '/help':
self.parent.wMain.values = ["refresh: Launch the portfolio refresher(n); n = 10 sec",
"action: Launch the buy and sell window",
"my: Launch your portfolio view",
"stock: Show all stocks",
"fetch: Fetch prices from Tadawul.com.sa",
"log_sell: Show the sell log",
"log_buy: Show the buy log",]
elif str(command_line) == '/action':
self.action('''%s %s %s "python action.py"''' %(emulator, action_geo, command), 'Action has been started. You can buy and sell from it')
elif str(command_line) == '/refresh':
self.action('''%s %s %s "python -c 'from refresh import run; run()'"''' %(emulator, main_geo, command), 'Your portfolio is being refreshed every 10 seconds')
elif str(command_line) == '/my':
self.action('''%s %s %s "python view.py"''' %(emulator, view_geo, command), 'Your portfolio view has started. Issue fetch command to add new prices')
elif str(command_line) == '/fetch':
self.action('''%s %s %s "python feeder.py"''' %(emulator, feed_geo, command), 'Fetching data...')
elif str(command_line) == '/log_sell':
log_entry = [line.rstrip('\n') for line in open('log/sell.log')]
self.parent.wMain.values = log_entry
elif str(command_line) == '/log_buy':
log_entry = [line.rstrip('\n') for line in open('log/buy.log')]
self.parent.wMain.values = log_entry
elif str(command_line) == '/stock':
self.parent.wMain.values = GetList('All')
elif str(command_line) == '/exit':
exit('Goodbye!')
self.parent.wMain.display()
class ThreadOpen(threading.Thread):
def __init__(self, cmd):
threading.Thread.__init__(self)
self.cmd = cmd
def run(self):
Popen(self.cmd, shell=True)
class FmSearchActive(npyscreen.FormMuttActiveTraditional):
ACTION_CONTROLLER = ActionControllerSearch
class LaunchApp(npyscreen.NPSApp):
def main(self):
F = FmSearchActive()
#Thanks to N.Cole
del F.wMain.handlers[ord('l')] #remove 'l' as find
F.wMain.handlers.update({"^F": F.wMain.h_set_filter}) #add CTRL-F
F.wStatus1.value = "LOG"
F.wStatus2.value = "COMMAND"
F.wMain.values = ['Command must start with /','/help for commands']
F.edit()
if __name__ == "__main__":
def Conf():
config = ConfigParser.ConfigParser()
config.read('conf/info.conf')
return config
App = LaunchApp()
try:
App.run()
except KeyboardInterrupt:
print("Goodbye")