def verify_file(json_file:str) -> bool:
    """
    take in a Json file and compare it vs a json schema to verify it
    Args:
        json_file: location of the json file

    Returns: True if valid
    """
    schema_path = os.path.join(PACKAGE_DOCUMENT_PARSER_PATH,'output_schema.json')
    try:
        with open(schema_path, 'r') as f:
            schema_dict = json.load(f)
        with open(json_file, 'r') as f:
            input_json_dict = json.load(f)
        jsonschema.validate(input_json_dict, schema_dict)
    except jsonschema.exceptions.ValidationError as e:
        print("well formed but invalid json")
        print(e)
        is_valid_json = False
        return is_valid_json
    except json.decoder.JSONDecodeError as e:
        print("not a json")
        is_valid_json = False
        traceback.print_exec()
        return is_valid_json
    else:
        is_valid_json = True
    return is_valid_json
    def post(cls, user_id: int):
        '''Resend confirmation email'''
        user = UserModel.find_by_id(user_id)

        if not user:
            return {'message': gettext('user_not_found')}, 404

        try:
            confirmation = user.most_recent_confirmation

            if confirmation:
                if confirmation.confirmed:
                    return {
                        'message': gettext('confirmation_already_confirmed')
                    }, 400

                confirmation.force_to_expire()

            new_confirmation = ConfirmationModel(user_id)
            new_confirmation.save_to_db()

            user.send_confirmation_email()

            return {'message': gettext('confirmation_resend_successful')}, 201
        except MailGunException as e:
            return {'message': str(e)}, 500
        except:
            traceback.print_exec()
            return {'message': gettext('confirmation_resend_fail')}, 500
Example #3
0
    def get_authkey(self, ident):
        with self.mongo:
            try:
                res = self.mongo.hpfeeds.auth_key.find_one(
                    {"identifier": ident})
            except:
                import traceback
                traceback.print_exec()
                return NONE
            finally:
                self.mongo.close()

        if not res: return None

        subchans = res.get('subscribe')
        pubchans = res.get('publish')
        secret = res.get('secret')
        ident = res.get('identifier')

        pubchans = json.dumps(pubchans)
        subchans = json.dumps(subchans)

        return dict(secret=secret,
                    ident=ident,
                    pubchans=pubchans,
                    subchans=subchans,
                    owner=ident)
Example #4
0
def service (request, version):
  try:
    if version != "1.0": 
       raise Http404   

    baseURL = request.build_absolute_uri ()
    xml = []
    xml.append ('<?xml version="1.0" encoding="utf-8"  ?>' ) 
    xml.append ('  <TileMapService '  + 
                '       version="1.0" services="' + baseURL + '" >' ) 
    xml.append ('     <Title>ShapeEditor Tile Map Service</Title>' )
    xml.append ('     <Abstract></Abstract> ' ) 
    xml.append ('     <TileMaps> ' ) 
    for shapefile in Shapefile.objects.all ():
         id = str ( shapefile.id )
         xml.append ( '       <TileMap title="' + shapefile.filename + '"'  )
         xml.append ( '          srs="EPSG:4326" ' ) 
         xml.append ( '          href="' + baseURL + '/' + id + '"/>'  )
    xml.append ('     </TileMaps> ' ) 
    xml.append ('  </TileMapService> ' )

    return HttpResponse ("\n".join (xml), mimetype="text/xml" ) 

  except:
    traceback.print_exec ()
    return HttpResponse (" ")
Example #5
0
def update_uuids():
    """
    Make sure that every record in the database has a uuid.
    """
    retval = {}
    for t in db.tables:
        print 'start table', t
        recs = db(db[t].id > 0).select()
        print 'found', len(recs), 'records'
        changed = 0
        dated = 0
        try:
            for r in recs:
                if r.uuid is None:
                    r.update_record(uuid=str(uuid.uuid4()))
                    changed += 1
                if r.modified_on is None:
                    r.update_record(modified_on=request.now)
                    dated += 1
            print 'changed', changed
        except:
            traceback.print_exec(5)
        retval[t] = ('changed {}'.format(changed),
                     'dated {}'.format(dated))

    return {'changes': retval}
Example #6
0
def service(request, version):
    try:
        if version != "1.0":
            raise Http404

        baseURL = request.build_absolute_uri()
        xml = []
        xml.append('<?xml version="1.0" encoding="utf-8"  ?>')
        xml.append('  <TileMapService ' + '       version="1.0" services="' +
                   baseURL + '" >')
        xml.append('     <Title>ShapeEditor Tile Map Service</Title>')
        xml.append('     <Abstract></Abstract> ')
        xml.append('     <TileMaps> ')
        for shapefile in Shapefile.objects.all():
            id = str(shapefile.id)
            xml.append('       <TileMap title="' + shapefile.filename + '"')
            xml.append('          srs="EPSG:4326" ')
            xml.append('          href="' + baseURL + '/' + id + '"/>')
        xml.append('     </TileMaps> ')
        xml.append('  </TileMapService> ')

        return HttpResponse("\n".join(xml), mimetype="text/xml")

    except:
        traceback.print_exec()
        return HttpResponse(" ")
Example #7
0
def extract_company_info(security, source, company_source):
    """Return the company information
	"""
    company = {
        "Market_Info": {},
        "Management_Info": {},
        "Address_Info": {},
        "Competitors_Info": {}
    }
    try:
        lexml = get_lexml(source)
        company_lexml = get_lexml(company_source)
        company['Market_Info'].update({
            "Ticker":
            get_exchange_security(lexml)[1],
            "Exchange":
            get_exchange_security(lexml)[0],
            "Company_Name":
            get_company_name(lexml),
            "Sector":
            get_sector_industry(lexml)[0],
            "Industry":
            get_sector_industry(lexml)[0]
        })
        company['Management_Info'] = extract_officers_data(company_lexml)
        company['Address_Info'].update({
            "Address":
            get_address(lexml),
            "Description":
            get_company_description(lexml)
        })
        company['Competitors_Info'] = get_competitors(lexml)
    except Exception, e:
        logging.warn("Error in extracting {0} information".format(security))
        traceback.print_exec()
