def upload(pnum, pind, ffiles, fpieces, fbytes,  buf):

        piece = tor.pieces[pind]
        url = piece.upload_url

        try:
            r = js._session.post(url=url, params={"api_key" : js._api_key}, files={'piece_file' : ("piece_file", buf) } )
            r.raise_for_status()

            bs = ET.fromstring(r.content)

            log(DEBUG3, "issueAPIRequest: node %r" % bs)

            status = bs.find("status")
            log(DEBUG2, "status=%r" % status)

            if status is None:
                raise APIError("%s protocol failure!"% url)

            if status.text != "SUCCESS":
                m = bs.find("message")
                h = bs.find("info_hash")
                if h is not None and m is not None:
                    raise APIError("%s failed: %s (info_hash=%s)!"% (url, unicode(urllib.unquote(m.text)), unicode(urllib.unquote(h.text))))
                elif m is not None:
                    raise APIError("%s failed: %s!"% (url, unicode(urllib.unquote(m.text))))
                else:
                    raise APIError("%s failed!"% url)


        except Exception, e:
            print "Caught %s uploading piece %d, aborting" % (e, pind)
Exemplo n.º 2
0
    def setcheckProgress(self, tor, npieces, piecesChecked, downloadedFiles, downloadedPieces, downloadedBytes):
        log(DEBUG3)
        tor.checkProgress = piecesChecked / float(npieces)
        tor.checkPieces = downloadedPieces
        tor.checkPercentage = downloadedBytes / float(tor.size)

        return self._quitPending
Exemplo n.º 3
0
 def delete(self):
     log(DEBUG)
     
     self._torrent.delete()
     
     if self._aria:
         self._aria.delete()
Exemplo n.º 4
0
    def updatePeers(self, force = False):
        if time.time() < self._peersValidUntil and not force:
            return

        log(DEBUG)

        try:
            bs = issueAPIRequest(self._jsit(), "/torrent/peers.csp", params = {"info_hash" : self._hash})

            fieldmap = {"direction" : "direction",
                        "ip_address" : "ip_address",
                        "peer_id" : "peer_id",
                        "percentage_as_decimal" : "percentage",
                        "port" : "port"
                        }


            self._peers = []

            for r in bs.find_all("row"):

                t = TPeer(self)
                fillFromXML(t, r, fieldmap)
                t.cleanupFields()

                self._peers.append(t)

            self._peersValidUntil = time.time() + peerValidityLength

        except Exception,e :
            log(ERROR, u"Caught exception %s updating peers for torrent %s!\n" % (e, self._name))
Exemplo n.º 5
0
    def markMsgRead(self, message_num):

        # unpack the seen cookie
        seencookiename = "%s.WR" % self.listname
        seencookie = self.ncgi.hdf.getValue("Cookie.%s" % seencookiename, "")
        if seencookie:
          c_parts = string.split(seencookie,",")
        else:
          c_parts = []
        mnum_str = "%s" % message_num

        try:
          index = c_parts.remove(mnum_str)
          log("already seen in cookie: %s" % message_num)
        except ValueError:
          log("markread: %s" % message_num)
          # yes, it's new!
          
          # make a new seen cookie! (only 200 entries)
          c_parts.insert(0,mnum_str)
          new_seencookie = string.join(c_parts[:200],",")
          self.ncgi.cookieSet(seencookiename,new_seencookie,persist=1)

          # add to whichread DB
          self.addToDB(message_num)

          # append to whichread log
          fp = open("%s/whichreadchanges.log" % self._path,"ab+")
          fp.write("%s %s\n" % (self._whichReadID,mnum_str))
          fp.close()
Exemplo n.º 6
0
    def isonlineServers(self):
        log(" *** isonlineServers()", 'debug')
        cur_time = time.strftime("%Y-%m-%d - %H:%M.%S")
        output = " {0}\n NETWORK MONITOR\n {1}\n {0}\n\n[SERVERS]\n".format('-'*21, cur_time)
        q = queue.Queue()
        
        for server in self.servers:
            p  = Async_ping(q, server, self.margin)
            p.run()
            q.put(p)
            output += p.output + '\n'
            log(p.server + ': ' + p.status, p.loglevel)

        if self.host:
            website_status = self.isonlineWebsite()
            output += website_status

        if self.mailsent:
            output += '   :) Mail sent at {0} because:\n   "{1}" was offline with error {2}.'.format(
                self.mailsent_time, self.host, self._website_error)
        elif self.mailsent == False:
            output += '   :( Could not send email at {0}, error {1}\n   (Website error was: {2})'.format(
                self.mailsent_time, self.mail_error, self._website_error)
        
        for i in range(1,self.polling):
            os.system("cls")
            next_in = "\n\n\n\n ===========\n Next in: {0}  \n ===========\n {1}".format(
                str(self.polling-i), self.__repr__())
            print(output + next_in)
            time.sleep(1)
Exemplo n.º 7
0
 def stringsHDF(self, prefix, locations, lang='en', exist=0, tiered=0):
     hdf = neo_util.HDF()
     if exist and lang == 'en': return hdf
     done = {}
     locations.sort()
     maps = self.tdb.maps.fetchRows( ('lang', lang) )
     maps_d = {}
     for map in maps:
         maps_d[int(map.string_id)] = map
     strings = self.tdb.strings.fetchRows()
     strings_d = {}
     for string in strings:
         strings_d[int(string.string_id)] = string
     count = 0
     for loc in locations:
         s_id = int(loc.string_id)
         if done.has_key(s_id): continue
         try:
             s_row = maps_d[s_id]
             if exist: continue
         except KeyError:
             try:
                 s_row = strings_d[s_id]
             except KeyError:
                 log("Missing string_id %d, skipping" % s_id)
                 continue
         count = count + 1
         if tiered:
             hdf.setValue("%s.%d.%d.%s" % (prefix, int(s_id) / TIER1_DIV, int(s_id) / TIER2_DIV, s_id), s_row.string)
         else:
             hdf.setValue("%s.%s" % (prefix, s_id), s_row.string)
         done[s_id] = 1
     if exist == 1: log("Missing %d strings for lang %s" % (count, lang))
     return hdf
