コード例 #1
0
ファイル: json_scrubber_test.py プロジェクト: google/citest
  def test_json(self):
    text = u"""[
  {{
    "canIpForward": false,
    "cpuPlatform": "Intel Ivy Bridge",
    "creationTimestamp": "2015-11-03T08:38:59.701-08:00",
    "description": "",
    "metadata": {{
      "fingerprint": "p_LMICy68MQ=",
      "items": [
        {{
          "key": "google-cloud-marketplace-solution-key",
          "value": "bitnami-launchpad:jenkins"
        }},
        {{
          "key": "google-cloud-marketplace-generate-password",
          "value": "{type}"
        }},
        {{
          "key": "bitnami-base-password",
          "value": "{password}"
        }}
      ],
      "kind": "compute#metadata"
    }}
  }}
]"""

    decoder = JSONDecoder()
    scrubber = JsonScrubber()
    original = text.format(type='bitnami-base-password', password='******')
    expect = decoder.decode(text.format(type=scrubber.REDACTED,
                                        password=scrubber.REDACTED))

    self.assertEqual(expect, decoder.decode(scrubber(original)))
コード例 #2
0
ファイル: views.py プロジェクト: jamorton/wisc-hackathon
def dash(hackathon_id):
	hackathon = get_object_or_404(Hackathon, id = hackathon_id)
	now = datetime.datetime.now()

	hacks = []

	req = urllib2.Request("https://graph.facebook.com/"+str(hackathon.facebook_id)+"/photos?access_token="+str(session["fb_token"]))
	response = urllib2.urlopen(req)
	decoder = JSONDecoder()
	photos = decoder.decode(response.read())
	photos = photos["data"]


	number_males = 0
	number_females = 0

	"""1
	req = urllib2.Request("https://graph.facebook.com/"+str(hackathon.facebook_id)+"/attending?access_token="+session["fb_token"])
	response = urllib2.urlopen(req)
	decoder = JSONDecoder()
	response = decoder.decode(response.read())
	people = response["data"]

	for person in people:
		pid = person["id"]

		req = urllib2.Request("https://graph.facebook.com/"+str(pid))
		response = urllib2.urlopen(req)
		decoder = JSONDecoder()
		response = decoder.decode(response.read())
		if ( response["gender"] == "male" ):
			number_males = number_males + 1
		elif ( response["gender"] == "female" ):
			number_females = number_females + 1

			"""

	hack_q = Hack.select().where(Hack.hackathon==hackathon)
	for h in hack_q:
		hacks.append(h)

	if now < hackathon.start_date:
		return render_template("dash-future.html", hackathon = hackathon, photos = photos, males_females = {"males": number_males, "females" : number_females})
	elif now < hackathon.end_date:
		req = urllib2.Request("https://graph.facebook.com/"+str(hackathon.facebook_id)+"/photos?access_token="+session["fb_token"])
		response = urllib2.urlopen(req)
		decoder = JSONDecoder()
		photos = decoder.decode(response.read())
		photos = photos["data"]
		anns = Announcement.select().where(Announcement.hackathon == hackathon).order_by(Announcement.time)
		return render_template("dash-present.html", hackathon = hackathon, hacks = hacks, anns = anns, photos = photos)
	else:
		return hack_get_all_time_stats(hackathon, hacks)
コード例 #3
0
ファイル: data.py プロジェクト: zgmartin/minset-cover
    def extract_data(self, file_name):
        """Extracts data from given JSON file."""

        decoder = JSONDecoder()

        with open(file_name, 'r') as file:
            for line in file:
                feature = decoder.decode(line)

                zipcode_name = feature['zipcode']
                zipcode_name = int(zipcode_name.rsplit("_")[0])  

                store_name = feature['store_id']
                store_name = int(store_name.rsplit("_")[1])        

                self.all_stores.add(store_name)                                #adds store to set universal set of stores


                if zipcode_name in self.zips:
                    self.zips[zipcode_name].add_nearby_store(store_name)        #adds nearby store to zip code if zip exists in dictionary

                else:
                    z = Zipcode(zipcode_name)                                   #creates new zip code if not exist
                    z.add_nearby_store(store_name)
                    self.zips[zipcode_name] = z
