imagestore.py
Go to the documentation of this file.
1 import os
2 import qi
3 
4 
5 app = qi.Application()
6 qicore = qi.module('qicore')
7 
8 log = qi.Logger("qicore.file.example.AliceServices.ImageStore")
9 
10 class ImageStore:
11  def __init__(self):
12  self.file_registry = dict()
13  self.counter = 0
14 
15  def __del__(self):
16  for name, path in self.file_registry.iteritems():
17  os.remove(path)
18 
19  # Store a copy of the image file and associate it with the provided name.
20  def store_image(self, image_file, name):
21  # Note that ideally this would be implemented in an asynchronous way,
22  # but for simplicity we will do it synchronously.
23 
24  log.info("Storing '{0}' file ...".format(name))
25 
26  # First, make a local copy in a temporary files directory:
27  temp_file_path = self._generate_temporary_file_path()
28 
29  # This call will block until the end because it returns a FutureSync.
30  qicore.copyToLocal(image_file, temp_file_path)
31 
32  # We now have a local copy of the remote file,
33  # so we don't need the remote access anymore.
34  del image_file
35 
36  # Now we can work with the local file.
37  self._store_file_in_database(name, temp_file_path)
38 
39  # Provide access to an image file associated with the provided name.
40  def get_image(self, name):
41  log.info("Getting '{0}' file ...".format(name))
42  file_location = self._find_file_location(name)
43 
44  if file_location:
45  log.info("'{0}' found at {1}".format(name, file_location))
46 
47  # Now we can open it and provide it to the user for reading.
48  return qicore.openLocalFile(file_location)
49  else:
50  return None # File not found!
51 
53  self.counter = self.counter + 1
54  return "./temp_image_file_{0}.data".format(self.counter)
55 
56  def _store_file_in_database(self, name, path):
57  #...fake it
58  self.file_registry[name] = path
59  log.info("'{0}' stored at {1}".format(name, path))
60 
61  def _find_file_location(self, name):
62  return self.file_registry[name]
63 
64 
65 
66 app.start()
67 
68 session = app.session
69 session.registerService("ImageStore", ImageStore())
70 
71 app.run()
72 
imagestore.ImageStore.counter
counter
Definition: imagestore.py:13
imagestore.ImageStore.store_image
def store_image(self, image_file, name)
Definition: imagestore.py:20
imagestore.ImageStore.get_image
def get_image(self, name)
Definition: imagestore.py:40
imagestore.ImageStore
Definition: imagestore.py:10
imagestore.ImageStore._store_file_in_database
def _store_file_in_database(self, name, path)
Definition: imagestore.py:56
imagestore.ImageStore.file_registry
file_registry
Definition: imagestore.py:12
imagestore.ImageStore._find_file_location
def _find_file_location(self, name)
Definition: imagestore.py:61
imagestore.ImageStore.__init__
def __init__(self)
Definition: imagestore.py:11
imagestore.ImageStore.__del__
def __del__(self)
Definition: imagestore.py:15
imagestore.ImageStore._generate_temporary_file_path
def _generate_temporary_file_path(self)
Definition: imagestore.py:52


naoqi_libqicore
Author(s): Aldebaran
autogenerated on Wed Sep 14 2022 02:22:41