Ejemplo n.º 1
0
    def safe_eval(self, str, locals=None, include_exceptions=False):

        # do not allow method calls to modules
        if not isinstance(str, basestring):
            # already templated to a datastructure, perhaps?
            if include_exceptions:
                return (str, None)
            return str
        if re.search(r'\w\.\w+\(', str):
            if include_exceptions:
                return (str, None)
            return str
        # do not allow imports
        if re.search(r'import \w+', str):
            if include_exceptions:
                return (str, None)
            return str
        try:
            result = None
            if not locals:
                result = _literal_eval(str)
            else:
                result = _literal_eval(str, None, locals)
            if include_exceptions:
                return (result, None)
            else:
                return result
        except Exception, e:
            if include_exceptions:
                return (str, e)
            return str
Ejemplo n.º 2
0
 def _submit(self, operationUrl, data):
     # type: (str, dict) -> dict
     
     orderedData = _OrderedDict()
     isBatch = "batch" in operationUrl
     
     if not self.submitRequests and "format" in data.keys():
         data.pop("format")
     
     for key in sorted(data.keys()):
         orderedData[key] = data[key]
     data = orderedData
     
     requestUrls = data.pop("requests") if isBatch else []
     requestAsParams = "&".join(["requests[]=" + url for url in requestUrls]) if isBatch else ""
         
     urlParams = _urlencode(data)
     urlParams += "&" + requestAsParams if isBatch else ""
     urlToSignature = operationUrl + urlParams + self.privateKey
     signature = _md5(urlToSignature.encode()).hexdigest()
     finalUrl = operationUrl + urlParams + "&signature=" + signature
     
     if self.submitRequests:
         if _DEBUG: print("Requesting URL:", finalUrl)
         response = _urlopen(finalUrl).read().decode()
         
         if self.responseFormat == "json":
             return _literal_eval(response)["response"]
         else:
             return response
     else:
         if _DEBUG: print("Generated URL:", finalUrl)
         return finalUrl
Ejemplo n.º 3
0
def literal_eval(string):
    if isinstance(string, _string_types):
        try:
            string = _literal_eval(string)
        except:
            sstring = string.split(',')
            if len(sstring) > 1:
                string = [literal_eval(o.strip()) for o in sstring]

    return string
Ejemplo n.º 4
0
 def _eval(self,value):
     """ Parse into Python objects using literal_eval. Resolve paths. """
     value = _literal_eval(value)
     try:
         basestring
     except NameError:
         basestring = str
     if isinstance(value,basestring):
         value = resolve_paths(value)
     return value
Ejemplo n.º 5
0
    def get_literal(self, parameter):
        """
        Evaluate parameter str value to Python object
        and return it.

        Args:
            parameter (str): Parameter to get

        Returns:
            object: evaluated parameter
        """
        value = self[parameter]
        try:
            return _literal_eval(value)
        except (ValueError, TypeError, SyntaxError):
            return value
Ejemplo n.º 6
0
def literal_eval(arg):
    if isinstance(arg, bool):
        return arg
    return _literal_eval(arg)
Ejemplo n.º 7
0
def _param_eval(string):
    """ safe literal eval """
    try:     return _literal_eval(string)
    except:  return string