Beispiel #1
0
def main():
	t1 = task("http://www.laurentluce.com/posts/python-threads-synchronization-locks-rlocks-semaphores-conditions-events-and-queues/")
	t2 = task("http://stackoverflow.com/questions/15651128/in-this-semaphore-example-is-it-necessary-to-lock-for-refill-and-buy")
	t3 = task("http://bbs.byr.cn/")
	event = Event()
	tasks = TaskQueue(event)
	pages = TaskQueue(None)
	tasks.add(t1)
	tasks.add(t2)
	tasks.add(t3)

	taskLock = BoundedSemaphore(tasks.numOfNewTasks)
	pageLock = BoundedSemaphore(1)
	f = open("test.txt",'w')
	Connector0 = Connector(tasks,taskLock,pages,pageLock,event,'',f, 3000)
	Connector1 = Connector(tasks,taskLock,pages,pageLock,event,'',f, 3001)
	Connector0.start()
	Connector1.start()

	Crawler0 = Crawler('',3000)
	Crawler1 = Crawler('',3001)

	Crawler0.start()
	Crawler1.start()

	Connector1.join()
	Connector0.join()
	Crawler0.join()
	Crawler1.join()
	f.close()
 def __init__(self):
     self.tasks = []
     self.server = "http://localhost:8888"
     self.getTaskUrl = "/api/getTask/"
     self.c = Connector()
     self.data = ""
     self.receipt_done = dict()
Beispiel #3
0
class MyWindow(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None, mode='online'):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)

        self.setWindowIcon(QIcon('app_icon.png'))

        QtCore.QObject.connect(self.speedSlider, QtCore.SIGNAL("valueChanged (int)"), self.MyCube,
                               QtCore.SLOT("updateSpeed(int)"))
        QtCore.QObject.connect(self.pushButton_Spin, QtCore.SIGNAL("clicked(bool)"), self.MyCube,
                               QtCore.SLOT("spin(bool)"))
        QtCore.QObject.connect(self.pushButton_X, QtCore.SIGNAL("clicked(bool)"), self.MyCube,
                               QtCore.SLOT("moveX(bool)"))
        QtCore.QObject.connect(self.pushButton_Y, QtCore.SIGNAL("clicked(bool)"), self.MyCube,
                               QtCore.SLOT("moveY(bool)"))
        QtCore.QObject.connect(self.pushButton_Z, QtCore.SIGNAL("clicked(bool)"), self.MyCube,
                               QtCore.SLOT("moveZ(bool)"))

        self.myConnector = Connector(self, mode=mode)



        self.timer = QtCore.QTimer()
        QtCore.QObject.connect(self.timer, QtCore.SIGNAL("timeout()"), self.myConnector.update)

        self.timer.start(50)  # cycle time in ms (50)

    def close(self):
        self.myConnector.close()
Beispiel #4
0
def fetch_day(date):
    logger.info(
        "START, extracting dividends, date={}".format(str(date))
    )
    url = util.get_endpoint_url()
    conn = Connector(url)
    cnt = 0
    for div in conn.get_daily_divs(date):
        try:
            # call REST API
            
            logger.debug(
                "Success processing payout, security=" + str(div)
            )

            cnt += 1
        except Exception as e:
            logger.error(
                "Error processing payout, security={}, "
                "messsage={}".format(
                    div.security_symbol,
                    str(e))
            )

    logger.info(
        "END, extracting dividends, date={}, div_processed={}".format(
            str(date), cnt)
    )
Beispiel #5
0
    def __init__(self):
        self.actions = list()
        self.confDir = os.path.expanduser("~") + "/Kam1n0"
        if not os.path.exists(self.confDir):
            os.makedirs(self.confDir)
        self.loadIcons()

        self.Kconf = self.getConfiguration()
        if self.Kconf is None:
            self.connector = Connector()
            self.Kconf = {}
            self.Kconf['cnns'] = {}
            cnnInfo = self.connector.toMap()
            self.Kconf['cnns'][cnnInfo['key']] = cnnInfo
            self.Kconf['default-cnn'] = cnnInfo['key']
            self.setConfiguration(self.Kconf)
        else:
            if self.Kconf['default-cnn'] is None:
                self.connector = None
            else:
                cnnInfo = self.Kconf['cnns'][self.Kconf['default-cnn']]
                self.connector = Connector(
                    protocol=cnnInfo['protocol'],
                    server=cnnInfo['server'],
                    port=cnnInfo['port'],
                    un=cnnInfo['un'],
                    pw=cnnInfo['pw']
                )

        global hooks
        hooks = Hooks()
        re = hooks.hook()
Beispiel #6
0
 def exportTo(self, connector: Connector):
     try:
         cur = self.conn.cursor()
         data = self._getAllTablesData(cur)
         connector.importData(data)
     except Exception as ex:
         print(ex)
 def __init__(self, contract_address, abi_json='erc20.json'):
     self.net = Connector()
     with open(abi_json, 'r') as abi:
         self.contract_abi = json.load(abi)
     self.contract = self.net.w3.eth.contract(
         address=Web3.toChecksumAddress(contract_address),
         abi=self.contract_abi)
Beispiel #8
0
class Updater:

    def __init__(self):
        self.dest_doc = '/mnt/results/twitterInfo.json'
        self.conn = Connector()

    def update_document(self):


        try:
            docs = self.conn.select_full_docs(self.conn.twitterdb_demo_results_pub)
            with open(self.dest_doc, 'w') as dest:
                for doc in docs:
                    dest.write(doc)
                    dest.write('\n')

        except BaseException:
            try:
                docs = self.conn.select_full_docs(self.conn.twitterdb0_demo_results_pub)
                with open(self.dest_doc, 'w') as dest:
                    for doc in docs:
                        dest.write(doc)
                        dest.write('\n')
            except BaseException:
                docs = self.conn.select_full_docs(self.conn.twitterdb1_demo_results_pub)
                with open(self.dest_doc, 'w') as dest:
                    for doc in docs:
                        dest.write(doc)
                        dest.write('\n')

        return
Beispiel #9
0
    def Query(self, receipt, receipt_date):

        res = None
        while res is None:
            if not self.c.session_valid:
                del self.c
                self.c = Connector()
                self.c.imgRslr.reportFail(self.c.imgCode, self.c.imgSHA)
                self.c.resolveImg()
            #log.info('[{}]Get Image {}:{}'.format(self.c.res.reason, self.c.tmp_file, self.c.imgCode))
            #log.info('{} and {}'.format(receipt , receipt_date))
            self.c.setPostData(receipt, receipt_date)
            self.c.postForm(self.c.postPath)
            #log.info('[{} {}]Post data'.format(self.c.res.status,self.c.res.reason))
            res = self.c.getInfo()

        if not self.c.info:
            return False
        else:
            log.info("===[Query Result]===")
            receipt = self.c.info
            receipt['money'] = receipt['money'].replace(',', '')
            for k, r in receipt.iteritems():
                print k + ":\t\t" + r

            self.receipt_done[self.c.info['id']] = (receipt['date'],
                                                    receipt['money'],
                                                    receipt['taxid'])
            return True
Beispiel #10
0
	def __check_connection (self):
		# Get a new connection object to lemonstand API
		c = Connector()
		identity = c.get_identity(self.config.api_host, self.config.api_access)
		connection = c.s3_connection(identity);
		self.connection = connection

		return
Beispiel #11
0
 def __init__(self):
     self.tasks = []
     self.server = "http://127.0.0.1:8000"
     self.getTaskUrl = "/api/getTask/"
     self.c = Connector()
     self.data = ""
     self.testsim = []
     self.receipt_done = dict()
Beispiel #12
0
    def run(self):
        ## get data from RabbitMQ
        getDataObj = Connector(self.logger, self.config)
        data = getDataObj.run()

        ## release lock
        self.lockObj.release()

        return(True)
Beispiel #13
0
def get_connection (configuration):
	# Start 
	print(Back.YELLOW + Fore.BLACK + 'LemonSync is initiating connection...' + Style.RESET_ALL)

	# Establish a connection to the LemonStand API, and then to s3
	c = Connector()
	identity = c.get_identity(configuration.api_host, configuration.api_access)
	connection = c.s3_connection(identity);

	return connection
Beispiel #14
0
def get_connection (configuration):
	# Start 
	print(Back.YELLOW + Fore.BLACK + 'LemonSync is initiating connection...' + Style.RESET_ALL)

	# Establish a connection to the LemonStand API, and then to s3
	c = Connector()
	identity = c.get_identity(configuration.api_host, configuration.store_host, configuration.api_access)
	connection = c.s3_connection(identity);

	return connection
Beispiel #15
0
 def createIndexProgressForm(self, funcs, cnn=None):
     if cnn is None:
         cnn = self.connector
     form = IndexProgressForm(cnn, funcs)
     form.Execute()
     code = form.ErrorCode
     content = form.Content
     form.Free()
     if code > OK:
         Connector.getCodeDescription(code, content)
         idaapi.warning("Connection failed. Please review your connection. \n \"%s\"" % content)
         self.openConnectionManagmentForm(ctx=None)
Beispiel #16
0
 def writeButton(self):
     self.infoLbl.set(Local('Please wait...'))
     self.root.update()
     try:
         self.validate()
         conf = self.gatherData()
         Connector.write(self.port.get(), conf)
         tkMessageBox.showinfo('Info', Local('Success!'))
     except Exception as e:
         tkMessageBox.showerror(Local('Error'), str(e))
         traceback.print_exc()
     finally:
         self.infoLbl.set('')
Beispiel #17
0
def connection():
    received_resolution_width = 1200
    received_resolution_height = 800

    connector = Connector(received_resolution_width,
                          received_resolution_height)
    dummy_screen_resolution_msg = to_screen_resolution_msg(2570, 2570)
    received_screen_resolution_msg = to_screen_resolution_msg(
        received_resolution_width, received_resolution_height)

    connector.exchange_screen_resolution(dummy_screen_resolution_msg,
                                         received_screen_resolution_msg)
    return connector
    def __init__(self,
                 contract_name,
                 contracts_folder=contracts_directory,
                 provider=infura_provider):
        Connector.__init__(self, provider)
        self.contract_file = f"{contracts_folder + contract_name}.sol"

        with open(self.contract_file, 'r') as contract_code:
            self.source_code = contract_code.read()

        self.compiled = compile_source(self.source_code)

        self.abi = self.compiled[f'<stdin>:{contract_name}']['abi']
        self.bin = self.compiled[f'<stdin>:{contract_name}']['bin']
