flexlibs library

FLExProject class

class flexlibs.FLExProject[source]

This class provides convenience methods for accessing a FieldWorks project by hiding some of the complexity of LCM. For functionality that isn’t provided here, LCM data and methods can be used directly via FLExProject.project, FLExProject.lp and FLExProject.lexDB; However, for long term use, new methods should be added to this class.

Usage:

from SIL.LCModel.Core.KernelInterfaces import ITsString, ITsStrBldr
from SIL.LCModel.Core.Text import TsStringUtils 

project = FLExProject()
try:
    project.OpenProject("my project",
                        writeEnabled = True/False)
except:
    #"Failed to open project"
    del project
    exit(1)

WSHandle = project.WSHandle('en')

# Traverse the whole lexicon
for lexEntry in project.LexiconAllEntries():
    headword = project.LexiconGetHeadword(lexEntry)

    # Use get_String() and set_String() with text fields:
    lexForm = lexEntry.LexemeFormOA                              
    lexEntryValue = ITsString(lexForm.Form.get_String(WSHandle)).Text
    newValue = convert_headword(lexEntryValue)
    mkstr = TsStringUtils.MakeString(newValue, WSHandle) 
    lexForm.Form.set_String(WSHandle, mkstr)
BestStr(stringObj)[source]

Generic string function for MultiUnicode and MultiString objects, returning the best Analysis or Vernacular string.

BuildGotoURL(objectOrGuid)[source]

Builds a URL that can be used with os.startfile() to jump to the object in Fieldworks. This method currently supports:

  • Lexical Entries, Senses and any object within the lexicon

  • Wordforms, Analyses and Wordform Glosses

  • Reversal Entries

  • Texts

CloseProject()[source]

Save any pending changes and dispose of the LCM object.

GetAllAnalysisWSs()[source]

Returns a set of language tags for all analysis writing systems used in this project.

GetAllSemanticDomains(flat=False)[source]

Returns a nested or flat list of all Semantic Domains defined in this project. The list is ordered.

Return items are ICmSemanticDomain objects.

GetAllVernacularWSs()[source]

Returns a set of language tags for all vernacular writing systems used in this project.

GetCustomFieldValue(senseOrEntryOrHvo, fieldID, languageTagOrHandle=None)[source]

Returns the field value for String, MultiString and Integer fields. Returns None for other field types. languageTagOrHandle only applies to MultiStrings; if None the best Analysis or Venacular string is returned.

Note: if the field is a vernacular WS field, then the languageTagOrHandle must be specified.

GetDateLastModified()[source]
GetDefaultAnalysisWS()[source]

Returns the Default Analysis Writing System: (Language-tag, Name)

GetDefaultVernacularWS()[source]

Returns the Default Vernacular Writing System: (Language-tag, Name)

GetLexicalRelationTypes()[source]

Returns an iterator over LexRefType objects, which define a type of lexical relation, such as Part-Whole.

Each LexRefType has:
  • MembersOC: containing zero or more LexReference objects.

  • MappingType: an enumeration defining the type of lexical relation.

LexReference objects have:
  • TargetsRS: the LexSense or LexEntry objects in the relation.

For example:
for lrt in project.GetLexicalRelationTypes():
if (lrt.MembersOC.Count > 0):
for lr in lrt.MembersOC:
for target in lr.TargetsRS:
if target.ClassName == “LexEntry”:

# LexEntry

else:

# LexSense

GetPartsOfSpeech()[source]

Returns a list of the Parts of Speech defined in this project.

GetPublications()[source]

Returns a list of the names of the publications defined in the project.

GetWritingSystems()[source]

Returns the Writing Systems that are active in this project as a list of tuples: (Name, Language-tag, Handle, IsVernacular). Use the Language-tag when specifying Writing System to other functions.

LexiconAddTagToField(senseOrEntryOrHvo, fieldID, tag)[source]

Appends the tag string to the end of the given field in the sense or entry inserting a semicolon between tags. If the tag is already in the field then it isn’t added.

LexiconAllEntries()[source]

Returns an iterator over all entries in the lexicon.

Each entry is of type:

SIL.LCModel.ILexEntry, which contains:
    - HomographNumber :: integer
    - HomographForm :: string
    - LexemeFormOA ::  SIL.LCModel.IMoForm
         - Form :: SIL.LCModel.MultiUnicodeAccessor
            - GetAlternative : Get String for given WS type
            - SetAlternative : Set string for given WS type
    - SensesOS :: Ordered collection of SIL.LCModel.ILexSense 
        - Gloss :: SIL.LCModel.MultiUnicodeAccessor
        - Definition :: SIL.LCModel.MultiStringAccessor
        - SenseNumber :: string
        - ExamplesOS :: Ordered collection of ILexExampleSentence
            - Example :: MultiStringAccessor