Example #8
0
 def runner2(self):
     while not self.task_completed:
         try:
             #print('Handler {} is processing'.format(self.id))
             if len(self.dispatcher_buckets) == 0:
                 break
             earliest_dispatcher = self.dispatcher_buckets[0]
             earliest_request = earliest_dispatcher.peak()
             for dispatcher in self.dispatcher_buckets[1:]:
                 request = dispatcher.peak()
                 if earliest_request is None:
                     earliest_request = request
                 if (earliest_request is not None) and (request
                                                        is not None):
                     # small than should be fine??
                     earliest_dispatcher = earliest_dispatcher if earliest_request.order < request.order else dispatcher
                     earliest_request = earliest_request if earliest_request.order < request.order else request
             if earliest_request is not None:
                 picked_request = earliest_dispatcher.get()
                 assert picked_request == earliest_request
                 self.queue.put(picked_request)
             gevent.sleep(0)
         except Exception as e:
             import traceback
             traceback.print_exec()
Example #9
0
def write(message, level=0, image=0, startedby='server', title=None):
    db = connectToDB()
    try:
        cursor = db.cursor()
        cursor.execute(
            "INSERT INTO log (time, message, level, image, startedby) values (NOW(), '"
            + message + "', " + str(level) + ", " + str(image) + ", '" +
            startedby + "')")
        db.commit()
        db.close()
    except:
        traceback.print_exec(file=sys.stdout)
    if level >= Config.pbMinPushLevel:
        for device in Config.pbDevices:
            if (image == 0):
                start_new_thread(
                    pushbullet.pushNote,
                    (device, title if title != None else 'Fishtank (' +
                     loglevels[level] + ')', message))
            else:
                start_new_thread(
                    pushbullet.pushFile,
                    (device, title if title != None else 'Fishtank (' +
                     loglevels[level] + ')', message,
                     open(Camera.getPictureFilename(image), "rb")))
    FishTank.increaseVersion()
    FishTank.save()
    def post(self):
        dados = atributos.parse_args()
        if not dados.get('email') or dados.get('email') is None:
            return {'message': 'Must have email field.'}, 400

        if UserModel.find_by_login(dados['login']):
            return {
                'message': f"The login '{dados['login']}' already exists"
            }, 400

        if UserModel.find_by_email(dados['email']):
            return {
                'message': f"The email '{dados['email']}' already exists"
            }, 400

        user = UserModel(**dados)
        user.ativo = False
        try:
            user.save_user()
            resp = user.send_confirmation_email()
            print('VaiCARAI', resp.status_code)
        except:
            user.delete_user()
            traceback.print_exec()
            return {'message': 'An internal server error has oourred'}
        return {'message': 'User created successfully!'}, 201  #Created
Example #11
0
def auto_connect(): 
    """SSH自动登录脚本"""
    if len(sys.argv) == 2:
        remote_server = sys.argv[1]
    else:
        print "使用方法:\n./pexpect_ssh.py 主机别名"
        sys.exit(1)

    if remote_server in SERVER:
        SSH = "ssh -p %s %s@%s " % (SERVER[remote_server][0],SERVER[remote_server][1],SERVER[remote_server][2])
    else:
        print "您输入了一个错误的主机别名"
        sys.exit(1)

    try: 
        child = pexpect.spawn(SSH)   
        index = child.expect(['password:'******'continue connecting (yes/no)?',pexpect.EOF, pexpect.TIMEOUT]) 
        if index == 0: 
            child.sendline(SERVER[remote_server][3])
            child.interact()
        elif index == 1:
            child.sendline('yes')
            child.expect(['password:'])
            child.sendline(SERVER[remote_server][3])
            child.interact()
        elif index == 2:
            print "子程序异常,退出!"
            child.close()
        elif index == 3:
            print "连接超时"
    except: 
        traceback.print_exec()
Example #12
0
def process_image():
    try:
        # convert string of image data to uint8
        nparr = np.frombuffer(request.data, np.uint8)
        # decode image
        bin_image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        print("start infering...")
        output_image_id, bin_output, box_data = perform_inference(bin_image)
        print("Done")
        # Save processed files for future references.
        output = output_image_id + '.png'
        save_image(bin_output, output)
        print(output + ' saved')

        # Convert to base64 encoding
        retval, buffer = cv2.imencode('.png', bin_output)
        base64_image = base64.b64encode(buffer).decode()

        return jsonify(
            id = output_image_id,
            blocks=box_data,
            img= base64_image
        ), 200
    except Exception as e:
        print(str(e))
        traceback.print_exec()
        abort(500)
Example #13
0
def set_timestamps():
    """
    Make sure that every record in the database has a uuid.
    """
    retval = {}
    mytime = datetime.datetime(2014, 1, 1)
    for t in db.tables:
        if t in []:
        #if t in ['lemmas', 'constructions', 'word_forms', 'badges', 'steps',
                 #'paths', 'plugin_slider_slides', 'plugin_slider_decks']
        print 'start table'
        recs = db(db[t].id > 0).select()
        print 'found', len(recs), 'records'
        dated = 0
        try:
            for r in recs:
                r.update_record(modified_on=mytime)
                dated += 1
            print 're-dated', dated
        except:
            traceback.print_exec(5)
        retval[t] = ('changed {}'.format(changed),
                     'dated {}'.format(dated))

    return {'changes': retval}
Example #14
0
 def sendStr(self, xstr):
   if self.clientid == "":
     Logger.error("C0WS", "send to a closed ws")
     return
   try:
     self.ws.send(xstr)
   except:
     traceback.print_exec()