class Contract():
    def __init__(self, contract_address, abi_json='erc20.json'):
        self.net = Connector()
        with open(abi_json, 'r') as abi:
            self.contract_abi = json.load(abi)
        self.contract = self.net.w3.eth.contract(
            address=Web3.toChecksumAddress(contract_address),
            abi=self.contract_abi)

    def function(self,
                 function,
                 *args,
                 sender=senderAddress,
                 chain=0,
                 gas=0,
                 gasPrice=0,
                 nonce=0,
                 key=pk,
                 call=False):

        func = self.contract.functions[function]

        if call:
            self.output = func(*args).call()
            return self.output
        else:
            sender = Web3.toChecksumAddress(sender)
            if chain is 0: chain = self.net.chainId()
            if gas is 0: gas = func(*args).estimateGas()
            if gasPrice is 0: gasPrice = self.net.gasPrice()
            if nonce is 0: nonce = self.net.nonce(sender)
            txn = func(*args).buildTransaction({
                'chainId': chain,
                'gas': gas,
                'gasPrice': gasPrice,
                'nonce': nonce,
            })
            signed = self.net.w3.eth.account.signTransaction(txn, key)
            txn_hash = self.net.w3.eth.sendRawTransaction(
                signed.rawTransaction)
            self.txn_receipt = self.net.w3.eth.waitForTransactionReceipt(
                txn_hash)
            return self.txn_receipt

    def call(self, function, *args, sender=senderAddress):

        func = self.contract.functions[function]
        self.output = func(*args).call()
        return self.output
Beispiel #20
0
    def __init__(self):
        self.logger.debug('PLTEConnector Init')
        Connector.__init__(self, PLTEManager.getInstance())

        self.plteQueId = int(ConfManager.getInstance().getConfigData(
            ConfManager.MSGQUEUE_INFO, "PLTEIB"))
        try:
            maxQSize = ConfManager.getInstance().getConfigData(
                ConfManager.MSGQUEUE_INFO, "MAX_QUEUE_SIZE")
            self.plteQueue = sysv_ipc.MessageQueue(
                self.plteQueId, max_message_size=int(maxQSize))

        except Exception as e:
            self.logger.error(
                "msgQueue Connection Failed.. PLTE QUEUE_ID[%d] SIZE[%s]" %
                (self.plteQueId, maxQSize))
Beispiel #21
0
    def Query(self, receipt, receipt_date):

        res = None
        while res is None:

            if not self.c.session_valid:
                del self.c
                self.c = Connector()
                #self.c.imgRslr.reportFail(self.c.imgCode, self.c.imgSHA)
                self.c.resolveImg()
            log.info('[{}]Get Image {}:{}'.format(self.c.res.reason, self.c.tmp_file, self.c.imgCode))
            log.info('{} and {}'.format(receipt , receipt_date))
            self.c.setPostData(receipt, receipt_date )
            self.c.postForm( self.c.postPath )
            log.info('[{} {}]Post data'.format(self.c.res.status,self.c.res.reason))
            res = self.c.getInfo()

            with open("out.html" , "w") as outFd:
                outFd.write(self.c.body)

        if not self.c.info:
            return False
        else:
            print("===[Query Result]===")
            receipt = self.c.info
            receipt['money'] = receipt['money'].replace(',','')
            for k,r in receipt.iteritems():
                print k+":\t\t"+r

            self.receipt_done[self.c.info['id']] = (receipt['date'],receipt['money'],receipt['taxid'])
            return True
Beispiel #22
0
    def run(self):
        self.maxSentries = 15
        if len(Connector.getPorts()) == 0:
            tkMessageBox.showerror(Local('Error'),
                                   Local('Serial port not found'))
            os._exit(0)
        self.reload = False
        self.protScanTask = None
        self.tempWindow = None
        self.histWindow = None

        #window
        root = self.root = Tkinter.Tk()
        root.resizable(False, False)
        root.title('GSMguard Configuration v.' + appVersion)
        root.iconbitmap(resource_path('./gg-icon.ico'))

        fleft = ttk.Frame(root)
        fleft.pack(side='left', anchor='n')
        fright = ttk.LabelFrame(root, text=Local('Schedule'))
        fright.pack(side='right', anchor='n', padx=3, pady=3, fill='y')

        self.makeLocaleSettings(fleft)
        self.makePortSettings(fleft)
        self.makeMainSettings(fleft)
        self.makeScheduleSettings(fright)

        self.updPortList()
        root.mainloop()
        return self.reload
Beispiel #23
0
    def readButton(self):
        self.infoLbl.set(Local('Please wait...'))
        self.root.update()
        try:
            conf = Connector.read(self.port.get(), self.readEventHist.get(),
                                  self.readTempStat.get())
            self.spreadData(conf)

            if self.histWindow:
                self.histWindow.destroy()
                self.histWindow = None
            if self.tempWindow:
                self.tempWindow.destroy()
                self.tempWindow = None

            if self.readEventHist.get():
                self.histWindow = Tkinter.Toplevel()
                x = self.root.winfo_x()
                y = self.root.winfo_y() + self.root.winfo_height() + 40
                self.histWindow.geometry('+%d+%d' % (x, y))
                EventHistory(self.histWindow, conf['eventHist'])
            if self.readTempStat.get():
                self.tempWindow = Tkinter.Toplevel()
                x = self.root.winfo_x() + self.root.winfo_width() + 10
                y = self.root.winfo_y()
                self.tempWindow.geometry('+%d+%d' % (x, y))
                TempPlot(self.tempWindow, conf['tempHist'])

        except Exception as e:
            tkMessageBox.showerror(Local('Error'), str(e))
            traceback.print_exc()
        finally:
            self.infoLbl.set('')
Beispiel #24
0
    def openConnectionManagmentForm(self, ctx):
        form = ConnectionManagementForm(self)
        form.Execute()
        self.setConfiguration(self.Kconf)
        #print self.Kconf

        # update connection:
        if self.Kconf['default-cnn'] is not None:
            cnnInfo = self.Kconf['cnns'][self.Kconf['default-cnn']]
            self.connector = Connector(protocol=cnnInfo['protocol'],
                                       server=cnnInfo['server'],
                                       port=cnnInfo['port'],
                                       un=cnnInfo['un'],
                                       pw=cnnInfo['pw'])
            self.connector.reset()
        else:
            self.connector = None
class BusinessSessionAbs:
    __connector = Connector()

    def __init__(self):
        print("BusinessSessionAbs.__init__")
        #self.__connector.runQuery( "SELECT crm_info_id FROM crm_information Limit 10" )
        Session = sessionmaker(bind=self.__connector.getEngine())
        self.session = Session()
 def insertLandmarks(self):
     connection = Connector()
     connection.makeConnection()
     writer=csv.writer(open("landmark_locations.csv",'a+'))
     train_path = 'dataset/final_train'
     landmark_dir_names = os.listdir(train_path)
     
     for landmark_dir_name in landmark_dir_names:
         dir_path = os.path.join(train_path, landmark_dir_name)
         full_path = str(dir_path) + "/details"
         with open(full_path) as f:
             content = f.read().splitlines()
         connection.insertIntoLandmark(str(landmark_dir_name), str(content[0]), str(content[1]), str(content[2]))
         data_row = []
         data_row.append(landmark_dir_name)
         data_row.append(content[0])
         data_row.append(content[1])
         writer.writerow(data_row)
         
         os.remove(full_path)
         temp_path = str(dir_path) + "/details~"
         if(os.path.isfile(temp_path)):
             os.remove(temp_path)
         
     connection.closeConnection()
Beispiel #27
0
 def workflow(self):
     
     import NtuaActivityStreamsEncoder
     
     from Connector import Connector
     message = Connector().ReceiveMessage()
     message = NtuaActivityStreamsEncoder.Decoder( message ).dictionary
     from UltraStupidTest import stupid
     message  = stupid(message)
     message = NtuaActivityStreamsEncoder.Encoder(author=message.findall(message.original,['author']),title=message['title'],
                                                  status=message['status'])
     
     from TwitterConnect import publishAtTwitter
     #This method listens at activity streams format
     publishAtTwitter(message)
     
     
     return 1
Beispiel #28
0
    def workflow(self):

        import NtuaActivityStreamsEncoder

        from Connector import Connector
        message = Connector().ReceiveMessage()
        message = NtuaActivityStreamsEncoder.Decoder(message).dictionary
        from UltraStupidTest import stupid
        message = stupid(message)
        message = NtuaActivityStreamsEncoder.Encoder(author=message.findall(
            message.original, ['author']),
                                                     title=message['title'],
                                                     status=message['status'])

        from TwitterConnect import publishAtTwitter
        #This method listens at activity streams format
        publishAtTwitter(message)

        return 1
Beispiel #29
0
class Outport(Port):
    def __init__(self, surface, x, y, r, data):
        super().__init__(surface, x, y, r, data)

        self.connector = Connector(self, self.surface)

    def move(self, x, y):
        self.surface.coords(self.port, x - self.r, y - self.r, x + self.r,
                            y + self.r)
        if self.connector.inport != None:
            self.surface.coords(self.connector.connector, x, y,
                                self.connector.secondX, self.connector.secondY)
        else:
            self.surface.coords(self.connector.connector, x, y, x, y)

    # When called, set its location to the given location
    def update(self, x, y):
        self.x = x
        self.y = y
        self.connector.updateFirst(x, y)
Beispiel #30
0
 def __init__(self,
              name="",
              account_number=0,
              balance=0,
              balance_limit=0,
              amount=0):
     self.connector = Connector()
     self.log = Logger()
     if amount > 0:
         self.name = name
         self.amount = amount
     elif type(name) == str and type(account_number) == int and type(
             balance_limit) == int:
         self.name = name
         self.account_number = account_number
         self.balance = balance
         self.balance_limit = balance_limit
         self.amount = amount
     else:
         self.log.log_message('ICPT')
         pass
class MyListener(StreamListener):

    def __init__(self):
        self.dp = DataProcessor()
        self.conf = Config()
        self.auth = tweepy.OAuthHandler(self.conf.consumer_key, self.conf.consumer_secret)
        self.auth.set_access_token(self.conf.access_token, self.conf.access_token_secret)
        self.conn = Connector()

    def on_data(self, raw_data):
        try:
            self.conn.insert_raw_twitter(raw_data, self.conn.twitterdb_demo_pub)

            target_info = self.dp.get_geo_twi_target_info(raw_data)

            self.conn.insert_raw_twitter_result(target_info, self.conn.twitterdb_demo_results_pub)

            if target_info:
                self.conn.insert_raw_twitter_result(target_info, self.conn.twitterdb_demo_results_pub)

            return True

        except BaseException as e:
            print("Error on_data:%s" % str(e))
        return True

    def on_error(self, status_code):
        if status_code == 420:
            print("ERROR: Rate limit reached")
        print(status_code)
        return True

    def on_timeout(self):
        print("ERROR: Timeout...")
        return True  # Don't kill the stream