LexiconAllEntriesSorted()[source]

Returns an iterator over all entries in the lexicon sorted by the (lower-case) headword.

LexiconClearField(senseOrEntryOrHvo, fieldID)[source]

Clears the string field or all of the strings (writing systems) in a multi-string field. Can be used to clear out a custom field.

LexiconEntryAnalysesCount(entry)[source]

Returns a count of the occurrences of the entry in the text corpus.

NOTE: As of Fieldworks 8.0.10 this calculation can be slightly off (the same analysis in the same text segment is only counted once), but is the same as reported in Fieldworks in the Number of Analyses column. See LT-13997.

LexiconFieldIsAnyStringType(fieldID)[source]

Returns True if the given field is any of the string types.

LexiconFieldIsMultiType(fieldID)[source]

Returns True if the given field is a multi string type (MultiUnicode or MultiString)

LexiconFieldIsStringType(fieldID)[source]

Returns True if the given field is a simple string type suitable for use with LexiconAddTagToField(), otherwise returns False.

LexiconGetCitationForm(entry, languageTagOrHandle=None)[source]

Returns the citation form for the entry in the Default Vernacular WS or other WS as specified by languageTagOrHandle.

LexiconGetEntryCustomFieldNamed(fieldName)[source]

Return the entry-level field ID given its name.

NOTE: fieldName is case-sensitive.

LexiconGetEntryCustomFields()[source]

Returns a list of the custom fields defined at Entry level. Each item in the list is a tuple of (flid, label)

LexiconGetExample(example, languageTagOrHandle=None)[source]

Returns the example text in the Default Vernacular WS or other WS as specified by languageTagOrHandle.

LexiconGetExampleTranslation(translation, languageTagOrHandle=None)[source]

Returns the translation of an example in the Default Analysis WS or other WS as specified by languageTagOrHandle.

NOTE: Analysis language translations of example sentences are stored as a collection (list). E.g.:

for translation in example.TranslationsOC:
    print (project.LexiconGetExampleTranslation(translation))
LexiconGetFieldText(senseOrEntryOrHvo, fieldID, languageTagOrHandle=None)[source]

Return the text value for the given entry/sense and field ID. Provided for use with custom fields. Returns the empty string if the value is null. languageTagOrHandle only applies to MultiStrings; if None the default Analysis writing system is returned.

Note: if the field is a vernacular WS field, then the languageTagOrHandle must be specified.

For normal fields the object can be used directly with get_String(). E.g.:

lexForm = lexEntry.LexemeFormOA
lexEntryValue = ITsString(lexForm.Form.get_String(WSHandle)).Text
LexiconGetHeadword(entry)[source]

Returns the headword for the entry

LexiconGetLexemeForm(entry, languageTagOrHandle=None)[source]

Returns the lexeme form for the entry in the Default Vernacular WS or other WS as specified by languageTagOrHandle.

LexiconGetPronunciation(pronunciation, languageTagOrHandle=None)[source]

Returns the Form for the Pronunciation in the Default Vernacular WS or other WS as specified by languageTagOrHandle.

LexiconGetPublishInCount(entry)[source]

Returns the PublishIn Count

LexiconGetSenseCustomFieldNamed(fieldName)[source]

Return the sense-level field ID given its name.

NOTE: fieldName is case-sensitive.

LexiconGetSenseCustomFields()[source]

Returns a list of the custom fields defined at Sense level. Each item in the list is a tuple of (flid, label)

LexiconGetSenseDefinition(sense, languageTagOrHandle=None)[source]

Returns the definition for the sense in the Default Analysis WS or other WS as specified by languageTagOrHandle.

LexiconGetSenseGloss(sense, languageTagOrHandle=None)[source]

Returns the gloss for the sense in the Default Analysis WS or other WS as specified by languageTagOrHandle.

LexiconGetSenseNumber(sense)[source]

Returns the sense number for the sense. (This is not available directly from ILexSense.)

LexiconGetSensePOS(sense)[source]

Returns the part of speech abbreviation for the sense.

LexiconGetSenseSemanticDomains(sense)[source]

Returns a list of Semantic Domain objects belonging to the sense. ToString() and Hvo are available.

LexiconNumberOfEntries()[source]
LexiconSenseAnalysesCount(sense)[source]

Returns a count of the occurrences of the sense in the text corpus.

LexiconSetExample(example, newString, languageTagOrHandle=None)[source]

Set the Default Vernacular string for the given Example:

  • newString must be unicode.

  • languageTagOrHandle specifies a different writing system.

