转换UNIX的Python程序在Windows中工作

我需要制作一个驱动DYMO LabelManager PnP标签打印设备的程序。 DYMO为此提供了一个SDK,但经过一番拼命尝试之后,我会说SDK是没用的。 然后,我find了一个名叫S.bronner的人编写的程序。 但问题是他的程序是在UNIX中为Python编写的,我需要它在Python中使用python。 所以我问,有没有人可以检查这个代码,并将其转换为Windows的工作对我来说? 我的Python技能不足以完成这一点。 这里是应该转换的代码

#!/usr/bin/env python DEV_CLASS = 3 DEV_vendOR = 0x0922 DEV_PRODUCT = 0x1001 DEV_NODE = None DEV_NAME = 'Dymo LabelManager PnP' FONT_FILENAME = '/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf' FONT_SIZERATIO = 7./8 import Image import ImageDraw import ImageFont import array import fcntl import os import re import struct import subprocess import sys import termios import textwrap class DymoLabeler: """ Create and work with a Dymo LabelManager PnP object. This class contains both mid-level and high-level functions. In general,the high-level functions should be used. However,special purpose usage may require the mid-level functions. That is why they are provided. However,they should be well understood before use. Look at the high-level functions for help. Each function is marked in its docstring with 'HLF' or 'MLF' in parentheses. """ def __init__(self,dev): """Initialize the LabelManager object. (HLF)""" self.maxBytesPerLine = 8 # 64 pixels on a 12mm-tape self.ESC = 0x1b self.SYN = 0x16 self.cmd = [] self.rsp = False self.bpl = None self.dtb = 0 if not os.access(dev,os.R_OK | os.W_OK): return False self.dev = open(dev,'r+') def sendCommand(self): """Send the already built command to the LabelManager. (MLF)""" if len(self.cmd) == 0: return cmdBin = array.array('B',self.cmd) cmdBin.tofile(self.dev) self.cmd = [] if not self.rsp: return self.rsp = False rspBin = self.dev.read(8) rsp = array.array('B',rspBin).tolist() return rsp def resetCommand(self): """Remove a partially built command. (MLF)""" self.cmd = [] self.rsp = False def buildCommand(self,cmd): """Add the next instruction to the command. (MLF)""" self.cmd += cmd def statusRequest(self): """Set instruction to get the device's status. (MLF)""" cmd = [self.ESC,ord('A')] self.buildCommand(cmd) self.rsp = True def dottab(self,value): """Set the bias text height,in bytes. (MLF)""" if value < 0 or value > self.maxBytesPerLine: raise ValueError cmd = [self.ESC,ord('B'),value] self.buildCommand(cmd) self.dtb = value self.bpl = None def tapeColor(self,value): """Set the tape color. (MLF)""" if value < 0: raise ValueError cmd = [self.ESC,ord('C'),value] self.buildCommand(cmd) def bytesPerLine(self,value): """Set the number of bytes sent in the following lines. (MLF)""" if value < 0 or value + self.dtb > self.maxBytesPerLine: raise ValueError if value == self.bpl: return cmd = [self.ESC,ord('D'),value] self.buildCommand(cmd) self.bpl = value def cut(self): """Set instruction to trigger cutting of the tape. (MLF)""" cmd = [self.ESC,ord('E')] self.buildCommand(cmd) def line(self,value): """Set next printed line. (MLF)""" self.bytesPerLine(len(value)) cmd = [self.SYN] + value self.buildCommand(cmd) def chainMark(self): """Set Chain Mark. (MLF)""" self.dottab(0) self.bytesPerLine(self.maxBytesPerLine) self.line([0x99] * self.maxBytesPerLine) def skipLines(self,value): """Set number of lines of white to print. (MLF)""" if value <= 0: raise ValueError self.bytesPerLine(0) cmd = [self.SYN] * value self.buildCommand(cmd) def initLabel(self): """Set the label initialization sequence. (MLF)""" cmd = [0x00] * 8 self.buildCommand(cmd) def getStatus(self): """Ask for and return the device's status. (HLF)""" self.statusRequest() rsp = self.sendCommand() print rsp def printLabel(self,lines,dottab): """Print the label described by lines. (HLF)""" self.initLabel self.tapeColor(0) self.dottab(dottab) for line in lines: self.line(line) self.skipLines(56) # advance printed matter past cutter self.skipLines(56) # add symmetric margin self.statusRequest() rsp = self.sendCommand() print rsp def die(message=None): if message: print >> sys.stderr,message sys.exit(1) def pprint(par,fd=sys.stdout): rows,columns = struct.unpack('HH',fcntl.ioctl(sys.stderr,termios.TIocgWINSZ,struct.pack('HH',0))) print >> fd,textwrap.fill(par,columns) def getDeviceFile(classID,vendorID,productID): # find file containing the device's major and minor numbers searchdir = '/sys/bus/hid/devices' pattern = '^%04d:%04X:%04X.[0-9A-F]{4}$' % (classID,productID) deviceCandidates = os.listdir(searchdir) foundpath = None for devname in deviceCandidates: if re.match(pattern,devname): foundpath = os.path.join(searchdir,devname) break if not foundpath: return searchdir = os.path.join(foundpath,'hidraw') devname = os.listdir(searchdir)[0] foundpath = os.path.join(searchdir,devname) filepath = os.path.join(foundpath,'dev') # get the major and minor numbers f = open(filepath,'r') devnums = [int(n) for n in f.readline().strip().split(':')] f.close() devnum = os.makedev(devnums[0],devnums[1]) # check if a symlink with the major and minor numbers is available filepath = '/dev/char/%d:%d' % (devnums[0],devnums[1]) if os.path.exists(filepath): return os.path.realpath(filepath) # check if the relevant sysfs path component matches a file name in # /dev,that has the proper major and minor numbers filepath = os.path.join('/dev',devname) if os.stat(filepath).st_rdev == devnum: return filepath # search for a device file with the proper major and minor numbers for dirpath,dirnames,filenames in os.walk('/dev'): for filename in filenames: filepath = os.path.join(dirpath,filename) if os.stat(filepath).st_rdev == devnum: return filepath def access_error(dev): pprint('You do not have sufficient access to the device file %s:' % dev,sys.stderr) subprocess.call(['ls','-l',dev],stdout=sys.stderr) print >> sys.stderr pprint('You probably want to add a rule in /etc/udev/rules.d along the following lines:',sys.stderr) print >> sys.stderr,' SUBSYstem=="hidraw",\' print >> sys.stderr,' ACTION=="add",' DEVPATH=="/devices/pci[0-9]*/usb[0-9]*/0003:0922:1001.*/hidraw/hidraw0",' GROUP="plugdev"' print >> sys.stderr pprint('Following that,turn off your device and back on again to activate the new permissions.',sys.stderr) # get device file name if not DEV_NODE: dev = getDeviceFile(DEV_CLASS,DEV_vendOR,DEV_PRODUCT) else: dev = DEV_NODE if not dev: die("The device '%s' Could not be found on this system." % DEV_NAME) # create dymo labeler object lm = DymoLabeler(dev) if not lm: die(access_error(dev)) # check for any text specified on the command line labeltext = [arg.decode(sys.stdin.encoding) for arg in sys.argv[1:]] if len(labeltext) == 0: die("No label text was specified.") # create an empty label image labelheight = lm.maxBytesPerLine * 8 lineheight = float(labelheight) / len(labeltext) fontsize = int(round(lineheight * FONT_SIZERATIO)) font = ImageFont.truetype(FONT_FILENAME,fontsize) labelwidth = max(font.getsize(line)[0] for line in labeltext) labelbitmap = Image.new('1',(labelwidth,labelheight)) # write the text into the empty image labeldraw = ImageDraw.Draw(labelbitmap) for i,line in enumerate(labeltext): lineposition = int(round(i * lineheight)) labeldraw.text((0,lineposition),line,font=font,fill=255) del labeldraw # convert the image to the proper matrix for the dymo labeler object labelrotated = labelbitmap.transpose(Image.ROTATE_270) labelstream = labelrotated.tostring() labelstreamrowlength = labelheight/8 + (1 if labelheight%8 != 0 else 0) if len(labelstream)/labelstreamrowlength != labelwidth: die('An internal problem was encountered while processing the label bitmap!') labelrows = [labelstream[i:i+labelstreamrowlength] for i in range(0,len(labelstream),labelstreamrowlength)] labelmatrix = [array.array('B',labelrow).tolist() for labelrow in labelrows] # optimize the matrix for the dymo label printer dottab = 0 while max(line[0] for line in labelmatrix) == 0: labelmatrix = [line[1:] for line in labelmatrix] dottab += 1 for line in labelmatrix: while len(line) > 0 and line[-1] == 0: del line[-1] # print the label lm.printLabel(labelmatrix,dottab)