Example #15
0
 def __getattr__(self, attr):
     try:
         return _META_.PARSER.__getattribute__(attr)
     except AttributeError:
         return _META_.ARGS.__getattribute__(attr)
     except:
         traceback.print_exec()
         exit(-1)
Example #16
0
def load_trec_corpus (file_path) :
  '''
  Load the corpus in TREC format

  file_path: string filesystem path to the corpus file
  '''
  global DB_CON
  db_cur = DB_CON.cursor()

  doc_imported = 0

  is_begin = False
  try:
    with open(file_path) as f:
      print '[Info] Loading %s' % file_path

      doc_id = ''
      str_list = []
      for line in f:
        #line = line.strip()
        if re.match(r'<DOC>', line):
          continue
        if re.match(r'<DOCNO> ', line):
          mo = re.match(r'<DOCNO> (.+) <\/DOCNO>', line)
          doc_id = mo.group(1)
          continue
        if re.match(r'<\/DOC>', line):
          ## import the document to DB
          doc_data = ''.join(str_list)

          if import_doc(db_cur, doc_id, doc_data) :
            doc_imported += 1

          ## clear the list: http://stackoverflow.com/a/850831/219617
          del str_list[:]
          # for debug purpose only
          #if doc_imported >= 500:
            #break

          ## perform commit every 1000 documents
          if 0 == doc_imported % 1000 :
            do_commit()
            ## for debug purpose only
            #break

        else:
          ## add the current string to str_list
          str_list.append(line)

  except IOError as e:
    print '-' * 60
    traceback.print_exec(file = sys.stdout)
    print '-' * 60
    exit(-1)

  do_commit()
  print '\n[Info] Summary:'
  print '[Info] %d documents imported' % doc_imported
Example #17
0
def ping(ip,lat,lng):
    try:
        t = time.time()
        status = subprocess.call(['ping', '-c4  ', ip], stdout=null, stderr=null)
        if status != 0:
            return None
        return ip,lat,lng,time.time() - t
    except:
        traceback.print_exec()
Example #18
0
def get_version(directory=None):
    packages = find_packages(basepath=directory if directory else os.getcwd())
    try:
        version = verify_equal_package_versions(packages.values())
    except RuntimeError as err:
        traceback.print_exec()
        error("Releasing multiple packages with different versions is "
                "not supported: " + str(err))
        sys.exit(1)
    return version
Example #19
0
 def market_data_handler(self, message):
     """
     Handles MarketData Messages received from server.
     :param message: Message received. Comes as a JSON.
     :type message: Dict.
     """
     try:
         self.process_market_data_message(message)
     except Exception:
         traceback.print_exec()
Example #20
0
def get_version(directory=None):
    packages = find_packages(basepath=directory if directory else os.getcwd())
    try:
        version = verify_equal_package_versions(packages.values())
    except RuntimeError as err:
        traceback.print_exec()
        error("Releasing multiple packages with different versions is "
              "not supported: " + str(err))
        sys.exit(1)
    return version
Example #21
0
 def set_openshift_gcp_project_from_google_creds(self):
     try:
         fh = open(os.environ.get('GOOGLE_APPLICATION_CREDENTIALS'))
         gcred = json.loads(fh.read())
         self.ocpinv().set_dynamic_cluster_var('openshift_gcp_project',
                                               gcred['project_id'])
     except:
         traceback.print_exec()
         raise Exception(
             "Unable to determine openshift_gcp_project from GOOGLE_APPLICATION_CREDENTIALS"
         )
 def Uninstall(self):
     if os.path.exists(self.xml_path_):
         command = "/usr/bin/pkginfo --rmd " + self.xml_path_
         os.system(command)
     try:
         for file_path in (self.icon_path_, self.execute_path_, self.xml_path_):
             if os.path.exists(file_path):
                 os.remove(file_path)
         print("Removing and unlinking files from installed locations [DONE]")
     except OSError:
         traceback.print_exec()
Example #23
0
 def analyze(self):
     jobs = list(self.conn.mining_seed_job.find().sort("_id", 1).skip(0).limit(100))
     jobs = list(self.conn.mining_seed_job.find())
     with open("../data/analyze.txt", "w") as fout:
         for job in jobs:
             try:
                 update_rate = self.frequency_analyze(job)
                 result = [str(job.get("_id")), job.get("url"), str(update_rate)]
                 fout.write("\t".join(result) + "\n")
             except Exception as e:
                 traceback.print_exec()
 def Uninstall(self):
   if os.path.exists(self.xml_path_):
     command = '/usr/bin/pkginfo --rmd ' + self.xml_path_
     os.system(command)
   try:
     for file_path in (self.icon_path_, self.execute_path_, self.xml_path_):
       if os.path.exists(file_path):
         os.remove(file_path)
     print('Removing and unlinking files from installed locations [DONE]')
   except OSError:
     traceback.print_exec()
Example #25
0
def inserirAutor(valor):
    try:
        banco = sqlite3.connect("Litterarius.db")
        cursor = banco.cursor()
        cursor.execute("INSERT INTO autores(autor) VALUES(?);", (valor, ))
        banco.commit()
        cursor.close()
        banco.close()
        print("autor inserido com sucesso")
    except:
        print("erro ao inserir autor")
        traceback.print_exec()
Example #26
0
def process_worker(task, aid):
    try:
        Accumulator.clear()
        logger.debug('run task %s %d', task, aid)
        result = task.run(aid)
        logger.debug('task %s result %s', task, result)
        accumUpdates = Accumulator.values()
        return (task, Success(), result, accumUpdates)
    except Exception, e:
        logger.debug('error in task %s, error: %s', task, e)
        import traceback
        traceback.print_exec()
        return (task, OtherFailure('exception:' + str(e)), None, None)
