Пример #1
0
def separate_regex_simple(voteddir, regexShr, regexDiff):
    ballots = []
    for dirpath, dirnames, filenames in os.walk(voteddir):
        imgnames = [f for f in filenames if util.is_image_ext(f)]
        shrPat = re.compile(regexShr)
        diffPat = re.compile(regexDiff)
        curmats = {}  # maps {str sim_pat: [(str imgpath, str diff_pat), ...]}
        for imgname in imgnames:
            imgpath = pathjoin(dirpath, imgname)
            sim_match = shrPat.match(imgname)
            diff_match = diffPat.match(imgname)
            if sim_match is None or diff_match is None:
                warn(
                    "Ballot {0} was skipped because it didn't "
                    "match the regular expressions.", imgpath)
                continue
            sim_part = sim_match.groups()[0]
            diff_part = diff_match.groups()[0]
            curmats.setdefault(sim_part, []).append((imgpath, diff_part))
        for sim_pat, tuples in curmats.iteritems():
            # sort by diffPart
            tuples_sorted = sorted(tuples, key=lambda t: t[1])
            imgpaths_sorted = [t[0] for t in tuples_sorted]
            ballots.append(imgpaths_sorted)
    return ballots
def separate_regex_ctr(voteddir, regexShr):
    """ Separates ballots whose filenames start with a shared prefix
    REGEXSHR, but then contain two incrementing counters (very-much
    Hart-specific), i.e. for the following images:
        Ballot A:
        339_1436_5_211_1.png
        339_1436_5_212_2.png
        339_1436_5_213_3.png
        Ballot B:
        339_1436_5_214_1.png
        339_1436_5_215_2.png
    """
    ballots = []
    shrPat = re.compile(regexShr)
    for dirpath, dirnames, filenames in os.walk(voteddir):
        imgnames = [f for f in filenames if util.is_image_ext(f)]
        curmats = {} # maps {str sim_pat: [(str imgpath, tuple ctr_vals), ...]}
        for imgname in imgnames:
            imgpath = pathjoin(dirpath, imgname)
            sim_match = shrPat.match(imgname)
            if sim_match == None:
                print "Warning: ballot {0} was skipped because it didn't \
match the regular expressions.".format(imgpath)
                continue
            sim_part = sim_match.groups()[0]
            # Assumes filename is := <SIM_PART>_N1_N2.png
            ctr_vals = [int(n) for n in os.path.splitext(imgname)[0][len(sim_part):].split("_")]
            curmats.setdefault(sim_part, []).append((imgpath, ctr_vals))
        for sim_pat, tuples in curmats.iteritems():
            # tuple TUPLES := [(str imgpath, (int N1, int N2)), ...]
            consecs = get_consecutives(tuples)
            for imgpaths in consecs:
                ballots.append(imgpaths)
    return ballots
def separate_singlesided(voteddir):
    ballots = []
    for dirpath, dirnames, filenames in os.walk(voteddir):
        imgnames = [f for f in filenames if util.is_image_ext(f)]
        for imgname in imgnames:
            imgpath = pathjoin(dirpath, imgname)
            ballots.append([imgpath])
    return ballots
Пример #4
0
def get_all_images(directory):
    '''
    Walk a given directory and return a list of all the filenames
    that are images.
    '''
    images = []
    for _, _, filenames in os.walk(directory):
        images.extend(f for f in filenames if util.is_image_ext(f))
    return images
def separate_alternating(voteddir, num_pages):
    ballots = []
    for dirpath, dirnames, filenames in os.walk(voteddir):
        imgnames = [f for f in filenames if util.is_image_ext(f)]
        imgnames_ordered = util.sorted_nicely(imgnames)
        if len(imgnames_ordered) % num_pages != 0:
            print "Uh oh -- there are {0} images in directory {1}, \
which isn't divisible by num_pages {2}".format(len(imgnames_ordered), dirpath, num_pages)
            pdb.set_trace()
            raise RuntimeError
        i = 0
        while imgnames_ordered:
            curballot = []
            for j in xrange(num_pages):
                imgpath = pathjoin(dirpath, imgnames_ordered.pop(0))
                curballot.append(imgpath)
            ballots.append(curballot)
    return ballots
Пример #6
0
def separate_regex_simple(voteddir, regexShr, regexDiff):
    ballots = []
    for dirpath, dirnames, filenames in os.walk(voteddir):
        imgnames = [f for f in filenames if util.is_image_ext(f)]
        shrPat = re.compile(regexShr)
        diffPat = re.compile(regexDiff)
        curmats = {} # maps {str sim_pat: [(str imgpath, str diff_pat), ...]}
        for imgname in imgnames:
            imgpath = pathjoin(dirpath, imgname)
            sim_match = shrPat.match(imgname)
            diff_match = diffPat.match(imgname)
            if sim_match == None or diff_match == None:
                print "Warning: ballot {0} was skipped because it didn't \
match the regular expressions.".format(imgpath)
                continue
            sim_part = sim_match.groups()[0]
            diff_part = diff_match.groups()[0]
            curmats.setdefault(sim_part, []).append((imgpath, diff_part))
        for sim_pat, tuples in curmats.iteritems():
            # sort by diffPart
            tuples_sorted = sorted(tuples, key=lambda t: t[1])
            imgpaths_sorted = [t[0] for t in tuples_sorted]
            ballots.append(imgpaths_sorted)
    return ballots
Пример #7
0
def separate_alternating(voteddir, num_pages):
    ballots = []
    for dirpath, dirnames, filenames in os.walk(voteddir):
        imgnames = [f for f in filenames if util.is_image_ext(f)]
        imgnames_ordered = util.sorted_nicely(imgnames)
        if len(imgnames_ordered) % num_pages != 0:
            raise ffwx.Panel.StepNotFinished(
                ('The selected directory:\n\n'
                 '  {0}\n\n'
                 'contains {1} images, but the '
                 'selected options indicate that pages should '
                 'come in sets of {2}, and {1} isn\'t divisible '
                 'by {2}.\n\n'
                 'Try double-checking your chosen settings and your '
                 'selected ballot directory.'.format(dirpath,
                                                     len(imgnames_ordered),
                                                     num_pages)))
        while imgnames_ordered:
            curballot = []
            for j in xrange(num_pages):
                imgpath = pathjoin(dirpath, imgnames_ordered.pop(0))
                curballot.append(imgpath)
            ballots.append(curballot)
    return ballots