def get_info(self, request):
     path = self.translate_path(request.form['path'])
     parts = path.partition('/representations')
     ip = parts[0]
     hrefs = self._get_href_variations(parts[1] + parts[2])
     namespace = '{http://ead3.archivists.org/schema/}'
     tree = ET.parse('%s/metadata/descriptive/EAD.xml' % ip)
     # regular file - daoset
     for href in hrefs:
         did_list = tree.findall(".//%sdid/*/%sdao[@href='%s']/../.."
                                 % (namespace, namespace, href))
         if did_list:
             o = xmltodict.parse(ET.tostring(did_list[0]))
             return json.dumps(o)
     # regular file - no daoset
     for href in hrefs:
         did_list = tree.findall(".//%sdid/%sdao[@href='%s']/.."
                                 % (namespace, namespace, href))
         if did_list:
             o = xmltodict.parse(ET.tostring(did_list[0]))
             return json.dumps(o)
     # directory
     for href in hrefs:
         did_list = tree.findall(".//%sc[@base='%s']/%sdid"
                                 % (namespace, href, namespace))
         if did_list:
             o = xmltodict.parse(ET.tostring(did_list[0]))
             return json.dumps(o)
     # fallback
     return flask.jsonify(
         error=404,
         error_text='Not Found',
         info='No metadata associated to this element'
     )
示例#2
0
文件: report.py 项目: nfco/netforce
def data_set_path(data, path, val):
    field, _, path2 = path.partition(".")
    if not path2:
        data[field] = val
        return
    parent = data.setdefault(field, {})
    data_set_path(parent, path2, val)
示例#3
0
 def GET(self):
     file_dir = pkg_resources.resource_filename("canopy", "__web__/static")
     path = os.path.join(file_dir, self.static_file)
     if not os.path.exists(path):
         raise web.NotFound(view.error.file_not_found(self.static_file))
     tx.response.headers.content_type = \
         mimetypes.guess_type(self.static_file)[0]
     web.header("X-Accel-Redirect", "/x/" + path.partition("/tree/")[2])
def make_asset(product_id, path, md5_check, class_label='hirise product'):
    asset = Asset()
    asset.class_label = class_label
    asset.instrument_name = 'HiRISE'
    asset.product_id = product_id
    asset.md5_check = md5_check
    asset.relative_file_path = path.partition(DATA_ROOT)[-1]
    asset.save()
示例#5
0
文件: report.py 项目: nfco/netforce
def data_get_path(data, path):
    field, _, path2 = path.partition(".")
    val = data.get(field)
    if not path2:
        return val
    if not val:
        return None
    return data_get_path(val, path2)
示例#6
0
def fixup(path, rje):
    """ Fix `path` for remote/local handling. """
    if ':' in path:  # Remote.
        host, colon, path = path.partition(':')
    elif rje:
        path = os.path.join('..', 'RJE', path)
    else:
        path = os.path.join('..', path)
    return path