Example #27
0
def coinbase_sock_connect():
    ws = create_connection(BASEURL_COINBASE)
    ws.send(
        json.dumps({
            "type":
            "subscribe",
            "product_ids": ["BTC-USD"],
            "channels": [
                #      "full",
                "ticker",
                #      "level2",
                #      "heartbeat",
                {
                    "name": "ticker",
                    "product_ids": ["BTC-USD"]
                }
            ]
        }))

    try:
        stdscr = curses.initscr()
        curses.noecho()
        curses.cbreak()
        win1 = curses.newwin(30, 30, 0, 0)
        win1.border()
        while True:
            response = ws.recv()
            if 'subscriptions' in response:
                continue
            side = yaml.safe_load(response)['side']
            size = yaml.safe_load(response)['last_size']
            price = yaml.safe_load(response)['price']
            win1.addnstr(1, 2, price + ' ' + size, curses.A_BOLD)
            win1.refresh()
            ch = win1.getch()
            if ch == ord('q'):
                break


#      win1.clear()
            win1.clrtoeol()
            #      win1.clrtobot()
            win1.refresh()
    except:
        traceback.print_exec()
    finally:
        win1.keypad(0)
        #stdscr.keypad(0)
        curses.echo()
        curses.nocbreak()
        curses.endwin()
Example #28
0
def process_worker(task, aid):
    try:
        Accumulator.clear()
        logger.debug("run task %s %d", task, aid)
        result = task.run(aid)
        logger.debug("task %s result %s", task, result)
        accumUpdates = Accumulator.values()
        return (task, Success(), result, accumUpdates)
    except Exception, e:
        logger.debug("error in task %s, error: %s", task, e)
        import traceback

        traceback.print_exec()
        return (task, OtherFailure("exception:" + str(e)), None, None)
Example #29
0
    def on_turn_off(self, message):

        results = re.findall(self.off_zone_pattern, message.body)
        zone = None
        if len(results) > 0:
            zone = results[0].lower()
        if zone:
            reply_body = "I've turned %s zone off." % zone
            try:
                self.zone_state(zone, False)
            except exceptions.OSError, e:
                traceback.print_exec()
                reply_body = "I was unable to turn on the %s zone." % zone
                reply_body += '\n%s' % traceback.format_exc()
Example #30
0
def import_iepg(db, link):
    try:
        print "url = %s" % link
        _id = md5(link).hexdigest()
        epg = fetch_content(link)
        doc = parse_iepg(epg)
        doc["_id"] = _id
        doc["type"] = "iepg"
        doc["url"] = link
        print "   id = %s" % doc["_id"]
        print "title = %s" % doc["program-title"]
        insert_or_update(db, doc)
    except Exception, e:
        traceback.print_exec()
Example #31
0
def alterarTransportadora(id, transportadora, cnpj):
    try:
        banco = sqlite3.connect('Litterarius.db')
        cursor = banco.cursor()
        cursor.execute(
            "UPDATE transportadoras SET "
            "transportadora=?, CNPJ=? "
            "WHERE transportadoras_id=?", (transportadora, cnpj, id))
        print("transportadora alterada com sucesso")
        banco.commit()
        cursor.close()
        banco.close()
    except:
        print("erro ao alterar transportadora")
        traceback.print_exec()
Example #32
0
	def insert(self,json):
		response_code=200
		obj_id=None
		try:
			query={"email":json["email"]}
			total = self.user.find(query).count()
			if(total==0):
				obj_id = self.user.insert(json)
				obj_id = json["email"]
			else:
				response_code = 409
			return {'res_code':response_code,"id":obj_id}
		except:
			traceback.print_exec()
			return {'res_code':500,"id":obj_id}
Example #33
0
def run_parallel_scripts(scripts, scripts_dir, config_dir, send_result=True):
    """Run scripts in parallel."""
    fail_count = 0
    # Make sure custom networking is only applied when the running script
    # requests it. Scripts which don't require custom networking(default)
    # run first.
    non_netconf_scripts = []
    netconf_scripts = []
    for script in scripts:
        if script.get("apply_configured_networking"):
            netconf_scripts.append(script)
        else:
            non_netconf_scripts.append(script)
    for nscripts in [non_netconf_scripts, netconf_scripts]:
        try:
            with CustomNetworking(nscripts, config_dir, send_result):
                # Start scripts which do not have dependencies first so they
                # can run while other scripts are installing.
                for script in sorted(
                        nscripts,
                        key=lambda i: (
                            len(i.get("packages", {}).keys()),
                            i["name"],
                        ),
                ):
                    if not install_dependencies([script], send_result):
                        fail_count += 1
                        continue
                    script["thread"] = Thread(
                        target=run_script,
                        name=script["msg_name"],
                        kwargs={
                            "script": script,
                            "scripts_dir": scripts_dir,
                            "send_result": send_result,
                        },
                    )
                    script["thread"].start()
                for script in nscripts:
                    script["thread"].join()
                    if script.get("exit_status") != 0:
                        fail_count += 1
        except SignalException:
            fail_count += len(nscripts)
        except Exception:
            traceback.print_exec()
            fail_count += len(nscripts)
    return fail_count