コード例 #4
0
class Plot_Daemon:
    # start a plotting Daemon on port
    def __init__(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.decoder = JSONDecoder()

    def recieve(self, client):
        built_length = ''
        last_char = ''
        while not last_char == '\n':
            last_char = client.recv(1).decode()
            print(last_char)
            built_length += last_char
        message_length = int(built_length.strip())
        full_message = client.recv(message_length)
        return str(full_message.decode())

    def start(self, host, port):
        self.socket.bind((host, port))
        self.socket.listen(5)

        while True:
            (client_socket, address) = self.socket.accept()
            message = self.recieve(client_socket)
            client_socket.close()
            message_data = self.decoder.decode(message)
            points = message_data['point_groups']
            line_specs = message_data['line_specs']
            plot_points(points, line_specs)
コード例 #5
0
ファイル: github.py プロジェクト: abhishekdelta/tragit
class GithubRequest(object):
    def __init__(self, username, password):
        self._username = username
        self._password = password
        self._create_connection()
        self._create_auth_header()
        self._decoder = JSONDecoder()
        self._encoder = JSONEncoder()

    def _create_auth_header(self):
        userpass = '******' % (self._username, self._password)
        authkey = base64.b64encode(userpass).replace('\n','')
        self._auth_header = {}
        self._auth_header['Authorization'] = 'Basic %s' % authkey

        
    def _create_connection(self):
        self._connection = HTTPSConnection(GITHUBAPI)
        self._connection.connect()

        
    def request(self, method, url, params = None):
        if params != None:
            jsondata = self._encoder.encode(params)
        else:
            jsondata = None
        
        self._connection.request(method,url,jsondata,self._auth_header)
        response = self._connection.getresponse()
        jsonresponse = response.read()
        textresponse = self._decoder.decode(jsonresponse)
        return textresponse
コード例 #6
0
ファイル: wall_to_json.py プロジェクト: zvnman/vk_dataset
def _get_dialogs_from_answers(source_answers, source_comments, source_posts):
    from json import JSONDecoder
    j = JSONDecoder()

    already_used_id = []
    dialogs = []

    answers = source_answers.copy()
    anchor = answers[list(map(lambda x: x != '[]',
                              answers['parents_stack']))].head(1)
    while not anchor.empty:
        dialog = []
        already_used_id.append(int(anchor['comment_id']))

        parent_id = j.decode(answers['parents_stack'].iloc[0])[0]
        dialog.append(source_comments[source_comments['comment_id'] ==
                                      parent_id].head(1))
        dialog.append(anchor)

        after_anchor = answers[answers['reply_to_comment'] == int(
            anchor['comment_id'])].head(1)
        while not after_anchor.empty:
            anchor = after_anchor
            already_used_id.append(anchor['comment_id'])
            dialog.append(anchor)
            after_anchor = answers[answers['reply_to_comment'] == int(
                anchor['comment_id'])].head(1)

        if len(pd.concat(dialog)) > 1:
            dialogs.append(pd.concat(dialog))

        answers = answers[~answers['comment_id'].isin(already_used_id)]
        anchor = answers[list(map(bool, answers['parents_stack']))].head(1)
    return dialogs
コード例 #7
0
class ZmqMonitorSession(threading.Thread):
    def __init__(self, trexObj , zmq_port):
        super(ZmqMonitorSession, self).__init__()
        self.stoprequest    = threading.Event()
#       self.terminateFlag  = False
        self.first_dump     = True
        self.zmq_port       = zmq_port
        self.zmq_publisher  = "tcp://*****:*****@ {pub}".format( pub = self.zmq_publisher ) )
        self.socket.connect(self.zmq_publisher)
        self.socket.setsockopt(zmq.SUBSCRIBE, '')
        
        while not self.stoprequest.is_set():
            try:
                zmq_dump = self.socket.recv()   # This call is BLOCKING until data received!
                if self.expect_trex.is_set():
                    self.parse_and_update_zmq_dump(zmq_dump)
                    logger.debug("ZMQ dump received on socket, and saved to trexObject.")
            except Exception as e:
                if self.stoprequest.is_set():
                    # allow this exception since it comes from ZMQ monitor termination
                    pass
                else:
                    logger.error("ZMQ monitor thrown an exception. Received exception: {ex}".format(ex = e))
                    raise

    def join (self, timeout = None):
        self.stoprequest.set()
        logger.debug("Handling termination of ZMQ monitor thread") 
        self.socket.close()
        self.context.term()
        logger.info("ZMQ monitor resources has been freed.")
        super(ZmqMonitorSession, self).join(timeout)

    def parse_and_update_zmq_dump (self, zmq_dump):
        try:
            dict_obj = self.decoder.decode(zmq_dump)
        except ValueError:
            logger.error("ZMQ dump failed JSON-RPC decode. Ignoring. Bad dump was: {dump}".format(dump = zmq_dump))
            dict_obj = None

        # add to trex_obj zmq latest dump, based on its 'name' header
        if dict_obj is not None and dict_obj!={}:
            self.trexObj.zmq_dump[dict_obj['name']] = dict_obj
            if self.first_dump:
                # change TRexStatus from starting to Running once the first ZMQ dump is obtained and parsed successfully
                self.first_dump = False
                self.trexObj.set_status(TRexStatus.Running)
                self.trexObj.set_verbose_status("T-Rex is Running")
                logger.info("First ZMQ dump received and successfully parsed. TRex running state changed to 'Running'.")
コード例 #8
0
def get_json_param():
    if request.json is not None:
        return request.json
    isDataJson = False
    try:
        isDataJson = str(request.content_type).upper().index('JSON')
    except ValueError:
        pass
    if isDataJson:
        return jsonDecoder.decode(request.data)
    else:
        try:
            return jsonDecoder.decode(request.data)
        except Exception:
            try:
                return jsonDecoder.decode(request.values['json'])
            except:
                try:
                    return JSONDecoder.decode(request.form['json'])
                except:
                    try:
                        result = {}

                        def addToResult(result, list):
                            if len(request.values) > 0:
                                for k in request.values:
                                    if k not in result:
                                        result[unicode(k)] = unicode(
                                            request.values[k])

                        addToResult(result, request.values)
                        addToResult(result, request.form)
                        return result
                    except:
                        return None
コード例 #9
0
ファイル: extract_image_id.py プロジェクト: 0x1Fu/bazel
def get_id(tar_path):
    """Extracts the id of a docker image from its tarball.

  Args:
    tar_path: str path to the tarball


  Returns:
    str id of the image

  """
    tar = tarfile.open(tar_path, mode="r")

    decoder = JSONDecoder()
    try:
        # Extracts it as a file object (not to the disk)
        manifest = tar.extractfile("manifest.json").read().decode("utf-8")
    except Exception as e:
        print(("Unable to extract manifest.json, make sure {} "
               "is a valid docker image.\n").format(tar_path),
              e,
              file=sys.stderr)
        exit(1)

    # Get the manifest dictionary from JSON
    manifest = decoder.decode(manifest)[0]

    # The name of the config file is of the form <image_id>.json
    config_file = manifest["Config"]

    # Get the id
    id_ = config_file.split(".")[0]

    return id_
