-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnq.py
More file actions
executable file
·153 lines (139 loc) · 4.91 KB
/
nq.py
File metadata and controls
executable file
·153 lines (139 loc) · 4.91 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python
#
# Copyright (c) 2011 Mark D. Pesce
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# This is NQ, the enqueueing module
# This module receives all of the things to be shared
# And keeps them in its own database
# So that when front end clients ask for them
# Well, here they are!
#
# Always call the send_shared() function
# And the rest of the work happens internally here
import os
import threading
import sqlite3
import sharer # module that actually dispatches the thingy to be shared
dbfname = 'nq.db' # Name of the NQ database, sensibly
semi = threading.Semaphore() # Start us up, with (we hope) a global semaphore
# Returns True if we are running on Android - use absolute paths
def isAndroid():
try:
import android
#print "Android!"
return True
except:
return False
def getDBName():
if isAndroid():
return "/sdcard/sl4a/scripts/db/v2/" + dbfname
else:
return 'db' + os.sep + dbfname
def open_listener_db():
# We want to open the database of listened-to stuffs
# We need to use the semaphore to make sure we really can do that
#print('Acquiring semaphore...')
semi.acquire()
#print('Acquired semaphore')
try:
testy = os.stat(getDBName())
except OSError: # No directory, apparently
if (isAndroid()):
try:
os.mkdir('/sdcard/sl4a/scripts/db')
print 'Created Android db directory'
except OSError:
#print 'Directory db already exists, will not create it'
x = 1 # placeholder
else:
try:
os.mkdir('db')
print('Created db directory')
except OSError:
#print('Directory db already exists, will not create it')
x = 2 # placeholder
# Try to connect to the plexbase.
# If we can't, we need to create it.
# We return a connection object thingy
try:
#print('Trying to connect')
connector = sqlite3.connect(getDBName())
#print('Successful connection')
# Do we know if we have the correct table in this database?
# Or any tables at all?
connector.execute('''create table if not exists shared (msgid text, type text, pluid text, timestamp text, data text)''')
except:
print('Some sort of exception connecting to listenbase')
connector = None
return connector
def close_listener_db(connector):
connector.commit()
connector.close()
semi.release()
#print('Semaphore released')
def send_shared(msgid, type, pluid, timestamp, data):
print("We want to share to something!")
print msgid, type, timestamp, data
connector = open_listener_db()
if (connector != None):
connector.execute('insert into shared values (?,?,?,?,?)', (msgid, type, pluid, timestamp, data))
close_listener_db(connector)
# Our work has just barely begun
# This is where we actually map a message type to a particular sharer.
# We should be prepared to send it off to the appropriate sharer, once we reckon which sharer that is.
# Now when this gets all fancy-like, it will handle failure recovery, etc.
sharer.share(msgid, type, pluid, timestamp, data)
def get_shared(index):
#print("We are getting index " + str(index))
#the_index = int(index)
connector = open_listener_db()
if (connector != None):
curs = connector.cursor()
curs.execute('select * from shared where ROWID=?',(index,))
result = curs.fetchone()
# Here we would take the row data and put it into retobj
retobj = None
close_listener_db(connector)
return retobj
else:
return None
def size_listened(index):
#print("We are getting the size")
the_index = int(index)
connector = open_listener_db()
if (connector != None):
curs = connector.cursor()
curs.execute('select * from shared where ROWID=?',(the_index,))
result = curs.fetchone()
# Need a size calcualtion right here
close_listener_db(connector)
return retsize
else:
return 0
def count_listened():
#print("count_listened")
connector = open_listener_db()
if (connector != None):
curs = connector.cursor()
curs.execute('select ROWID from shared') # Cheap and fast
count = len(curs.fetchall())
close_listener_db(connector)
return count