def add_k8s_json_k8s_kpis_to_dict(json_str, d):
    try:
        parsed_json = json.loads(json_str)
    except:
        traceback.print_exec()
    items = parsed_json['items']
    timestamp = None
    for item in items:
        pod_name = item['metadata']['name']
        namespace = item['metadata']['namespace']
        # Use same timestamp for all entries to avoid strange behavior
        if timestamp is None:
            timestamp = item['timestamp']
        window = item['window']
        containers = item['containers']
        if window[-1] == 's':
            window = window[0:-1]
        window = int(window)
        for container in containers:
            container_name = container['name']
            cpu = container['usage']['cpu']
            memory = container['usage']['memory']
            # Originally in nano-cores. converting to full cores
            if cpu[-1] == 'n':
                cpu = float(cpu[0:-1]) / 1000000000
            else:
                # Case of "Zero"
                cpu = float(cpu)
            # Originally in KiB. Converting to GB
            if memory[-2:] == 'Ki':
                memory = memory[0:-2]
                memory = float(memory) * 1000 / 1024 / 1024 / 1024
            elif memory[-2:] == 'Mi':
                memory = memory[0:-2]
                memory = float(memory) * 1000 * 1000 / 1024 / 1024 / 1024
            else:
                # Case of "Zero"
                memory = float(memory)
            d['pod'].append(pod_name)
            d['container'].append(container_name)
            d['namespace'].append(namespace)
            d['timestamp'].append(dateutil.parser.isoparse(timestamp))
            # d['timestamp'].append(timestamp)
            d['window'].append(window)
            # Originally in nano-cores. converting to full cores
            d['cpu'].append(cpu)
            # In MB
            d['memory'].append(memory)
Example #35
0
def alterarPagamento():
    try:
        banco = sqlite3.connect('Litterarius.db')
        cursor = banco.cursor()
        cursor.execute(
            "UPDATE pagamentos SET "
            " pago=?"
            " WHERE pagamentos_id=? AND"
            " parcelas_id = ?", ())
        print("parcela alterada com sucesso")
        banco.commit()
        cursor.close()
        banco.close()
    except:
        print("erro ao alterar parcela")
        traceback.print_exec()
Example #36
0
    def callback_tpc_visual(self, data):

        #objDetects = data.objDetects
        #peopleDetects = data.peopleDetects
        #peopleRecog = data.peopleRecog
        #self.ooi = data.ooi                 # bool

        if self.visual_cb_flag is False:
            header = data.header
            self.roi = (data.roi.x, data.roi.y, data.roi.height,
                        data.roi.width)
            self.motion_detected = data.motion_detected
            print(
                "****call back execute once. get pub msg. motion_detected/roi ",
                self.motion_detected, self.roi)
            if self.motion_detected and (not self.motion_detect_block):
                #self.actGoal.goal_id = "actPattern"
                #self.actGoal.header =
                self.actGoal.op = actPatternGoal.PATTERN_OPERATION
                self.actGoal.pattern = actPatternGoal.VISUAL_TRACK
                self.actGoal.visual_track_switch = True

                (self.actGoal.roi.x, self.actGoal.roi.y,
                 self.actGoal.roi.height, self.actGoal.roi.width) = self.roi
                print(
                    '===CMD MOVE=== Will do client req in callback. switch is:, x is:',
                    self.actGoal.visual_track_switch, self.actGoal.roi.x)
                res = self.act_client_req(self.actGoal)

                if actPatternResult.SUCCEEDED == res.result:
                    self.mycortexpMsg.evt_visual_track = cortexpMsg.VTRACK_DONE
                    try:
                        self.cortexp_pub.publish(self.mycortexpMsg)
                    except:
                        traceback.print_exec()
                        print("~~~~~~~~~~~~ DBG:: Pub Once!~~~~~~~~~~~")
            else:
                self.actGoal.visual_track_switch = False
                print(
                    '=--CMD NO MOVE--=Will do client req in callback. switch is:, x is:',
                    self.actGoal.visual_track_switch, self.actGoal.roi.x)
                #res = self.act_client_req(self.actGoal)

            self.visual_track_switch_last = self.motion_detected  # for memory

        # Set the flag
        self.visual_cb_flag = True
Example #37
0
        def handle(self):
            print(self.__class__.__name__ + self.handle.__name__ + " start")
            print("Curent thread name:{}".format(
                threading.current_thread().name))
            try:
                while True:
                    bytes = self.rfile.readline().strip()
                    if len(bytes) == 0:
                        break

                    ZSingleton.get_instance().request_handle_callback(
                        bytes, self.wfile)

                self.request.close()

            except:
                traceback.print_exec()
Example #38
0
 def modify(self, node, attr, value, c_attr=None, c_value=None):
     try:
         nd = self.root.findall(node)
         if nd is None:
             return
         for n in nd:
             if c_attr:
                 if n.attrib[c_attr] == c_value:
                     print(value)
                     n.attrib[attr] = value
             else:
                 n.attrib[attr] = value
         self.tree.write(self.doc)
         return True
     except:
         traceback.print_exec()
         return False
Example #39
0
def root (request):
#  return HttpResponse ("Tile Map Service")
  try:
    baseURL = request.build_absolute_uri ()
    xml = []
    xml.append ('<?xml version="1.0" encoding="utf-8"  ?>' ) 
    xml.append ('<Services>' ) 
    xml.append ('  <TileMapService '  + 
                '       title="ShapeEditor Tile Map Service" '     + 
                '       version="1.0" href="' + baseURL + '/1.0"  />' ) 
    xml.append  ('</Services>') 
    traceback.print_exc
    return HttpResponse ("\n".join (xml), mimetype="text/xml" ) 

  except:
    traceback.print_exec ()
    return HttpResponse (" ")
Example #40
0
def root(request):
    #  return HttpResponse ("Tile Map Service")
    try:
        baseURL = request.build_absolute_uri()
        xml = []
        xml.append('<?xml version="1.0" encoding="utf-8"  ?>')
        xml.append('<Services>')
        xml.append('  <TileMapService ' +
                   '       title="ShapeEditor Tile Map Service" ' +
                   '       version="1.0" href="' + baseURL + '/1.0"  />')
        xml.append('</Services>')
        traceback.print_exc
        return HttpResponse("\n".join(xml), mimetype="text/xml")

    except:
        traceback.print_exec()
        return HttpResponse(" ")
