Пример #1
0
def get(): # -> bool

  targetpath = INST_DIR + '/' + APPLOC_FILENAME
  tmppath = TMP_DIR + '/' + APPLOC_FILENAME
  url = APPLOC_URL
  size = APPLOC_FILESIZE

  dprint("enter: size = %s, url = %s" % (size, url))

  from sakurakit import skfileio
  if os.path.exists(targetpath) and skfileio.filesize(targetpath) == size:
    dprint("leave: already downloaded")
    return True

  from sakurakit import sknetio
  ok = False
  with SkProfiler("fetch"):
    if sknetio.getfile(url, tmppath, flush=False): # flush=false to use more memory to reduce disk access
      ok = skfileio.filesize(tmppath) == size
      if ok:
        os.rename(tmppath, targetpath)
  if not ok and os.path.exists(tmppath):
    skfileio.removefile(tmppath)
  dprint("leave: ok = %s" % ok)
  return ok
Пример #2
0
def compile(dic, csv, exe='mecab-dict-index', dicdir='', call=subprocess.call):
    """csv2dic. This process would take several seconds
  @param  dic  unicode  path
  @param  csv  unicode  path
  @param* exe  unicode  path
  @param* dicdir  unicode
  @param* call  launcher function
  @return  bool
  """
    # MeCab would crash for empty sized csv
    if skfileio.filesize(csv) < MIN_CSV_SIZE:
        dwarn("insufficient input csv size", csv)
        return False
    args = [
        exe,
        '-f',
        'utf8',  # from utf8
        '-t',
        'utf8',  # to utf8
        '-u',
        dic,
        csv,
    ]
    if dicdir:
        args.extend(('-d', dicdir))
    return call(args) in (0, True) and os.path.exists(dic)
Пример #3
0
def get(): # -> bool
  url = DIC_URL
  minsize = MIN_DIC_SIZE
  path = TMP_DIR + '/' + DIC_FILENAME
  path_compressed = path + '.gz'

  dprint("enter: url = %s, minsize = %s" % (url, minsize))

  #from sakurakit import skfileio
  #if os.path.exists(path) and skfileio.filesize(path) == size:
  #  dprint("leave: already downloaded")
  #  return True

  ok = False
  import gzip
  from sakurakit import skfileio, sknetio
  with SkProfiler("fetch"):
    # gzip=True to automatically extract gzip
    # flush=false to use more memory to reduce disk access
    if sknetio.getfile(url, path_compressed, flush=False, gzip=False):
      # Note: gzip=True does not extract gzip, it decompresses the header ... probs? >_<
      with gzip.open(path_compressed, 'rb') as f_in, open(path, 'wb') as f_out:
        f_content = f_in.read()
        f_out.write(f_content)
      ok = skfileio.filesize(path) > minsize
  if ok:
    skfileio.removefile(path_compressed)
  elif os.path.exists(path):
    skfileio.removefile(path)
    skfileio.removefile(path_compressed)
#  if not ok and os.path.exists(path):
#    skfileio.removefile(path)
  dprint("leave: ok = %s" % ok)
  return ok
Пример #4
0
def get(lang):  # str -> bool
    filename = DICS[lang]
    url = DIC_URL % filename
    minsize = MIN_DIC_SIZE
    path = TMP_DIR + '/' + filename
    targetpath = TARGET_DIR + '/' + filename

    dprint("enter: url = %s, minsize = %s" % (url, minsize))

    #from sakurakit import skfileio
    #if os.path.exists(path) and skfileio.filesize(path) == size:
    #  dprint("leave: already downloaded")
    #  return True

    ok = False
    from sakurakit import skfileio, sknetio
    with SkProfiler("fetch"):
        # gzip=True to automatically extract gzip
        # flush=false to use more memory to reduce disk access
        if sknetio.getfile(url, path, flush=False, gzip=True):
            ok = skfileio.filesize(path) > minsize
    if ok:
        os.renames(path, targetpath)
    elif os.path.exists(path):
        skfileio.removefile(path)
    dprint("leave: ok = %s" % ok)
    return ok
Пример #5
0
    def _save(self):
        post = {}
        post['content'] = self.postContent = self._getContent()
        post['lang'] = self.postLanguage = self._getLanguage()

        #import dataman
        #user = dataman.manager().user()
        #post['login'] = user.name
        #post['pasword'] = user.password

        if post['content']:
            imageData = ''
            if self.imagePath:
                imageTitle = self._getImageTitle()
                if imageTitle:
                    image = {
                        'filename': self.imagePath,
                        'title': imageTitle,
                        'size': skfileio.filesize(self.imagePath),
                    }
                    imageData = json.dumps(image)

            if self.topicId:
                post['topic'] = self.topicId
            if self.replyId:
                post['reply'] = self.replyId
            post['type'] = self.postType
            postData = json.dumps(post)
            self.q.postReceived.emit(postData, imageData)
            #self.postContent = '' # clear content but leave language

            growl.msg(my.tr("Edit submitted"))