Exemplo n.º 8
0
def sendmail(BOUNCE_RETURN_ADDRESS,TO_ADDRESS_LIST,BODY):
    global DISABLED
    if DISABLED:
        log ("Not sending to %s" % repr(TO_ADDRESS_LIST))
        return
    try:
        mod_to_list = []
        for address in TO_ADDRESS_LIST:
            mod_to_list.append(shell_escape(address))

        mod_bounce_address = shell_escape(BOUNCE_RETURN_ADDRESS)
            
        cmd = "/usr/lib/sendmail -oi -f'%s' -- %s" % (mod_bounce_address,
                                            string.join(mod_to_list, ' '))
        fp = os.popen(cmd, "w", 16384)
        fp.write(BODY)
        r = fp.close()
        if not r: return
        if os.WIFEXITED(r):
            if os.WEXITSTATUS(r):
                raise "cmd '%s' returned %d" % (cmd, os.WEXITSTATUS(r))
        elif os.WIFSIGNALED(r):
            raise "cmd '%s' ended on signal %d" % (cmd, os.WTERMSIG(r))
        elif os.WIFSTOPPED(r):
            raise "cmd '%s' ended on signal %d" % (cmd, os.WSTOPSIG(r))
    except IOError:
        delayed_queue(BOUNCE_RETURN_ADDRESS, TO_ADDRESS_LIST, BODY)
    except:
        import handle_error
        handle_error.handleException("Unable to send message")
        delayed_queue(BOUNCE_RETURN_ADDRESS, TO_ADDRESS_LIST, BODY)
Exemplo n.º 9
0
 def __init__(self, cleanupLeftovers = False, port = 6800, private = False):
     
     log(DEBUG, "Starting aria process...\n");
     
     # Start aria process
     self._username = "******" + str(random.randint(0,100000)) * private
     self._password = "******" + str(random.randint(0,100000)) * private
     
     
     self._proc = subprocess.Popen(["aria2c", "--enable-rpc=true", "--rpc-user="******"--rpc-passwd="+self._password, "--rpc-listen-port=%d" % port], 
                                   stdout=subprocess.PIPE)
     self._server = xmlrpclib.ServerProxy('http://%s:%s@localhost:%d/rpc' % (self._username, self._password, port))
     
     socket.setdefaulttimeout(5) # Do a quick timeout to catch problems
     
     # wait for server to start up
     running = False
     while not running:
         try:
             self._server.aria2.tellActive()
             running = True
         except IOError:
             time.sleep(0.2)
         except xmlrpclib.ProtocolError, e:
             log(ERROR, u"Couldn't connect to aria process. Is an old one still running? Aborting...\n")
             raise e
Exemplo n.º 10
0
Arquivo: test.py Projeto: jeske/netcon
    def test_setup(self):
        ndb = self.ndb
        # feed in machines
        t1 = ndb.machines.getMachine("t1")
        t4 = ndb.machines.getMachine("t4")
        t5 = ndb.machines.getMachine("t5")

        # set machine roles...
        allroles = ndb.roles.fetchAllRows()
        if not allroles:
            role = ndb.roles.newRow()
            role.name = "testrole"
            role.is_shared=1
            role.save()

            allroles = [role]

        for a_role in allroles:
            ndb.mach_roles.addMachineRole(t1,a_role)
            ndb.mach_roles.addMachineRole(t4,a_role)
            ndb.mach_roles.addMachineRole(t5,a_role)

            # create role triggers
            cur_triggers = ndb.role_triggers.fetchRows( ('role_id', a_role.role_id) )
            if not cur_triggers:
                ntrigger = ndb.role_triggers.newRow()
                ntrigger.role_id = a_role.role_id
                serv_for_trig = ndb.services.getService("disk/size:cur")
                log("serv_for_trig: %s" % serv_for_trig.serv_id)
                ntrigger.serv_id = serv_for_trig.serv_id
                ntrigger.level = "1"
                ntrigger.source_pattern = "sda1"
                ntrigger.save()
Exemplo n.º 11
0
    def percentage(self):
        # This can happen if everything has been downloaded already.
        if len(self._gids) == 0:
            return 100.
            
        total = 0.
        completed = 0.
        
        # Use multicall to reduce pressure on aria2
        mc = xmlrpclib.MultiCall(self._aria()._server)
        
        for g in self._gids:
            mc.aria2.tellStatus(g, ["completedLength", "totalLength"])

        res = mc()
                
        for i,s in enumerate(res):
            log(DEBUG2, "%s: total=%s completed=%s\n" % (self._gids[i], s["totalLength"], s["completedLength"]))
            total     += int(s["totalLength"])
            completed += int(s["completedLength"])
       
        # Do we know the full size a priory?
        if self._fullsize:
            total = self._fullsize
            completed += self._finishedBytes    # Add completed a priori parts
        
        # This can happen at startup, before any sizes are known
        if total == 0:
            return 0
        
        # Make sure we return 100 for completion. Float math is tricky.
        if total == completed:
            return 100.
            
        return completed * 100. / total