Example #41
0
    def train(self):
        try:
            init_diar = Diar.read_seg(self.input_seg)
            #init_diar = segmentation.self.init_seg(cep, show)
            init_diar.pack(50)
            Diar.write_seg(self.init_seg, init_diar)
            gd_diar = segmentation.segmentation(self.cep, init_diar,
                                                self.win_size)
            Diar.write_seg(self.gd_seg, gd_diar)
        except Exception as e:
            traceback.print_exec()
            print("initialziation fault")

        #performing experiment
        self.create_seg_bic_linear(self.cep, gd_diar)
        self.create_seg_bic_hac(self.cep, self.linear_bic_dir)
        self.create_seg_iv_AHC(self.bic_hac_dir, self.input_show)
        self.create_seg_viterbi(self.cep, self.hac_iv_dir)
Example #42
0
def shitmex_sock_connect():
    ws = create_connection(BASEURL_SHITMEX)
    ws.send(json.dumps({"op": "subscribe", "args": ["trade:XBTUSD"]}))

    while True:
        response = ws.recv()
        if 'action' in response and yaml.safe_load(
                response)['action'] == "partial":
            print("Exiting the while loop, because \"action\":\"partial\"")
            break

    try:
        stdscr = curses.initscr()
        curses.noecho()
        curses.cbreak()
        win1 = curses.newwin(30, 30, 0, 0)
        win1.border()
        while True:
            response = ws.recv()
            if 'data' in response:
                #        timestamp = yaml.safe_load(response)['data'][0]['timestamp']
                #        symbol = yaml.safe_load(response)['data'][0]['symbol']
                #        side = yaml.safe_load(response)['data'][0]['side']
                price = yaml.safe_load(response)['data'][0]['price']
                size = yaml.safe_load(response)['data'][0]['size']
                win1.addnstr(1, 2, str(price) + ' ' + str(size), curses.A_BOLD)
                ch = win1.getch()
                #  win1.clear()
                win1.clrtoeol()
                ch = stdscr.getch()
                if ch == ord('q'):
                    break
                win1.refresh()
            #  win1.clrtobot()
#        win1.refresh()
    except:
        traceback.print_exec()
    finally:
        win1.keypad(0)
        #stdscr.keypad(0)
        curses.echo()
        curses.nocbreak()
        curses.endwin()
Example #43
0
 def load_weight_list(self, weight_list):
   try:
     with open(weight_list) as f:
       for line in f.readlines():
         values = line.strip().split('\t')
         if not 4 == len(values):
           print 'Invalid record: %s!' % line.strip()
           continue
         point = DictItem()
         point.id = values[0]
         point.weight = float(values[1])
         point.ent = values[2]
         point.query = values[3]
         self._weight_hash[point.id] = point
   except IOError as e:
     print '-' * 60
     traceback.print_exec(file = sys.stdout)
     print '-' * 60
     exit(-1)
Example #44
0
def pexRun(host,cmd,password):
    try:
        child = pexpect.spawn(cmd)
        index = child.expect(['password:'******'continue connecting (yes/no)?',pexpect.EOF, pexpect.TIMEOUT])
        if index == 0:
            child.sendline(password)
            child.interact()
        elif index == 1:
            child.sendline('yes')
            child.expect(['password:'])
            child.sendline(password)
            child.interact()
        elif index == 2:
            print " %s 子程序异常,退出!" %host
            child.close()
        elif index == 3:
            print "%s 连接超时" %host
    except:
        traceback.print_exec()
Example #45
0
 def GetMethodNodekey(self, method):
     """
     Get a method node key string from a given method
     
     Parameters
     ------------
     method: EncodedMethod 
     
     Return
     ------------
     TYPE: ""
     CONTENT: the key string of specific node
     """
     key = ""
     try:
         key = "%s %s %s" % (method.get_class_name(), method.get_name(), method.get_descriptor())
     except Exception, e: 
         print "[E]Given method exception!"          
         traceback.print_exec()
Example #46
0
 def load_weight_list(self, weight_list):
     try:
         with open(weight_list) as f:
             for line in f.readlines():
                 values = line.strip().split('\t')
                 if not 4 == len(values):
                     print 'Invalid record: %s!' % line.strip()
                     continue
                 point = DictItem()
                 point.id = values[0]
                 point.weight = float(values[1])
                 point.ent = values[2]
                 point.query = values[3]
                 self._weight_hash[point.id] = point
     except IOError as e:
         print '-' * 60
         traceback.print_exec(file=sys.stdout)
         print '-' * 60
         exit(-1)
Example #47
0
 def insert(self, sql_str):
     if len(sql_cache) < cache_max_num:
         self.sql_cache.append(sql_str)
         return 0
     else:
         cursor = conn.cursor()
         try:
             for sql_item in self.sql_cache:
                 cursor.execute(sql_item)
             conn.commit()
             cursor.close()
             conn.close()
             self.sql_cache = [] # reset sql cache
         except:
             print 'insert failed'
             conn.rollback()
             traceback.print_exec()
             sys.exit(1) 
         else:
             return 0
Example #48
0
    def get_authkey(self, ident):
        with self.mongo:
            try:
                res = self.mongo.hpfeeds.auth_key.find_one({"identifier":ident})
            except:
                import traceback
                traceback.print_exec()
                return NONE
            finally:
                self.mongo.close()

        if not res: return None

        subchans = res.get('subscribe')
        pubchans = res.get('publish')
        secret = res.get('secret')
        ident = res.get('identifier')

        pubchans = json.dumps(pubchans)
        subchans = json.dumps(subchans)

        return dict(secret=secret, ident=ident, pubchans=pubchans, subchans=subchans, owner=ident)