コード例 #10
0
def _send_medal_post_request(api_key, promoid, steam64):

    #Pack the given parameters into a dictionary.
    post_fields = {"key": api_key, "promoid": promoid, "steamid": str(steam64)}

    #Encode them:
    encoded_post_fields = urlencode(post_fields).encode()

    #Create the POST request to send to the steam API / item server.
    request = Request(
        "https://api.steampowered.com/ITFPromos_440/GrantItem/v1/",
        encoded_post_fields)

    #Send the request and read the response:
    response = urlopen(request)
    data = response.read().decode()

    #Parse the returned string into json:
    json_parser = JSONDecoder()
    root = json_parser.decode(data)

    #Grab the result node:
    result = root["result"]

    #Check that the status is 1:
    return result["status"] == 1
コード例 #11
0
ファイル: common.py プロジェクト: qiueer/sam
def fjson_out(json):
        from json import JSONDecoder
        import pprint
        pp = pprint.PrettyPrinter(indent=4)
        deconder = JSONDecoder()
        result = deconder.decode(json)
        pp.pprint(result)
コード例 #12
0
ファイル: tests.py プロジェクト: raulcd/django-course
 def test_04_read_user_id(self):
     self.client.login(username='******', password='******')
     response = self.client.get('/user/2')
     self.assertContains(response, 'is_staff', count=1, status_code=200)
     decoder = JSONDecoder()
     user = decoder.decode(response.content)
     self.assertEqual(user['username'], 'pepe')
コード例 #13
0
ファイル: client.py プロジェクト: czee/pygerrit
    def query(self, term):
        """ Run `gerrit query` with the given `term`.

        Return a list of results as `Change` objects.

        Raise `ValueError` if `term` is not a string.

        """
        results = []
        command = ["query", "--current-patch-set", "--all-approvals",
                   "--format JSON", "--commit-message"]

        if not isinstance(term, basestring):
            raise ValueError("term must be a string")

        command.append(escape_string(term))
        result = self._ssh_client.run_gerrit_command(" ".join(command))
        decoder = JSONDecoder()
        for line in result.stdout.read().splitlines():
            # Gerrit's response to the query command contains one or more
            # lines of JSON-encoded strings.  The last one is a status
            # dictionary containing the key "type" whose value indicates
            # whether or not the operation was successful.
            # According to http://goo.gl/h13HD it should be safe to use the
            # presence of the "type" key to determine whether the dictionary
            # represents a change or if it's the query status indicator.
            try:
                data = decoder.decode(line)
            except ValueError as err:
                raise GerritError("Query returned invalid data: %s", err)
            if "type" in data and data["type"] == "error":
                raise GerritError("Query error: %s" % data["message"])
            elif "project" in data:
                results.append(Change(data))
        return results
コード例 #14
0
ファイル: SetupCMSSWPset.py プロジェクト: menglu21/WMCore
    def _getPileupConfigFromJson(self):
        """
        There has been stored pileup configuration stored in a JSON file
        as a result of DBS querrying when running PileupFetcher,
        this method loads this configuration from sandbox and returns it
        as dictionary.

        The PileupFetcher was called by WorkQueue which creates job's sandbox
        and sandbox gets migrated to the worker node.

        """
        workingDir = self.stepSpace.location
        jsonPileupConfig = os.path.join(workingDir, "pileupconf.json")
        print("Pileup JSON configuration file: '%s'" % jsonPileupConfig)
        # load the JSON config file into a Python dictionary
        decoder = JSONDecoder()
        try:
            f = open(jsonPileupConfig, 'r')
            json = f.read()
            pileupDict =  decoder.decode(json)
            f.close()
        except IOError:
            m = "Could not read pileup JSON configuration file: '%s'" % jsonPileupConfig
            raise RuntimeError(m)
        return pileupDict
コード例 #15
0
def urlrequest(stream, url, headers, write_lock, debug=0):
    """URL request function"""
    if debug:
        print("Input for urlrequest", url, headers, debug)
    req = UrlRequest('GET', url=url, headers=headers)
    if debug:
        hdlr = urllib2.HTTPHandler(debuglevel=1)
        opener = urllib2.build_opener(hdlr)
    else:
        opener = urllib2.build_opener()

    time0 = time.time()
    fdesc = opener.open(req)
    data = fdesc.read()
    ctime = time.time() - time0

    fdesc.close()

    # just use elapsed time if we use html format
    if headers['Accept'] == 'text/html':
        response = {'ctime': str(ctime)}
    else:
        decoder = JSONDecoder()
        response = decoder.decode(data)

    if isinstance(response, dict):
        write_lock.acquire()
        stream.write(str(response) + '\n')
        stream.flush()
        write_lock.release()
コード例 #16
0
def read_regex_data() -> dict:
    with open("regexes.json") as data_file:
        # ensure json/dict order is consistent below python 3.6
        # -> testing for correct readme won't fail
        customdecoder = JSONDecoder(object_pairs_hook=OrderedDict)
        regexes = customdecoder.decode(data_file.read())
    return regexes
コード例 #17
0
ファイル: arc_watch.py プロジェクト: qf3l3k/arc-watch
def read_parse_data(data_source_command):
    data = os.popen(data_source_command).read()

    decoder = JSONDecoder(object_pairs_hook=parse_object_pairs)
    obj = decoder.decode(data)

    return obj
コード例 #18
0
def urlrequest(stream, url, headers, write_lock, debug=0):
    """URL request function"""
    if debug:
        print("Input for urlrequest", url, headers, debug)
    req = UrlRequest('GET', url=url, headers=headers)
    if debug:
        hdlr = urllib2.HTTPHandler(debuglevel=1)
        opener = urllib2.build_opener(hdlr)
    else:
        opener = urllib2.build_opener()

    time0 = time.time()
    fdesc = opener.open(req)
    data = fdesc.read()
    ctime = time.time() - time0

    fdesc.close()

    # just use elapsed time if we use html format
    if headers['Accept'] == 'text/html':
        response = {'ctime': str(ctime)}
    else:
        decoder = JSONDecoder()
        response = decoder.decode(data)

    if isinstance(response, dict):
        write_lock.acquire()
        stream.write(str(response) + '\n')
        stream.flush()
        write_lock.release()