Exemplo n.º 12
0
    def computeTrendTime_Linear(self,serv_id,source_id,target):
	# 0. load data

	h_data = self.ndb.monitor_history.fetchRows(
	    [ ('serv_id', serv_id),
	      ('source_id', source_id)] )

        # we have to have data to compute trends!
        if len(h_data) < 10:
            return -1

        coeff = self._linCoeff(h_data)

	def lin_f(x,par):
	    m,b = par
	    return (m*x) + b

	def lin_fsolve(y,par):
	    m,b = par
	    return (y-b) / m 

	b,m = coeff
	values = m,b

        try:
	    at = lin_fsolve(float(target),values)
            time_to_target = (at - time.time())
            log("time_to_target = " + str(time_to_target))
        except ZeroDivisionError:
            time_to_target = -1
	return time_to_target
Exemplo n.º 13
0
def main(argv, stdout, environ):
  progname = argv[0]
  optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])

  testflag = 0
  if len(args) != 0:
    usage(progname)
    return

  lock = do_lock(DISCUSS_DATA_ROOT, "archiver.lock")

  global DONE

  #signal.signal(signal.SIGTERM, handleSignal)

  log("archiver: start")
  try:
    while not DONE:
        try:
            archive_dirs()
        except:
            handle_error.handleException("Archiver Error")
        if DONE: break
        # tlog("sleeping")
        time.sleep(10)
  finally:
    os.unlink(os.path.join(DISCUSS_DATA_ROOT, "archiver.lock"))
Exemplo n.º 14
0
 def log (self, level = None, msg = None):
     if msg is None:
         msg = self.message
     if level is None:
         level = self.level
         
     log (self.__class__.__module__, level, msg)
Exemplo n.º 15
0
    def loadMap(self, file, prefix, lang):
        log("Loading map for language %s" % lang)
        hdf = neo_util.HDF()
        hdf.readFile(file)
        obj = hdf.getChild(prefix)
        updates = 0
        new_r = 0
        while obj is not None:
            s_id = obj.name()
            str = obj.value()

            try:
                map_r = self.tdb.maps.fetchRow( [('string_id', s_id), ('lang', lang)])
            except odb.eNoMatchingRows:
                map_r = self.tdb.maps.newRow()
                map_r.string_id = s_id
                map_r.lang = lang
                new_r = new_r + 1

            if map_r.string != str:
                updates = updates + 1
                map_r.string = str
                map_r.save()

            obj = obj.next()
        log("New maps: %d  Updates: %d" % (new_r, updates - new_r))
Exemplo n.º 16
0
    def start(self):
        SHOULD_DISPLAY = 1
        if self._israwpage:
            SHOULD_DISPLAY = 0
        
        ncgi = self.ncgi
        
        if self.readDefaultHDF:
            try:
                if not self.pagename is None:
                    ncgi.hdf.readFile("%s.hdf" % self.pagename)
            except:
                log("Error reading HDF file: %s.hdf" % (self.pagename))

        DISPLAY_ERROR = 0
        ERROR_MESSAGE = ""
        # call page main function!
        try:
            self.main()
        except DisplayDone:
            SHOULD_DISPLAY = 0
        except Redirected:
            # catch redirect exceptions
            SHOULD_DISPLAY = 0
        except DisplayError, num:
            ncgi.hdf.setValue("Query.error", str(num))
            if self._error_template:
                ncgi.hdf.setValue("Content", self._error_template)
            else:
                DISPLAY_ERROR = 1
Exemplo n.º 17
0
def geocode_by_semantics(project, address, port):
    from pymongo import MongoClient
    client = MongoClient(address, port)
    db = client[project]
    search_json = {'$or': [{'latlng': [0, 0]}, {'latlng': [-1, -1]}], 'verified': True}
    users = db.users.find(search_json)
    count = db.users.find(search_json).count()
    print count
    i = 0
    for user in users:
        i += 1
        verified_info = user['verified_info']
        username = user['username']

        verified_info = verified_info.replace(u'主持人', '').replace(u'职员', '').replace(u'院长', '').replace(u'经理', '')
        verified_info = verified_info.split(u' ')[0]
        if verified_info == u'前' or u'www' in verified_info or u'律师' in verified_info or u'学者' in verified_info or u'作家' in verified_info or u'媒体人' in verified_info or u'诗人' in verified_info:
            verified_info = ''
        locational_info = verified_info
        if locational_info == '':
            locational_info = username
        if verified_info != '':
            latlng = geocode(verified_info)
        else:
            continue

        log(NOTICE, '#%d geocode the user by its semantic info %s. %d posts remain. latlng: %s ' % (i, verified_info.encode('gbk', 'ignore'), count - i, str(latlng)))

        if latlng[0] != -1 and latlng[0] != 0:
            db.users.update({'userid': user['userid']}, {'$set': {'latlng': latlng}})

    log(NOTICE, "mission compeletes.")
Exemplo n.º 18
0
 def piecePriority(self, p):
     prio = self.priority + (1. / math.log10(max(self._size - self.downloadedBytes, 0) + 10)) * 1000
     if self._pdl().hasServerFailed(p.url):
         prio -= 10000000
     
     log(DEBUG2, "%s (%s) self._size=%d self.downloadedBytes=%d prio=%f" % (self._jtorrent().name, p, self._size, self.downloadedBytes, prio))
     return prio