Example #49
0
def get_upstream_meta(upstream_dir):
    meta = None
    # Check for stack.xml
    stack_path = os.path.join(upstream_dir, 'stack.xml')
    info("Checking for package.xml(s)")
    # Check for package.xml(s)
    try:
        from catkin_pkg.packages import find_packages
        from catkin_pkg.packages import verify_equal_package_versions
    except ImportError:
        error("catkin_pkg was not detected, please install it.",
              file=sys.stderr)
        sys.exit(1)
    packages = find_packages(basepath=upstream_dir)
    if packages == {}:
        info("package.xml(s) not found, looking for stack.xml")
        if os.path.exists(stack_path):
            info("stack.xml found")
            # Assumes you are at the top of the repo
            stack = parse_stack_xml(stack_path)
            meta = {}
            meta['name'] = [stack.name]
            meta['version'] = stack.version
            meta['type'] = 'stack.xml'
        else:
            bailout("Neither stack.xml, nor package.xml(s) were detected.")
    else:
        info("package.xml(s) found")
        try:
            version = verify_equal_package_versions(packages.values())
        except RuntimeError as err:
            traceback.print_exec()
            bailout("Releasing multiple packages with different versions is "
                    "not supported: " + str(err))
        meta = {}
        meta['version'] = version
        meta['name'] = [p.name for p in packages.values()]
        meta['type'] = 'package.xml'
    return meta
Example #50
0
def register(request):
    result={
        'isSuccessful':False
    }
    try:
        data = json.loads(request.body)
        username = data['username']
        print username
        password = data['password']
        print password
        if isUsernameValid(username):
            user = User(username=username)
            user.password = make_password(password)
            user.save()
            result['isSuccessful'] = True
        else:
            result['isSuccessful'] = False
            result['error'] = 'Invalid username or password'
    except:
        traceback.print_exec()
        result['isSuccessful'] = False
        result['error'] = 'Invalid username or password'
    finally:
        return HttpResponse(json.dumps(result), content_type="application/json")
Example #51
0
	def record_handle(self, t):
		global threadPlotManager
		global readingMap
		global record
		global record_name
		global gui

		global mlock

		global record_cnt

		global old_seq


		key = ""

		if len(t) == 5 and t[0] == 'S' and t[1] == 'G':
			value = ord(t[2])
			if value:
				gui = True
			else:
				gui = False
			return

		if (len(t) == 14 and t[0] == 'A' and t[1] == 'T'):
			length = ord(t[2])
			node_id = ord(t[3])
			key = "%s_%s" % (self.host, node_id)
#			if key not in readingMap:
#				continue
#			with mlock:
#				self.create_entry(self.host, node_id)
			(month, day, year, hour, minute, sec, msec)= struct.unpack('<bbbbbbH',t[4:12])
			"""
			if msec > 1000:
				msec = 0
			cur_t = datetime.now()
			if month > 12 or month < 1:
				month = cur_t.month
			if day > 31 or day < 1:
				day = cur_t.day
			if hour > 23 or hour < 0:
				hour = cur_t.hour
			if minute > 59 or minute < 0:
				minute = cur_t.minute
			if sec > 59 or sec < 0:
				sec = cur_t.second
			"""
			cur_t = datetime.now()
#			month = cur_t.month
#			day = cur_t.day
#			year = cur_t.year
			for key in readingMap.keys():
				if key.split('_')[0] == self.host:
					old_time = readingMap[key]['time']
					new_time = datetime(year + 2000, month, day, hour, minute, sec, msec * 1000)
					if new_time == old_time:
						print "SAME TIME"
					if new_time < old_time:
						td = new_time - old_time
						if td > timedelta(seconds = 1):
							print key, "time stamp is old ", new_time, old_time
							self.sendTimePkt(self.host)
					try:
						if new_time > old_time:
							print self.host, key, "Update Time", new_time, old_time
							readingMap[key]['time'] = new_time
					except Exception:
						print "TIME FORMAT ERROR"
						errlog = open('error.log', 'a+')
						traceback.print_exec(file=errlog)
						errlog.write("%d %d %d %d %d %d %d\n" % year, month, day, hour, minute, sec, msec)
						errlog.flush()
						errlog.close()
			debug = True
			if debug:
				print "Host:%s\t" % self.host,
#				print "LEN: %3d\t" % length,
				print "ID: 0x%02X\t" % node_id,
#				for key in readingMap.keys():
				if key in readingMap:
					print "Time: %s" % readingMap[key]['time'],
				print ""
			return

		if (len(t) > 13 and t[0] == 'A' and t[1] == 'D'):
			record_cnt += 1
			length = ord(t[2])
			node_id = ord(t[3])
			key = "%s_%s" % (self.host, node_id)
			with mlock:
				self.create_entry(self.host, node_id)
			seq = ord(t[4])
			debug = False
			if debug:
				if old_seq:
					if seq == old_seq:
						print "!!!!! seq is the same"
					elif seq > old_seq:
						if seq - old_seq > 1:
							print "!!!!!!Lost", (seq - old_seq), seq, old_seq
					else:
						if seq + 256 - old_seq > 1:
							print "!!!!!!Lost", (seq + 256 - old_seq), seq, old_seq
					old_seq = seq
				else:
					old_seq = seq
			value = struct.unpack('>hhh', t[5:11])
			timestamp = struct.unpack('<H',t[11:13])
			ts = timestamp[0] % 1000
			d = timedelta(milliseconds = ts)

			for idx in range(3):
				readingMap[key]['data'][idx].update_reading(value[idx])
			prev_rtime = readingMap[key]['time']
			timediff = 1000 / readingMap[key]['sps']
			if timediff < 1:
			 	timediff = 1
			exp_d = timedelta(milliseconds = timediff)
			exp_rtime = prev_rtime + exp_d
			rtime = datetime(prev_rtime.year, prev_rtime.month, prev_rtime.day, prev_rtime.hour, prev_rtime.minute, prev_rtime.second, ts * 1000)
			if ((exp_rtime - rtime) > exp_d):
				tmp_rtime = rtime + timedelta(seconds = 1)
				if (tmp_rtime - exp_rtime) < timedelta(milliseconds = (timediff * 3)):