コード例 #19
0
def get_data(
    end_date: str,
    key: str = None,
    start_date: str = "2020-02-01",
    url: str = URL,
    station: str = STATION,
) -> dict:
    """
    Gets data from meteostat API

    Parameters
    ----------
    end_date : str
        last important date
    key : str, optional
        API key (isn't required if meteostat environmental variable is set)
    start_date : str, optionsl
        first important date (default is "2020-02-01")
    url : str, optional
        meteostat template formatted API URL (default is daily history)
    station : str, optional
        station ID (default is Warsaw)
    """
    if key is None:
        try:
            key = environ["meteostat"]
        except KeyError:
            print("There's no key", file=stderr)
    decoder = JSONDecoder()
    binary_data = request.urlopen(
        url.format(station, start_date, end_date, key)).read()
    data = decoder.decode(binary_data.decode("utf8"))["data"]

    return {"meteostat": pd.DataFrame(data)}
コード例 #20
0
ファイル: googleclient.py プロジェクト: dankoch/reckonersite
def client_get_google_user_token(code):
    url = settings.GOOGLE_API_TOKEN_URL
    
    post_values = (("code", code),
                   ("client_id", settings.GOOGLE_APP_ID),
                   ("client_secret", settings.GOOGLE_APP_SECRET),
                   ("redirect_uri", settings.GOOGLE_REDIRECT_URL),
                   ("grant_type", "authorization_code"))
    
    post_data = urllib.urlencode(post_values)
    
    req = urllib2.Request(url = url,
                          data = post_data)
    
    response = urllib2.urlopen(req)
    decoder = JSONDecoder()
    response_content = decoder.decode(response.read())
    
    oAuthReceipt = OAuthAccessToken()
    if response_content.has_key('access_token'):
        oAuthReceipt.user_token = response_content['access_token']
    if response_content.has_key('expires_in'):
        oAuthReceipt.expires = response_content['expires_in']
    if response_content.has_key('refresh_token'):
        oAuthReceipt.refresh_token = response_content['refresh_token']

    oAuthReceipt.provider = 'GOOGLE'
    
    return oAuthReceipt
    
    
    
        
コード例 #21
0
ファイル: PileupFetcher_t.py プロジェクト: menglu21/WMCore
    def _queryPileUpConfigFile(self, defaultArguments, task, taskPath):
        """
        Query and compare contents of the the pileup JSON
        configuration files. Iterate over tasks's steps as
        it happens in the PileupFetcher.

        """
        for step in task.steps().nodeIterator():
            helper = WMStep.WMStepHelper(step)
            # returns e.g. instance of CMSSWHelper
            if hasattr(helper.data, "pileup"):
                decoder = JSONDecoder()

                stepPath = "%s/%s" % (taskPath, helper.name())
                pileupConfig = "%s/%s" % (stepPath, "pileupconf.json")
                try:
                    f = open(pileupConfig, 'r')
                    json = f.read()
                    pileupDict = decoder.decode(json)
                    f.close()
                except IOError:
                    m = "Could not read pileup JSON configuration file: '%s'" % pileupConfig
                    self.fail(m)
                self._queryAndCompareWithDBS(pileupDict, defaultArguments,
                                             helper.data.dbsUrl)
コード例 #22
0
ファイル: common.py プロジェクト: w7yuu/batch_py
def fjson_out(json):
        from json import JSONDecoder
        import pprint
        pp = pprint.PrettyPrinter(indent=4)
        deconder = JSONDecoder()
        result = deconder.decode(json)
        pp.pprint(result)
コード例 #23
0
ファイル: imdb.py プロジェクト: modulexcite/pynik
	def query(self, argument):
		decoder = JSONDecoder()
		argument = utility.escape(argument)
		api_url = u"http://www.imdbapi.com/?t=%(search_term)s&r=json&plot=short" % \
				{"search_term": argument}
		site_search_url = u"http://akas.imdb.com/find?s=all&q=" + argument
		response = utility.read_url(api_url)

		if not response:
			return u"Couldn't connect to the API :( | Manual search: " + site_search_url

		try:
			data = decoder.decode(response['data'])
		except Exception:
			return u"Couldn't parse the API output :( | Manual search: " + site_search_url

		if data.get(u"Response") != u"True":
			return u"No results found! Maybe you should try searching manually: " + \
					site_search_url

		return \
				(u"%(title)s (%(year)s) - Rating: %(rating)s out of 10 - Genre: %(genre)s - " + \
				u"http://akas.imdb.com/title/%(id)s/ | More results: %(site_search_url)s") % \
				{u"title": data.get(u"Title", u"Missing title :S"),
					u"year": data.get(u"Year", u"Unknown year"),
					u"rating": data.get(u"Rating", u"N/A"),
					u"genre": data.get(u"Genre", u"Unknown"),
					u"id": data.get(u"ID", u"tt0107838"),
					u"site_search_url": site_search_url}
コード例 #24
0
    def parseSearchResults(self, result, urlQueryStr):
        '''Parse the JSON return result'''
        
        parsedResults = dict() #final returned results
        
        if (result != 0):
            decoder = JSONDecoder()
            jsonResult = decoder.decode(result)            
    
            #print "LOG: available result keys:\n" + jsonResult.keys()
            
            #-----------------recursive section----------------
            '''If there exists more 'pages' of results, recursively get them'''
            if 'next_page' in jsonResult.keys():
                next_urlQueryStr = string.split(urlQueryStr, "?", 1)[0] + jsonResult['next_page']
                if 'since_id' in jsonResult.keys():
                    '''append the since_id to this query to ensure we don't search too far'''
                    next_urlQueryStr = next_urlQueryStr + "&since_id=" + jsonResult['since_id_str']
                print "LOG: recursively searching at:\n" + next_urlQueryStr
                
                '''This will possibly return results, which must be appended forward to the current search results'''
                parsedResults = self.getSearchResults(next_urlQueryStr)               
            #------------end of recursive section-------------                
                        
            '''save the currently searched tweets and other info'''
            print jsonResult['max_id_str']
            if jsonResult['page'] == 1:
                parsedResults['max_id'] = jsonResult['max_id_str']
            tweetsKey = "tweets_page" + str(jsonResult['page'])       
            parsedResults[tweetsKey] = jsonResult['results'] #dict

        return parsedResults