Пример #6
0
def get(): # return bool
  url = UNIDIC_URL
  path = TMP_DIR + '/' + UNIDIC_FILENAME + UNIDIC_SUFFIX
  size = UNIDIC_FILESIZE

  dprint("enter: size = %s, url = %s" % (size, url))

  from sakurakit import skfileio
  if os.path.exists(path) and skfileio.filesize(path) == size:
    dprint("leave: already downloaded")
    return True

  from sakurakit import sknetio
  ok = False
  with SkProfiler("fetch"):
    if sknetio.getfile(url, path, flush=False): # flush=false to use more memory to reduce disk access
      ok = skfileio.filesize(path) == size
  if not ok and os.path.exists(path):
    skfileio.removefile(path)
  dprint("leave: ok = %s" % ok)
  return ok
Пример #7
0
  def _save(self):
    topic = {}

    v = self._getTitle()
    if v and v != self.topicTitle:
      topic['title'] = self.topicTitle = v

    v = self._getContent()
    if v and v != self.topicContent:
      topic['content'] = self.topicContent = v

    v = self._getLanguage()
    if v != self.topicLanguage:
      topic['lang'] = self.topicLanguage = v

    if self.topicType != 'review':
      v = self._getType()
      if v != self.topicType:
        topic['type'] = self.topicType = v

    imageData = ''
    if self.imagePath:
      imageTitle = self._getImageTitle()
      if imageTitle:
        image = {
          'filename': self.imagePath,
          'title': imageTitle,
          'size': skfileio.filesize(self.imagePath),
        }
        imageData = json.dumps(image)
    elif self.imageTitle:
      if self.imageId:
        v = self._getImageTitle()
        if v and v != self.imageTitle:
          topic['imageTitle'] = v
      else:
        topic['image'] = 0

    ticketData = ''
    if self.topicType == 'review':
      scores = self._getScores()
      if scores and scores != self.scores:
        ticketData = json.dumps(scores)

        topic['updateScore'] = True
        try:
          for k in scores.iterkeys():
            if not self.scores or not self.scores.get(k):
              topic['newScore'] = True
              break
        except Exception, e:
          dwarn(e)
Пример #8
0
 def _browseImage(self):
     FILTERS = "%s (%s)" % (tr_("Image"), defs.UPLOAD_IMAGE_FILTER)
     path, filter = QtWidgets.QFileDialog.getOpenFileName(
         self.q, my.tr("Select the file to upload"), "", FILTERS)
     if path:
         sz = skfileio.filesize(path)
         if sz > defs.MAX_UPLOAD_IMAGE_SIZE:
             growl.warn(
                 my.tr("File to upload is too large") +
                 " &gt;= %s" % defs.MAX_UPLOAD_IMAGE_SIZE)
         elif sz:
             self.imagePath = path
             self._refreshImage()
Пример #9
0
def getld(lang):  # str -> bool
    url = DICS[lang].get(
        'url') or "http://%s/pub/lingoes/%s.ld2" % (initdefs.DOMAIN_ORG, lang)
    size = DICS[lang]['size']
    path = LD_DIR + '/' + lang + LD_SUFFIX

    dprint("enter: lang = %s, size = %s" % (lang, size))

    from sakurakit import skfileio
    if os.path.exists(path) and skfileio.filesize(path) == size:
        dprint("leave: already downloaded")
        return True

    ok = False
    from sakurakit import sknetio
    with SkProfiler("fetch"):
        if sknetio.getfile(
                url, path, flush=False
        ):  # flush=false to use more memory to reduce disk access
            ok = skfileio.filesize(path) == size
    if not ok and os.path.exists(path):
        skfileio.removefile(path)
    dprint("leave: ok = %s" % ok)
    return ok
Пример #10
0
    def _save(self):
        if not self.subjectId or not self.subjectType:
            return
        topic = {}
        topic['title'] = self.topicContent = self._getTitle()
        topic['content'] = self.topicContent = self._getContent()
        topic['lang'] = self.topicLanguage = self._getLanguage()

        if self.topicType == 'review':
            topic['type'] = self.topicType
        else:
            topic['type'] = self._getType()

        #import dataman
        #user = dataman.manager().user()
        #topic['login'] = user.name
        #topic['pasword'] = user.password

        if topic['content'] and topic['title']:
            imageData = ''
            if self.imagePath:
                imageTitle = self._getImageTitle()
                if imageTitle:
                    image = {
                        'filename': self.imagePath,
                        'title': imageTitle,
                        'size': skfileio.filesize(self.imagePath),
                    }
                    imageData = json.dumps(image)

            topic['subjectId'] = self.subjectId
            topic['subjectType'] = self.subjectType

            topicData = json.dumps(topic)

            ticketData = ''
            if self.topicType == 'review':
                tickets = {}
                for k, v in self.scoreEdits.iteritems():
                    score = v.value()
                    if score:
                        tickets[k] = score
                if tickets:
                    ticketData = json.dumps(tickets)
            self.q.topicReceived.emit(topicData, imageData, ticketData)
            #self.topicContent = '' # clear content but leave language

            growl.msg(my.tr("Edit submitted"))