Beispiel #32
0
 def queryCompositionAnalysis(self, ctx):
     print "Generating binary surrogate for composition query..."
     surrogate = IDAutils.GetBinarySurrogate()
     if not surrogate:
         print "Cannot generate the binary surrogate"
         return 0
     code, content = self.connector.tryLoginAndExecute(
         queryFunction=self.connector.querySurrogateComposition,
         params=[
             surrogate, self.Kconf['default-threshold'],
             self.Kconf['default-topk']
         ])
     if code > OK:
         Connector.getCodeDescription(code, content)
         idaapi.warning(
             "Connection failed. Please review your connection. \n \"%s\"" %
             content)
         self.openConnectionManagmentForm(ctx=None)
     else:
         print "Opening composition view..."
         thread = Thread(target=self.connector.openCompositionPage)
         thread.start()
Beispiel #33
0
def format_data_to_json(raw_data):

    data = {'situations': [], 'actions': [], 'connectors': []}

    for child in raw_data:
        # TODO : Refactor style to config
        # TODO : Use pattern Strategy, Factory?
        if child.get('style', None) == "whiteSpace=wrap;html=1;aspect=fixed;":
            situation = Situation(child)
            if situation.is_valid:
                data['situations'].append(situation.as_json())

        elif child.get('style', None) == "ellipse;whiteSpace=wrap;html=1;":
            action = Action(child)
            if action.is_valid:
                data['actions'].append(action.as_json())

        elif child.get('style', None) is not None:
            connector = Connector(child)
            if connector.is_valid:
                data['connectors'].append(connector.as_json())

    json_data = json.dumps(data, ensure_ascii=False)
    return json_data
    def OnFormChange(self, fid):

        if fid == self.fvChooser.id:
            return 1

        if fid == self.chkSkipLib.id:
            if self.GetControlValue(self.chkSkipLib) == 1:
                self.SetControlValue(self.chkSearchAll, 0)
                self.SetControlValue(self.fvChooser, getNotLibIndex(allFuncs))
                self.activated = True

        if fid == self.chkSearchAll.id:
            if self.GetControlValue(self.chkSearchAll) == 1:
                self.SetControlValue(self.chkSkipLib, 0)
                self.SetControlValue(self.fvChooser, range(len(allFuncs)))
                self.activated = True

        if fid == self.dpServer:
            if len(self.Kconf['cnns'].keys()) > 0:
                # update self.cnn
                selectedInd = self.GetControlValue(self.dpServer)
                key = self.Kconf['cnns'].keys()[selectedInd]
                cnnInfo = self.Kconf['cnns'][key]
                self.cnn = Connector.Connector(protocol=cnnInfo['protocol'],
                                               server=cnnInfo['server'],
                                               port=cnnInfo['port'],
                                               un=cnnInfo['un'],
                                               pw=cnnInfo['pw'])

        if fid == self.txtSim:
            self.threshold = float(self.GetControlValue(self.txtSim))

        if fid == self.txtTopK:
            self.topk = int(self.GetControlValue(self.txtTopK))

        if fid == -2:
            funcInds = self.GetControlValue(self.fvChooser)
            global allFuncs
            if allFuncs is not None:
                funcs = [allFuncs[x] for x in funcInds]
                if len(funcs) < 1:
                    print "number of selected functions is less than 1"
                else:
                    self.funcs += funcs

        return 1
    def openConnectionManagmentForm(self, ctx):
        form = ConnectionManagementForm(self)
        form.Execute()
        self.setConfiguration(self.Kconf)

        # update connection:
        if self.Kconf['default-cnn'] is not None:
            cnnInfo = self.Kconf['cnns'][self.Kconf['default-cnn']]
            self.connector = Connector(
                protocol=cnnInfo['protocol'],
                server=cnnInfo['server'],
                port=cnnInfo['port'],
                un=cnnInfo['un'],
                pw=cnnInfo['pw']
            )
            self.connector.reset()
        else:
            self.connector = None
Beispiel #36
0
    def CreateCSV(self):
        writer=csv.writer(open("landmark_locations.csv",'a+'))
        writer.writerow(['Landmark', 'Longitude', 'Latitiude'])
        connection = Connector()
        connection.makeConnection()
        results = connection.retriveLandmarkLocation()

        for i in range(len(results)):
            data_row = []
            data_row.append(results[i].Name)
            data_row.append(results[i].location)
            data_row.append(results[i].location2)
            writer.writerow(data_row)
            
        connection.closeConnection()
Beispiel #37
0
class Crawler(object):
    def __init__(self):
        self.tasks = []
        self.dbmgr = DBManager()
        self.c = Connector()
        self.data = ""
    def getTasks(self, task):
        """ getTasks """
        pass

    def Query(self):


        #log.info('Connect to {}'.format(c.domain))

        res = None
        while res is None:

            if not self.c.session_valid:
                self.c.imgRslr.reportFail(self.c.imgCode, self.c.imgSHA)
                self.c.resolveImg()
            log.info('[{}]Get Image {}:{}'.format(self.c.res.reason, self.c.tmp_file, self.c.imgCode))
            log.info('{} and {}'.format(self.id_tag + str(self.id_num).zfill(8) , self.rec_date))
            self.c.setPostData( self.id_tag + str(self.id_num).zfill(8) , self.rec_date )
            self.c.postForm( self.c.postPath )
            log.info('[{} {}]Post data'.format(self.c.res.status,self.c.res.reason))
            res = self.c.getInfo()

            with open("out.html" , "w") as outFd:
                outFd.write(self.c.body)

        if not bool(self.c.info):
            print "No Record"
            return False

        print("===[Query Result]===")

        for k,r in self.c.info.iteritems():
            print k+":\t\t"+r
            if k == 'id':
                res_id = r
            if k == 'money':
                res_money = r.replace(',','')
            if k == 'date':
                res_date = r
            if k == 'taxid':
                res_taxid = r

        self.receipt[res_id] = (res_date,res_money,res_taxid)
        return True

    def Crawl(self , num , date, direct, distance):
        self.rec_id = num
        self.rec_date = date

        self.id_tag = self.rec_id[0:2]
        self.id_num = int(self.rec_id[2:10])
        self.receipt = {}
        cont = 0
        cont2 = 0

        while ( abs(int(self.id_num) - int(self.rec_id[2:10])) < distance):
            success = self.Query()
            if success is True :
                self.id_num += direct
                cont = 0
                cont2 = 0
            elif (success is False)  and (abs(cont2) < 3):
                print '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
                self.id_num += direct
                cont2 += direct
            elif  (success is False) and (cont==0) and (abs(cont2) >= 3):
                self.rec_date = TimeConvert(self.rec_date , direct)
                print "TimeConvert : "+ self.rec_date
                self.id_num -= cont2
                cont2 = 0
                cont+=1
            elif (success is False) and (cont!=0) and (abs(cont2) >= 3) :
                break
            else:
                log.error('main loop unusually break')
                sys.exit(1)
        return self.receipt
Beispiel #38
0
class CompatibilityMode_702 :
    """Use this class for old 702 Stubs compatibility only """
    FETCH_ALL = -1 # get all results in one call (USE CAUTIOUSLY)
    
    CLASSFACTORY         = "com.sefas.workflowservice.WorkflowFactory"
    
    METHOD_INIT_CONTEXT = "initContext"
    METHOD_TERMINATE_CONTEXT = "terminateContext"
    METHOD_START_WORKFLOW = "startWorkflow"
    METHOD_STOP_WORKFLOW = "stopWorkflow"
    METHOD_KILL_WORKFLOW = "killWorkflow"
    METHOD_CLEAN_JOB_TICKET_MONITORING = "cleanJobTicketMonitoring"
    METHOD_WAIT_WORKFLOW_END = "waitWorkflowEnd"
    ARGS = "args"


    def __init__ (self , 
                   encoding="UTF8" , # default encoding to UTF8
                   hostName="localhost" , 
                   port=29100 
                 ):
        """ constructor just store provided object information """
        self._host = hostName 
        self._port = port
        self._connection = None
        self._encoding = encoding
        self._param = {} 
        self._env = {} 
        self._paramSended = {} 
        self._envSended = {} 
        self._engineProp = None 
        self._envProp = None 
        self._islock = True 
        
    def connect(self) :
        """ connect to Smd Service """
        if (self._connection == None):
            self._connection = Connector(self._host , self._port , self._encoding, CompatibilityMode_702.CLASSFACTORY)
        return self._connection

    def disconnect(self):
        """ Disconnect from Smd Service """
        if (self._connection != None):
            self._connection.close() 
            self._connection = None
        return

    def initContext (self, workflowName):
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, CompatibilityMode_702.METHOD_INIT_CONTEXT)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, CompatibilityMode_702.ARGS)
        self._connection.marshall(Connector.SET_VALUE, CompatibilityMode_702.ARGS, workflowName)
        # proceed with call
        # return a workflow loader context id
        return self._connection.unmarshall(self._connection.marshall(Connector.CALL))

    def terminateContext (self, workflowContextId):
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, CompatibilityMode_702.METHOD_TERMINATE_CONTEXT)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, CompatibilityMode_702.ARGS)
        self._connection.marshall(Connector.SET_VALUE, CompatibilityMode_702.ARGS, workflowContextId)
        # proceed with call
        self._connection.marshall(Connector.CALL)

    def startWorkFlow (self, workflowContextId, xpdl, jobTicketData):
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, CompatibilityMode_702.METHOD_START_WORKFLOW)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, CompatibilityMode_702.ARGS)
        self._connection.marshall(Connector.SET_VALUE, CompatibilityMode_702.ARGS, workflowContextId)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, CompatibilityMode_702.ARGS)
        self._connection.marshall(Connector.SET_VALUE, CompatibilityMode_702.ARGS, xpdl)
        # populate parameters
        if jobTicketData != None:
            self._connection.marshall(Connector.DEF_STRING, CompatibilityMode_702.ARGS)
            self._connection.marshall(Connector.SET_VALUE, CompatibilityMode_702.ARGS, jobTicketData)
        # proceed with call
        # return a workflow running context id
        return self._connection.unmarshall(self._connection.marshall(Connector.CALL))

    def cleanJobTicketMonitoring (self, jobTicketId):
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, CompatibilityMode_702.METHOD_CLEAN_JOB_TICKET_MONITORING)
        if jobTicketId != None:
            self._connection.marshall(Connector.DEF_STRING, CompatibilityMode_702.ARGS)
            self._connection.marshall(Connector.SET_VALUE, CompatibilityMode_702.ARGS, jobTicketId)
        # proceed with call
        # return a workflow running context id
        return self._connection.unmarshall(self._connection.marshall(Connector.CALL))
        

    def stopWorkFlow (self, workflowContextId):
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, CompatibilityMode_702.METHOD_STOP_WORKFLOW)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, CompatibilityMode_702.ARGS)
        self._connection.marshall(Connector.SET_VALUE, CompatibilityMode_702.ARGS, workflowContextId)
        # populate parameters
        # proceed with call
        # return a workflow running context id
        return self._connection.unmarshall(self._connection.marshall(Connector.CALL))

    def killWorkFlow (self, workflowContextId):
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, CompatibilityMode_702.METHOD_KILL_WORKFLOW)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, CompatibilityMode_702.ARGS)
        self._connection.marshall(Connector.SET_VALUE, CompatibilityMode_702.ARGS, workflowContextId)
        # populate parameters
        # proceed with call
        # return a workflow running context id
        return self._connection.unmarshall(self._connection.marshall(Connector.CALL))

    def waitWorkFlowEnd (self, workflowRunningContextId, timeOut=50000):
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, CompatibilityMode_702.METHOD_WAIT_WORKFLOW_END)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, CompatibilityMode_702.ARGS)
        self._connection.marshall(Connector.SET_VALUE, CompatibilityMode_702.ARGS, workflowRunningContextId)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, CompatibilityMode_702.ARGS)
        self._connection.marshall(Connector.SET_VALUE, CompatibilityMode_702.ARGS, timeOut)
        # proceed with call
        return self._connection.marshall(Connector.CALL)