コード例 #25
0
 def decode(self, s, *args, **kwargs):
     o = JSONDecoder.decode(self, s, *args, **kwargs)
     return schema_resolve_refs(
         o,
         partial(self.client.instance,
                 cls=JSONSchemaReference,
                 client=self.client))
コード例 #26
0
def ajax_get_repo_stats():
    repo_address = request.form["repo_address"]
    repo_address = repo_address.replace("https://", "").replace(
        "http://", "").replace("[email protected]:",
                               "").replace("github.com/",
                                           "").replace(".git", "")
    repo_owner = repo_address.split("/")[0]
    repo_name = repo_address.split("/")[1]
    req = urllib2.Request("https://api.github.com/repos/" + repo_owner + "/" +
                          repo_name + "/commits?per_page=10000")
    response = urllib2.urlopen(req)
    decoder = JSONDecoder()
    commits = decoder.decode(response.read())
    user_commits = {}
    biggest = 0
    top_committer = ""
    for c in commits:
        committer = c["committer"]
        if committer == None:
            continue
        committer = committer["login"]
        if (not user_commits.has_key(committer)):
            user_commits[committer] = 0
        user_commits[committer] = user_commits[committer] + 1
        if (user_commits[committer] > biggest):
            top_committer = committer
    return {"commit-number": len(commits), "top-committer": top_committer}
コード例 #27
0
class ExternalR2:
    
    def __init__(self, cmd, cwd, args=[]):
        self.cmd = cmd
        self.cwd = cwd
        self.args = args
        self.je = JSONEncoder(ensure_ascii=True)
        self.jd = JSONDecoder()
        self.proc = None
        self.output = None
        self.err = None

    def start(self, sendData=None):
        self.proc = Popen(args=[self.cmd] + self.args, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=self.cwd)
        self.output, self.err = self.proc.communicate(input=sendData)
        return self
    
    def start_fitness(self, x):
        j = self.je.encode(x.tolist())
        b = b64encode(j.encode())
        return self.start(sendData=b)
    
    def wait(self):
        pResult = self.proc.wait()
        if (pResult != 0):
            raise Exception("The process exited with an error: {}".format(self.err))

        # The process is expected to output a JSON.
        return self.jd.decode(b64decode(self.output).decode())
コード例 #28
0
class JSON(AbstractCodec):
    def __init__(self, types=(), converters=(), custom_typeid='__type__'):
        self.types = AbstractCodec.expand_datatype_list(types)
        self.converters = AbstractCodec.expand_converter_list(converters)
        self.custom_typeid = custom_typeid

        self.encoder = JSONEncoder(default=self._box_object)
        self.decoder = JSONDecoder(object_hook=self._unbox_object)

        self.encoders = dict(
            (datatype, (idx, encode))
            for idx, (datatype, encode, decode) in enumerate(self.types))
        self.inflaters = tuple(
            (idx + len(self.types), inflate)
            for idx, (inflate, deflate) in enumerate(self.converters))

        self.decoders = tuple(decode
                              for datatype, encode, decode in self.types)
        self.decoders += tuple(deflate for inflate, deflate in self.converters)

    def _box_object(self, obj):
        try:
            typeid, encode = self.encoders[type(obj)]
            return {self.custom_typeid: (typeid, encode(obj))}
        except KeyError:
            for typeid, inflate in self.inflaters:
                result = inflate(obj)
                if result is not None:
                    break
            else:
                raise TypeError('Cannot encode objects of type %s' %
                                type(obj).__name__)

            return {self.custom_typeid: (typeid, result)}

    def _unbox_object(self, obj):
        try:
            typeid, data = obj[self.custom_typeid]
        except (KeyError, ValueError):
            return obj

        try:
            decode = self.decoders[typeid]
        except IndexError:
            raise TypeError('Invalid type identifier %s' % typeid)

        return decode(data)

    def decode(self, data):
        try:
            return self.decoder.decode(data)
        except BaseException as e:
            raise ValueError('Failed to decode "%s"' % str(data))

    def encode(self, obj):
        try:
            return self.encoder.encode(obj)
        except BaseException as e:
            raise ValueError('Failed to encode "%s"' % str(obj))
コード例 #29
0
ファイル: view.py プロジェクト: joker234/skarphed
    def get_from_json(cls, json):
        """
        creates a view from a json that looks like this:

        {'s':<page_id>,
         'v':{'<space_id>':<widget_id>,'<space_id>':<widget_id>,[...]},
         'b':{'<box_id>':[<widget_id>, <widget_id>, [...]]},
         'c':{'<wgt_id>': {<widget_args>},'wgt_id':{<widget_args>},[...]},
         'p':<wgt_id>
        }

        's' is the page that this view is going to be rendered on 
        'v' is a dictionary that maps space_ids to widget_ids
        'b' represents box-packed widgets
        'c' maps parameters to widgets
        'p' is an OPTIONAL parameter. if a html-form is submitted, this 
            contains a widget_id to 
        """

        json = unquote(json)
        jd = JSONDecoder()
        try:
            json = jd.decode(json)
        except ValueError:
            raise ViewException(ViewException.get_msg(7))

        view = View(cls._core)
        if json.has_key('s'):
            view.set_page(json['s'])
        else:
            raise ViewException(ViewException.get_msg(6))

        if json.has_key('v'):
            for key, value in json['v'].items(): #transform indices back to int
                json['v'][int(key)] = value
                del(json['v'][key])
            view.set_space_widget_mapping(json['v'])
        else:
            view.set_space_widget_mapping({})

        if json.has_key('b'):
            for key, value in json['b'].items(): #transform indices back to int
                json['b'][int(key)] = value
                del(json['b'][key])
            view.set_box_mapping(json['b'])
        else:
            view.set_box_mapping({})

        if json.has_key('c'):
            for key, value in json['c'].items(): #transform indices back to int
                json['c'][int(key)] = value
                del(json['c'][key])
            view.set_widget_param_mapping(json['c'])
        else:
            view.set_widget_param_mapping({})
        if json.has_key('p'):
            view.set_post_widget_id(json['p'])

        return view