#					print self.host, "RTIME SLOW", rtime, exp_rtime
#					print "Correct to", tmp_rtime
					rtime = tmp_rtime
				else:
					print self.host, "RTIME SLOW", rtime, exp_rtime

#				if exp_rtime.second - rtime.second == 1:
#					if abs(ts - (exp_rtime.microsecond/ 1000)) < timediff:
#						rtime = datetime(exp_rtime.year, exp_rtime.month, exp_rtime.day, exp_rtime.hour, exp_rtime.minute, exp_rtime.second, ts * 1000)
#						print "Correct to", timediff, rtime
			readingMap[key]['time'] = rtime
#			if re.search("111", key):
#				print "%02d:%02d:%02d.%03d\t" % (rtime.hour, rtime.minute, rtime.second, rtime.microsecond / 1000)
#				print "RTIME ", rtime, exp_rtime
#			readingMap[key]['time'] = rtime
			debug = False
			if record:
				output = "%3d\t[%03d]\t" % (node_id, seq)
				output += "%02d:%02d:%02d.%03d\t" % (rtime.hour, rtime.minute, rtime.second, rtime.microsecond / 1000)
				for idx in range(3):
					output += "%5d\t" % value[idx]
				output += "\n"
				readingMap[key]['fd'].write(output)
			if debug:
				print "Host:%s\t" % self.host,
				print "ID: %3d\t" % node_id,
				print "seq:%3d\t" % seq,
				print value, rtime

			if record:
				cur_t = datetime.now()
				if key not in readingMap:
					print "no key ", key
					return
				if "ftime" not in readingMap[key]:
					print "not ftime"
				if cur_t - readingMap[key]['ftime'] >= timedelta(minutes = 1):
#				if cur_t - readingMap[key]['ftime'] >= timedelta(seconds = 10):
						if readingMap[key]['save_order'] == (cur_t.minute % 5):
							print "log switching ", key, readingMap[key]['save_order']
							fname = readingMap[key]['fname']
							readingMap[key]['fname'] = ""
							p = multiprocessing.Process(target = closeFile, args = (readingMap[key]['fd'], fname))
							p.start()
							p.join(None)
#					print "save processing done"
							readingMap[key]['fd'] = None
#					readingMap[key]['fd'].flush()
#					readingMap[key]['fd'].close()
							host = key.split('_')[0]
							sid = key.split('_')[1]
							self.create_entry(host, sid)
#			p = multiprocessing.Process(target = relay, args = (fname,))
#			p.start()
			return

		print "invalid pkt", len(t)
		for c in t:
			if c == 'A' or c == 'D' or c == 'T':
				print c,
			else:
				print "0x%02X" % ord(c),
		print ""
Example #52
0
def load_trec_corpus (file_path) :
  '''
  Load the corpus in TREC format

  file_path: string filesystem path to the corpus file
  '''
  global DB_CON
  global doc_id_dict

  db_cur = DB_CON.cursor()

  doc_imported = 0
  so_far = 0
  total = os.popen("grep \"<DOC>\" "+WARC_CORPUS_FILE+" | wc -l").read()
  total = int(total.rstrip())

  is_begin = False
  try:
    with open(file_path) as f:
      print '[Info] Loading %s' % file_path

      doc_id = ''
      str_list = []
      for line in f:
        #line = line.strip()
        if re.match(r'<DOC>', line):
          continue
        if re.match(r'<DOCNO>', line):
          mo = re.match(r'<DOCNO>(.+)<\/DOCNO>', line)
          doc_id = mo.group(1)
          continue
        if re.match(r'<TEXT>', line):
          continue
        if re.match(r'<\/TEXT>', line):
          continue
        if re.match(r'<\/DOC>', line):
          so_far += 1
          progress = '\r[%10d / %10d]' % (so_far, total)
          sys.stdout.write(progress)
          sys.stdout.flush()

          # skip documents not in the doc_ret_dict
          if doc_id in doc_id_dict :
            ## import the document to DB
            doc_data = ''.join(str_list)

            if import_doc(db_cur, doc_id, doc_data) :
              doc_imported += 1

          ## clear the list: http://stackoverflow.com/a/850831/219617
          del str_list[:]
          # for debug purpose only
          #if doc_imported >= 500:
            #break

          ## perform commit every 100 documents
          if 0 == doc_imported % 100 :
            do_commit()
            ## for debug purpose only
            #break

        else:
          ## add the current string to str_list
          str_list.append(line)

  except IOError as e:
    print '-' * 60
    print 'Error %d: %s' % (e.args[0],e.args[1])
    traceback.print_exec(file = sys.stdout)
    print '-' * 60
    exit(-1)

  do_commit()
  print '\n[Info] Summary:'
  print '[Info] %d documents imported' % doc_imported
Example #53
0
Ice.loadSlice('Printer.ice')
import Demo

class PrinterI(Demo.Printer):
    def printString(self, s, current=None):
        print(s)

status = 0
ice = None
try:
    ic = Ice.initialize(sys.argv)
    adapter = ic.createObjectAdapterWithEndpoints("SimplePrinterAdapter", "default -h localhost -p 10000")
    object = PrinterI()
    adapter.add(object, ic.stringToIdentity("SimplePrinter"))
    adapter.activate()
    ic.waitForShutdown()
except:
    traceback.print_exc()
    status = 1

if ic:
    # Clean up
    try:
        ic.destroy()
    except:
        traceback.print_exec()
        status = 1

sys.exit(status)
Example #54
0
def send_data(addr, message): #{{{
	try:
		sock.sendto(message + "\x00", addr)
	except:
		if debug: traceback.print_exec()