Beispiel #39
0
class JdbcClient:
    """Python Client interface to the SqlFactory SMD service """

    FETCH_ALL = -1 # get all results in one call (USE CAUTIOUSLY)
    CLASSFACTORY = 'com.sefas.gridclient.sqlservices.SqlExecFactory'
    
    METHOD_NAME_SET_DBSCHEMA = "setDBSchema"
    METHOD_NAME_SET_DBCATALOG = "setDBCatalog"

    PARAM_NAME_DBSCHEMA = "dbSchema"
    PARAM_NAME_DBCATALOG = "dbCatalog"

    def __init__ ( self , 
                   encoding="UTF8" , # default encoding to UTF8
                   hostName="localhost" ,
                   port=29100,
                   variableDict = None 
                 ):
        """ constructor just store provided object information """
        self._host = hostName 
        self._port = port
        self._connection = None
        self._encoding = encoding 
        self._variableDict = variableDict 

    def connect( self ) :
        """ connect to SqlFactory Service """
        if ( self._connection != None ):
            return
        self._connection = Connector( self._host , self._port , self._encoding,  self.CLASSFACTORY )

    def sqlInit( self ,  jdbcDriver ):
        """ load JdbcDriver return Nothing or exception on failure """ 
        self._connection.marshall( Connector.SET_METHOD , "sqlInit" ) 
        # populate parameters
        self._connection.marshall( Connector.DEF_STRING , "driver"  )
        self._connection.marshall( Connector.SET_VALUE , "driver" , jdbcDriver )
        self._connection.marshall( Connector.DEF_STRING , "encoding"  )
        self._connection.marshall( Connector.SET_VALUE , "encoding" , self._encoding )
        # proceed with call 
        self._connection.marshall( Connector.CALL )

    def sqlOpen( self ,  jdbcString , userid , passwd ):
        """ connect to requested database through the provided jdbc string """ 
        self._connection.marshall( Connector.SET_METHOD , "sqlOpen" ) 
        # populate parameters
        self._connection.marshall( Connector.DEF_STRING , "dbstring"  )
        self._connection.marshall( Connector.SET_VALUE , "dbstring" , jdbcString )
        self._connection.marshall( Connector.DEF_STRING , "user"  )
        self._connection.marshall( Connector.SET_VALUE , "user" , userid )
        self._connection.marshall( Connector.DEF_STRING , "passwd"  )
        self._connection.marshall( Connector.SET_VALUE , "passwd" , passwd )
        # proceed with call 
        ret = self._connection.marshall( Connector.CALL )
        # marshall the Db id back
        return int(self._connection.unmarshall( ret ))
        
    def execSql( self , dbHandle , statement ):
        """ execute provided sql statement on given dbHandle """ 
        self._connection.marshall( Connector.SET_METHOD , "execSql" ) 
        # populate parameters
        self._connection.marshall( Connector.DEF_INTEGER , "dbHandle"  )
        self._connection.marshall( Connector.SET_VALUE , "dbHandle" , str(dbHandle) )
        self._connection.marshall( Connector.DEF_STRING , "statement"  )
        self._connection.marshall( Connector.SET_VALUE , "statement" , statement )
        # proceed with call 
        ret = self._connection.marshall( Connector.CALL )
        # marshall cursor id back or None if cursorId is -1
        returned = int(self._connection.unmarshall( ret ))
        if returned == -1 :
            return None
        return returned
        
    def callSql( self , dbHandle , callfx , *args  ):
        """ call callfx stored procedure with providing argument list """ 
        # build the jdbc call string argument
        statement = '{call %s (' % callfx
        statementArgs = ''
        for arg in args :
            statement = ''.join( [ statement , '?' ] )
            statementArgs = ''.join( [ statementArgs , "'" , arg  , "'" ] )
            if arg != args[-1] :
                statement = ''.join( [ statement , ',' ] )
                statementArgs = ''.join( [ statementArgs , ',' ] )
            
        statement = ' '.join( [ statement , ') }' ] )
        self._connection.marshall( Connector.SET_METHOD , "callSql" ) 
        # populate parameters
        self._connection.marshall( Connector.DEF_INTEGER , "dbHandle"  )
        self._connection.marshall( Connector.SET_VALUE , "dbHandle" , str(dbHandle) )
        self._connection.marshall( Connector.DEF_STRING , "statement"  )
        self._connection.marshall( Connector.SET_VALUE , "statement" , statement )
        self._connection.marshall( Connector.DEF_STRING , "statementargs"  )
        self._connection.marshall( Connector.SET_VALUE , "statementargs" , statementArgs )
        # proceed with call 
        ret = self._connection.marshall( Connector.CALL )
        # marshall cursor id back or None if cursorId is -1
        returned = int(self._connection.unmarshall( ret ))
        if returned == -1 :
            return None
        return returned
        
    def sqlCursorClose( self ,  csHandle ):
        """ close a previously opened cursor on a given database handle for concurent querries""" 
        self._connection.marshall( Connector.SET_METHOD , "sqlCursorClose" ) 
        # populate parameters
        self._connection.marshall( Connector.DEF_INTEGER , "cshandle"  )
        self._connection.marshall( Connector.SET_VALUE , "cshandle" , csHandle )
        # proceed with call 
        self._connection.marshall( Connector.CALL )
        
    def sqlClose( self ,  dbHandle ):
        """ close a previously opened db connection""" 
        self._connection.marshall( Connector.SET_METHOD , "sqlClose" ) 
        # populate parameters
        self._connection.marshall( Connector.DEF_INTEGER , "dbhandle"  )
        self._connection.marshall( Connector.SET_VALUE , "dbhandle" , dbHandle )
        # proceed with call 
        self._connection.marshall( Connector.CALL )
        
    def sqlFetch( self ,  csHandle , nbrow = 1):
        """ close a previously opened cursor on a given database handle for concurent querries""" 
        self._connection.marshall( Connector.SET_METHOD , "sqlFetch" ) 
        # populate parameters
        self._connection.marshall( Connector.DEF_INTEGER , "cshandle"  )
        self._connection.marshall( Connector.SET_VALUE , "cshandle" , csHandle )
        self._connection.marshall( Connector.DEF_INTEGER , "nbrow"  )
        self._connection.marshall( Connector.SET_VALUE , "nbrow" , nbrow )
        # proceed with call 
        ret = self._connection.marshall( Connector.CALL )
        # unmarshall sqlresult back
        returned = self._connection.unmarshall( ret )
        if returned == -1 :
            return None
        # Fetch provides a Python compliant result back so using 
        # eval is simply fast and elegant here
        dc = self._connection._decoder # NB USED inside eval string
        returned = eval(returned , sys._getframe(0).f_globals ,sys._getframe(0).f_locals)
        if len(returned) == 0 :
            return None
        return returned
    
    def setDBSchema( self, dbSchema ):
        #ETHOD_NAME_SET_DBSCHEMA = "setDBSchema"
        self._connection.marshall( Connector.SET_METHOD, self.METHOD_NAME_SET_DBSCHEMA ) 
        # populate parameters
        self._connection.marshall( Connector.DEF_STRING, self.PARAM_NAME_DBSCHEMA  )
        self._connection.marshall( Connector.SET_VALUE, self.PARAM_NAME_DBSCHEMA, dbSchema )
        # proceed with call 
        self._connection.marshall( Connector.CALL )

    def setDBCatalog( self, dbCatalog ):
        self._connection.marshall( Connector.SET_METHOD, self.METHOD_NAME_SET_DBCATALOG ) 
        # populate parameters
        self._connection.marshall( Connector.DEF_STRING, self.PARAM_NAME_DBCATALOG  )
        self._connection.marshall( Connector.SET_VALUE, self.PARAM_NAME_DBCATALOG, dbCatalog )
        # proceed with call 
        self._connection.marshall( Connector.CALL )
    
    def __trace( self , msg , verbose ) :
        if verbose :
            print msg

        
    def __isStoredProc( self , sqlStmt ):
        """ return true if current sqlstmt is a create function or package """
        curCheck = sqlStmt.replace('%%cr%%' , '').lstrip()
        storeprocChecker = curCheck.split(' ')
        if len(storeprocChecker) < 2 :
            return False
        if ( storeprocChecker[0] == 'CREATE' or 'create' ) :
            if storeprocChecker[1] == 'or' or 'OR' :
                pos = 3
            else :
                pos = 2
            if len(storeprocChecker) <= pos : 
                return False
              
            if storeprocChecker[pos] == 'package' or \
               storeprocChecker[pos] == 'PACKAGE' or \
               storeprocChecker[pos] == 'function' or \
               storeprocChecker[pos] == 'FUNCTION' or \
               storeprocChecker[pos] == 'trigger' or \
               storeprocChecker[pos] == 'TRIGGER' :
                return True
            else:
                if ( storeprocChecker[pos] == 'type' or \
                     storeprocChecker[pos] == 'TYPE' ) and \
                   ( storeprocChecker[pos+1] == 'body' or \
                     storeprocChecker[pos+1] == 'BODY' ) :
                    return True
                return False
            
    def __checkStoredProc( self , sqlStmt ):
        """ split non stored procedure sequences """
        if self.__isStoredProc(sqlStmt) :
            return None
        else:
            return sqlStmt.split(';')

    def __emptySql( self , sqlStmt ):
        """ check for empty sql statement """
        curCheck = sqlStmt.replace('%%cr%%' , '').lstrip()
        if curCheck == '' or curCheck == '.'  or curCheck == 'exit' :
            # dot is sometime used as kind of separator = ignore it
            return True
        return False
    
    def _purgeLeadingSpace( self , sqlStmts ):
        """ return sqlstm removing leading \n and spaces"""
        sqlStmts = sqlStmts.lstrip(' ')
        sqlStmts = sqlStmts.lstrip('\n')
        return sqlStmts
    
    def __procesQuoted ( self , sqlStmt ) :
        """ process ; inside quote """
        sqlStmt = sqlStmt.replace( ';' , '%%semicol%%' )
        return sqlStmt
        
        
    def __purgeComments( self , sqlStmts ) :
        """ purge ; / inside sqlcomments """
        sqlStmts = sqlStmts.split('\n')
        i = 0
        inMultiLineComment = False
        while i < len(sqlStmts) :
            # remove leading spaces
            sqlStmts[i] = sqlStmts[i].lstrip()
            sqlStmt = sqlStmts[i] 
            # check for -- / and ; in current line
            multiLineComPos = sqlStmt.find('/*')
            if multiLineComPos != -1 :
                inMultiLineComment = True
            if inMultiLineComment :
                multiLineComPosEnd = sqlStmt.find('*/')
                if multiLineComPosEnd == -1 :
                    del sqlStmts[i]
                else :
                    sqlStmts[i] = sqlStmt[multiLineComPosEnd+2:]
                    i = i+1
                    inMultiLineComment = False
            else :
                commentPos = sqlStmt.find('--')
                quotePos = sqlStmt.find("'")
                semicolPos = sqlStmt.find(';')
                # check for sqlplus / execute statement separator 
                wk = sqlStmts[i].strip()
                if len(wk) == 1 and wk == '/':
                    pass
                else :
                    # replace any / 
                    sqlStmts[i] = sqlStmt.replace( '/' , '%%slash%%' )
                    sqlStmt = sqlStmts[i] 
                if commentPos != -1 :
                    if semicolPos > commentPos :
                        sqlStmts[i] = sqlStmt.replace( ';' , '%%semicol%%' )
                else :
                    if semicolPos != -1 :
                        # only change when / or ; in constants
                        while quotePos != -1 :
                            # process multiple paired quoted 
                            endQuote = sqlStmt.find("'",quotePos+1)
                            sliced = self.__procesQuoted(sqlStmt[quotePos:endQuote+1])
                            newEndQuote = (quotePos-1) + len(sliced) # recomp after transformation
                            sqlStmts[i] = ''.join([sqlStmt[0:quotePos] , sliced , sqlStmt[endQuote+1:]   ])
                            sqlStmt = sqlStmts[i]
                            quotePos = sqlStmt.find("'",newEndQuote+1)
                    # don't care about '.' DOT end of sqlblocs and get rid of them
                    if len(wk) == 1 and wk == '.' :
                        del sqlStmts[i]

                i = i+1
        returned = '\n'.join(sqlStmts) ;
        return returned
        
    def __purgeLeadingComments( self , sqlStmts ) :
        """ purge -- before sql commands """
        sqlStmts = sqlStmts.replace('%%cr%%' , '\n')
        sqlStmts = sqlStmts.split('\n')
        # check for leading --
        while ( sqlStmts != [] ) and \
              (  len( sqlStmts[0] ) == 0 or \
                 sqlStmts[0][:2] == '--' ) :
            del sqlStmts[0]
        sqlStmts = '\n'.join(sqlStmts)
        return sqlStmts.replace('\n','%%cr%%')
        
    def __isDrop( self , sqlStmt ) :
        first = sqlStmt.split()[0].strip().upper()
        if first == 'DROP' :
            return True
        return False
        

    def __processScriptUnit( self , scriptFName , dbHandle , sqlStmt ) :
        """ process single script SQL element """
        try :
            isDrop = False
            sqlStmt = self.__purgeLeadingComments(sqlStmt)
            sqlStmt = sqlStmt.replace('}', '%%clbracket%%' )
            sqlStmt = sqlStmt.replace('{', '%%opbracket%%' )
            # check for emptyness after
            if not self.__emptySql(sqlStmt) :
                isDrop = self.__isDrop(sqlStmt) 
                cursor = self.execSql(dbHandle , sqlStmt)
                # fetch until the end of the resultset requesting 10 rows by fetch
                if cursor != None :
                    srows = self.sqlFetch( cursor , self.FETCH_ALL )
                    sqlStmt = sqlStmt.replace('%%cr%%' , '')
                    print scriptFName + ":"+ sqlStmt + " rows = " , srows
                    self.sqlCursorClose(cursor)
        except SmdClientError , f :
            # ignore error on SQL drop (May be refined) 
            if not isDrop :
                sqlStmt = sqlStmt.replace( '%%cr%%' , '\n' )
                print 'sqlprocessing error : ' + str(f)
                print 'on statement :' + sqlStmt