コード例 #30
0
ファイル: tests.py プロジェクト: raulcd/django-course
 def test_05_create_user(self):
     self.client.login(username='******', password='******')
     json = simplejson.dumps({"username": "******", "first_name": "", "last_name": "", "is_active": True, "is_superuser": False, "is_staff": False, "last_login": "******", "groups": [], "user_permissions": [], "password": "******", "email": "", "date_joined": "2011-03-23 06:02:28"})
     response = self.client.post('/user/', data=json, content_type='application/json')
     self.assertContains(response, 'username', count=1, status_code=200)
     decoder = JSONDecoder()
     user = decoder.decode(response.content)
     self.assertEqual(user['username'], 'otro_user')
コード例 #31
0
ファイル: api.py プロジェクト: jamorton/wisc-hackathon
def ajax_get_event_attendees():
	hackathon = get_object_or_404(Hackathon, id = request.form["hackathon_id"])
	req = urllib2.Request("https://graph.facebook.com/"+str(hackathon.facebook_id)+"/attending?access_token="+session["fb_token"])
	print "https://graph.facebook.com/"+str(hackathon.facebook_id)+"/attending?access_token="+session["fb_token"]
	response = urllib2.urlopen(req)
	decoder = JSONDecoder()
	attending = decoder.decode(response.read())["data"]
	return {"attending": attending}
コード例 #32
0
ファイル: models.py プロジェクト: boxed/WCS-Hub
 def to_dict(self):
     decoder = JSONDecoder()
     result = dict(
         [
             (p[:-len('_json')], decoder.decode(getattr(self, p))) if p.endswith('_json') else (p, getattr(self, p))
             for p in self.properties()
         ]
         +[('id', unicode(self.key().id()))])
     return result
コード例 #33
0
 def _receive(self, rtl):
     poller = poll()
     poller.register(rtl.stdout.fileno(), POLLIN)
     poller.register(rtl.stderr.fileno(), POLLIN)
     try:
         decoder = JSONDecoder()
         while self._resume:
             ready = poller.poll(0.2)
             if not ready:
                 continue
             err = ''
             for fd, event in ready:
                 if fd == rtl.stderr.fileno():
                     err = rtl.stderr.readline().strip()
                     if err:
                         self.log.warning(err)
                     break
             if err:
                 continue
             js = rtl.stdout.readline()
             if not js:
                 self.log.error('No data, restart')
                 sleep(0.5)
                 ready = poller.poll(0.2)
                 if ready:
                     for fd, event in ready:
                         if fd == rtl.stderr.fileno():
                             err = rtl.stderr.read()
                             for line in err.split('\n'):
                                 line = line.strip()
                                 if line:
                                     self.log.warning(line)
                                 if line.startswith('Unable to open'):
                                     self.log.fatal('No RTL-SDR device')
                                     self._resume = False
                 return None
             try:
                 data = decoder.decode(js)
             except ValueError as ex:
                 self.log.error('Error: %s', ex)
                 self.log.info('JSON: %s', js)
                 continue
             self.log.debug('JS: %s', data)
             dev_id = data.get('id', 0)
             dev_rid = data.get('rid', 0)
             dev_ch = data.get('channel', 0)
             device = (dev_id, dev_rid, dev_ch)
             dev_str = ':'.join(['%d' % it for it in device])
             if dev_str not in self._devices:
                 self.log.warning('Device %s ignored', dev_str)
                 continue
             self.log.debug('Pushing message to %s', dev_str)
             sensor = self._devices[dev_str]
             if self._rrd:
                 self._rrd.push(sensor, data)
     except KeyboardInterrupt:
         self._resume = False
コード例 #34
0
ファイル: debug_alert.py プロジェクト: archlevel/knowing
def compare_and_notify():
    """
    iterate through all charts, checking data whether it is out of the marks, notify the related people
    """

    json_decoder = JSONDecoder()

    charts = session.query(Charts).all()

    # now = datetime.datetime.now()
    now = datetime.datetime.strptime(sys.argv[1], "%Y-%m-%d_%H:%M:%S")

    for i in charts:
        logger.debug("checking chart \"%s\"" % i.name)
        if i.alert_mode and i.alert_enable:
            try:
                duration = json_decoder.decode(i.alert_duration)
                alert_start = duration["start"]
                alert_end = duration["end"]

                if not (alert_start <= now <= alert_end):
                    continue
            except:
                pass

            td = datetime.date.today()
            tablename = 't_chartdata_%s' % td.strftime('%Y%m%d')

            latency = i.alert_latency
            delta5mins = datetime.timedelta(minutes = 5)
            delta7days = datetime.timedelta(days = 7)
            tablename_lastweek = 't_chartdata_%s' % (td - delta7days).strftime('%Y%m%d')
            start = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute) - datetime.timedelta(minutes = latency)
            end = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute)
            data_this_time = __get_chartdata(i, tablename, start, end)
            logger.debug("data_this_time: %s" % str(data_this_time))

            if i.alert_mode == CONST_HWM or i.alert_mode == CONST_LWM:
                if i.alert_mode == CONST_HWM:
                    level, msg = __get_alert_level_hwm(i, data_this_time)
                if i.alert_mode == CONST_LWM:
                    level, msg = __get_alert_level_lwm(i, data_this_time)
            else:
                start = start - delta7days
                end = end - delta7days
                data_last_week = __get_chartdata(i, tablename_lastweek, start, end)
                logger.debug("data_last_week: %s" % str(data_last_week))
                level, msg = __get_alert_level_range(i, data_this_time, data_last_week)

            logger.debug("%s: %s, %s" % (i.name, level, msg))


            add_events(i, level, msg)

            if level == CONST_LEVEL_CRITICAL:
                __notify(i, msg)