在Windows C ++中将语言环境感知的stringparsing为date

我怎样才能让rubyShellWords.shellescape工作与多字节字符?

android中aapt.exe的function是什么?

Windbg:打破定时器/调度程序中断并打印EIP

当mongod正在运行时,Crtl + C不会查杀进程,并且nodemon引发错误端口已经被使用

FONT_FILENAME = '/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf' // should be changed to path to the font on your system

由于文件系统的差异而无法工作。

searchdir = '/sys/bus/hid/devices' // take a look at "pywinusb" library (?)

也不会工作,你必须以不同的方式获得设备。 不知道从哪里。 同样的问题是

filepath = '/dev/char/%d:%d' % (devnums[0],devnums[1])

这在Windows中是无法访问的,您必须以不同的方式进行操作。

除此之外,一切看起来都是独立于操作系 如果在解决了以前的三个问题后有任何错误,请将它们编辑成您的问题。

相关文章

本篇内容主要讲解“gitee如何上传代码”,感兴趣的朋友不妨来...
这篇“从gitee上下的代码如何用”文章的知识点大部分人都不太...
这篇文章主要介绍“gitee如何下载仓库里的项目”,在日常操作...
本篇内容主要讲解“怎么在Gitee上更新代码”,感兴趣的朋友不...
本文小编为大家详细介绍“怎么将工程托管到gitee”,内容详细...
这篇文章主要介绍了gitee中图片大小如何调整的相关知识,内容...