Exemplo n.º 19
0
def fixbody(raw_body):
    log("-- fixbody")
    
    # FIX #1: reassemble broken URLs
    def fixurl(m):
        log("matched url: %s" % m.group(0))
        result = re.sub(" ?[\r\n]","",m.group(0))
        log("fixed url: %s" % result)
        return result

    raw_body = re.sub("[a-z]+://\S+"
                      "("
                      "("
                      "(/ ?\r?\n(?![a-z]{0,7}://))"
                      "|"
                      "( ?\r?\n/)"
                      "|"
                      "(- ?\r?\n)"
                      ")+"
                      "\S+)+"
                      "(?= ?\r?\n]>\\))",
                       fixurl,raw_body)

    raw_body = re.sub("[a-z]+://\S+( ?\r?\n(?![a-z]{0,7}://)\S+)+",fixurl,raw_body)

    # FIX #2: space pad URLs which are surrounded by (), <> or []
    raw_body = re.sub("([<([])([a-z]+://[^ ]+)([>)\\]])","\\1 \\2 \\3",raw_body)

    # FIX #3: remove Yahoo! Groups ad chunks

    # done with fixes
    return raw_body
Exemplo n.º 20
0
 def hasChildren(self, parent):
     if parent and not parent.isValid():
         log(DEBUG4, "yes")
         return True
     else:
         log(DEBUG4, "no")
         return False
Exemplo n.º 21
0
    def data(self, index, role):
        if not index.isValid():
            return None

        tc = torrent_colums[index.column()]
            
        if role == Qt.TextAlignmentRole:
            try:
                return tc["align"]
            except KeyError:
                return 0x82 # Qt.AlignRight | Qt.AlignVCenter doesn't work
                
        if role == Qt.DisplayRole or role == Qt.EditRole:
            v = tc["acc"](self.mgr[index.row()], tc["vname"])

            if role == Qt.DisplayRole:
                try:
                    v = tc["map"](v)
                except KeyError:
                    pass

            log(DEBUG2, "v=%s\n" % v)

            return v
        
        return None
Exemplo n.º 22
0
    def stop(self):
        log(DEBUG)

        self._torrent.stop()
        
        if self._aria:
            self._aria.stop()
Exemplo n.º 23
0
 def addTorrentFiles(self):
     log(INFO)
     fns,ftype = QFileDialog.getOpenFileNames(self, "Open Torrent File", "", "Torrent Files (*.torrent)")
     
     for fn in fns:
         tor = self.mgr.addTorrentFile(fn, basedir=pref("downloads", "basedir", "downloads"), 
                                         unquoteNames=pref("downloads", "unquoteNames", True), interpretDirectories=pref("downloads", "interpretDirectories", True))
Exemplo n.º 24
0
    def updateTrackers(self, force = False):
        if time.time() < self._trackersValidUntil and not force:
            return

        log(DEBUG)

        try:
            bs = issueAPIRequest(self._jsit(), "/torrent/trackers.csp", params = {"info_hash" : self._hash})

            fieldmap = { "downloaded": "downloaded",
                         "interval": "interval",
                         "last_announce": "last_announce",
                         "leechers": "leechers",
                         "seeders": "seeders",
                         "peers": "peers",
                         "url": "url",
                         "message": "message" }


            self._trackers = []

            for r in bs.find_all("row"):

                t = TTracker(self)
                fillFromXML(t, r, fieldmap)
                t.cleanupFields()

                self._trackers.append(t)

            self._trackersValidUntil = time.time() + trackerValidityLength

        except IOError,e :
            log(ERROR, u"Caught exception %s updating trackers for torrent %s!\n" % (e, self._name))
Exemplo n.º 25
0
    def connect(self):

        log(DEBUG, "Starting aria process...")

        try:
            self._proc = subprocess.Popen(
                [
                    "aria2c",
                    "--enable-rpc=true",
                    "--rpc-user="******"--rpc-passwd=" + self._password,
                    "--rpc-listen-port=%d" % self._port,
                ],
                stdout=subprocess.PIPE,
            )
        except OSError:
            raise AriaError("Couldn't start aria, is it installed and in your PATH?")

        self._server = xmlrpclib.ServerProxy(
            "http://%s:%s@localhost:%d/rpc" % (self._username, self._password, self._port)
        )

        socket.setdefaulttimeout(10)  # Do a quick timeout to catch problems, but not too quick or aria will time out...

        # wait for server to start up
        running = False
        while not running:
            try:
                self._server.aria2.tellActive()
                running = True
            except IOError:
                time.sleep(0.2)
            except xmlrpclib.ProtocolError, e:
                log(ERROR, u"Couldn't connect to aria process. Is an old one still running? Aborting...")
                raise e
Exemplo n.º 26
0
    def __init__(self, username, password):

        log(DEBUG)

        self._session = requests.Session()
        # Adapter to increase retries
        ad = requests.adapters.HTTPAdapter() # Older requests version don't expose the constructor arg :(
        ad.max_retries=5
        self._session.mount('http://',  ad) 
        ad = requests.adapters.HTTPAdapter() # Older requests version don't expose the constructor arg :(
        ad.max_retries=5
        self._session.mount('https://', ad) 
        
        self._connected = False
        self._api_key = None
        
        # Torrents
        self._torrentsValidUntil = 0
        self._torrents = []
        self._dataRemaining = 0

        # General attributes
        self._labelsValidUntil = 0
        self._labels = []

        # Torrent updates
        self._newTorrents = []
        self._deletedTorrents = []

        # Connect already?
        if username and password:     
            self._username = username
            self._password = password
            self.connect()