Beispiel #40
0
 def connect(self) :
     """ connect to Smd Service """
     if (self._connection == None):
         self._connection = Connector(self._host , self._port , self._encoding, CompatibilityMode_702.CLASSFACTORY)
     return self._connection
 def __init__(self, url, api_key, api_user = '', api_pass = ''):
     self.url = url
     self.api_key = api_key
     Connector.__init__(self, url, api_key, api_user, api_pass)
def executer(oneLine):
    # for oneLine in eList:
    if oneLine.operation=='upload':
        Conn=Connector(oneLine.ip,oneLine.port,oneLine.userName,oneLine.passWord,oneLine.connWay)
        Conn.sftpUpload(oneLine.restList[1].strip(),oneLine.restList[0].strip())
    elif oneLine.operation=='download':
        Conn=Connector(oneLine.ip,oneLine.port,oneLine.userName,oneLine.passWord,oneLine.connWay)
        Conn.sftpDownLoad(oneLine.restList[1].strip(),oneLine.restList[0].strip())
    elif oneLine.operation=='execute':
        Conn=Connector(oneLine.ip,oneLine.port,oneLine.userName,oneLine.passWord,oneLine.connWay)
        Conn.sshExecute(oneLine.restList)
    elif oneLine.operation=='monitor':
        Conn=Connector(oneLine.ip,oneLine.port,oneLine.userName,oneLine.passWord,oneLine.connWay)
        Conn.sshMonitor(oneLine.restList,oneLine.filePath,oneLine.interval,oneLine.times)
