def create(uri, alias = ""): # create datastore root dir if it does not exist if not os.path.exists(Datastore._dsroot): os.makedirs(Datastore._dsroot) from urlparse import urlparse ohash = urlparse(uri) if ohash.scheme not in Datastore._schemes: return None # return None if given uri is already created if os.path.exists(os.path.join(Datastore._dsroot, uri)): return None # fails to create the datastore if alias exists if alias and os.path.exists(os.path.join(Datastore._dsroot, alias)): return None # if alias not provided, then try to create a default one if not alias: for avail in range(1, 1024): if not os.path.exists(os.path.join(Datastore._dsroot, Datastore._dsprefix + str(avail))): alias = Datastore._dsprefix + str(avail) break # if alias not provided nor the default one created successfully, # return a failure. if not alias: return None uuid = utils.uuidgen() path = os.path.join(Datastore._dsroot, uuid) if ohash.scheme == "file": # only a link is required for a local dir os.symlink(ohash.path, path) else: os.mkdirs(path, 0755) cwd = os.getcwd() os.chdir(Datastore._dsroot) os.symlink(uri, alias) # create anonymous dir if it does not exist anonym = utils.uuidgen() if not os.path.exists(os.path.join(path, Datastore._anonymous)): os.makedirs(os.path.join(path, anonym), 0755) os.chdir(path) os.symlink(anonym, Datastore._anonymous) os.chdir(cwd) return Datastore(uri, alias)
def export_price_sheet(self): inputParams = self.getInput() try: order = Orders.get(Orders.id == int(inputParams['id'])) details = OrderDetails.select().where(OrderDetails.order == order).order_by(OrderDetails.id.desc()) self.privData['ORDER'] = order self.privData['DETAILS'] = details import pdfcrowd # create an API client instance client = pdfcrowd.Client("luo_brian", "8fee9a05739553c92673004a3ec80201") # convert an HTML string and save the result to a file import os import config import utils tmpFile = os.path.join(config.UPLOAD_DIR, 'temp', '%s.pdf' % utils.uuidgen()) ofile = open(tmpFile, 'wb') html = self.display('price-sheet-details-pdf') #html="<head></head><body>我的转换</body>" client.convertHtml(html, ofile) ofile.close() buf = open(tmpFile).read() os.unlink(tmpFile) web.header('Content-Type', 'application/pdf') return buf except Exception, e: print e return self.error(msg='获取订单详情失败!')
def create_group(self, alias = None): if not alias: alias = os.tempnam(self.path) name = "__vm__" + alias if os.path.exists(os.path.join(self.path, name)): return None for retry in range(3): guid = utils.uuidgen() path = os.path.join(self.path, guid) if not os.path.exists(path): os.mkdirs(path, 0755) os.symlink(path, os.path.join(path, name)) return VolumeGroup(self, guid, alias) return None
def __init__(self, rawdata): uploadDir = config.UPLOAD_DIR uploadTemp = os.path.join(uploadDir, 'temp') self._mimetype = None from utils import uuidgen self._uuid = uuidgen() source = os.path.join(uploadTemp, self._uuid) open(source, 'w').write(rawdata) import magic try: ms = magic.open(magic.MAGIC_MIME) ms.load() self._mimetype = ms.file(source).lower().split(';')[0] ms.close() except Exception, e: self._mimetype = magic.from_file(source, mime=True)
def __init__(self, data={}, uuid=None): dict.__init__(self) if not uuid: uuid = uuidgen() self.__dict__['uuid'] = uuid dict.update(self, data)