Next / Previous / Contents / Shipman's homepage

5.4. entmarch: main()

entmarch
# - - - - -   m a i n

def main():
    """Generate entity definitions for one entity group.

      [ if (unidata's data file is readable and valid) and
        (the command line names an entity group in that file) ->
          sys.stdout  +:=  ENTITY definitions for character entities
              from that file that are in that entity group
        else ->
          sys.stderr  +:=  error message(s) ]
    """

The first step is to get the group name from the command line.

entmarch
    #-- 1
    # [ if the command line is valid ->
    #     groupName  :=  the group name from the command line
    #   else ->
    #     sys.stderr  +:=  error message
    #     stop execution ]
    argList = sys.argv[1:]
    if len(argList) != 1:
        fatal("*** Usage: {0} GROUPNAME".format(sys.argv[0]))
    else:
        groupName = sys.argv[1]

Next we open the data file, look up the given group name, and write a comment containing that name.

entmarch
    #-- 2
    # [ if unidata's data file is readable and valid ->
    #     uniData  :=  a UniData instance representing that file
    #   else ->
    #     sys.stderr  +:=  error message
    #     stop execution ]
    try:
        uniData = unidata.UniData()
    except (IOError, OSError) as x:
        fatal("Can't open the data file: {0}".format(str(x)))

    #-- 3
    # [ if groupName names an entity group in uniData ->
    #     uniGroup  :=  the corresponding UniGroup instance
    #     sys.stdout  +:=  heading showing groupName
    #   else ->
    #     sys.stderr  +:=  error message
    #     stop execution ]
    try:
        uniGroup = uniData.findGroup(groupName)
        print("<!--Entity group '{0}'-->".format(uniGroup.name))
    except KeyError:
        fatal("No such Unicode entity group: '{0}'.".format(groupName))

Each entity group contains multiple entity sets. For each set, we call Section 5.5, “entmarch: extractSet() to write the definitions for entities in that set.

entmarch
    #-- 4
    # [ sys.stdout  +:=  entity definitions for entities in uniGroup ]
    for entSet in sorted([entSet
                          for entSet in uniGroup.genEntSets()]):
        extractSet(uniData, entSet)