Beispiel #43
0
class TaskSolver(object):
    def __init__(self):
        self.tasks = []
        self.dbmgr = DBManager()
        self.c = Connector()
        self.data = ""
        self.receipt_done = dict()
    def getTasks(self, task):
        """ getTasks """
        pass

    def Query(self, receipt, receipt_date):

        res = None
        while res is None:

            if not self.c.session_valid:
                del self.c
                self.c = Connector()
                #self.c.imgRslr.reportFail(self.c.imgCode, self.c.imgSHA)
                self.c.resolveImg()
            log.info('[{}]Get Image {}:{}'.format(self.c.res.reason, self.c.tmp_file, self.c.imgCode))
            log.info('{} and {}'.format(receipt , receipt_date))
            self.c.setPostData(receipt, receipt_date )
            self.c.postForm( self.c.postPath )
            log.info('[{} {}]Post data'.format(self.c.res.status,self.c.res.reason))
            res = self.c.getInfo()

            with open("out.html" , "w") as outFd:
                outFd.write(self.c.body)

        if not self.c.info:
            return False
        else:
            print("===[Query Result]===")
            receipt = self.c.info
            receipt['money'] = receipt['money'].replace(',','')
            for k,r in receipt.iteritems():
                print k+":\t\t"+r

            self.receipt_done[self.c.info['id']] = (receipt['date'],receipt['money'],receipt['taxid'])
            return True

    def _modify_receipt_num(self, receipt, delta):
        receipt_eng = receipt[0:2]
        receipt_num = int(receipt[2:10])
        receipt_num += delta
        return receipt_eng+str(receipt_num)

    def solve_task(self, task_dict):
        distance = task_dict['distance']
        date = task_dict['date']
        direction = task_dict['direction']
        receipt = task_dict['receipt']

        start_receipt = receipt
        start_receipt_num = int(start_receipt[2:10])
        current_receipt = start_receipt
        current_receipt_num = int(current_receipt[2:10])
        success_count = 0


        while (abs(start_receipt_num - current_receipt_num) < distance):
            success = self.Query(current_receipt, date)
            if success is True:
                current_receipt = self._modify_receipt_num(current_receipt,direction)
                current_receipt_num = int(current_receipt[2:10])
                success_count += 1
            elif success is False:
                break
            else:
                log.error('main loop unusually break')
                sys.exit(1)

        result = {
                'success':success_count,
                'error':distance-success_count,
                }
        receipt = self.receipt_done
        return_data = {'result':result,'receipt':self.receipt_done,'task':task_dict}

        return return_data

    def start_solver(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_address = ('127.0.0.1', 5555)
        print  'connecting to {} '.format(server_address)
        sock.connect(server_address)
        while True:
            task_str = sock.recv(512)
            task = json.loads(task_str)
            if task['action'] == "solve":
                task_dict = task['task']
                print "Recieve task : {}".format(task_dict)
                result = solver.solve_task(task_dict)
                print "send : \n{}".format(result)
                sock.sendall(json.dumps(result))
                print "Task done!!"
            elif task['action'] == "close":
                sock.close()
                break
class Kam1n0PluginManager():

    def __init__(self):
        self.actions = list()
        self.confDir = os.path.expanduser("~") + "/Kam1n0"
        if not os.path.exists(self.confDir):
            os.makedirs(self.confDir)
        self.loadIcons()

        self.Kconf = self.getConfiguration()
        if self.Kconf is None:
            self.connector = Connector()
            self.Kconf = {}
            self.Kconf['cnns'] = {}
            cnnInfo = self.connector.toMap()
            self.Kconf['cnns'][cnnInfo['key']] = cnnInfo
            self.Kconf['default-cnn'] = cnnInfo['key']
            self.setConfiguration(self.Kconf)
        else:
            if self.Kconf['default-cnn'] is None:
                self.connector = None
            else:
                cnnInfo = self.Kconf['cnns'][self.Kconf['default-cnn']]
                self.connector = Connector(
                    protocol=cnnInfo['protocol'],
                    server=cnnInfo['server'],
                    port=cnnInfo['port'],
                    un=cnnInfo['un'],
                    pw=cnnInfo['pw']
                )

        global hooks
        hooks = Hooks()
        re = hooks.hook()

    def loadIcons(self):
        self.icon = {}
        self.icon[IDAutils.ICON_SEARCH] = IDAutils.loadIcon(IDAutils.ICON_SEARCH)
        self.icon[IDAutils.ICON_SEARCHMULTI] = IDAutils.loadIcon(IDAutils.ICON_SEARCHMULTI)
        self.icon[IDAutils.ICON_INDEX] =  IDAutils.loadIcon(IDAutils.ICON_INDEX)
        self.icon[IDAutils.ICON_INDEXS] =  IDAutils.loadIcon(IDAutils.ICON_INDEXS)
        self.icon[IDAutils.ICON_SETT] =  IDAutils.loadIcon(IDAutils.ICON_SETT)
        self.icon[IDAutils.ICON_CONN] =  IDAutils.loadIcon(IDAutils.ICON_CONN)

    def removeAllAction(self):
        for action in self.actions:
            action.unregisterAction()

    def registerActions(self):

        action = ActionWrapper(
            id="Kam1n0:queryCurrent",
            name="Search current function",
            icon=self.icon[IDAutils.ICON_SEARCH],
            tooltip="Search the current function",
            shortcut="Ctrl+Shift+s",
            menuPath="Search/next code",
            callback=self.queryCurrentFunction,
            args=None
        )
        self.actions.append(action)
        if not action.registerAction():
            return 1

        action = ActionWrapper(
            id="Kam1n0:querySelected",
            name="Select functions to search",
            icon=self.icon[IDAutils.ICON_SEARCHMULTI],
            tooltip="Select functions to search",
            shortcut="Ctrl+Shift+a",
            menuPath="Search/next code",
            callback=self.querySelectedFunctions,
            args=None
        )
        self.actions.append(action)
        if not action.registerAction():
            return 1

        action = ActionWrapper(
            id="Kam1n0:indexCurrent",
            name="Index current function",
            icon=self.icon[IDAutils.ICON_INDEX],
            tooltip="Index current function",
            shortcut="Ctrl+Shift+k",
            menuPath="Edit/Export data",
            callback=self.indexCurrentFunction,
            args=None
        )
        self.actions.append(action)
        if not action.registerAction():
            return 1

        action = ActionWrapper(
            id="Kam1n0:indexSelected",
            name="Index selected function",
            icon=self.icon[IDAutils.ICON_INDEXS],
            tooltip="Index selected function",
            shortcut="Ctrl+Shift+j",
            menuPath="Edit/Export data",
            callback=self.indexSelectedFunctions,
            args=None
        )
        self.actions.append(action)
        if not action.registerAction():
            return 1

        action = ActionWrapper(
            id="Kam1n0:connectionManagement",
            name="Manage connection",
            icon=self.icon[IDAutils.ICON_CONN],
            tooltip="Manage connection",
            shortcut="",
            menuPath="Edit/Kam1n0/",
            callback=self.openConnectionManagmentForm,
            args=None
        )
        self.actions.append(action)
        if not action.registerAction(True):
            return 1

        action = ActionWrapper(
            id="Kam1n0:storageManagement",
            name="Manage storage",
            icon=self.icon[IDAutils.ICON_SETT],
            tooltip="Manage storage",
            shortcut="",
            menuPath="Edit/Kam1n0/",
            callback=self.openAdminForm,
            args=None
        )
        self.actions.append(action)
        if not action.registerAction(False):
            return 1

        return 0

    def indexCurrentFunction(self, ctx):
        func = IDAutils.GetCurrentFunction()
        if not func:
            print "Current address does not belong to a function"
            return 0
        self.createIndexProgressForm([func])

    def indexSelectedFunctions(self, ctx):
        if ctx.form_title == "Functions window":
            funcs = []
            for fidx in ctx.chooser_selection:
                func = idaapi.getn_func(fidx - 1)
                funcs.append(func)
            self.createIndexProgressForm(funcs)
        else:
            form = IndexSelectionForm(self)
            ok = form.Execute()
            funcs = form.funcs
            s_cnn = form.cnn
            form.Free()
            if ok == 1:
                self.createIndexProgressForm(funcs, s_cnn)

    def querySelectedFunctions(self, ctx):
        if ctx.form_title == "Functions window":
            funcs = []
            for fidx in ctx.chooser_selection:
                func = idaapi.getn_func(fidx - 1)
                funcs.append(func)
            self.createProgressForm(funcs)
        else:
            form = SelectionForm(self)
            ok = form.Execute()
            funcs = form.funcs
            s_cnn = form.cnn
            form.Free()
            if ok == 1:
                self.createProgressForm(funcs, s_cnn)

    def queryCurrentFunction(self, ctx):
        func = IDAutils.GetCurrentFunction()
        if not func:
            print "Current address does not belong to a function"
            return 0
        self.createProgressForm([func])

    def openConnectionManagmentForm(self, ctx):
        form = ConnectionManagementForm(self)
        form.Execute()
        self.setConfiguration(self.Kconf)

        # update connection:
        if self.Kconf['default-cnn'] is not None:
            cnnInfo = self.Kconf['cnns'][self.Kconf['default-cnn']]
            self.connector = Connector(
                protocol=cnnInfo['protocol'],
                server=cnnInfo['server'],
                port=cnnInfo['port'],
                un=cnnInfo['un'],
                pw=cnnInfo['pw']
            )
            self.connector.reset()
        else:
            self.connector = None

    def openAdminForm(self, ctx):
        self.connector.openAdminPage()

    def createProgressForm(self, funcs, cnn=None):
        if cnn is None:
            cnn = self.connector
        form = SearchProgressForm(cnn, funcs)
        form.Execute()
        code = form.ErrorCode
        content = form.Content
        form.Free()
        if code > OK:
            Connector.getCodeDescription(code, content)
            idaapi.warning("Connection failed. Please review your connection. \n \"%s\"" % content)
            self.openConnectionManagmentForm(ctx=None)


    def createIndexProgressForm(self, funcs, cnn=None):
        if cnn is None:
            cnn = self.connector
        form = IndexProgressForm(cnn, funcs)
        form.Execute()
        code = form.ErrorCode
        content = form.Content
        form.Free()
        if code > OK:
            Connector.getCodeDescription(code, content)
            idaapi.warning("Connection failed. Please review your connection. \n \"%s\"" % content)
            self.openConnectionManagmentForm(ctx=None)

    def getConfiguration(self):
        try:
            with open(self.confDir + '/plugin-conf.pkl', 'rb') as f:
                return pickle.load(f)
        except:
            return None

    def setConfiguration(self, map):
        with open(self.confDir + '/plugin-conf.pkl', 'wb') as f:
            pickle.dump(map, f, pickle.HIGHEST_PROTOCOL)
Beispiel #45
0
    def imageSearch(self, descriptorsJson, locationJson, bearingJson):
#     def imageSearch(self):
        descriptors = json.loads(descriptorsJson)
        descriptors = descriptors['desc']
        descriptors = descriptors.replace(';', '],[')
        descriptors = descriptors.replace('\n', '')
        descriptors = '[' + descriptors + ']'
        descriptors = descriptors.replace('[','').split('],')
        descriptors = [map(float, s.replace(']','').split(',')) for s in descriptors]
        descriptors = np.array(descriptors)
                             
        location = json.loads(locationJson)
        location = location['loc']
        location = location.replace('[','').split('],')
        location = [map(float, s.replace(']','').split(',')) for s in location]
        location = np.array(location)
        
        bearing = json.loads(bearingJson)
        bearing = bearing['bearing']
        bearing = bearing.replace('[', '').split('],')
        bearing = [map(float, s.replace(']','').split(',')) for s in bearing]

#         a = 'temp'
#         b = os.listdir(a)
#         for c in b:
#             d = os.path.join(a,c)
#         img = cv2.imread(d)
#         fea_det = cv2.FeatureDetector_create("ORB")
#         des_ext = cv2.DescriptorExtractor_create("ORB")
#         kpts = fea_det.detect(img)
#         kpts, descriptors = des_ext.compute(img, kpts)
#         location = [6.908844, 79.6454667]
#         location = np.array(location)
         
        clusterNumber = self.findCluster(location.reshape(1,-1))
        file_name = "Cluster_" + str(clusterNumber[0]) + ".pkl"
        clf, classes_names, stdSlr, k, voc = joblib.load(file_name)
        test_features = np.zeros((1, k), "float32")
        words, distance = vq(descriptors, voc)
        
        for w in words:
            test_features[0][w] += 1
                 
        test_features = stdSlr.transform(test_features)

        writer=csv.writer(open("buddha_statue_test_data.csv",'a+'))
        try:
            predictions = [classes_names[i] for i in clf.predict(test_features)]
            prediction_probability = np.amax(clf.predict_proba(test_features))
                
            print str(predictions[0]) + " - " + str(prediction_probability)
            data_row = []
            data_row.append(str(predictions[0]))
            data_row.append(str(prediction_probability))
            writer.writerow(data_row)
        except Exception as e:
            print e
        
        connection = Connector()
        connection.makeConnection()
        
        
        
        if(prediction_probability>=0.8):
            results = connection.retriveLandmarkDescription(predictions[0])
            connection.closeConnection()
            
            data=[]
            
            suggetion_path = "dataset/landmarks/" + str(results[0].Name)
            image_names = os.listdir(suggetion_path)
            full_path = os.path.join(suggetion_path, image_names[0])
            with open(full_path, "rb") as imageFile:
                encoded_image = base64.b64encode(imageFile.read())
            
                   
            data.append({
                         "thumbnail": encoded_image,
                         "location": {
                                      "Lognitude": results[0].location, 
                                      "Latitiude": results[0].location2
                                      },
                         "description": results[0].Description,
                         "title": results[0].Name,    
                         "key": str(results[0].rid),   
                         })
            
            json_data = json.dumps(data)
            print json_data
            return json_data
        else:
            results = connection.getLandmarkSuggestions(clusterNumber[0])
            data = []
            for i in range(len(results)):
                suggetion_path = "dataset/landmarks/" + str(results[i].Name)
                image_names = os.listdir(suggetion_path)
                full_path = os.path.join(suggetion_path, image_names[0])
                with open(full_path, "rb") as imageFile:
                    encoded_image = base64.b64encode(imageFile.read())
                
                data.append({
                             "thumbnail": encoded_image,
                             "location": {
                                          "Lognitude": results[i].location, 
                                          "Latitiude": results[i].location2
                                          },
                             "description": results[i].Description,
                             "title": results[i].Name,    
                             "key": str(results[i].rid),   
                             })
            
            json_data = json.dumps(data)
            print json_data
            connection.closeConnection()
            return json_data       
Beispiel #46
0
class ExecEngine:
    """Python Client interface to the SqlFactory SMD service """

    FETCH_ALL = -1 # get all results in one call (USE CAUTIOUSLY)
    CLASSFACTORY         = "com.sefas.EngineStarter.SmdEngineStarter"
    
    METHOD_ADD_ENV = "addEnv"
    METHOD_ADD_PARAM = "addParam"
    METHOD_LOAD_ENV_PROP = "loadEnvProp"
    METHOD_LOAD_ENGINES_PROP = "loadEnginesProp"
    METHOD_LOCK = "lock"

    METHOD_START = "start"
    METHOD_START_ASSEMBLY = "startAssembly"
    METHOD_START_CODR = "startCodr"
    METHOD_START_COMPO = "startCompo"
    METHOD_START_DRV = "startDrv"
    METHOD_START_SORT = "startSort"

    METHOD_RESULT_LOG        = "resultLog"
    METHOD_RESULT_EXIT_CODE  = "resultExitCode"
    METHOD_RESULT_ERR        = "resultErr"
    METHOD_RESULT_MESSAGE    = "resultMessage"
    METHOD_RESULT_VERSION    = "resultVersion"
    
    METHOD_PARAM_VARNAME = "paramVarName"
    METHOD_PARAM_VARVALUE = "paramVarValue"
    METHOD_PARAM_FILENAME = "paramFileName"
    METHOD_PARAM_LOCK     = "paramLock"
    METHOD_PARAM_ENGINE_KEY     = "engineKey"

    def __init__ (self , 
                   encoding="UTF8" , # default encoding to UTF8
                   hostName="localhost" , 
                   port=29100 
                 ):
        """ constructor just store provided object information """
        self._host = hostName 
        self._port = port
        self._connection = None
        self._encoding = encoding
        self._param = {} 
        self._env = {} 
        self._paramSended = {} 
        self._envSended = {} 
        self._engineProp = None 
        self._envProp = None 
        self._islock = True 
        
    def connect(self) :
        """ connect to SqlFactory Service """
        if (self._connection != None):
            return
        self._connection = Connector(self._host , self._port , self._encoding, self.CLASSFACTORY)
        self.lock(True)

    def disconnect(self):
        """ Disconnect from SqlFactory Service """
        if (self._connection != None):
            self._connection.close() 
            self._connection = None

    def addEnv (self, envVarName, envVarValue):
        """ add an environment variable to the call """
        self._env[envVarName] = envVarValue
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_ADD_ENV)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, self.METHOD_PARAM_VARNAME)
        self._connection.marshall(Connector.SET_VALUE, self.METHOD_PARAM_VARNAME, envVarName)
        self._connection.marshall(Connector.DEF_STRING, self.METHOD_PARAM_VARVALUE)
        self._connection.marshall(Connector.SET_VALUE, self.METHOD_PARAM_VARVALUE, envVarValue)
        # proceed with call
        return self._connection.marshall(Connector.CALL)

    def addParam (self, paramVarName, paramVarValue):
        """ add a parameter to the call """
        self._param[paramVarName] = paramVarValue
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_ADD_PARAM)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, self.METHOD_PARAM_VARNAME)
        self._connection.marshall(Connector.SET_VALUE, self.METHOD_PARAM_VARNAME, paramVarName)
        self._connection.marshall(Connector.DEF_STRING, self.METHOD_PARAM_VARVALUE)
        self._connection.marshall(Connector.SET_VALUE, self.METHOD_PARAM_VARVALUE, paramVarValue)
        # proceed with call
        return self._connection.marshall(Connector.CALL)

    def loadEnvProp (self, envPropertiesFileName):
        """ set the environement properties file name to be used on the server """
        self._envProp = envPropertiesFileName 
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_LOAD_ENV_PROP)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, self.METHOD_PARAM_FILENAME)
        self._connection.marshall(Connector.SET_VALUE, self.METHOD_PARAM_FILENAME, envPropertiesFileName)
        # proceed with call
        return self._connection.marshall(Connector.CALL)

    def loadEnginesProp (self, enginesPropertiesFileName):
        """ set the propertie file name of the server to be used """
        self._engineProp = enginesPropertiesFileName 
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_LOAD_ENGINES_PROP)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, self.METHOD_PARAM_FILENAME)
        self._connection.marshall(Connector.SET_VALUE, self.METHOD_PARAM_FILENAME, enginesPropertiesFileName)
        # proceed with call
        return self._connection.marshall(Connector.CALL)

    def lock (self, synchronizedCall):
        """ management of the sychroneous or asynchroneus call """
        self._islock = synchronizedCall 
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_LOCK)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, self.METHOD_PARAM_LOCK)
        if synchronizedCall:
            self._connection.marshall(Connector.SET_VALUE, self.METHOD_PARAM_LOCK, "true")
        else :
            self._connection.marshall(Connector.SET_VALUE, self.METHOD_PARAM_LOCK, "false")
        # proceed with call
        return self._connection.marshall(Connector.CALL)

    def start (self, engineKey):
        """ execute the command on the smd server """
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_START)
        # populate parameters
        self._connection.marshall(Connector.DEF_STRING, self.METHOD_PARAM_ENGINE_KEY)
        self._connection.marshall(Connector.SET_VALUE, self.METHOD_PARAM_ENGINE_KEY, engineKey)
        # proceed with call
        return self._connection.marshall(Connector.CALL)

    def getReturnDetail (self):
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_RESULT_EXIT_CODE)
        exitCode = self._connection.unmarshall(self._connection.marshall(Connector.CALL))
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_RESULT_VERSION)
        version = self._connection.unmarshall(self._connection.marshall(Connector.CALL))
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_RESULT_MESSAGE)
        message = self._connection.unmarshall(self._connection.marshall(Connector.CALL))
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_RESULT_LOG)
        log = self._connection.unmarshall(self._connection.marshall(Connector.CALL))
        self._connection.marshall(Connector.SET_METHOD, self.METHOD_RESULT_ERR)
        err = self._connection.unmarshall(self._connection.marshall(Connector.CALL))
        return "Exit Code = %s\nVersion = %s\nMessage = %s\nLog = %s\nError = %s" %(exitCode,version,message,log,err) 

    def printReturnDetail (self):
        print self.getReturnDetail()
        
    def manageExecReturn (self, ret):
        """ execute the command on the smd server """
        returned = int(self._connection.unmarshall( ret ))
        if returned != 0:
            print "Execution return code not 0 : "
            self.printReturnDetail()
