def _illustrate_one_material(self, material_directory_path): print('Illustrating {path!s}{sep}'.format( path=material_directory_path.relative_to( self._score_package_path.parent), sep=os.path.sep)) material = self._import_material(material_directory_path) if not hasattr(material, '__illustrate__'): template = ' Cannot illustrate material of type {}.' message = template.format(type(material).__name__) print(message) sys.exit(1) with systemtools.Timer() as timer: try: lilypond_file = material.__illustrate__() except: traceback.print_exc() sys.exit(1) self._report_time(timer, prefix='Abjad runtime') ly_path = self._write_lilypond_ly( lilypond_file=lilypond_file, output_directory_path=material_directory_path, ) self._write_lilypond_pdf( ly_path=ly_path, output_directory_path=material_directory_path, )
def _illustrate_one_segment(self, segment_directory): print('Illustrating {path!s}{sep}'.format( path=segment_directory.relative_to( self._score_package_path.parent), sep=os.path.sep)) segment_name = segment_directory.name segment_names = self._read_segments_list_json() previous_segment_name = None index = None if segment_name in segment_names: index = segment_names.index(segment_name) if 0 < index: previous_segment_name = segment_names[index - 1] index += 1 previous_metadata = {} if previous_segment_name: previous_segment_metadata_path = self._segments_path.joinpath( previous_segment_name, 'metadata.json', ) previous_metadata = self._read_json(previous_segment_metadata_path) segment_metadata_path = segment_directory.joinpath('metadata.json') metadata = self._read_json(segment_metadata_path) metadata['segment_count'] = len(segment_names) metadata['segment_number'] = index metadata['first_bar_number'] = ( previous_metadata.get('measure_count', 0) + previous_metadata.get('first_bar_number', 1)) segment_package_path = self._path_to_packagesystem_path( segment_directory) definition_import_path = segment_package_path + '.definition' try: module = self._import_path( definition_import_path, self._score_repository_path, ) segment_maker = getattr(module, 'segment_maker') except (ImportError, AttributeError): traceback.print_exc() sys.exit(1) with systemtools.Timer() as timer: try: lilypond_file = segment_maker.run( metadata=metadata, previous_metadata=previous_metadata, ) except: traceback.print_exc() sys.exit(1) metadata = segment_maker.metadata self._write_json(metadata, segment_metadata_path) self._report_time(timer, prefix='Abjad runtime') ly_path = self._write_lilypond_ly( lilypond_file, output_directory=segment_directory, ) self._write_lilypond_pdf( ly_path=ly_path, output_directory=segment_directory, )
def _write_lilypond_pdf( self, ly_path, output_directory, ): from abjad import abjad_configuration pdf_path = output_directory.joinpath('illustration.pdf') message = ' Writing {!s} ... ' message = message.format( pdf_path.relative_to(self._score_repository_path)) print(message, end='') command = '{} -dno-point-and-click -o {} {}'.format( # not sure why the call to abjad_configuration.get() returns none: #abjad_configuration.get('lilypond_path', 'lilypond'), 'lilypond', str(ly_path).replace('.ly', ''), str(ly_path), ) with systemtools.Timer() as timer: with systemtools.TemporaryDirectoryChange(str(ly_path.parent)): exit_code = subprocess.call(command, shell=True) if exit_code: print('Failed!') sys.exit(1) print('OK!') self._report_time(timer, prefix='LilyPond runtime')
def bootstrap_pass_two( cls, model_class, name_attr='name', ): skipped_template = u'{} [SKIPPED] (Pass 2) (id:{}) [{:.8f}]: {}' changed_template = u'{} (Pass 2) (id:{}) [{:.8f}]: {}' corpus = {} maximum_id = model_class.select(peewee.fn.Max(model_class.id)).scalar() for i in range(1, maximum_id + 1): query = model_class.select().where(model_class.id == i) if not query.count(): continue document = list(query)[0] with systemtools.Timer(verbose=False) as timer: changed = document.resolve_references(corpus) if not changed: message = skipped_template.format( model_class.__name__.upper(), document.id, timer.elapsed_time, getattr(document, name_attr), ) print(message) continue document.save() message = changed_template.format( model_class.__name__.upper(), document.id, timer.elapsed_time, getattr(document, name_attr), ) print(message)
def as_pdf( self, pdf_file_path=None, illustrate_function=None, remove_ly=False, strict=None, **keywords ): r'''Persists client as PDF. Autogenerates file path when `pdf_file_path` is none. .. container:: example >>> staff = abjad.Staff("c'4 e'4 d'4 f'4") >>> for x in persist(staff).as_pdf(): # doctest: +SKIP ... x ... '/Users/josiah/.abjad/output/1416.pdf' 0.047142982482910156 0.7839350700378418 Returns output path, elapsed formatting time and elapsed rendering time when PDF output is written. ''' from abjad.tools import systemtools if strict is not None: assert isinstance(strict, int), repr(strict) if illustrate_function is None: assert hasattr(self._client, '__illustrate__'), repr(self._client) if pdf_file_path is not None: pdf_file_path = str(pdf_file_path) pdf_file_path = os.path.expanduser(pdf_file_path) without_extension = os.path.splitext(pdf_file_path)[0] ly_file_path = '{}.ly'.format(without_extension) else: ly_file_path = None result = self.as_ly( ly_file_path, illustrate_function=illustrate_function, strict=strict, **keywords ) ly_file_path, abjad_formatting_time = result without_extension = os.path.splitext(ly_file_path)[0] pdf_file_path = '{}.pdf'.format(without_extension) timer = systemtools.Timer() with timer: success = systemtools.IOManager.run_lilypond(ly_file_path) lilypond_rendering_time = timer.elapsed_time if remove_ly: os.remove(ly_file_path) return ( pdf_file_path, abjad_formatting_time, lilypond_rendering_time, success, )
def as_midi(self, midi_file_path=None, remove_ly=False, **keywords): r'''Persists client as MIDI file. Autogenerates file path when `midi_file_path` is none. .. container:: example >>> staff = abjad.Staff("c'4 e'4 d'4 f'4") >>> for x in persist(staff).as_midi(): # doctest: +SKIP ... x ... '/Users/josiah/.abjad/output/1415.midi' 0.07831692695617676 1.0882699489593506 Returns output path, elapsed formatting time and elapsed rendering time. ''' from abjad.tools import lilypondfiletools from abjad.tools import systemtools assert hasattr(self._client, '__illustrate__') illustration = self._client.__illustrate__(**keywords) assert hasattr(illustration, 'score_block') block = lilypondfiletools.Block(name='midi') illustration.score_block.items.append(block) if midi_file_path is not None: midi_file_path = os.path.expanduser(midi_file_path) without_extension = os.path.splitext(midi_file_path)[0] ly_file_path = '{}.ly'.format(without_extension) else: ly_file_path = None result = type(self)(illustration).as_ly(ly_file_path, **keywords) ly_file_path, abjad_formatting_time = result timer = systemtools.Timer() with timer: systemtools.IOManager.run_lilypond(ly_file_path) lilypond_rendering_time = timer.elapsed_time if os.name == 'nt': extension = 'mid' else: extension = 'midi' midi_file_path = '{}.{}'.format( os.path.splitext(ly_file_path)[0], extension, ) if remove_ly: os.remove(ly_file_path) return midi_file_path, abjad_formatting_time, lilypond_rendering_time
def as_ly(self, ly_file_path=None, illustrate_function=None, **kwargs): r'''Persists client as LilyPond file. Autogenerates file path when `ly_file_path` is none. :: >>> staff = Staff("c'4 e'4 d'4 f'4") >>> for x in persist(staff).as_ly('~/example.ly'): # doctest: +SKIP ... x ... '/Users/josiah/Desktop/test.ly' 0.04491996765136719 Returns output path and elapsed formatting time when LilyPond output is written. ''' from abjad import abjad_configuration from abjad.tools import systemtools if illustrate_function is None: assert hasattr(self._client, '__illustrate__') illustrate_function = self._client.__illustrate__ illustration = illustrate_function(**kwargs) if ly_file_path is None: ly_file_name = systemtools.IOManager.get_next_output_file_name() ly_file_path = os.path.join( abjad_configuration.abjad_output_directory, ly_file_name, ) else: ly_file_path = os.path.expanduser(ly_file_path) assert ly_file_path.endswith('.ly'), ly_file_path timer = systemtools.Timer() with timer: lilypond_format = format(illustration, 'lilypond') abjad_formatting_time = timer.elapsed_time directory = os.path.dirname(ly_file_path) systemtools.IOManager._ensure_directory_existence(directory) if not os.path.exists(ly_file_path): with open(ly_file_path, 'w') as file_pointer: file_pointer.write(lilypond_format) return ly_file_path, abjad_formatting_time
def bootstrap_pass_two_single( cls, entity_type, entity_id, annotation='', corpus=None, progress=None, ): skipped_template = u'{} (Pass 2) {:.3%} [{}]\t[SKIPPED] (id:{}) [{:.8f}]: {}' changed_template = u'{} (Pass 2) {:.3%} [{}]\t (id:{}) [{:.8f}]: {}' query = cls.select().where( cls.entity_id == entity_id, cls.entity_type == entity_type, ) if not query.count(): return document = query.get() corpus = corpus or {} with systemtools.Timer(verbose=False) as timer: changed = document.resolve_references(corpus) if not changed: message = skipped_template.format( cls.__name__.upper(), progress, annotation, (document.entity_type, document.entity_id), timer.elapsed_time, document.name, ) print(message) return document.save() message = changed_template.format( cls.__name__.upper(), progress, annotation, (document.entity_type, document.entity_id), timer.elapsed_time, document.name, ) print(message)
def get_network(entity_id, entity_type, on_mobile=False, cache=True, roles=None): import discograph assert entity_type in ('artist', 'label') template = 'discograph:/api/{entity_type}/network/{entity_id}' cache_key = discograph.RelationGrapher.make_cache_key( template, entity_type, entity_id, roles=roles, ) if on_mobile: template = '{}/mobile'.format(template) cache_key = cache_key.format(entity_type, entity_id) cache = False if cache: data = discograph.RelationGrapher.cache_get(cache_key) if data is not None: return data entity_type = entity_name_types[entity_type] entity = get_entity(entity_type, entity_id) if entity is None: return None if not on_mobile: max_nodes = 75 degree = 12 else: max_nodes = 25 degree = 6 relation_grapher = discograph.RelationGrapher( center_entity=entity, degree=degree, max_nodes=max_nodes, roles=roles, ) with systemtools.Timer(exit_message='Network query time:'): with discograph.PostgresModel._meta.database.execution_context(): data = relation_grapher() if cache: discograph.RelationGrapher.cache_set(cache_key, data) return data
def bootstrap_pass_one( cls, model_class, xml_tag, id_attr='id', name_attr='name', skip_without=None, ): # Pass one. template = u'{} (Pass 1) (idx:{}) (id:{}) [{:.8f}]: {}' xml_path = Bootstrapper.get_xml_path(xml_tag) print(xml_path) with gzip.GzipFile(xml_path, 'r') as file_pointer: iterator = Bootstrapper.iterparse(file_pointer, xml_tag) for i, element in enumerate(iterator): data = None try: with systemtools.Timer(verbose=False) as timer: data = model_class.tags_to_fields(element) if skip_without: if any(not data.get(_) for _ in skip_without): continue if element.get('id'): data['id'] = element.get('id') data['random'] = random.random() document = model_class.create(**data) message = template.format( model_class.__name__.upper(), i, getattr(document, id_attr), timer.elapsed_time, getattr(document, name_attr), ) print(message) except peewee.DataError as e: pprint.pprint(data) traceback.print_exc() raise(e)
def as_png( self, png_file_path=None, remove_ly=False, illustrate_function=None, **keywords ): r'''Persists client as PNG. .. container:: example >>> staff = abjad.Staff() >>> measure = abjad.Measure((4, 4), "c'4 d'4 e'4 f'4") >>> command = abjad.LilyPondLiteral(r'\break', 'after') >>> abjad.attach(command, measure[-1]) >>> staff.extend(measure * 200) >>> result = persist(staff).as_png() # doctest: +SKIP >>> for x in result[0]: # doctest: +SKIP ... x ... '/Users/josiah/Desktop/test-page1.png' '/Users/josiah/Desktop/test-page2.png' '/Users/josiah/Desktop/test-page3.png' '/Users/josiah/Desktop/test-page4.png' '/Users/josiah/Desktop/test-page5.png' '/Users/josiah/Desktop/test-page6.png' '/Users/josiah/Desktop/test-page7.png' '/Users/josiah/Desktop/test-page8.png' '/Users/josiah/Desktop/test-page9.png' '/Users/josiah/Desktop/test-page10.png' '/Users/josiah/Desktop/test-page11.png' '/Users/josiah/Desktop/test-page12.png' '/Users/josiah/Desktop/test-page13.png' '/Users/josiah/Desktop/test-page14.png' '/Users/josiah/Desktop/test-page15.png' Autogenerates file path when `png_file_path` is none. Returns output path(s), elapsed formatting time and elapsed rendering time. ''' from abjad.tools import systemtools if illustrate_function is None: assert hasattr(self._client, '__illustrate__') if png_file_path is not None: png_file_path = os.path.expanduser(png_file_path) without_extension = os.path.splitext(png_file_path)[0] ly_file_path = '{}.ly'.format(without_extension) else: ly_file_path = None result = self.as_ly( ly_file_path, illustrate_function=illustrate_function, **keywords ) ly_file_path, abjad_formatting_time = result original_directory = os.path.split(ly_file_path)[0] original_ly_file_path = ly_file_path temporary_directory = tempfile.mkdtemp() temporary_ly_file_path = os.path.join( temporary_directory, os.path.split(ly_file_path)[1], ) shutil.copy(original_ly_file_path, temporary_ly_file_path) timer = systemtools.Timer() with timer: success = systemtools.IOManager.run_lilypond( temporary_ly_file_path, flags='--png', ) lilypond_rendering_time = timer.elapsed_time png_file_paths = [] for file_name in os.listdir(temporary_directory): if not file_name.endswith('.png'): continue source_png_file_path = os.path.join( temporary_directory, file_name, ) target_png_file_path = os.path.join( original_directory, file_name, ) shutil.move(source_png_file_path, target_png_file_path) png_file_paths.append(target_png_file_path) shutil.rmtree(temporary_directory) if remove_ly: os.remove(ly_file_path) if 1 < len(png_file_paths): png_file_paths.sort( key=lambda x: int(self._png_page_pattern.match(x).groups()[0]), ) return ( tuple(png_file_paths), abjad_formatting_time, lilypond_rendering_time, success, )
def as_ly(self, ly_file_path=None, candidacy=False, illustrate_function=None, **kwargs): r'''Persists client as LilyPond file. Autogenerates file path when `ly_file_path` is none. :: >>> staff = Staff("c'4 e'4 d'4 f'4") >>> for x in persist(staff).as_ly('~/example.ly'): # doctest: +SKIP ... x ... '/Users/josiah/Desktop/test.ly' 0.04491996765136719 When `candidacy` is true, writes LilyPond output only when LilyPond output would differ from any existing output file. When `candidacy` is false, writes LilyPond output even if LilyPond output would not differ from any existing output file. Returns output path and elapsed formatting time when LilyPond output is written. Returns false when when LilyPond output is not written due to `candidacy` being set. ''' from abjad import abjad_configuration from abjad.tools import systemtools if illustrate_function is None: assert '__illustrate__' in dir(self._client) illustrate_function = self._client.__illustrate__ illustration = illustrate_function(**kwargs) if ly_file_path is None: ly_file_name = systemtools.IOManager.get_next_output_file_name() ly_file_path = os.path.join( abjad_configuration.abjad_output_directory, ly_file_name, ) else: ly_file_path = os.path.expanduser(ly_file_path) assert ly_file_path.endswith('.ly'), ly_file_path timer = systemtools.Timer() with timer: lilypond_format = format(illustration, 'lilypond') abjad_formatting_time = timer.elapsed_time directory = os.path.dirname(ly_file_path) systemtools.IOManager._ensure_directory_existence(directory) if not os.path.exists(ly_file_path) or not candidacy: with open(ly_file_path, 'w') as file_pointer: file_pointer.write(lilypond_format) return ly_file_path, abjad_formatting_time base, extension = os.path.splitext(ly_file_path) candidate_path = base + '.candidate' + extension with systemtools.FilesystemState(remove=[candidate_path]): with open(candidate_path, 'w') as file_pointer: file_pointer.write(lilypond_format) if systemtools.TestManager.compare_files( ly_file_path, candidate_path, ): return False else: shutil.move(candidate_path, ly_file_path) return ly_file_path, abjad_formatting_time
def as_pdf(self, pdf_file_path=None, candidacy=False, illustrate_function=None, remove_ly=False, **kwargs): r'''Persists client as PDF. Autogenerates file path when `pdf_file_path` is none. :: >>> staff = Staff("c'4 e'4 d'4 f'4") >>> for x in persist(staff).as_pdf(): # doctest: +SKIP ... x ... '/Users/josiah/.abjad/output/1416.pdf' 0.047142982482910156 0.7839350700378418 When `candidacy` is true, writes PDF output only when PDF output would differ from any existing output file. When `candidacy` is false, writes PDF output even if PDF output would not differ from any existing output file. Returns output path, elapsed formatting time and elapsed rendering time when PDF output is written. Returns false when when PDF output is not written due to `candidacy` being set. ''' from abjad.tools import systemtools if illustrate_function is None: assert '__illustrate__' in dir(self._client) if pdf_file_path is not None: pdf_file_path = os.path.expanduser(pdf_file_path) without_extension = os.path.splitext(pdf_file_path)[0] ly_file_path = '{}.ly'.format(without_extension) else: ly_file_path = None result = self.as_ly(ly_file_path, candidacy=candidacy, illustrate_function=illustrate_function, **kwargs) if candidacy and result == False: return False ly_file_path, abjad_formatting_time = result without_extension = os.path.splitext(ly_file_path)[0] pdf_file_path = '{}.pdf'.format(without_extension) timer = systemtools.Timer() with timer: success = systemtools.IOManager.run_lilypond( ly_file_path, candidacy=candidacy, ) lilypond_rendering_time = timer.elapsed_time if remove_ly: os.remove(ly_file_path) if candidacy and not success: return False else: return ( pdf_file_path, abjad_formatting_time, lilypond_rendering_time, success, )