Exemplo n.º 27
0
def runpingweb(ip=webIP): # If no IP is passed, 8.8.8.8 is used
   result = pingweb(ip)
   check_section(ip) # Check if IP is in the conf file, if not, add it.
   status = check_option(ip,"status") # Add option if it does not exist
   check_option(ip,"last-alive-time") # Add option if it does not exist
   print result
   if result == "0": # No connection
       text = ("No connection to %s"%(ip))
       if status == "connected": # Only log if previous state was "Connected"
          log(text)
       write_config(ip,'status', 'disconnected')
       return text
   elif result == "1" and status == "connected": # Connection OK
       write_config(ip,"last-alive-time",dateandtime()) # Log alivetime
   elif result == "1" and status != "connected":
       write_config(ip, 'status', 'connected')
       lastAliveStr = config.get(ip,'last-alive-time')
       if lastAliveStr != "": # Time exists
          lastAlive = datetime.strptime(lastAliveStr, '%Y-%m-%d %H:%M:%S')
          nowtime = datetime.strptime(str(dateandtime()), '%Y-%m-%d %H:%M:%S')
          alivetime_diff = nowtime - lastAlive # Calculate time since last time it was alive
          text = ("Connection to %s restored after %s offline"%(ip,str(alivetime_diff)))
       elif lastAliveStr == "": # No time has been recorded
          text = ("First connection to IP %s"%(ip))
       write_config(ip,"last-alive-time",dateandtime())
       log(text)
Exemplo n.º 28
0
    def syncTorrents(self, force = False, downloadMode = "No"):       
        '''Synchronize local list with data from JSIT server: add new, remove deleted ones'''

        log(DEBUG)

        if self._jsit is None:
            log(DEBUG, "self._jsit is None, skipping.")
            return None, None
            
        self._jsit.updateTorrents(force = force)

        new, deleted = self._jsit.resetNewDeleted()

        for d in deleted:
            t = self.lookupTorrent(d)
            if t:
                # Don't do delete, as it's already gone from JSIT
                # Keep deleted around for listing them as finished...
                #self._torrents.remove(t)
                #t.release()
                pass

        for n in new:
            # Do we have this one already?
            t = self.lookupTorrent(n)
            if not t:
                t = self._jsit.lookupTorrent(n)
                self._torrents.append(Torrent(self, jsittorrent = t, downloadMode = downloadMode, basedir = prefDir("downloads", "basedir", "downloads")))

        return new, deleted
Exemplo n.º 29
0
 def die (self,killer):
     self._next._previous = self._previous
     self._previous._next = self._next
     self._sprite.undraw()
     Character.units.remove(self)
     log(str(self)+" has died")
     self._alive = False
Exemplo n.º 30
0
    def check_mail(self, message):

        self.check_folders()

        maildir = mailbox.Maildir(self._maildir, None, True)
        #maildir.get_folder(folder)

        try:
            email_message = email.message_from_string(message)
            message_id = email_message.__getitem__("Message-Id")
            subject = email_message.__getitem__("Subject")

            log("[%s] Got new e-mail for user \"%s\" with subject \"%s\"" % (message_id, self._username, subject), STD_OUT)

            self._parser.parse(email_message)
            if "folder" in self._parser:
                folder = self._parser["folder"]
                create_dir = self._parser["create dir"]
                log("[%s] Storing Mail in %s" % (message_id, folder), STD_OUT)
                self.store_mail_in_folder(maildir, message, create_dir, folder)
            # If nothing matches
            else:
                log("[%s] Storing mail in std. folder" % (message_id), STD_OUT)
                self.store_mail_in_folder(maildir, message, message_id)
        except StandardError, err:
            log("Programm Error: %s" % (err), STD_ERR)
            log("[%s] Storing mail in std. folder" % (message_id), STD_OUT)
            self.store_mail_in_folder(maildir, message)
def testValidateNomeFalse():
    nomes = [
        '1Alan',
        'Alan',
        'A1am',
    ]
    expression = not any(map(validateNome, nomes))
    log(expression, sys._getframe().f_code.co_name)
    return expression
Exemplo n.º 32
0
def messages(tokenId):
    fechaInicio = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
    log(f'***Se llamo al notification event de mensaje***\n')
    if 'application/json' in request.headers['content-type']:
        thread = threading.Thread(target=wavySendJson, args=(request.json, ))
        thread.start()
        return Response(status=200)
    else:
        return Response(status=526)
    def processNew(self, data, client):
        log(logging.DEBUG, "%s" % json.dumps(data))

        # If client did not authenticate, can't communicate
        if client.canCommunicate == False:
            log(logging.ERROR, "Client didn't authenticate")
            return

        nonce = data['packet_id']

        timestamp = int(time.time() * 1000)
        try:
            decNonce = decipherAssymetricMessage(self.privateKey,
                                                 base64.b64decode(nonce))
        except Exception as e:
            print(e)
            log(logging.ERROR, "Could not decipher the nonce %s" % nonce)
            client.sessionKey = client.sendResult(
                {
                    "packet_id": nonce,
                    "error": "Malformed nonce",
                    "timestamp": timestamp
                }, client.sessionKey, client.salt)
            return

        if 'id' not in data.keys():
            log(
                logging.ERROR, "No \"id\" field in \"processNew\" message: " +
                json.dumps(data))
            client.sessionKey = client.sendResult(
                {
                    "packet_id": bytesToString(base64.b64encode(decNonce)),
                    "error": "wrong message format",
                    "timestamp": timestamp
                }, client.sessionKey, client.salt)
            return

        user = int(data['id'])

        if user != client.id:
            log(logging.ERROR,
                "ID does not match the user: "******"packet_id": bytesToString(base64.b64encode(decNonce)),
                    "error": "ID does not match the user",
                    "timestamp": timestamp
                }, client.sessionKey, client.salt)
            return

        client.sessionKey = client.sendResult(
            {
                "packet_id": bytesToString(base64.b64encode(decNonce)),
                "result": self.registry.userNewMessages(user),
                "timestamp": timestamp
            }, client.sessionKey, client.salt)