Beispiel #47
0
    
    def cleanup():
        # Disconnecting before exit
        logger.debug("Cleanup operations initiated")
        connector.disconnect(status="405")
        connector.view.close()
        logger.debug("All cleanup operations completed")
        
    
    # Logging system initialization
#    sys.stdout = open("log.txt", "a+")
#    sys.stderr = open("errors.txt", "a+") 
    logger = logging.getLogger('RocketOne')
    logger.setLevel(logging.DEBUG)
    fh = logging.handlers.RotatingFileHandler('debug.log',
                                      mode='a',
                                      maxBytes=524288,
                                      backupCount=0)
    fh.setLevel(logging.INFO)
    formatter = logging.Formatter(
                '%(asctime)s - %(levelname)s - %(name)s - %(message)s')
    fh.setFormatter(formatter)
    logger.addHandler(fh)
    logger.info('Launch')
    app = QtGui.QApplication(sys.argv)
    
    connector = Connector()
    connector.read_settings()
    # Perform some cleanup before exit
    sys.exitfunc = cleanup
    sys.exit(app.exec_())
 def updateLandmarkClass(self):
     connection = Connector()
     connection.makeConnection()
     connection.updateLandmarkClusterID(self.clusterList, self.landmarks)
     connection.closeConnection()
Beispiel #49
0
 def __init__(self):
     self.tasks = []
     self.dbmgr = DBManager()
     self.c = Connector()
     self.data = ""
Beispiel #50
0
import gdata.apps

#Example how to authorize you application service
from Connector import Connector
multidomainObj = gdata.apps.service.AppsService()
Connector.connectByOauth(multidomainObj)
print multidomainObj.RetrieveAllUsers()


#Example how to create OAuth token
from OAuth import OAuth
from OAuth import POSSIBLE_SCOPES
OAuth(domain="gridcommons.com", 
                         clientKey="your client key", 
                         clientSecret="your client secret", 
                         scopes=POSSIBLE_SCOPES, 
                         appName="application name").getAccessToken()
Beispiel #51
0
 def __init__(self):
     self.tasks = []
     self.dbmgr = DBManager()
     self.c = Connector()
     self.data = ""
     self.receipt_done = dict()
Beispiel #52
0
 def connect(self) :
     """ connect to SqlFactory Service """
     if (self._connection != None):
         return
     self._connection = Connector(self._host , self._port , self._encoding, self.CLASSFACTORY)
     self.lock(True)
Beispiel #53
0
class OsCommandClient:
    """Python Client interface to the SqlFactory SMD service """

    FETCH_ALL = -1 # get all results in one call (USE CAUTIOUSLY)
    CLASSFACTORY         = "com.sefas.gridclient.osservices.OsCommandFactory"
    STRCR = "\\n" 
    CR = "\n" 
    
    COMMAND           = "command"
    GETSTDOUT         = "getStdOut"
    GETSTDERR         = "getStdErr"
  
    _ENCODING_PROPERTY_ = "file.encoding"
    _ARGS_              = "args"

    def __init__ ( self , 
                   encoding="UTF8" , # default encoding to UTF8
                   hostName="localhost" ,
                   port=29100 
                 ):
        """ constructor just store provided object information """
        self._host = hostName 
        self._port = port
        self._connection = None
        self._encoding = encoding 

    def connect( self ) :
        """ connect to SqlFactory Service """
        if ( self._connection != None ):
            return
        self._connection = Connector( self._host , self._port , self._encoding,  self.CLASSFACTORY )

    def disconnect( self ):
        """ Disconnect from SqlFactory Service """
        if ( self._connection != None ):
            self._connection.close() 
            self._connection = None

    def command ( self, commandLine, priority=PRIORITY_UNUSED):
        """ execute the command on the smd server """
        self._connection.marshall( Connector.SET_METHOD, self.COMMAND)
        # populate parameters
        self._connection.marshall( Connector.DEF_STRING, self._ARGS_)
        self._connection.marshall( Connector.SET_VALUE, self._ARGS_, commandLine)
        self._connection.marshall( Connector.DEF_INTEGER, self._ARGS_)
        self._connection.marshall( Connector.SET_VALUE, self._ARGS_, priority)
        # proceed with call
        ret = self._connection.marshall( Connector.CALL )
        # marshall cursor id back or None if cursorId is -1
        returned = int(self._connection.unmarshall( ret ))
        if returned == -1 :
            return None
        return returned

    def getStd ( self, which):
        """ execute the command on the smd server """
        self._connection.marshall( Connector.SET_METHOD, which)
        # proceed with call
        ret = self._connection.marshall( Connector.CALL )
        returned = self._connection.unmarshall( ret )
        return returned

    def getStdOut ( self):
        return self.getStd(self.GETSTDOUT)

    def getStdErr ( self):
        return self.getStd(self.GETSTDERR)
 def addConnector(self, children):
     name = self.name + str(len(self.connectors))
     connector = Connector(self, name)
     for child in children:
         connector.addNode(child)
     self.connectors[name] = connector
