Beispiel #1
0
def load_json(path):
    """ given a path, return a structure with the contained JSON """
    dirname = os.path.dirname(path)
    if not dirname:
        name = os.path.basename(path)
        thisdir = os.path.dirname(os.path.realpath(__file__))
        path = os.path.join(thisdir, name + '.json')

    filep = None
    try:
        filep = open(path)
    except IOError:
        L.warning("Can't open " + path + " for reading")
        return False

    data = {}
    filestr = filep.read()
    try:
        data = json.loads(filestr)
    except ValueError:
        L.error("Error: packet not valid json:" + filestr)
        filep.close()
        return False

    filep.close()
    return data
Beispiel #2
0
def gen_files(json_result, parent, name):
    file_name, file_extension = os.path.splitext(name)
    value = join_path(parent, name)
    key = normal_key(file_name)
    if valid_ext(file_extension):
        json_result[key] = value
        # L.info("+ %s = %s", key, value)
    else:
        L.warning("- %s = %s", key, value)
    return json_result
Beispiel #3
0
	def addCapsule(
			openingDate=None,
			closingDate=None,
			lat=None,
			lng=None,
			tll=0, 
			anonymous=True, 
			encrypt=False, 
			user=None,
			content=None,
			password=None
		):
		"""
		Add a new capsule
		Create a new capsule and assign it to current user ( owner )
		content is uniencoded
		in : all the defining capsule parameters
		out : TSCid if ok, None otherwise
		"""
		L.info("addCapsule called")
		L.debug({
			'openingDate' : openingDate, 
			'closingDate' : closingDate, 
			'lat' : lat,
			'lng' : lng, 
			'ttl' : tll,
			'anonymous' : anonymous,
			'encrypt' : encrypt,
			'user' : user,
			'content' : content, 
			'pwd len' : len(password) if password is not None else -1
		})
		try:
			tsc = \
				TimespaceCapsule(
					openingDate = openingDate,
					closingDate = closingDate,
					content = content,
					positionLat = lat,
					positionLng = lng,
					positionTll = tll,
					anonymous = anonymous,
					owner = user,
					encrypt = encrypt,
					password = password 
				)
			tsc.put()
		except Exception as e:
			L.warning("Got exception!")
			L.exception(e)
			return None
		else:
			L.info("Capsule created")
			return tsc.TSCid
Beispiel #4
0
    def addCapsule(openingDate=None,
                   closingDate=None,
                   lat=None,
                   lng=None,
                   tll=0,
                   anonymous=True,
                   encrypt=False,
                   user=None,
                   content=None,
                   password=None):
        """
		Add a new capsule
		Create a new capsule and assign it to current user ( owner )
		content is uniencoded
		in : all the defining capsule parameters
		out : TSCid if ok, None otherwise
		"""
        L.info("addCapsule called")
        L.debug({
            'openingDate': openingDate,
            'closingDate': closingDate,
            'lat': lat,
            'lng': lng,
            'ttl': tll,
            'anonymous': anonymous,
            'encrypt': encrypt,
            'user': user,
            'content': content,
            'pwd len': len(password) if password is not None else -1
        })
        try:
            tsc = \
             TimespaceCapsule(
              openingDate = openingDate,
              closingDate = closingDate,
              content = content,
              positionLat = lat,
              positionLng = lng,
              positionTll = tll,
              anonymous = anonymous,
              owner = user,
              encrypt = encrypt,
              password = password
             )
            tsc.put()
        except Exception as e:
            L.warning("Got exception!")
            L.exception(e)
            return None
        else:
            L.info("Capsule created")
            return tsc.TSCid
Beispiel #5
0
def gen_folders(json_result, path, parent):
    path = abs_path(path)
    if valid_folder(path):
        files = os.listdir(path)
        L.info("+ dir: %s", path)
        for name in files:
            full_path = join_path(path, name)
            if os.path.isdir(full_path):
                json_result = gen_folders(json_result, full_path,
                                          join_path(parent, name))
            elif os.path.isfile(full_path):
                json_result = gen_files(json_result, parent, name)
    else:
        L.warning("- dir: %s", path)
    return json_result