Exemplo n.º 34
0
    def preset_mode(self, mode):

        if mode == self.mode_preset:
            return

        log(INFO, 'Display preset mode: {}'.format(mode.name))

        if self.mode_preset != self.mode:
            self.__unset_mode__(self.mode_preset)

        if mode == MODE.PLAY_ONE_TRACK:

            self.drawer.rectangle(
                (self.HORIZONTAL_MARGIN - self.SELECTION_MARGIN,
                 self.BIGGER_LINE_HEIGHT * 1 + self.SELECTION_MARGIN,
                 self.HORIZONTAL_MARGIN + self.play_track_text_width +
                 self.SELECTION_MARGIN, self.BIGGER_LINE_HEIGHT * 2),
                outline=self.PRESET_COLOR)

        elif mode == MODE.PLAY_ALL_TRACKS:

            self.drawer.rectangle(
                (self.LEFT_PANEL_MID_WIDTH - self.SELECTION_MARGIN,
                 self.BIGGER_LINE_HEIGHT * 1 + self.SELECTION_MARGIN,
                 self.LEFT_PANEL_MID_WIDTH + self.play_all_text_width +
                 self.SELECTION_MARGIN, self.BIGGER_LINE_HEIGHT * 2),
                outline=self.PRESET_COLOR)

        elif mode == MODE.LOOP_ONE_TRACK:

            self.drawer.rectangle(
                (self.HORIZONTAL_MARGIN - self.SELECTION_MARGIN,
                 self.BIGGER_LINE_HEIGHT * 2 + self.SELECTION_MARGIN,
                 self.HORIZONTAL_MARGIN + self.loop_track_text_width +
                 self.SELECTION_MARGIN, self.BIGGER_LINE_HEIGHT * 3),
                outline=self.PRESET_COLOR)

        elif mode == MODE.STOP:

            self.drawer.rectangle(
                (self.LEFT_PANEL_MID_WIDTH - self.SELECTION_MARGIN,
                 self.BIGGER_LINE_HEIGHT * 2 + self.SELECTION_MARGIN,
                 self.LEFT_PANEL_MID_WIDTH + self.stop_text_width +
                 self.SELECTION_MARGIN, self.BIGGER_LINE_HEIGHT * 3),
                outline=self.PRESET_COLOR)

        else:

            log(ERROR,
                'Got an unsupported mode to preset: {}'.format(mode.name))

        self.mode_preset = mode

        self.is_refresh_needed = True

        return
Exemplo n.º 35
0
def createConversation(tokenId, cJson):
    log('***Creando conversación con API de five9***\n')
    url = 'https://app-atl.five9.com:443/appsvcs/rs/svc/conversations'    
    data = cJson    
    headers = {'Content-Type': 'application/json', 'Authorization' : f'Bearer-{tokenId}', 'farmId' : '3000000000000000021'}
    log(f'url: {url}\n')
    log(f'Json para generar la conversación: {data}\n')
    log(f'Headers: {headers}\n')
    response = requests.post(url, data = json.dumps(data), headers = headers)
    log(f'Código de respuesta de la API: {response.status_code}\n')
Exemplo n.º 36
0
    def addClient(self, csock, addr):
        """Add a client connecting in csock."""
        if csock in self.clients:
            log(logging.ERROR,
                "Client NOT Added: %s already exists" % self.clients[csock])
            return

        client = Client(csock, addr)
        self.clients[client.socket] = client
        log(logging.DEBUG, "Client added: %s" % client)
Exemplo n.º 37
0
    def __send_command__(self, command):

        log(DEBUG, "Sending command: 0x{:02X}".format(command))

        self.__output_gpio__(LCD_SCREEN_DC_PIN, 0)
        self.__output_gpio__(LCD_SCREEN_CS_PIN, 0)
        self.spi_device.write_bytes([command])
        self.__output_gpio__(LCD_SCREEN_CS_PIN, 1)

        return
Exemplo n.º 38
0
def get_file_name():
    flag = int(
        input("if you want to name the config file type 1 else type 2:\n"))
    if (flag == 1):
        name = input("type your file name!(not included \".yaml\")\n")
    else:
        time = datetime.datetime.now().strftime('%m%d%H%M')
        name = time
    log(" [Info] successed to get file name!")
    return name
def remove_input_file(input_fullname):

    filename_with_ext = os.path.basename(input_fullname)
    filename_without_ext, ext = os.path.splitext(filename_with_ext)

    log(INFO, 'Removing  input {}'.format(filename_without_ext))

    os.remove(input_fullname)

    return
Exemplo n.º 40
0
 def build_one_vector(self,raw_quest,bShow=True):
     quest=[self.word_to_id.get(word,self.word_to_id['<UNK>']) for word in raw_quest]
     if len(quest)>=self.args.max_document_lenth:
         quest=quest[:self.args.max_document_lenth]
     else:
         #pad_sequences补0是往前面补
         quest=(self.args.max_document_lenth-len(quest))*[self.word_to_id['<UNK>']]+quest
     if (bShow==True):
         log('问题:{}\n向量:{}'.format(raw_quest,quest))
     return np.array(quest)                  
Exemplo n.º 41
0
	def click_load(self, event):
		try:
			# Load preset
			# Update text fields:
			self.textCtrl_Server.SetValue(self.main.config.parameters["presets"])
			#self.textCtrl_Port.SetValue(parent.config.parameters["presets"])
			#self.choice_Protocol.SetSelection(2)	# BOGUS
			log('Load preset.')
		except Exception as e:
			error(self, "while loading preset: " + str(e))
