def __init__(self, waitables, paths, translationPaths = None): self.on_file_reloaded = signals.Signal() self.on_file_reload_failed = signals.Signal() self.runningCheckAt = 0 self.startedAt = int(time.time()) self.processed = {} self.waitables = waitables self.translationPaths = translationPaths or [] if isinstance(paths, basestring): paths = [paths] self.handles = {} paths = map(os.path.abspath, paths) commonprefix = os.path.commonprefix(paths) if commonprefix: paths = [commonprefix] for path in paths: if not os.path.exists(path): log.warn("SpyFolder: Can't spy on non-existing folder %s" % path) continue handle = win32api.FindFirstChangeNotification(path, True, win32api.FILE_NOTIFY_CHANGE_LAST_WRITE) if handle == win32api.INVALID_HANDLE_VALUE: log.warn('SpyFolder: got invalid handle for %s' % path) continue waitables.InsertHandle(handle, self) self.handles[handle] = path log.info('AutoCompiler: Now spying on %s using handle %s.', path, handle)
class JobManager(plugin.DocumentPlugin): started = signals.Signal() # Job finished = signals.Signal() # Job, success def __init__(self, document): self._job = None def start_job(self, job): """Starts a Job on our behalf.""" if not self.is_running(): self._job = job job.done.connect(self._finished) job.start() self.started(job) app.jobStarted(self.document(), job) def _finished(self, success): self.finished(job, success) app.jobFinished(self.document(), self._job, success) def job(self): """Returns the last job if any.""" return self._job def is_running(self): """Returns True when a job is running.""" if self._job: return self._job.is_running() and not self._job.is_aborted()
def __new__(cls, *args, **kwargs): obj = industry.Base.__new__(cls) obj._typeID = None obj._level = None obj._errors = [] obj.on_updated = signals.Signal() obj.on_errors = signals.Signal() return obj
def __init__(self, typeID, itemID, stacksize): self.typeID = typeID self.itemID = itemID self.stacksize = stacksize self.isOpening = False self._loot = None self.onOpen = signals.Signal() self.onFinish = signals.Signal()
def __init__(self, typeID, adapter=None): self._adapter = adapter or SkinPanelAdapter() self._lock = locks.Lock() self.Reset(typeID) self.onChange = signals.Signal() self.onSkinsChange = signals.Signal() self.onSkinsChange.connect(self.onChange) self._adapter.RegisterNotify(self)
def __init__(self, flagID, parentController): self.flagID = flagID self.moduleItemID = None self.chargeItemID = None self.parentController = parentController self.ghostFittingExtension = ShipFittingSlotControllerGhostFittingExtension( self) self.on_online_state_change = signals.Signal() self.on_item_fitted = signals.Signal()
def __init__(self, itemID): super(SkillExtractorControllerBase, self).__init__() self.itemID = itemID self.isCompleted = False self._skills = None self._skillsByID = None self._extracted = defaultdict(list) self.onUpdate = signals.Signal() self.onSkillListUpdate = signals.Signal() self.onSkillListUpdate.connect(self.onUpdate)
def __new__(cls, blueprint, activityID, *args, **kwargs): obj = industry.Base.__new__(cls) obj._blueprint = blueprint obj._activityID = activityID if obj.activity is None: raise RuntimeError('Invalid Activity for Blueprint') obj.jobID = None obj.characterID = None obj.corporationID = None obj.status = industry.STATUS_UNSUBMITTED obj.installerID = None obj.startDate = None obj.endDate = None obj._materials = copy.deepcopy(obj.activity.materials) obj._modifiers = [] obj._skills = {} obj._slots = {} obj._distance = None obj._materialEfficiency = None obj._timeEfficiency = None obj._random = None obj.request = {} obj.prices = {} obj._modifiers_cache = None obj.on_validate = signals.Signal() obj.on_updated = signals.Signal() obj.on_errors = signals.Signal() obj.on_delete = signals.Signal() obj.on_facility = signals.Signal() obj.on_input_location = signals.Signal() obj.on_output_location = signals.Signal() obj.on_dirty = signals.Signal() obj.on_dirty.connect(obj.update) return obj
def __new__(cls, *args, **kwargs): obj = industry.Base.__new__(cls) obj._typeID = None obj._quantity = 0 obj._available = 0 obj._original = 0 obj._errors = [] obj._options = [] obj._modifiers = [] obj._probability = 1 obj.on_updated = signals.Signal() obj.on_errors = signals.Signal() obj.on_select = signals.Signal() return obj
class EditorDocument(AbstractDocument): """A Frescobaldi document for use in the main editor view. Basically this is an AbstractDocument with signals added.""" urlChanged = signals.Signal() # new url, old url closed = signals.Signal() loaded = signals.Signal() saving = signals.SignalContext() saved = signals.Signal() @classmethod def new_from_url(cls, url, encoding=None): d = super(EditorDocument, cls).new_from_url(url, encoding) if not url.isEmpty(): d.loaded() app.documentLoaded(d) return d def __init__(self, url=None, encoding=None): super(EditorDocument, self).__init__(url, encoding) self.modificationChanged.connect(self.slotModificationChanged) app.documents.append(self) app.documentCreated(self) def slotModificationChanged(self): app.documentModificationChanged(self) def close(self): self.closed() app.documentClosed(self) app.documents.remove(self) def load(self, url=None, encoding=None, keepUndo=False): super(EditorDocument, self).load(url, encoding, keepUndo) self.loaded() app.documentLoaded(self) def save(self, url=None, encoding=None): url, filename = super().save(url, encoding) with self.saving(), app.documentSaving(self): self._save(url, filename) self.saved() app.documentSaved(self) def setUrl(self, url): old = super(EditorDocument, self).setUrl(url) if url != old: self.urlChanged(url, old) app.documentUrlChanged(self, url, old)
def __init__(self, groupID, filterSettings, **kw): label = kw.pop('label', evetypes.GetGroupNameByGroup(groupID)) super(SkillGroupData, self).__init__(label=label, **kw) self.filterSettings = filterSettings self.onChildUpdated = signals.Signal() for child in self.GetChildren(): child.skill.onUpdate.connect(self.onChildUpdated)
class DocumentIconProvider(plugin.DocumentPlugin): """Provides an icon for a Document.""" iconChanged = signals.Signal() def __init__(self, doc): doc.modificationChanged.connect(self._send_icon) mgr = jobmanager.manager(doc) mgr.started.connect(self._send_icon) mgr.finished.connect(self._send_icon) def _send_icon(self): self.iconChanged() def icon(self, mainwindow=None): doc = self.document() job = jobmanager.job(doc) if job and job.is_running() and not jobattributes.get(job).hidden: icon = 'lilypond-run' elif mainwindow and doc is engrave.Engraver.instance(mainwindow).stickyDocument(): icon = 'pushpin' elif doc.isModified(): icon = 'document-save' elif job and not job.is_running() and not job.is_aborted() and job.success: icon = 'document-compile-success' elif job and not job.is_running() and not job.is_aborted(): icon = 'document-compile-failed' else: icon = 'text-plain' return icons.get(icon)
class VariableManager(plugin.DocumentPlugin): """Caches variables in the document and monitors for changes. The changed() Signal is emitted some time after the list of variables has been changed. It is recommended to not change the document itself in response to this signal. """ changed = signals.Signal() # without argument def __init__(self, doc): self._updateTimer = QTimer(singleShot=True, timeout=self.slotTimeout) self._variables = self.readVariables() if doc.__class__ == document.EditorDocument: doc.contentsChange.connect(self.slotContentsChange) doc.closed.connect(self._updateTimer.stop) # just to be sure def slotTimeout(self): variables = self.readVariables() if variables != self._variables: self._variables = variables self.changed() def slotContentsChange(self, position, removed, added): """Called if the document changes.""" if (self.document().findBlock(position).blockNumber() < _LINES or self.document().findBlock(position + added).blockNumber() > self.document().blockCount() - _LINES): self._updateTimer.start(500) def variables(self): """Returns the document variables (cached) as a dictionary. This method is recommended.""" if self._updateTimer.isActive(): # an update is pending, force it self._updateTimer.stop() self.slotTimeout() return self._variables def readVariables(self): """Reads the variables from the document and returns a dictionary. Internal.""" count = self.document().blockCount() blocks = [self.document().firstBlock()] if count > _LINES * 2: blocks.append(self.document().findBlockByNumber(count - _LINES)) count = _LINES def lines(block): for i in range(count): yield block.text() block = block.next() variables = {} for block in blocks: variables.update(m.group(1, 2) for n, m in positions(lines(block))) return variables
def __init__(self, parent_statement=None, event_loop=eventLoop()): """Initialize the ThreadExecutor object @param parent_statement: prievous statement defining the execution environment for the first statement """ import signals self.sig_statement_executing = signals.Signal() self.sig_statement_complete = signals.Signal() self.sig_complete = signals.Signal() self.parent_statement = parent_statement self.statements = [] self.lock = thread.allocate_lock() self.event_loop = event_loop self.last_complete = -1 self.last_signalled = -1 self.complete = False self.interrupted = False
def __init__(self, typeID, points): super(SkillBase, self).__init__() self._typeID = typeID self._points = points self._unmodifiedPoints = points self._isRequired = False self._isQueued = False self._isRestricted = False self._rank = None self._level = None self._dirty = False self.onUpdate = signals.Signal()
class Dialog(widgets.dialog.Dialog): """Dialog to run arbitrary external job.Job commands and show the log.""" job_done = signals.Signal() def __init__(self, parent, auto_accept=False): super(Dialog, self).__init__(parent, buttons=( 'cancel', 'ok', )) self.setWindowModality(Qt.WindowModal) self.setIconSize(32) self.auto_accept = auto_accept self.log = log.Log(self) self.job = None self.setMainWidget(self.log) qutil.saveDialogSize(self, "job-dialog/dialog/size", QSize(480, 800)) def abort_job(self): self.job.abort() self.reject() def run(self, j, msg=_("Run external command")): """Run the given job.""" self.job = j self.job.done.connect(self.slot_job_done) self.log.connectJob(j) self.setWindowTitle(j.title()) self.setMessage(msg) self.button('ok').setEnabled(False) self.button('cancel').clicked.connect(self.abort_job) j.start() self.exec() def slot_job_done(self): if self.job.success: if self.auto_accept: self.accept() self.button('ok').setEnabled(True) else: self.setMessage(_("Job failed! Please inspect log")) self.setIcon('critical') self.button('cancel').clicked.connect(self.reject) self.job_done.emit()
def __init__(self): self.state = structures.STATE_UNKNOWN self.damage = (None, None, None) self.vulnerable = None self.timerStart = None self.timerEnd = None self.timerPaused = None self.firstAggressed = None self.lastAggressed = None self.repairing = None self.unanchoring = None self.schedule = structures.Schedule( required=self.GetRequiredHours(), timeZoneOffset=8 if boot.region == 'optic' else 0) self.OnStateChanged = signals.Signal() self.OnDamageChanged = signals.Signal() self.OnVulnerabilityChanged = signals.Signal() self.OnScheduleChanged = signals.Signal() self.OnTimerChanged = signals.Signal() self.OnRepairingChanged = signals.Signal() self.OnAggressionChanged = signals.Signal() self.OnUnanchoringChanged = signals.Signal() self.OnFirstAggressed = signals.Signal() self.schedule.OnChange.connect(self.HandleScheduleChanged)
def AttachComponent(cls): if not hasattr(cls, '_attached_ui_components'): setattr(cls, '_attached_ui_components', []) cls._attached_ui_components.append(component) for methodName in component.__observed_methods__: method = getattr(cls, methodName, None) if method is None: continue signal = getattr(method, '_component_method_observers', None) if signal is None: signal = signals.Signal() def GetWrapper(_method, _signal): @functools.wraps(method) def Wrapper(*args, **kwargs): try: result = _method(*args, **kwargs) except: logger.exception( 'Method %s raised an exception before signal could emit', methodName) raise try: _signal(*args, **kwargs) except Exception: logger.exception( 'Failed while notifying observers for method %s', methodName) return result return Wrapper wrapper = GetWrapper(method, signal) setattr(wrapper, '_component_method_observers', signal) setattr(cls, methodName, wrapper) signal.connect(getattr(component, methodName)) return cls
def __init__(self, fightersSvc): self.fightersSvc = fightersSvc sm.RegisterNotify(self) self.fightersInLaunchTubes = {} self.fightersInSpaceByID = {} self.fightersInSpaceByTube = {} self.statusByTube = {} self.activeAbilities = {} self.abilityChargeCounts = {} self.abilityCooldowns = {} self.incomingEwarByFighterID = defaultdict(dict) self.abilityTargetTracker = AbilityTargetTracker() if session.shipid: self._UpdateStateForShip() self.signalOnFighterTubeStateUpdate = signals.Signal() self.signalOnFighterTubeContentUpdate = signals.Signal() self.signalOnFighterInSpaceUpdate = signals.Signal() self.signalOnAbilityActivationStatusUpdate = signals.Signal() self.signalOnIncomingEwarStarted = signals.Signal() self.signalOnIncomingEwarEnded = signals.Signal()
class Job(object): """Manages a process. Set the command attribute to a list of strings describing the program and its arguments. Set the directory attribute to a working directory. The environment attribute is a dictionary; if you set an item it will be added to the environment for the process (the rest will be inherited from the system); if you set an item to None, it will be unset. Call start() to start the process. The output() signal emits output (stderr or stdout) from the process. The done() signal is always emitted when the process has ended. The history() method returns all status messages and output so far. When the process has finished, the error and success attributes are set. The success attribute is set to True When the process exited normally and successful. When the process did not exit normally and successfully, the error attribute is set to the QProcess.ProcessError value that occurred last. Before start(), error and success both are None. The status messages and output all are in one of five categories: STDERR, STDOUT (output from the process) or NEUTRAL, FAILURE or SUCCESS (status messages). When displaying these messages in a log, it is advised to take special care for newlines, esp when a status message is displayed. Status messages normally have no newlines, so you must add them if needed, while output coming from the process may continue in the same line. Jobs that run LilyPond will use objects of job.lilypond.Job or derived special classes. """ output = signals.Signal() done = signals.Signal() started = signals.Signal() title_changed = signals.Signal() # title (string) def __init__(self, command=[], args=None, directory="", environment={}, title="", input="", output="", priority=1, runner=None, decode_errors='strict', encoding='latin1'): self.command = command if type(command) == list else [command] self._input = input self._output = output self._runner = runner self._arguments = args if args else [] self._directory = directory self.environment = environment self._encoding = encoding self.success = None self.error = None self._title = "" self._priority = priority self._aborted = False self._process = None self._history = [] self._starttime = 0.0 self._elapsed = 0.0 self.decoder_stdout = self.create_decoder(STDOUT) self.decoder_stderr = self.create_decoder(STDERR) self.decode_errors = decode_errors # codecs error handling def add_argument(self, arg): """Append an additional command line argument if it is not present already.""" if not arg in self._arguments: self._arguments.append(arg) def arguments(self): """Additional (custom) arguments, will be inserted between the -d options and the include paths. May for example stem from the manual part of the Engrave Custom dialog.""" return self._arguments def create_decoder(self, channel): """Return a decoder for the given channel (STDOUT/STDERR). This method is called from the constructor. You can re-implement this method to return another decoder, or you can set the decoders manually by setting the `decoder_stdout` and `decoder_stderr` manually after construction. This decoder is then used to decode the 8bit bytestrings into Python unicode strings. The default implementation returns a 'latin1' decoder for both channels. """ return codecs.getdecoder(self._encoding) def directory(self): return self._directory def set_directory(self, directory): self._directory = directory def filename(self): """File name of the job's input document. May be overridden for 'empty' jobs.""" return self._input def set_input(self, filename): self._input = filename def set_input_file(self): """configure the command to add an input file if one is specified.""" filename = self.filename() if filename: self.command.append(filename) def output_argument(self): return self._output def output_file(self): return self._output_file def runner(self): """Return the Runner object if the job is run within a JobQueue, or None if not.""" return self._runner def set_runner(self, runner): """Store a reference to a Runner if the job is run within a JobQueue.""" self._runner = runner def title(self): """Return the job title, as set with set_title(). The title defaults to an empty string. """ return self._title def set_title(self, title): """Set the title. If the title changed, the title_changed(title) signal is emitted. """ old, self._title = self._title, title if title != old: self.title_changed(title) def priority(self): return self._priority def set_priority(self, value): self._priority = value def start(self): """Starts the process.""" self.configure_command() self.success = None self.error = None self._aborted = False self._history = [] self._elapsed = 0.0 self._starttime = time.time() if self._process is None: self.set_process(QProcess()) self._process.started.connect(self.started) self.start_message() if os.path.isdir(self._directory): self._process.setWorkingDirectory(self._directory) if self.environment: self._update_process_environment() self._process.start(self.command[0], self.command[1:]) def configure_command(self): """Process the command if necessary. In a LilyPondJob this is the essential part of composing the command line from the job options. This implementation simply creates a list from the main command, any present arguments, the input and the output (if present). """ self.command.extend(self._arguments) if self._input: if type(self._input) == list: self.command.extend(self._input) else: self.command.append(self._input) if self._output: if type(self._output) == list: self.command.extend(self._output) else: self.command.append(self._output) def start_time(self): """Return the time this job was started. Returns 0.0 when the job has not been started yet. """ return self._starttime def elapsed_time(self): """Return how many seconds this process has been running.""" if self._elapsed: return self._elapsed elif self._starttime: return time.time() - self._starttime return 0.0 def abort(self): """Abort the process.""" if self._process: self._aborted = True self.abort_message() if os.name == "nt": self._process.kill() else: self._process.terminate() def is_aborted(self): """Returns True if the job was aborted by calling abort().""" return self._aborted def is_running(self): """Returns True if this job is running.""" return bool(self._process) def failed_to_start(self): """Return True if the process failed to start. (Call this method after the process has finished.) """ return self.error == QProcess.FailedToStart def set_process(self, process): """Sets a QProcess instance and connects the signals.""" self._process = process if process.parent() is None: process.setParent(QCoreApplication.instance()) process.finished.connect(self._finished) process.error.connect(self._error) process.readyReadStandardError.connect(self._readstderr) process.readyReadStandardOutput.connect(self._readstdout) def _update_process_environment(self): """(internal) initializes the environment for the process.""" se = QProcessEnvironment.systemEnvironment() for k, v in self.environment.items(): se.remove(k) if v is None else se.insert(k, v) self._process.setProcessEnvironment(se) def message(self, text, type=NEUTRAL): """Output some text as the given type (NEUTRAL, SUCCESS, FAILURE, STDOUT or STDERR).""" self.output(text, type) self._history.append((text, type)) def history(self, types=ALL): """Yield the output messages as two-tuples (text, type) since the process started. If types is given, it should be an OR-ed combination of the status types STDERR, STDOUT, NEUTRAL, SUCCESS or FAILURE. """ for msg, type in self._history: if type & types: yield msg, type def stdout(self): """Return the standard output of the process as unicode text.""" return "".join([line[0] for line in self.history(STDOUT)]) def stderr(self): """Return the standard error of the process as unicode text.""" return "".join([line[0] for line in self.history(STDERR)]) def _finished(self, exitCode, exitStatus): """(internal) Called when the process has finished.""" self.finish_message(exitCode, exitStatus) success = exitCode == 0 and exitStatus == QProcess.NormalExit self._bye(success) def _error(self, error): """(internal) Called when an error occurs.""" self.error_message(error) if self._process.state() == QProcess.NotRunning: self._bye(False) def _bye(self, success): """(internal) Ends and emits the done() signal.""" self._elapsed = time.time() - self._starttime if not success: self.error = self._process.error() self.success = success self._process.deleteLater() self._process = None self.done(success) def _readstderr(self): """(internal) Called when STDERR can be read.""" output = self._process.readAllStandardError() self.message( self.decoder_stderr(output, self.decode_errors)[0], STDERR) def _readstdout(self): """(internal) Called when STDOUT can be read.""" output = self._process.readAllStandardOutput() self.message( self.decoder_stdout(output, self.decode_errors)[0], STDOUT) def start_message(self): """Called by start(). Outputs a message that the process has started. """ name = self.title() or os.path.basename(self.command[0]) self.message(_("Starting {job}...").format(job=name), NEUTRAL) def abort_message(self): """Called by abort(). Outputs a message that the process has been aborted. """ name = self.title() or os.path.basename(self.command[0]) self.message(_("Aborting {job}...").format(job=name), NEUTRAL) def error_message(self, error): """Called when there is an error (by _error()). Outputs a message describing the given QProcess.Error. """ if error == QProcess.FailedToStart: self.message( _("Could not start {program}.\n" "Please check path and permissions.").format( program=self.command[0]), FAILURE) elif error == QProcess.ReadError: self.message(_("Could not read from the process."), FAILURE) elif self._process.state() == QProcess.NotRunning: self.message(_("An unknown error occurred."), FAILURE) def finish_message(self, exitCode, exitStatus): """Called when the process finishes (by _finished()). Outputs a message on completion of the process. """ if exitCode: self.message( _("Exited with return code {code}.").format(code=exitCode), FAILURE) elif exitStatus: self.message( _("Exited with exit status {status}.").format( status=exitStatus), FAILURE) else: time = self.elapsed2str(self.elapsed_time()) self.message( _("Completed successfully in {time}.").format(time=time), SUCCESS) @staticmethod def elapsed2str(seconds): """Return a short display for the given time period (in seconds).""" minutes, seconds = divmod(seconds, 60) if minutes: return "{0:.0f}'{1:.0f}\"".format(minutes, seconds) return '{0:.1f}"'.format(seconds)
""" Manages the history of the open documents of a MainWindow. Contains smart logic to switch documents if the active document is closed. If no documents remain, listen to other HistoryManager instances and make the document set-current there also current here. """ import weakref import app import signals # This signal is emitted whenever a MainWindow sets a document current (active) # Any HistoryManager can listen to it and follow it if no document is current _setCurrentDocument = signals.Signal() # Document class HistoryManager(object): """Keeps the history of document switches by the user. If a document is closed, the previously active document is set active. If no documents remain, nothing is done. """ def __init__(self, mainwindow, othermanager=None): self.mainwindow = weakref.ref(mainwindow) self._documents = list(othermanager._documents if othermanager else app.documents) self._has_current = bool(self._documents) mainwindow.currentDocumentChanged.connect(self.setCurrentDocument) app.documentCreated.connect(self.addDocument, 1)
try: import popplerqt5 except ImportError: popplerqt5 = None import app import plugin import resultfiles import signals import popplertools _cache = weakref.WeakValueDictionary() # This signal gets emitted when a finished Job has created new PDF document(s). documentUpdated = signals.Signal() # Document @app.jobFinished.connect def _on_job_finished(document, job): if group(document).update(): documentUpdated(document, job) def group(document): """Returns a DocumentGroup instance for the given text document.""" return DocumentGroup.instance(document) def load(filename): """Returns a Poppler.Document for the given filename, caching it (weakly).
different ways (via a Python extension module or by embedding the PortMIDI C library directly). The available() method returns True if portmidi is available, False if not. Inside Frescobaldi, interact with this module to get input and outputs etcetera, not with portmidi directly. """ import portmidi import signals portmidi.init() aboutToRestart = signals.Signal() # emitted before re-init PortMIDI settingsChanged = signals.Signal() # emitted when ports are changed, etc def available(): """Returns True if portmidi is available, False if not.""" return portmidi.available() def restart(): """Restarts PortMIDI.""" aboutToRestart() portmidi.quit() portmidi.init() settingsChanged()
""" from __future__ import unicode_literals import contextlib import os from PyQt4.QtCore import QFileSystemWatcher, QUrl import app import plugin import signals __all__ = ['documentChangedOnDisk', 'DocumentWatcher', 'start', 'stop'] documentChangedOnDisk = signals.Signal() # Document # one global QFileSystemWatcher instance watcher = None class DocumentWatcher(plugin.DocumentPlugin): """Maintains if a change was detected for a document.""" def __init__(self, d): self.changed = False def isdeleted(self): """Return True if some change has occurred, the document has a local filename, but the file is not existing on disk. """
def __init__(self, offer, store): self.offer = offer self.store = store self.account = store.GetAccount() self.onAurBalanceChanged = signals.Signal() self.account.SubscribeToAurumBalanceChanged(self.onAurBalanceChanged)
class TextFonts(QObject): """Provide information about available text fonts. These are exactly the fonts that can be seen by LilyPond. This is only produced upon request but then stored permanently during the program's runtime. load_fonts() will run LilyPond to determine the list of fonts, optionally reporting to a log.Log widget if given. Since this is an asynchronous process GUI elements that want to use the results have to connect to the 'loaded' signal which is emitted after LilyPond has completed and the results been parsed. A Fonts() object is immediately available as fonts.available_fonts, and its is_loaded member can be requested to test if fonts have already been loaded. """ loaded = signals.Signal() def __init__(self, lilypond_info): super(TextFonts, self).__init__() self.lilypond_info = lilypond_info self._tree_model = FontTreeModel(self) self._misc_model = MiscTreeModel(self) self.job = None self.load_fonts() def reset(self, log_widget=None): self._log = [] self._tree_model.reset() self._misc_model.reset() # needs to be reset for the LilyPond-dependent fonts self.font_db = QFontDatabase() self._is_loaded = False def log(self): return self._log def acknowledge_lily_fonts(self): """Add the OpenType fonts in LilyPond's font directory to Qt's font database. This should be relevant (untested) when the fonts are not additionally installed as system fonts.""" # TODO: Move the the filtering here. # It's not correct to first add the notation fonts to the font debug # only to filter them again later. Besides, there might be other valid # fonts caught by the filter. font_dir = os.path.join(self.lilypond_info.datadir(), 'fonts', 'otf') for lily_font in os.listdir(font_dir): self.font_db.addApplicationFont(os.path.join(font_dir, lily_font)) def add_style_to_family(self, families, family_name, input): """Parse a font face definition provided by LilyPond. There is some guesswork involved since there may be discrepancies between the fonts/styles reported by LilyPond and those available in QFontDatabase. To discuss this the function is heavily commented. See also http://lists.gnu.org/archive/html/lilypond-user/2018-07/msg00338.html """ def un_camel(style): """ Try to 'fix' a class of errors when LilyPond reports e.g. 'BoldItalic' instead of 'Bold Italic'. It is unclear if this actually fixes the source of the issue or just one arbitrary example. """ # The following regular expression would be a 'proper' # un-camel-ing, but this seems not to be relevant. #un_cameled = re.sub('([^ ])([A-Z][a-z])', r'\1 \2', style) #return re.sub('([A-Za-z])([0-9])', r'\1 \2', un_cameled) return ("Bold Italic" if style == "BoldItalic" else style) if not family_name in families.keys(): families[family_name] = {} family = families[family_name] input = input.strip().split(':') # This is a safeguard against improper entries if len(input) == 2: # The first segment has always one or two entries: # - The font family name # - The font subfamily name if it differs from the base name. # Therefore the series is always the *last* entry in the list. # We "unescape" hyphens because this escape is not necessary # for our purposes. sub_family = input[0].split(',')[-1].replace('\\-', '-') if not sub_family in family.keys(): family[sub_family] = [] qt_styles = self.font_db.styles(sub_family) lily_styles = input[1][6:].split(',') match = '' if not qt_styles: # In some cases Qt does *not* report available styles. # In these cases it seems correct to use the style reported # by LilyPond. In very rare cases it seems possible that # LilyPond reports multiple styles for such fonts, and for now # we have to simply ignore these cases so we take the first # or single style. match = un_camel(lily_styles[0]) else: # Match LilyPond's reported styles with those reported by Qt. # We need to un-camel the LilyPond-reported style name, but # this may not be a final fix (see comment to un_camel()). # If no match is found we simply hope that the style # reported by LilyPond will do. for style in lily_styles: style = un_camel(style) if style in qt_styles: match = style break if not match: match = un_camel(lily_styles[0]) if not match in family[sub_family]: family[sub_family].append(match) else: pass # TODO: issue a warning? # In my examples *some* fonts were parsed improperly # and therefore skipped. # I *think* this happens at the stage of splitting the # LilyPond log into individual lines. #print("Error when parsing font entry:") #print(name) #print(input) def flatten_log(self): """Flatten job history into flat string list.""" for line in self.job.history(): # lines in Job.history() are tuples of text and type, # we're only interested in the text. lines = line[0].split('\n') for l in lines: self._log.append(l) def is_loaded(self): return self._is_loaded def load_fonts(self, log_widget=None): """Run LilyPond to retrieve a list of available fonts. Afterwards process_results() will parse the output and build info structures to be used later. If a log.Log widget is passed as second argument this will be connected to the Job to provide realtime feedback on the process. Any caller should connect to the 'loaded' signal because this is an asynchronous task that takes long to complete.""" self.reset() self.acknowledge_lily_fonts() self.run_lilypond(log_widget) def misc_model(self): return self._misc_model def model(self): return self._tree_model def parse_entries(self): """Parse the LilyPond log and push entries to the various lists and dictionaries. Parsing the actual font style definition is deferred to add_style_to_family().""" regexp = re.compile('(.*)\\-\d+') families = {} config_files = [] config_dirs = [] font_dirs = [] last_family = None for e in self._log: if e.startswith('family'): # NOTE: output of this process is always English, # so we can hardcode the splice indices original_family = e[7:] # filter size-indexed font families basename = regexp.match(original_family) last_family = basename.groups( )[0] if basename else original_family elif last_family: # We're in the second line of a style definition if not last_family.endswith('-brace'): self.add_style_to_family(families, last_family, e) last_family = None elif e.startswith('Config files:'): config_files.append(e[14:]) elif e.startswith('Font dir:'): font_dirs.append(e[10:]) elif e.startswith('Config dir:'): config_dirs.append(e[12:]) return families, config_files, config_dirs, font_dirs def process_results(self): """Parse the job history list to dictionaries.""" self.flatten_log() families, config_files, config_dirs, font_dirs = self.parse_entries() self._tree_model.populate(families) self._misc_model.populate(config_files, config_dirs, font_dirs) self._is_loaded = True self.job = None self.loaded.emit() def run_lilypond(self, log_widget=None): """Run lilypond from info with the args list, and a job title.""" # TODO: Use the global JobQueue info = self.lilypond_info j = self.job = job.Job([info.abscommand() or info.command] + ['-dshow-available-fonts']) j.set_title(_("Available Fonts")) j.done.connect(self.process_results) if log_widget: log_widget.connectJob(j) j.start()
import os from PyQt5.QtCore import QSettings, QUrl import app import util import signals import qsettings from . import documentation # cache the LilyPond Documentation instances _documentations = None allLoaded = signals.Signal() def docs(): """Returns the list of Documentation instances that are found.""" global _documentations if _documentations is None: _documentations = [documentation.Documentation(url) for url in urls()] _sort_docs() # check whether they need to fully load their version number yet _check_doc_versions() return list(_documentations) def clear(): """Clears the cached documentation instances."""
def __init__(self): sm.RegisterNotify(self) self.ball = None self.on_new_itemID = signals.Signal() self.wantedspeed = None
class State(object): signal = signals.Signal() def __init__(self): self.value = None self.running = False
def __init__(self, vgsCrestConnection): self.vgsCrestConnection = vgsCrestConnection self.aurumBalance = None self.transactionHref = None self.accountAurumBalanceChanged = signals.Signal() self.redeemingTokensUpdated = signals.Signal()