示例#7
0
def _embed_relation(resource, path, document, ancestors):
	"""Embeds entities of a given (eventually multilevel) relation into
	the document.

	:param resource: resource of the document
	:param path: dot separated chain of relation names
	:param document: document to embed into
	:param ancestors: list of entities on the current 'path' of embedding

	List in the `ancestors` parameter containing tuples of resource
	name and entity id is used to prevent embedding of an entity into
	itself and to limit the depth of nested embedding.
	"""
	# Extract the topmost relation from the chain of relations and check
	# if there is a reference to a related entity in the actual document
	rel_name, _, tail = path.partition('.')
	relation = config.DOMAIN[resource]['relations'].get(rel_name)
	if not relation or relation['field'] not in document: return

	# Embed unless the entity is already embedded
	if rel_name not in document:
		# Retrieve the related entities
		related_resource = current_app.data.driver.db[relation['resource']]
		value = document[relation['field']]
		results = related_resource.find({relation['fkey']: value})
		entities = []
		for result in results:
			# Prevent embedding of an entity into itself
			if (relation['resource'], result['id']) in ancestors:
				continue
			result.pop('_id')
			# Omit xxx_id property in embedded entity - it is redundant with id it references
			if relation['fkey'] != 'id':
				result.pop(relation['fkey'])
			entities.append(result)
		if entities:
			# Either entity or list of entities will be embedded depending on singular or plural of relation name
			if rel_name.endswith('s'):
				document[rel_name] = entities
			else:
				document[rel_name] = entities[0]
			# Omit xxx_id property in embedding entity - it is redundant with id it references
			if relation['field'] != 'id':
				document.pop(relation['field'])

	# Recursively resolve deeper levels of embedding (limited to 3 levels)
	if tail and rel_name in document and len(ancestors) < 3:
		entities = document[rel_name]
		if not isinstance(entities, list):
			entities = [entities]
		for subdoc in entities:
			ancestors.append((relation['resource'], subdoc['id']))
			_embed_relation(relation['resource'], tail, subdoc, ancestors)
			ancestors.pop()
示例#8
0
def normalize_path(path):
    """
    Returns a path without a dangling ipc:// in front of it.

    >>> path1 = 'ipc:///home/ucuser/.unnaturalCode/socket'
    >>> path2 =       '/home/ucuser/.unnaturalCode/socket'
    >>> normalize_path(path1) == normalize_path(path2)
    True
    """

    if path.startswith('ipc://'):
        _head, _sep, tail = path.partition('ipc://')
        return tail
    return path
示例#9
0
			def recursiveDict( source, path ):
				(head, sep, tail) = path.partition( '.' )
				lNames = [ s.lower() for s in source.keys() ]
				names = [ s for s in source.keys() ]
				if head.lower() in lNames:
					properName = names[ lNames.index( head.lower() ) ]
					entry = source[ properName ]
					if len( tail ) and isinstance( entry, dict ):
						return recursiveDict( entry, tail )
					elif len( tail ) and isinstance( entry, list ):
						for subentry in entry:
							result = recursiveDict( subentry, tail )
							if result is not None: return result
					elif len( tail ) == 0:
						return entry
				return None
示例#10
0
文件: scanner.py 项目: openstack/reno
 def _get_subtree(self, tree, path):
     "Given a tree SHA and a path, return the SHA of the subtree."
     try:
         if os.sep in path:
             # The tree entry will only have a single level of the
             # directory name, so if we have a / in our filename we
             # know we're going to have to keep traversing the
             # tree.
             prefix, _, trailing = path.partition(os.sep)
             mode, subtree_sha = tree[prefix.encode('utf-8')]
             subtree = self[subtree_sha]
             return self._get_subtree(subtree, trailing)
         else:
             # The tree entry will point to the SHA of the contents
             # of the subtree.
             mode, sha = tree[path.encode('utf-8')]
             result = self[sha]
             return result
     except KeyError:
         # Some part of the path wasn't found, so the subtree is
         # not present. Return the sentinel value.
         return None
示例#11
0
 def __init__(self, entries):
     parsed = {}
     for path in filter(None, entries):
         parent, _, remainder = path.partition('/')
         parsed.setdefault(parent, []).append(remainder)
     self._entries = parsed or None  # None is a leaf node
示例#12
0
def find_dir(name, path=THIS_DIR):
    """Return the shortest path containing name if possible, or None otherwise."""
    p = path.partition(name)
    return p[0] + p[1] if p[2] else None
示例#13
0
 def _generate_category_name(self, path):
     if path == self.library.path:
         return os.path.split(path)[1]
     else:        
         return path.partition(self.library.path)[2][1:]
示例#14
0
文件: model.py 项目: karolaug/pisak
 def _generate_category_name(path):
     pic_dir = xdg.get_dir('pictures')
     if path == pic_dir:
         return os.path.split(path)[1]
     else:        
         return path.partition(pic_dir)[2][1:]