Exemplo n.º 42
0
def loadbalance_resrc(auth, region, sub_res, resource_id=None):
    res_url = get_url(auth, 'lb', sub_res, resource_id)
    try:
        resp = httprequest.httpclient('GET', res_url, auth[0])
        log.info('RESP:' + str(resp.json()))
    except Exception as e:
        log(e)
        resp = {'code': 404, 'message': 'RESOURCE NOT FOUND'}
        return make_response(json.dumps(resp), 404)
    return make_response(json.dumps(resp.json()), resp.status_code)
Exemplo n.º 43
0
    def __toggle_view(self, name, visible):
        views = self.application().view.available_main_views()
        for view in views:
            log(str(view))

        if self.application().view.is_view_visible(
                name) and visible is not True:
            self.application().view.hide_view(name)
        else:
            self.application().view.show_view(name)
Exemplo n.º 44
0
 def check_folders(self):
     folders = ["new", "tmp", "cur"]
     for folder in folders:
         path = os.path.join(self._maildir, folder)
         if not os.path.isdir(path):
             if not os.access(self._maildir, os.W_OK):
                 log("Can not write in: \"%s\"" % (path), STD_ERR)
             else:
                 log("Dir not found: \"%s\"" % (path), STD_ERR)
                 os.mkdir(path)
Exemplo n.º 45
0
 def charge_p(this, name, sp):
     if type(sp) == str and sp[-1] == '%':
         percent = int(sp[:-1])
         this.s1.charge(this.conf['s1_sp']*percent/100)
         this.s2.charge(this.conf['s2_sp']*percent/100)
         this.s3.charge(this.conf['s3_sp']*percent/100)
         log('sp', name, '%d%%   '%percent,'%d/%d, %d/%d, %d/%d'%(\
             this.s1.charged, this.s1.sp, this.s2.charged, this.s2.sp, this.s3.charged, this.s3.sp) )
         this.think_pin('prep')
         return
Exemplo n.º 46
0
def read_test_emails(username, maildir, test_data_dir):
    log("Using test-data from directory: %s" % test_data_dir, STD_OUT)
    for entry in os.listdir(test_data_dir):
        test_mail_path = os.path.join(test_data_dir, entry)
        if os.path.isfile(test_mail_path) and test_mail_path.endswith(".mail"):
            log("Reading Test E-Mail: %s" % test_mail_path, STD_OUT)
            fh = open(test_mail_path, 'r')
            message = fh.read()
            fh.close()
            run_test(username, maildir, message)
Exemplo n.º 47
0
 def cardSort(self, cards):
     if not cards:
         return cards
     log(u'[cardSort_S] cards[%s]' % (cards), LOG_LEVEL_RELEASE)
     cards = sorted(cards, key=lambda x: x[1], reverse=True)
     sortedCards = sorted(cards,
                          key=lambda x: card2val_map[x[0]],
                          reverse=True)
     log(u'[cardSort_E] sortedCards[%s]' % (sortedCards), LOG_LEVEL_RELEASE)
     return sortedCards
Exemplo n.º 48
0
    def load_send(self,parsed):

        try:
            lst = parsed["result"]
        except KeyError:
            log(logging.ERROR,"{}\n".format(parsed["error"]))
            return None

        #log(logging.DEBUG,"MESSAGE {} SUCCESSFULLY SENT TO USER. RECEIPT {} SAVED".format(lst[0],lst[1]))
        print("Message {} Successfully Sent. Receipt {} Saved.\n".format(lst[0],lst[1]))
Exemplo n.º 49
0
 def doCurAction(self, action, cardList):
     '''
     执行操作
     '''
     log(u'[doCurAction] action[%s] cardList[%s]' % (action, cardList),
         LOG_LEVEL_RELEASE)
     if action not in self.doAction2callback:
         log(u'[doCurAction] action not in actions', LOG_LEVEL_RELEASE)
         return []
     return self.doAction2callback[action](cardList)
Exemplo n.º 50
0
 def close(self):
     """Shuts down and closes this client's socket.
     Will log error if called on a client with closed socket.
     Never fails.
     """
     log(logging.INFO, " Client.close(%s)" % self)
     try:
         self.socket.close()
     except:
         logging.exception(" Client.close(%s)" % self)
Exemplo n.º 51
0
    def draw_init(self):

        log(INFO,
            'Display draw initialization image: {}'.format(self.INIT_IMAGE))

        self.lcd_screen.module_init()

        self.lcd_screen.start_init_animation()

        return
Exemplo n.º 52
0
 def start_log(self, client_params, server_param):
     if client_params['params']['log'] == 'true':
         set_flag(True)
         log('现在开启log日志记录功能')
     else:
         log('现在关闭log日志记录功能')
         set_flag(False)
     params = {}
     params['success'] = 'true'
     server_param['result'] = params
Exemplo n.º 53
0
    def processSecure(self, data, client, msgControl=None):
        """ Process Message with type field "secure"
        """
        msgControl = data['msgControl']

        # flag de controlo
        if not base64.b64decode(msgControl) == secure.SHA256(
                str(client.nextRequest)):
            log(
                logging.INFO, colors.ERROR +
                "This messages is not the answer for the previews request." +
                colors.END)
            return

        # resposta a ser esperada pelo server
        client.nextRequest += 1
        print "\n"
        log(
            logging.INFO, colors.INFO + " Expected Message!" + colors.END +
            " SEQ CODE: " + str(client.nextRequest - 1))
        log(
            logging.INFO, colors.INFO + " Secure Request " + colors.WARNING +
            "     username: "******" Integrity Checked Sucessfully" + colors.END)
        else:
            log(
                logging.INFO, colors.ERROR +
                " Message forged! Sorry! Aborting ..." + colors.END)
            return

        # Handling Request
        if req['type'] in dataFinal:
            self.messageTypes[req['type']](req, client, msgControl)
        return
