Updates for a working local scanner source
This commit is contained in:
+48
-15
@@ -42,7 +42,8 @@ from .literals import (SOURCE_CHOICES, SOURCE_CHOICES_PLURAL,
|
||||
IMAP_PORT, IMAP_SSL_PORT,
|
||||
SOURCE_CHOICE_IMAP_EMAIL, DEFAULT_IMAP_INTERVAL,
|
||||
IMAP_DEFAULT_MAILBOX,
|
||||
SOURCE_CHOICE_LOCAL_SCANNER, SOURCE_ICON_IMAGES)
|
||||
SOURCE_CHOICE_LOCAL_SCANNER, SOURCE_ICON_IMAGES,
|
||||
DEFAULT_LOCAL_SCANNER_FILE_FORMAT)
|
||||
from .compressed_file import CompressedFile, NotACompressedFile
|
||||
from .conf.settings import POP3_TIMEOUT
|
||||
#from . import sources_scheduler
|
||||
@@ -382,8 +383,12 @@ class IMAPEmail(EmailBaseModel):
|
||||
|
||||
|
||||
class LocalScanner(InteractiveBaseModel):
|
||||
# scanner device string to scanner instance cache dict
|
||||
_scanner_cache = {}
|
||||
|
||||
class NoSuchScanner(Exception):
|
||||
pass
|
||||
|
||||
is_interactive = True
|
||||
source_type = SOURCE_CHOICE_LOCAL_SCANNER
|
||||
default_icon = SOURCE_ICON_IMAGES
|
||||
@@ -393,33 +398,61 @@ class LocalScanner(InteractiveBaseModel):
|
||||
|
||||
@classmethod
|
||||
def get_scanners(cls):
|
||||
imagescanner.settings.ENABLE_NET_BACKEND = False
|
||||
imagescanner.settings.ENABLE_TEST_BACKEND = False
|
||||
|
||||
iscanner = imagescanner.ImageScanner(remote_search=False)
|
||||
scanners = iscanner.list_scanners()
|
||||
imagescanner.settings.LOGGING_LEVEL = logging.FATAL
|
||||
imagescanner.settings.ENABLE_NET_BACKEND = False
|
||||
imagescanner.settings.ENABLE_TEST_BACKEND = False
|
||||
|
||||
for scanner in scanners:
|
||||
LocalScanner._scanner_cache[scanner._device] = scanner
|
||||
LocalScanner._scanner_cache[unicode(scanner._device)] = {
|
||||
'scanner': scanner,
|
||||
'description': u'%s: %s - %s - %s <%s>' % (scanner.id, scanner.manufacturer, scanner.name, scanner.description, scanner._device)
|
||||
}
|
||||
|
||||
return scanners
|
||||
|
||||
@classmethod
|
||||
def get_scanner(cls, device):
|
||||
try:
|
||||
return cls._scanner_cache[device]
|
||||
except KeyError:
|
||||
raise cls.NoSuchScanner
|
||||
|
||||
@classmethod
|
||||
def get_scanner_choices(cls):
|
||||
scanners = cls.get_scanners()
|
||||
def get_scanner_choices(cls, description_only=False):
|
||||
if description_only:
|
||||
template_func = lambda scanner: (scanner['description'])
|
||||
else:
|
||||
template_func = lambda scanner: (scanner['scanner']._device, scanner['description'])
|
||||
|
||||
return [(scanner._device, u'%s: %s - %s - %s <%s>' % (scanner.id, scanner.manufacturer, scanner.name, scanner.description, scanner._device)) for scanner in scanners]
|
||||
return [template_func(scanner) for scanner in LocalScanner._scanner_cache.values()]
|
||||
|
||||
def get_scanner(self, fail=False):
|
||||
def scanner(self, _fail=False):
|
||||
try:
|
||||
return LocalScanner._scanner_cache[self.device]
|
||||
except IndexError:
|
||||
if fail==False:
|
||||
return LocalScanner._scanner_cache[self.scanner_device]['scanner']
|
||||
except KeyError:
|
||||
if _fail == False:
|
||||
# Refresh the cache before trying one last time
|
||||
LocalScanner.get_scanners()
|
||||
return self.get_scanner(fail=True)
|
||||
return self.scanner(_fail=True)
|
||||
else:
|
||||
# TODO: raise specialized exception
|
||||
raise Exception('Error getting scanner')
|
||||
raise self.__class__.NoSuchScanner
|
||||
|
||||
def scan(self, as_image=False):
|
||||
image = self.scanner().scan()
|
||||
if as_image:
|
||||
return image
|
||||
else:
|
||||
buf = StringIO()
|
||||
image.save(buf, DEFAULT_LOCAL_SCANNER_FILE_FORMAT)
|
||||
return PseudoFile(buf, name=unicode(datetime.datetime.now()).replace(u'.', u'_').replace(u' ', u'_'))
|
||||
|
||||
# This code make new_version upload fail, use it for debugging
|
||||
#buf = StringIO()
|
||||
#buf.write(image.tostring())
|
||||
#buf.seek(0)
|
||||
#return buf
|
||||
|
||||
class Meta(InteractiveBaseModel.Meta):
|
||||
verbose_name = _(u'local scanner')
|
||||
|
||||
Reference in New Issue
Block a user