NOTE: using this function will lose any formatting that might have been present in the example string.

LexiconSetFieldInteger(senseOrEntryOrHvo, fieldID, integer)[source]

Sets the integer value for the given entry/sense and field ID. Provided for use with custom fields.

LexiconSetFieldText(senseOrEntryOrHvo, fieldID, text, languageTagOrHandle=None)[source]

Set the text value for the given entry/sense and field ID. Provided for use with custom fields.

NOTE: writes the string in one writing system only (defaults to the default analysis WS).

For normal fields the object can be used directly with set_String(). E.g.:

lexForm = lexEntry.LexemeFormOA
mkstr = TsStringUtils.MakeString("text to write", WSHandle) 
lexForm.Form.set_String(WSHandle, mkstr)
LexiconSetSenseGloss(sense, gloss, languageTagOrHandle=None)[source]

Set the Default Analysis gloss for the given sense:

  • gloss must be unicode.

  • languageTagOrHandle specifies a different writing system.

Object(hvoOrGuid)[source]

Returns the CmObject for the given Hvo or guid (str or System.Guid). Refer to .ClassName to determine the LCM class.

ObjectCountFor(repository)[source]

Returns the number of objects in the given repository. repository is specified by the interface class, such as:

  • ITextRepository

  • ILexEntryRepository

All repository names can be viewed by opening a project in LCMBrowser, which can be launched via the Help menu. Add “I” to the front and import from SIL.LCModel.

ObjectRepository(repository)[source]

Returns an object repository. repository is specified by the interface class, such as:

  • ITextRepository

  • ILexEntryRepository

ObjectsIn(repository)[source]

Returns an iterator over all the objects in the given repository. repository is specified by the interface class, such as:

  • ITextRepository

  • ILexEntryRepository

All repository names can be viewed by opening a project in LCMBrowser, which can be launched via the Help menu. Add “I” to the front and import from SIL.LCModel.

OpenProject(projectName, writeEnabled=False)[source]

Open a project. The project must be closed with CloseProject() to save any changes, and release the lock.

projectName:
  • Either the full path including “.fwdata” suffix, or

  • The name only, to open from the default project location.

writeEnabled:

Enables changes to be written to the project, which will be saved on a call to CloseProject(). LCM will raise an exception if changes are attempted without opening the project in this mode.

Note:

A call to OpenProject() may fail with a FP_FileLockedError exception if the project is open in Fieldworks (or another application). To avoid this, project sharing can be enabled within the Fieldworks Project Properties dialog. In the Sharing tab, turn on the option “Share project contents with programs on this computer”.

ProjectName()[source]

Returns the display name of the current project.

PublicationType(publicationName)[source]

Returns the PublicationType object (a CmPossibility) for the given publication name. (A list of publication names can be found using GetPublications().)

ReversalEntries(languageTag)[source]

Returns an iterator for the reversal entries for the given language tag (eg ‘en’). Returns None if there is no reversal index for that writing system.

ReversalGetForm(entry, languageTagOrHandle=None)[source]

Returns the citation form for the reversal entry in the Default Vernacular WS or other WS as specified by languageTagOrHandle.

ReversalIndex(languageTag)[source]

Returns the ReversalIndex that matches the given languageTag string (eg ‘en’). Returns None if there is no reversal index for that writing system.

ReversalSetForm(entry, form, languageTagOrHandle=None)[source]

Sets the Default Analysis reversal form for the given reversal entry:

  • form must be unicode.

  • languageTagOrHandle can be used to specify a different writing system.

TextsGetAll(supplyName=True, supplyText=True)[source]

A Generator that returns tuples of (Name, Text) where:

  • Name is the best vernacular or analysis name.

  • Text is a string with newlines separating paragraphs.

Passing supplyName/Text=False returns only the texts or names.

TextsNumberOfTexts()[source]

Returns the total number of texts in the project.

UnpackNestedPossibilityList(possibilityList, objClass, flat=False)[source]

Returns a nested or flat list of a Fieldworks Possibility List. objClass is the class of object to cast the CmPossibility elements into.

Return items are objects with properties/methods:
  • Hvo - ID (value not the same across projects)

  • Guid - Global Unique ID (same across all projects)

  • ToString() - String representation.

WSHandle(languageTag)[source]

Returns the Handle of the writing system for the given languageTag. Ignores case and ‘-‘/’_’ differences. Returns None if the language tag is not found.

WSUIName(languageTagOrHandle)[source]

Returns the UI name of the writing system for the given languageTag or Handle. Ignores case and ‘-‘/’_’ differences. Returns None if the language tag is not found.