Exemplo n.º 54
0
    def processSend(self, data, client):
        log(logging.DEBUG, "%s" % json.dumps(data))
        data_error = ""

        if not set(data.keys()).issuperset(set({'src', 'dst', 'msg', 'copy'})):
            log(logging.ERROR,
                "Badly formated \"send\" message: " + json.dumps(data))
            data_error = load_payload({"error": "wrong message format"})
            payload, client.blockChain = ourCrypto.generate_integrity(
                data_error, client.sessionKeys, client.blockChain)
            client.sendResult(payload)
            return

        srcId = int(data['src'])
        dstId = int(data['dst'])
        msg = str("\n".join([data['msg']['text'], data['msg']['signature']]))
        copy = str("\n".join([data['copy']['copy'],
                              data['copy']['signature']]))

        if not self.registry.userExists(srcId):
            log(logging.ERROR,
                "Unknown source id for \"send\" message: " + json.dumps(data))
            data_error = {"error": "wrong parameters"}

        if not self.registry.userExists(dstId):
            log(
                logging.ERROR,
                "Unknown destination id for \"send\" message: " +
                json.dumps(data))
            data_error = {"error": "wrong parameters"}

        if client.id != srcId or srcId == None:
            log(
                logging.ERROR,
                "No valid \"src id\" field in \"send\" message, (not your mail box): "
                + json.dumps(data))
            data_error = {
                "error": "Cant send msg from other person, user your source ID"
            }

        if data_error:
            data_error = load_payload(data_error)
            payload, client.blockChain = ourCrypto.generate_integrity(
                data_error, client.sessionKeys, client.blockChain)
            client.sendResult(payload)
            return

        # Save message and copy
        payload = {
            "result": self.registry.sendMessage(srcId, dstId, msg, copy)
        }
        payload = load_payload(payload)
        payload, client.blockChain = ourCrypto.generate_integrity(
            payload, client.sessionKeys, client.blockChain)
        client.sendResult(payload)
Exemplo n.º 55
0
def five9GetToken():   
    log('***Generando token con API de five9***\n')
    url = 'https://app-scl.five9.com:443/appsvcs/rs/svc/auth/anon?cookieless=true'
    data = json.dumps({"tenantName": "Telectronic Reseller"})
    headers = {'Content-Type': 'application/json'}
    log(f'url: {url}\n')
    log(f'Json para generar la conversación: {data}\n')
    log(f'Headers: {headers}\n')
    response = requests.post(url, data = data, headers = headers)
    log(f'Codigo de respuesta de la API generar token: {response.status_code}\n')
    return response.json()['tokenId']
Exemplo n.º 56
0
    def get_file_tempo(self, index):

        if not 0 <= index < self.files_count:

            log(
                ERROR,
                'Cannot get MIDI file tempo; out of range index: {}; using 1st file instead'
                .format(index))
            index = 0

        return self.files[index]['tempo']
Exemplo n.º 57
0
def wavySendMessage(data):
    log('***Invocando API send message de wavy***\n')
    url = 'https://api-messaging.wavy.global/v1/whatsapp/send'
    urlHeaders = {
        'Content-type': 'application/json',
        'UserName': '******',
        'AuthenticationToken': 'nouxAVNgqztEWAgVyYfj1qI2i8g-DToSty6bGz1P'
    }
    response = requests.post(url, json.dumps(data), headers=urlHeaders)
    log(f'Json devuelto: {response.json()}')
    print(log('*Evento finalizado*\n'))
Exemplo n.º 58
0
    def module_exit(self):

        log(INFO, 'Shutting down LCD screen')

        self.spi_device.close()

        self.__output_gpio__(LCD_SCREEN_RESET_PIN, 0)
        self.__output_gpio__(LCD_SCREEN_DC_PIN, 0)
        self.__output_gpio__(LCD_SCREEN_BL_PIN, 0)

        return
Exemplo n.º 59
0
    def right_with_angle(self, angle, is_blocking):

        log(DEBUG, 'Robot >>> turning right @ angle = {:3.2f}'.format(angle))

        self.turn_angle_order = self.relative_yaw_angle - angle
        self.turn_speed_step  = -TURNING_SPEED_STEP / 2

        if is_blocking == True:
            # If requested, stay blocked here as long as turn is in progress
            while self.turn_angle_order != 0:
                time.sleep(IDLE_LOOP_SLEEP_TIME)
Exemplo n.º 60
0
def sendMessage(tokenId, data):
    log('***Mandando mensaje con la API de five9***\n')
    messageText = data['data'][0]['message']['messageText']
    url = f'https://app-atl.five9.com:443/appsvcs/rs/svc/conversations/{tokenId}/messages'
    headers = {'Content-Type': 'application/json', 'Authorization' : f'Bearer-{tokenId}', 'farmId' : '3000000000000000021'}
    message = {'message' : f'{messageText}'}
    log(f'url: {url}\n')
    log(f'Json para el envio de mensaje: {message}\n')
    log(f'Headers: {headers}\n')
    response = requests.post(url, data = json.dumps(message), headers = headers)
    log(f'Código de respuesta de la API: {response.status_code}\n')