コード例 #35
0
 def _setupFunctions(self):
     f = open('/var/www/fabui/application/plugins/pcbmill/assets/python/jsbuttons.json', 'r')
     fc = f.read()
     jsonD = JSONDecoder()
     funcDict = jsonD.decode(fc)
     f.close()
     
     for key, value in funcDict.iteritems():
         if value['function'] != 'notUsed' and value['function'] != '':
             self.activeFunctions[key] = value
コード例 #36
0
    def get_channel_list(self):
        """获取频道列表

        """
        try:
            r = urlopen(self.channel_list_url)
            json = JSONDecoder()
            self.channel_list = json.decode(r.read().decode())
        except HTTPError as err:
            print(err)
コード例 #37
0
ファイル: rpc.py プロジェクト: joker234/skarphed
 def __call__(self, instructions):
     jd = JSONDecoder()
     je = JSONEncoder()
     answer = {}
     try:
         instructions = jd.decode(instructions)
     except ValueError, e:
         answer['error'] = "could not decode instructions"
         self._core.response_body.append(je.encode(answer))
         return
コード例 #38
0
 def __init__(self, source):
     self.__dict__ = OrderedDict()
     if os.path.isfile(source):
         with open(source, 'r') as f:
             jsonString = f.read()
     else:
         jsonString = source
     decoder = JSONDecoder(object_hook=OrderedDict)
     self.__dict__ = decoder.decode(jsonString)  # json.loads(jsonString)
     self._jsonFile = source
コード例 #39
0
ファイル: tests.py プロジェクト: raulcd/django-course
 def test_03_read_user_admin(self):
     self.client.login(username='******', password='******')
     response = self.client.get('/user/')
     #There are two users in the fixtures and if the user is admin will return 2
     self.assertContains(response, 'is_staff', count=2, status_code=200)
     decoder = JSONDecoder()
     user = decoder.decode(response.content)
     self.assertEqual(len(user), 2)
     self.assertEqual(user[0]['username'], 'admin')
     self.assertEqual(user[1]['username'], 'pepe')
コード例 #40
0
def add_hero():
    data = request.get_json()
    connector = mysql.connect()
    d = JSONDecoder()
    request_data = d.decode( json.dumps(data))
    cur = connector.cursor()
    cur.execute("insert into heroes (id, name) values (null, %(name)s)", request_data)
    connector.commit()
    connector.close()
    return jsonify({'result': 'ok'})
コード例 #41
0
ファイル: rpc.py プロジェクト: joker234/skarphed
 def __call__(self, instructions):
     jd = JSONDecoder()
     je = JSONEncoder()
     answer = {}
     try:
         instructions = jd.decode(instructions)
     except ValueError, e:
         answer['error'] = "could not decode instructions"
         self._core.response_body.append(je.encode(answer))
         return
コード例 #42
0
ファイル: fm.py プロジェクト: houhlin/hellopy
    def get_channel_list(self):
        """获取频道列表

        """
        try:
            r = urlopen(self.channel_list_url)
            json = JSONDecoder()
            self.channel_list = json.decode(r.read().decode())
        except HTTPError as err:
            print(err)
コード例 #43
0
def update_hero():
    data = request.get_json()
    connector = mysql.connect()
    d = JSONDecoder()
    request_data = d.decode( json.dumps(data))
    cur = connector.cursor()
    cur.execute("update heroes set name = %(name)s where id = %(id)s;", request_data)
    connector.commit()
    connector.close()
    return jsonify({'result': 'ok'})
コード例 #44
0
ファイル: rss_watcher.py プロジェクト: pubgem/rss-aggregator
    def load_list(self, path):
        """
        """

        from json import JSONDecoder
        from rss_aggregator import models
        d = JSONDecoder()

        with open(path, 'r') as f:
            for i in d.decode(f.read()):
                models.RSSFeed.load(i)
コード例 #45
0
 def on_login(self, response):
     jsd = JSONDecoder()
     res = jsd.decode(response.body_as_unicode())
     if res['username'] == cfg.LOGIN and res['loggedIn']:
         self.log_print('Logged in... Will do the job...')
         #Постим сообщение
         #return Request(self.domain_name + '/add_comment.jsp?topic=%s'%cfg.REPORT_TO,
         #               callback=self.on_report_form_enter)
         self.topic = []
         return Request(self.tracker_page, callback=self.on_tracker_enter)
     return None
コード例 #46
0
ファイル: Requests.py プロジェクト: sharmaprajesh/WMCore
 def decode(self, data):
     """
     decode the data to python from json
     """
     if data:
         decoder = JSONDecoder()
         thunker = JSONThunker()
         data = decoder.decode(data)
         unthunked = thunker.unthunk(data)
         return unthunked
     return {}
コード例 #47
0
ファイル: Requests.py プロジェクト: PerilousApricot/WMCore
 def decode(self, data):
     """
     decode the data to python from json
     """
     if data:
         decoder = JSONDecoder()
         thunker = JSONThunker()
         data = decoder.decode(data)
         unthunked = thunker.unthunk(data)
         return unthunked
     else:
         return {}