示例#15
0
    async def post(self, path):
        '''Starts a task which runs some NLP algorithms on the files pointed 
        to by path if the func query paramter is set. Otherwise the body is
        interpeted as a binary file and a new file whose name is given by the
        file name query parameter is created

        In case a task is started the user can poll for the status of the task 
        by using the returned URL
        '''

        action = self.get_query_argument('func', default=None)

        if action is not None:
            #Send a request to a worker, which will process the
            #resources at the given path with NLP algorithms.
            #The user can poll the status of the task through the
            #returned URL 
            conn_pool = ConnectionPool.instance()

            conn = await conn_pool.get()

            #Send message for task to a worker
            try:
                #Create body of the message and encode it to json 
                msg_body = {
                    'path': path
                }
                json_body = json.dumps(msg_body)

                #Create message and send it to the worker
                msg = Message(type=Message.Type['Request'],
                              body=json_body.encode('utf-8'))
                conn.send_msg(msg)

                #Retrieve response of worker
                resp = await conn.receive_msg()
                self.write(resp.body.decode('utf-8'))
            finally:
                conn_pool.put(conn)

            self.set_status(202)
        else:
            file_ = None
            fs = filesystem.instance()
            collection_id = path.partition('/')[0]

            try:
                file_name = self.get_query_argument('filename')
                file_path = os.path.join(path, file_name)
                fs.create(file_path)
                file_ = fs.open(file_path, mode='wb')
            except tornado.web.MissingArgumentError:
                logger.info('Tried to upload file to collection {} without specifying a filename.'.format(collection_id))
                raise
            except FileExistsError:
                logger.info('File {!r} already exists'.format(file_path))
                raise
            except FileNotFoundError:
                logger.info(
                    'Collection {!r} does not exist'.format(collection_id)
                )
                raise
                
            await file_.write(self.request.body)
            file_.close()

            self.set_status(201)

        self.finish()
示例#16
0
    def main(self):
        keys = self.env.get('plist_keys', {"CFBundleShortVersionString": "version"})

        # Many types of paths are accepted. Figure out which kind we have.
        path = os.path.normpath(self.env['info_path'])

        try:
            # Wrap all other actions in a try/finally so if we mount an image,
            # it will always be unmounted.

            # Check if we're trying to read something inside a dmg.
            if '.dmg' in path:
                (dmg_path, dmg, dmg_source_path) = path.partition(".dmg")
                dmg_path += ".dmg"

                mount_point = self.mount(dmg_path)
                path = os.path.join(mount_point, dmg_source_path.lstrip('/'))
            else:
                dmg = False

            # Finally check whether this is at least a valid path
            if not os.path.exists(path):
                raise ProcessorError("Path '%s' doesn't exist!" % path)

            # Is the path a bundle?
            info_plist_path = self.get_bundle_info_path(path)
            if info_plist_path:
                path = info_plist_path

            # Does it have a 'plist' extension (naively assuming 'plist' only names, for now)
            elif path.endswith('.plist'):
                # Full path to a plist was supplied, move on.
                pass

            # Might the path contain a bundle at its root?
            else:
                path = self.find_bundle(path)

            # Try to read the plist
            self.output("Reading: %s" % path)
            try:
                info = FoundationPlist.readPlist(path)
            except (FoundationPlist.NSPropertyListSerializationException,
                    UnicodeEncodeError) as err:
                raise ProcessorError(err)

            # Copy each plist_keys' values and assign to new env variables
            self.env["plist_reader_output_variables"] = {}
            for key, val in keys.items():
                try:
                    self.env[val] = info[key]
                    self.output("Assigning value of '%s' to output variable '%s'" % (self.env[val], val))
                    # This one is for documentation/recordkeeping
                    self.env["plist_reader_output_variables"][val] = self.env[val]
                except KeyError:
                    raise ProcessorError(
                        "Key '%s' could not be found in the plist %s!" % (key, path))

        finally:
            if dmg:
                self.unmount(dmg_path)