Beispiel #55
0
def run_detector():
    # import os
    # os.environ['SDL_VIDEODRIVER'] = 'windib'
    # os.environ['SDL_VIDEODRIVER'] = 'directx'


    parser = OptionParser(usage="""\
    Detect SnapMyinfo QRcodes in a live video stream

    Usage: %prog [options] camera_index
    """)

    parser.add_option('-f','--fs','--fullscreen',
                      dest="fullscreen", default=False,
                      action='store_true',
                      help="""Run the Live Decoder full screen.""")

    parser.add_option('--hw','--hw_accel',
                    dest="hw_accel", default=False,
                    action='store_true',
                    help="""Runs pygame with the directx hw driver (if avail). Automatically assumes fullscreen.""")

    parser.add_option('-s','--scale',
                    dest="scale", default=4.0,
                    action='store', type="float",
                    help="""Sets the precision of code tracking. Valid values are >1.0 and less than 8.0. Lower values represent more accurate tracking, but consume more CPU.""")

    parser.add_option('--flip',
                    dest="flip", default=False,
                    action='store_true',
                    help="""Flip the video image horizontally before processing.""")

    parser.add_option('-m','--max_cards',
                    dest="tracker_count", default=3, type="int",
                    action="store",
                    help="""The number of simultaneous snap codes that can be tracked in the video stream.""")


    parser.add_option('--nv','--no_video',
                    dest="no_video", default=False, 
                    action="store_true",
                    help="""A debugging option, turns off the video stream from the web cam.""")

    parser.add_option('-d','--debug',
                    dest="debug", default=False, 
                    action="store_true",
                    help="""Debugging option, turns on fps display and additional tracking data on the display.""")


    opts, args = parser.parse_args()

    import os
    os.environ['SDL_VIDEODRIVER'] = 'windib'
    if opts.hw_accel:
        os.environ['SDL_VIDEODRIVER'] = 'directx'
        opts.fullscreen = True

    # initialize pygame
    pygame.init()

    display_size = (800,600)
    video_size = (1280,720)
    
    # scale is roughly equivalent to tracking precision
    # the higher the scale, the less precise, but faster, the gross
    # tracking will be scale of 1 means 1:1 tracking, but can be slow
    # recommend 2-4 as a good scale/precision factor. higher res images
    # usually benefit from higher scale
    scale = opts.scale
    
    
    # this can be used to throttle the max framerate processed. 0 means no throttle
    max_frame_rate = 30
    
    names =  [args[0]]
    name = names[0]

    if not opts.no_video:
        # connect to web camera and set the webcam options
        capture = cvCreateCameraCapture( int(name) )
        cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, video_size[0] )
        cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, video_size[1] )
    
        # query the camera once to get the camera properties
        # the frame is just a throwaway
        cvQueryFrame( capture )

        # get the camera properties
        (o_width,o_height) = [cvGetCaptureProperty(capture, prop) for prop in [CV_CAP_PROP_FRAME_WIDTH,CV_CAP_PROP_FRAME_HEIGHT]]
        video_size = (int(o_width),int(o_height))
    else:
        blank = cvCreateImage(video_size,8,3)
        cvZero(blank)

    # create the pygame display
    flags = 0
    if opts.fullscreen:
        flags = pygame.FULLSCREEN
    if opts.hw_accel:
        flags = flags|pygame.HWSURFACE|pygame.DOUBLEBUF

    display_layer = pygame.display.set_mode( display_size,  flags ) 

    video = pygame.Surface(video_size).convert()

    # set the window name
    pygame.display.set_caption('Live Detector') 

    # some debug information
    # print the current driver being used
    print 'Driver %s\nVideo Input Resolution: %s\nDisplay Resolution: %s\n' % (pygame.display.get_driver(), video_size, display_size)

    # for convert to work, pygame video mode has to be set
    image_buffer = ImageBuffer(video_size,display_size,scale)
    if opts.no_video:
        image_buffer.frame_buffer = cvCreateImage(video_size,8,3)
        # blank = cvCreateImage(video_size,8,3)
        # cvZero(blank)
    worker = GrossTracker(image_buffer) 
    
    # pool of tracker objects
    pool = TrackerPool(image_buffer, opts.tracker_count)
    thread_objects.append(pool)


    status = Status()
    
    connector = Connector(pool,status)
    connector.start()
    thread_objects.append(connector)
    
    # for i in range(4):
    #     win_name = "thread-%d" % i
    #     cvNamedWindow(win_name, CV_WINDOW_AUTOSIZE)
    
    pyg_clock = pygame.time.Clock()
    update_count = 0
    last_rects = []
    last_fills = []
    hud_last_fills = []

    if opts.debug:
        dbg = DebugDisplay()

    snap_logo = pygame.image.load('./images/snap_logo.png').convert_alpha()
    
    still = False
    running = True
    fps = 0.0
    while running:
        pyg_clock.tick(max_frame_rate)
        if update_count > 20:
            fps = pyg_clock.get_fps()
            update_count = 0
        update_count += 1

        # get the pygame events
        events = pygame.event.get()
        for e in events:
            # 'quit' event key
            if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
                running = False
            elif e.type == KEYDOWN and e.unicode == u't':
                still = True
            
            
        # take a frame from the web camera
        if opts.no_video:
            cvCopy(blank,image_buffer.frame_buffer)
        else:
            image_buffer.frame_buffer = cvQueryFrame( capture )

        if opts.flip:
            cvFlip(image_buffer.frame_buffer,image_buffer.frame_buffer,1)
            
        # update the image buffer with the latest frame
        image_buffer.update()

        # analyze the small frame to find collapsed candidates
        squares = worker.analyze_frame()        

        # check squares and assign a tracker if new
        pool.check(squares)
        # update all trackers
        pool.update()

        status.update()
        
        # clear the paint buffer
        for rect in last_rects:
            pygame.gfxdraw.rectangle(image_buffer.paint_buffer, rect, Color(0,0,0))
        last_rects = []
        
        for blank in last_fills:
            image_buffer.paint_buffer.fill((0,0,0),blank)
        last_fills = []


        for blank in hud_last_fills:
            image_buffer.hud_buffer.fill((0,0,0,0),blank)
        hud_last_fills = []



        # draw the sprite and tracker boundaries
        # boundaries will be replaced (or turned off)
        pool.sort_active()
        for t_id in pool.active_trackers:
            #rect = pool.trackers[t_id].get_bound_rect()
            center = pool.trackers[t_id].get_avg_center(2)

            frame_color = Color(128,255,128)
            if pool.trackers[t_id].sprite:
                # print str(dir(pool.trackers[t_id].sprite))
                sprite_size = pool.trackers[t_id].sprite.get_size()
                # print str(sprite_size)
                x_diff = sprite_size[0] / 2.0
                y_diff = sprite_size[1] / 2.0
                
                # frame_color = Color(250,250,255)
                
                rect = pygame.Rect(center.x * image_buffer.display_scale[0] - x_diff, center.y * image_buffer.display_scale[1] - y_diff, sprite_size[0],sprite_size[1])
                # pygame.gfxdraw.rectangle(image_buffer.paint_buffer, rect, pool.trackers[t_id].color)
                # last_rects.append(rect)
                #if pool.trackers[t_id].user_id:
                image_buffer.paint_buffer.blit(pool.trackers[t_id].sprite,(rect.x ,rect.y ))
                last_fills.append(rect) #pygame.Rect(rect.x ,rect.y ,closer_img.size[0],closer_img.size[1]))
            else:
                # rect = pygame.Rect(center.x * scale - 100, center.y * scale - 100, 200,200)
                # 
                # 
                # pygame.gfxdraw.rectangle(image_buffer.paint_buffer, rect, frame_color)
                # last_rects.append(rect)

                #c = pygame.Color(164,229,135,150)
                #c1 = pygame.Color(164,229,135,255)
                c = pygame.Color(229,229,135,200)
                # c1 = pygame.Color(229,229,135,255)

                pygame.gfxdraw.filled_polygon(image_buffer.hud_buffer, pool.trackers[t_id].get_bounding_points(),c)
                # pygame.gfxdraw.polygon(image_buffer.hud_buffer, pool.trackers[t_id].get_bounding_points(),c1)
                # pygame.gfxdraw.rectangle(image_buffer.hud_buffer, rect, frame_color)
                # pygame.gfxdraw.filled_polygon(image_buffer.hud_buffer, pool.trackers[t_id].get_bounding_points(),c)
                hud_last_fills.append(pool.trackers[t_id].get_bound_rect())
                


        # draw the orphans and frame rate display
        # debug for now, lets me know when it's trying to lock onto something
        if opts.debug:
            fps_sprite = dbg.gen_sprite('''%.2f fps''' % fps)
            image_buffer.hud_buffer.blit(fps_sprite,(image_buffer.display_size[0]-100,10))
            fps_rect = pygame.Rect(video_size[0]-100,10, dbg.size[0], dbg.size[1])
            hud_last_fills.append(fps_rect)
            
            for orphans in pool.orphan_frames:
                for orphan in orphans:
                    orphan = pygame.Rect(orphan.x * image_buffer.display_scale[0], orphan.y * image_buffer.display_scale[1], orphan.width * image_buffer.display_scale[0], orphan.height * image_buffer.display_scale[1])
                    pygame.gfxdraw.rectangle(image_buffer.paint_buffer, orphan, Color(190,255,190))
                    last_rects.append(orphan)

        # no data in the frame buffer means we're done
        if not image_buffer.frame_buffer:
            break

        # Surf_dat array is not oriented the same way as the pyame display so
        # first it's transposed. Then the
        # the surface RGB values are not the same as the cameras
        # this means that two of the channels have to be swapped in the numpy
        # array before being blit'ted onto the array
        surf_dat = image_buffer.frame_buffer.as_numpy_array().transpose(1,0,2)[...,...,::-1]
        
        # blit_array zaps anything on the surface and completely replaces it with the array, much
        # faster than converting the bufer to a surface and bliting it
        # replaces video with the data in surf_dat
        surfarray.blit_array(video,surf_dat)
        
        # this resizes the video surface and stores it in display_layer. Completely
        # overwrites whatever is in display_layer
        pygame.transform.scale(video, image_buffer.display_size, display_layer)
        
        # blit the paint buffer onto the surface. Paint buffer has a chromakey so all black values will show through
        display_layer.blit(image_buffer.paint_buffer,(0,0))

        # the "HUD" is added next. 
        display_layer.blit(image_buffer.hud_buffer,(0,0))

        # Write out the status sprite to the display_layer
        if status.sprite:
            display_layer.blit(status.sprite,(10,image_buffer.display_size[1] - status.height - 10 ))


        # finally watermark the screen with the company logo and name of the product
        # putting it here means it always shows above the other layers
        logo_size = snap_logo.get_size()
        display_layer.blit(snap_logo,(image_buffer.display_size[0]-logo_size[0]-10 , image_buffer.display_size[1]-logo_size[1]-10))


        if still == True:
            pygame.image.save(display_layer, 'test.jpg')
            still = False

        # flip() actually displays the surface
        pygame.display.flip()

    # we've left the loop
    # exit
    print 'exiting...'