homeResFile.py :  » GUI » TTX-FontTools » fonttools-2.3 » Lib » fontTools » misc » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » GUI » TTX FontTools 
TTX FontTools » fonttools 2.3 » Lib » fontTools » misc » homeResFile.py
"""Mac-only module to find the home file of a resource."""

import sstruct
import array
import calldll
import macfs, Res


def HomeResFile(res):
  """Return a path to the file in which resource 'res' lives."""
  return GetFileLocation(res.HomeResFile())


def GetFileLocation(refNum):
  """Return a path to the open file identified with refNum."""
  pb = ParamBlock(refNum)
  return pb.getPath()

#
# Internal cruft, adapted from MoreFiles
#

_InterfaceLib = calldll.getlibrary("InterfaceLib")
GetVRefNum = calldll.newcall(_InterfaceLib.GetVRefNum, "None", "InShort", "OutShort")
_getInfo = calldll.newcall(_InterfaceLib.PBGetFCBInfoSync, "Short", "InLong")


_FCBPBFormat = """
  qLink:        l
  qType:        h
  ioTrap:       h
  ioCmdAddr:    l
  ioCompletion: l
  ioResult:     h
  ioNamePtr:    l
  ioVRefNum:    h
  ioRefNum:     h
  filler:       h
  ioFCBIndx:    h
  filler1:      h
  ioFCBFINm:    l
  ioFCBFlags:   h
  ioFCBStBlk:   h
  ioFCBEOF:     l
  ioFCBPLen:    l
  ioFCBCrPs:    l
  ioFCBVRefNum: h
  ioFCBClpSiz:  l
  ioFCBParID:   l
"""

class ParamBlock:
  
  """Wrapper for the very low level FCBPB record."""
  
  def __init__(self, refNum):
    self.__fileName = array.array("c", "\0" * 64)
    sstruct.unpack(_FCBPBFormat, 
        "\0" * sstruct.calcsize(_FCBPBFormat), self)
    self.ioNamePtr = self.__fileName.buffer_info()[0]
    self.ioRefNum = refNum
    self.ioVRefNum = GetVRefNum(refNum)
    self.__haveInfo = 0
  
  def getInfo(self):
    if self.__haveInfo:
      return
    data = sstruct.pack(_FCBPBFormat, self)
    buf = array.array("c", data)
    ptr = buf.buffer_info()[0]
    err = _getInfo(ptr)
    if err:
      raise Res.Error, ("can't get file info", err)
    sstruct.unpack(_FCBPBFormat, buf.tostring(), self)
    self.__haveInfo = 1
  
  def getFileName(self):
    self.getInfo()
    data = self.__fileName.tostring()
    return data[1:ord(data[0])+1]
  
  def getFSSpec(self):
    self.getInfo()
    vRefNum = self.ioVRefNum
    parID = self.ioFCBParID
    return macfs.FSSpec((vRefNum, parID, self.getFileName()))
  
  def getPath(self):
    return self.getFSSpec().as_pathname()


if __name__ == "__main__":
  fond = Res.GetNamedResource("FOND", "Helvetica")
  print HomeResFile(fond)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.