def process(self, chain, document:Document, **keyargs): ''' @see: HandlerProcessor.process Process the jinja templates. ''' assert isinstance(document, Document), 'Invalid document %s' % document templates = {} for path, stream in self.listPaths().items(): if self._rPatternTemplate.match(path): try: templates[path] = stream.read().decode('utf8') finally: if isinstance(stream, IClosable): stream.close() elif self._rPatternCopy.match(path): try: folder = path[:path.index('/')] except ValueError: folder = self.pathDocumentation else: folder = os.path.join(self.pathDocumentation, folder.replace('/', os.sep)) if not os.path.exists(folder): os.makedirs(folder) file = os.path.join(self.pathDocumentation, path.replace('/', os.sep)) try: with open(file, 'wb') as dest: pipe(stream, dest) finally: if isinstance(stream, IClosable): stream.close() document.loader = DictLoader(templates)
def insert(self, content): ''' @see: IMetaDataService.insert ''' assert isinstance(content, Content), 'Invalid content %s' % content if not content.getName(): raise InputError(_('No name specified for content')) metaData = MetaDataMapped() metaData.CreatedOn = datetime.now() metaData.Name = content.getName() metaData.Type = self._metaType.Key metaData.typeId = self._metaType.id metaData.thumbnailId = self._thumbnail.id try: self.session().add(metaData) self.session().flush((metaData,)) path = abspath(join(self.processing_dir_path, '.'.join((str(metaData.Id), metaData.Name)))) with open(path, 'w+b') as fobj: pipe(content, fobj) metaData.SizeInBytes = getsize(path) self.session().flush((metaData,)) with open(path, 'rb') as fobj: self.cdmArchive.publishFromFile(self._reference(metaData), fobj) for handler in self.metaDataHandlers: assert isinstance(handler, IMetaDataHandler), 'Invalid handler %s' % handler if handler.process(metaData.Id, path): break else: remove(path) except SQLAlchemyError as e: handle(e, metaData) return metaData.Id
def configureMongrel2(): if not options.isFlag(FLAG_CONFIG_MONGREL2): return folders = [path.join('mongrel2', name) for name in ('logs', 'run', 'tmp')] folders.append(path.join('shared', 'upload')) folder = options.mongrel2Folder or 'workspace' for name in folders: folder = path.join(folder, name) if not path.isdir(folder): makedirs(folder) updateConfig = False if server_type() != 'mongrel2': updateConfig = True support.persist(server_type, 'mongrel2') sendIdent = send_ident() if sendIdent is None: updateConfig = True sendIdent = str(uuid4()) support.persist(send_ident, sendIdent) replace = {} replace['${send_spec}'] = send_spec() replace['${send_ident}'] = sendIdent replace['${recv_spec}'] = recv_spec() replace['${recv_ident}'] = recv_ident() replace['${server_port}'] = str(server_port()) if updateConfig: saveConfigurations(context.configurationsExtract()) conf = openURI(path.join(pythonPath(), 'resources', 'ally.conf')) conf = codecs.getreader('utf8')(conf) conf = ReplaceInStream(conf, replace) with open(path.join(folder, 'ally.conf'), 'w') as f: pipe(conf, f) with open(path.join(folder, 'README-Mongrel2.txt'), 'wb') as f: pipe( openURI(path.join(pythonPath(), 'resources', 'README-Mongrel2.txt')), f) log.info('Configured \'%s\' mongrel2 workspace' % folder)
def configureMongrel2(): if not options.isFlag(FLAG_CONFIG_MONGREL2): return folders = [path.join('mongrel2', name) for name in ('logs', 'run', 'tmp')] folders.append(path.join('shared', 'upload')) folder = options.mongrel2Folder or 'workspace' for name in folders: folder = path.join(folder, name) if not path.isdir(folder): makedirs(folder) updateConfig = False if server_type() != 'mongrel2': updateConfig = True support.persist(server_type, 'mongrel2') sendIdent = send_ident() if sendIdent is None: updateConfig = True sendIdent = str(uuid4()) support.persist(send_ident, sendIdent) replace = {} replace['${send_spec}'] = send_spec() replace['${send_ident}'] = sendIdent replace['${recv_spec}'] = recv_spec() replace['${recv_ident}'] = recv_ident() replace['${server_port}'] = str(server_port()) if updateConfig: saveConfigurations(context.configurationsExtract()) conf = openURI(path.join(pythonPath(), 'resources', 'ally.conf')) conf = codecs.getreader('utf8')(conf) conf = ReplaceInStream(conf, replace) with open(path.join(folder, 'ally.conf'), 'w') as f: pipe(conf, f) with open(path.join(folder, 'README-Mongrel2.txt'), 'wb') as f: pipe(openURI(path.join(pythonPath(), 'resources', 'README-Mongrel2.txt')), f) log.info('Configured \'%s\' mongrel2 workspace' % folder)
def insert(self, imageInfo, image): ''' @see: IImagePersistanceService.insert ''' assert isinstance(imageInfo, api.ImageInfo), 'Invalid image info %s' % imageInfo assert isinstance(image, Content), 'Invalid image content %s' % image imageData = ImageData() imageData.CreatedOn = datetime.now() imageData.typeId = self._typeId() try: self.session().add(imageData) self.session().flush((imageData,)) reference = self.format_file_name % {'id': imageData.Id, 'file': image.getName() or self.default_file_name} path = join(self.image_dir_path, reference) with open(path, 'wb') as fobj: pipe(image, fobj) assert isinstance(imageData, MetaDataMapped) imageData.reference = reference imageData.SizeInBytes = getsize(path) #TODO: implement read the actual meta data imageData.Width = 100 imageData.Height = 100 self.session().flush((imageData,)) imageInfoDb = copy(imageInfo, ImageInfo()) imageInfoDb.MetaData = imageData.Id self.session().add(imageInfoDb) self.session().flush((imageInfoDb,)) except SQLAlchemyError as e: handle(e, imageInfoDb) imageInfo.Id = imageInfoDb.Id return imageInfoDb.Id
def config(): assert isinstance(application.options, OptionsMongrel2), 'Invalid application options %s' % application.options if not application.options.configMongrel2: return workspace = application.options.configMongrel2 folders = [path.join('mongrel2', name) for name in ('logs', 'run', 'tmp')] folders.append(path.join('shared', 'upload')) for name in folders: folder = path.join(workspace, name) if not path.isdir(folder): makedirs(folder) configFile = application.options.configurationPath if path.isfile(configFile): with open(configFile, 'r') as f: config = load(f) else: print('The configuration file "%s" doesn\'t exist, create one by running the the application ' 'with "-dump" option, also change the application properties "server_type" configuration to "mongrel2" ' 'and also adjust the "recv_spec", "send_spec" and "server_port" accordingly' % configFile, file=sys.stderr) sys.exit(1) try: context.open(aop.modulesIn('__setup__.**'), config=config) updateConfig = False if server_type() != 'mongrel2': updateConfig = True support.persist(server_type, 'mongrel2') sendIdent = send_ident() if sendIdent is None: updateConfig = True sendIdent = str(uuid4()) support.persist(send_ident, sendIdent) replace = {} try: replace['${send_spec}'] = send_spec() replace['${send_ident}'] = sendIdent replace['${recv_spec}'] = recv_spec() replace['${recv_ident}'] = recv_ident() replace['${server_port}'] = str(server_port()) if updateConfig: if path.isfile(configFile): renames(configFile, configFile + '.bak') with open(configFile, 'w') as f: save(context.configurations(force=True), f) print('Updated the "%s" configuration file' % configFile) finally: context.deactivate() except SystemExit: raise except: print('-' * 150, file=sys.stderr) print('A problem occurred while configuring Mongrel2', file=sys.stderr) traceback.print_exc(file=sys.stderr) print('-' * 150, file=sys.stderr) else: conf = openURI(path.join(pythonPath(), 'resources', 'ally.conf')) conf = codecs.getreader('utf8')(conf) conf = ReplaceInFile(conf, replace) with open(path.join(workspace, 'ally.conf'), 'w') as f: pipe(conf, f) with open(path.join(workspace, 'README-Mongrel2.txt'), 'wb') as f: pipe(openURI(path.join(pythonPath(), 'resources', 'README-Mongrel2.txt')), f) print('Configured "%s" mongrel2 workspace' % workspace)