Пример #11
0
def get(dic): # str -> bool
  url = DIC_URL + DICS[dic]['file']
  path = TMP_DIR + '/' + DICS[dic]['file']

  dprint("enter: dic = %s, url = %s" % (dic, url))

  #from sakurakit import skfileio
  #if os.path.exists(path) and skfileio.filesize(path) == size:
  #  dprint("leave: already downloaded")
  #  return True

  ok = False
  from sakurakit import skfileio, sknetio
  with SkProfiler("fetch"):
    if sknetio.getfile(url, path, flush=False): # flush=false to use more memory to reduce disk access
      ok = skfileio.filesize(path) == DICS[dic]['size']
  if not ok and os.path.exists(path):
    skfileio.removefile(path)
  dprint("leave: ok = %s" % ok)
  return ok
Пример #12
0
def get(family): # str -> bool
  font = FONTS[family]
  url = font['dl']
  path = TMP_DIR + '/font-%s.%s' % (family, font['format'])

  dprint("enter: family = %s, url = %s" % (family, url))

  #from sakurakit import skfileio
  #if os.path.exists(path) and skfileio.filesize(path) == size:
  #  dprint("leave: already downloaded")
  #  return True

  ok = False
  from sakurakit import skfileio, sknetio
  with SkProfiler("fetch"):
    if sknetio.getfile(url, path, flush=False): # flush=false to use more memory to reduce disk access
      ok = skfileio.filesize(path) == font['size']
  if not ok and os.path.exists(path):
    skfileio.removefile(path)
  dprint("leave: ok = %s" % ok)
  return ok
Пример #13
0
def get(lang):  # str -> bool
    dprint("enter: lang = %s" % lang)
    url = URL + DICS[lang]['file']
    path = TMP_DIR + '/' + FILENAME_TPL % lang

    dprint("enter: url = %s" % url)

    #from sakurakit import skfileio
    #if os.path.exists(path) and skfileio.filesize(path) == size:
    #  dprint("leave: already downloaded")
    #  return True

    ok = False
    from sakurakit import skfileio, sknetio
    with SkProfiler("fetch"):
        if sknetio.getfile(
                url, path, flush=False
        ):  # flush=false to use more memory to reduce disk access
            ok = skfileio.filesize(path) > MIN_FILESIZE
    if not ok and os.path.exists(path):
        skfileio.removefile(path)
    dprint("leave: ok = %s" % ok)
    return ok
Пример #14
0
def get(dic):  # str -> bool
    url = URL_TPL % dic
    minsize = MIN_FILESIZE
    path = TMP_DIR + '/' + FILENAME_TPL % dic

    dprint("enter: url = %s, minsize = %s" % (url, minsize))

    #from sakurakit import skfileio
    #if os.path.exists(path) and skfileio.filesize(path) == size:
    #  dprint("leave: already downloaded")
    #  return True

    ok = False
    from sakurakit import skfileio, sknetio
    with SkProfiler("fetch"):
        if sknetio.getfile(
                url, path, flush=False
        ):  # flush=false to use more memory to reduce disk access
            ok = skfileio.filesize(path) > minsize
    if not ok and os.path.exists(path):
        skfileio.removefile(path)
    dprint("leave: ok = %s" % ok)
    return ok
Пример #15
0
def get():  # -> bool
    url = DIC_URL
    minsize = MIN_DIC_SIZE
    path = TMP_DIR + '/' + DIC_FILENAME

    dprint("enter: url = %s, minsize = %s" % (url, minsize))

    #from sakurakit import skfileio
    #if os.path.exists(path) and skfileio.filesize(path) == size:
    #  dprint("leave: already downloaded")
    #  return True

    ok = False
    from sakurakit import skfileio, sknetio
    with SkProfiler("fetch"):
        # gzip=True to automatically extract gzip
        # flush=false to use more memory to reduce disk access
        if sknetio.getfile(url, path, flush=False, gzip=True):
            ok = skfileio.filesize(path) > minsize
    if not ok and os.path.exists(path):
        skfileio.removefile(path)
    dprint("leave: ok = %s" % ok)
    return ok
Пример #16
0
    def _save(self):
        v = self._getContent()
        post = {}
        if v and v != self.postContent:
            post['content'] = self.postContent = v

        v = self._getLanguage()
        if v != self.postLanguage:
            post['lang'] = self.postLanguage = v

        imageData = ''
        if self.imagePath:
            imageTitle = self._getImageTitle()
            if imageTitle:
                image = {
                    'filename': self.imagePath,
                    'title': imageTitle,
                    'size': skfileio.filesize(self.imagePath),
                }
                imageData = json.dumps(image)
        elif self.imageTitle:
            if self.imageId:
                v = self._getImageTitle()
                if v and v != self.imageTitle:
                    post['imageTitle'] = v
            else:
                post['image'] = 0

        if post or imageData:
            post['id'] = self.postId
            post['userName'] = self.userName

            postData = json.dumps(post)
            self.q.postChanged.emit(postData, imageData)

            growl.msg(my.tr("Edit submitted"))
Пример #17
0
def _is_good_video(path):
  """
  @param  path  unicode
  @return  bool
  """
  return bool(path) and skfileio.filesize(path) > 1000 # at least 1K