コード例 #48
0
class Decoder(XbmcRemoteObject):
    '''
    Class that picks up messages received by Sender and decodes them, then
    passes them on to Controller
    '''

    def __init__(self, application):
        XbmcRemoteObject.__init__(self, application)
        self.decoder = JSONDecoder()
        self.signal_connect("xbmc_received", self.add)

    def add(self, signaller, json, data=None):
        '''Generate a python object from the json string'''
        json_object = self.decoder.decode(json)
        self.decode(json_object)

    def decode(self, json_object):
        '''
        Extract the important information from a list of dictionaries or lists
        '''

        for response in json_object:
            if isinstance(response, list):
                # The response could actually be a list of responses
                print 'recursing'
                self.decode(response)
            else:
                # Otherwises the response will be a dictionary.
                if 'error' in response:
                    print 'decoded error'
                    kind = 'error'
                    identifier = response['id']
                    result = response['error']
                elif 'result' in response:
                    print 'decoded result'
                    kind = 'response'
                    identifier = response['id']
                    result = response['result']
                elif 'method' in response:
                    print 'decoded notification'
                    if response['method'] == 'Announcement':
                        kind = 'announcement'
                        identifier = None
                        result = response['params']['message']
                    else:
                        kind = 'notification'
                        identifier = response['method']
                        result = response['params']['data']
                else:
                    break

                data = {'kind': kind, 'data': result, 'id': identifier}
                self.emit('xbmc_decoded', data)
コード例 #49
0
ファイル: windows.py プロジェクト: ineff/timercicletta
 def updateSchedule(self): # This function will update the schedule
    
    days = ['lu','ma','me','gi','ve','sa','do'] 
    dec = JSONDecoder(encoding='ascii') 
    rawdata = urlopen('http://www.radiocicletta.it:80/programmi.json').read() # We retrieve the json in str type
    # Now we extract from string rawdata the list of programs active (stato == 1)
    listaProgs = filter(lambda x: x['stato'] == '1',dec.decode(rawdata)['programmi']) 
    # Finally insert in the dictionary schedule the list of start time of the programs
    for today in days:
       self.schedule[today] = map(lambda x: fromArr2Time(x['start'][1:3]),
                                  filter(lambda x: x['start'][0] == today, listaProgs)) 
    
    return True
コード例 #50
0
ファイル: version_check.py プロジェクト: ArseniyK/Sunflower
	def __threaded_check(self):
		"""Method called in separate thread"""
		try:
			# get data from web
			url_handler = urllib.urlopen(self.URL)
			data = url_handler.read()

		finally:
			decoder = JSONDecoder()
			releases = decoder.decode(data)

			with gtk.gdk.lock:
				self._entry_latest.set_text(releases[0]['tag_name'])
コード例 #51
0
ファイル: Weather.py プロジェクト: rkania3/Python
def  weather(zipcode):
  url = 'http://api.openweathermap.org/data/2.5/weather?zip={' + str(zipcode) + '},us'
  response = urllib.urlopen(url)
  weather_html = response.read()
  
  decoder = JSONDecoder()
  weather_data = decoder.decode(weather_html)
  
  city = weather_data['name']
  
  kelvin = weather_data['main']['temp']
  fahrenheit = 1.8*(kelvin-273.15) + 32
  
  print("You are in %s and it is %f degrees outside!" % (city, fahrenheit))
コード例 #52
0
ファイル: spinnaker.py プロジェクト: jtk54/citest
  def set_http_response(self, http_response):
    """Updates specialized fields from http_response.

    Args:
      http_response: [HttpResponseType] From the last status update.
    """
    # super(SpinnakerStatus, self).set_http_response(http_response)
    if http_response.http_code is None:
      self.__current_state = 'Unknown'
      return

    decoder = JSONDecoder()
    self.__json_doc = decoder.decode(http_response.output)
    self._update_response_from_json(self.__json_doc)
コード例 #53
0
class FrameConverter(object):
    def __init__(self):
        self.debug = True
        self.encoder = JSONEncoder()
        self.decoder = JSONDecoder()

    def setDebug(self, debug):
        self.debug = debug

    def encode(self, depthFrame, colorFrame, labelFrame, skeletonFrame):
        encodedObject = {
            "depth": self.encode_image(depthFrame),
            "color": self.encode_image(colorFrame),
            "label": labelFrame,
            "skeleton": skeletonFrame,
        }
        encodedJSON = self.encoder.encode(encodedObject)
        if self.debug:
            decodedFrame = self.decode(encodedJSON)
            assert np.array_equal(decodedFrame["depth_image"], depthFrame)
            assert np.array_equal(decodedFrame["color_image"], colorFrame)
            assert np.array_equal(decodedFrame["label"], labelFrame)
            assert np.array_equal(decodedFrame["skeleton"], skeletonFrame)

        return encodedJSON

    def decode(self, json):
        decodedDict = self.decoder.decode(json)
        depthFrame = self.decode_image(decodedDict["depth"])
        colorFrame = self.decode_image(decodedDict["color"])
        labelFrame = decodedDict["label"]
        skeletonFrame = decodedDict["skeleton"]

        return {"depth_image": depthFrame, "color_image": colorFrame, "label": labelFrame, "skeleton": skeletonFrame}

    def encode_image(self, original_image):
        encoded_image = base64.b64encode(original_image)
        image_shape = original_image.shape
        return {"image": encoded_image, "shape": image_shape}

    def decode_image(self, encoded_image_frame):
        try:
            depthFrame = base64.decodestring(encoded_image_frame)
            bytes = bytearray(depthFrame)
            image = Image.open(io.BytesIO(bytes))
            encoded_image = array(image)
        except:
            return ""
        return encoded_image