Example #1
0
    def translateMod(self):
        params = None
        width = None
        (token, toktype) = self.lex()
        if token != u'{' and token != u'[':
            raise Exception(u'Expecting an adjustment at line ' +
                            text(self.lineno))
        basic = token == u'{'

        self.output.write(u'[' if basic else u'[[')

        while True:
            (token, toktype) = self.lex()
            if toktype == Translator.userop:
                if token == u'}' or token == u']':
                    if token == (u']' if basic else u'}'):
                        raise Exception(
                            u'Adjustment delimeters don\'t match at line ' +
                            text(self.lineno))
                    self.output.write(u']' if basic else u']]')
                    return (params, width)
                if token == u'|':
                    self.colortarget = True
                self.output.write(token)

            elif toktype == Translator.userrational or toktype == Translator.userstring:
                self.output.write(token)

            elif toktype == Translator.usertargetmod or toktype == Translator.usermod:
                if toktype == Translator.usertargetmod:
                    self.colortarget = True

                if token == u'p' or token == u'param':
                    (token, toktype) = self.lex()
                    if toktype != Translator.userstring:
                        raise Exception(
                            u'Error parsing adjustment parameter at line ' +
                            text(self.lineno))
                    params = token
                elif token == u'width':
                    saveOutput = self.output
                    self.output = io.StringIO()
                    while True:
                        (token, toktype) = self.lex()
                        if (token == u']' or token == u'}'
                                or toktype == Translator.usermod
                                or toktype == Translator.usertargetmod):
                            self.unlex(token, toktype)
                            break
                        self.output.write(token)

                    width = self.output.getvalue()
                    self.output.close()
                    self.output = saveOutput
                else:
                    self.output.write(token)

            else:
                raise Exception(u'Error parsing adjustment at line ' +
                                text(self.lineno))
Example #2
0
 def translateLoopPath(self, count):
     self.output.write(u'loop ' + count)
     (token, toktype) = self.lex()
     if token != u'*':
         raise Exception(u'Error parsing loop at line ' + text(self.lineno))
     self.translateMod()
     (token, toktype) = self.lex()
     if toktype == Translator.userstring or toktype == Translator.userpathop:
         self.translatePathElement(token)
     elif token == u'{':
         self.output.write(u'{')
         self.translatePathBody()
     else:
         raise Exception(u'Error parsing loop at line ' + text(self.lineno))
Example #3
0
def completexlate(cfdg2txt):
    try:
        with closing(translate.Translator(cfdg2txt)) as cfdgxlate:
            cfdgxlate.translate()
            return flask.json.jsonify({
                u'cfdg3txt': cfdgxlate.output.getvalue(),
                u'colortarget': cfdgxlate.colortarget,
                u'discards': cfdgxlate.extraChars
            })
    except Exception as e:
        flask.abort(400, text(e))
Example #4
0
 def translatePathBody(self):
     while True:
         (token, toktype) = self.lex()
         if toktype == Translator.userstring or toktype == Translator.userpathop:
             self.translatePathElement(token)
         elif toktype == Translator.userrational:
             self.translateLoopPath(token)
         elif token == u'}':
             self.output.write(u'}')
             return
         else:
             raise Exception(u'Error parsing path at line ' +
                             text(self.lineno))
Example #5
0
 def translateRuleBody(self):
     while True:
         (token, toktype) = self.lex()
         if toktype == Translator.userstring:
             self.output.write(token)
             self.translateMod()
         elif toktype == Translator.userrational:
             self.translateLoopRule(token)
         elif token == u'}':
             self.output.write(u'}')
             return
         else:
             raise Exception(u'Error parsing rule at line ' +
                             text(self.lineno))
Example #6
0
 def imageHelper(self, url):
     vurl = url + u"?" + text(self.imageversion)
     if self.S3:
         return S3_dir + vurl
     else:
         return vurl
Example #7
0
def put_design(fdesign):
    if not isinstance(fdesign, dict):
        flask.abort(400, u'No data received.')
    if not current_user.is_authenticated:
        if u'screenname' not in fdesign or u'password' not in fdesign:
            flask.abort(401, u'Not logged in/no user credentils provided.')

        newuser = user.canLogin(fdesign['screenname'], fdesign['password'])
        if newuser is None:
            flask.abort(401, u'Incorrect login credentials.')
        login_user(newuser, remember=False)

    upload.formfix(fdesign)

    design_id = fdesign.get('designid', 0)
    new_tags = fdesign.get('tags', [])
    if not isinstance(design_id, int) or design_id < 0:
        flask.abort(400, u'Bad design id.')
    if not isinstance(new_tags, list):
        flask.abort(400, u'Bad tags.')
    if not all(isinstance(elem, text) for elem in new_tags):
        flask.abort(400, u'Bad tags.')

    cfdgPresent = (u'cfdgfile' in flask.request.files
                   and flask.request.files[u'cfdgfile'].filename != u'')
    imagePresent = (u'imagefile' in flask.request.files
                    and flask.request.files[u'imagefile'].filename != u'')
    cfdgJson = (flask.request.is_json and 'cfdgfile' in fdesign
                and fdesign['cfdgfile'] is not None)
    imageJson = (flask.request.is_json and 'imagefile' in fdesign
                 and fdesign['imagefile'] is not None)

    if design_id != 0:
        d = design.DesignbyID(design_id)  # Get design from database
        if d is None:
            flask.abort(404, u'Design not found.')
        orig_tags = d.tags
        orig_tagids = d.tagids

        if not gal_utils.validateOwner(d.owner):
            flask.abort(401, u'Unauthorized to edit this design.')

        d.init(**fdesign)  # Merge in changes from POST
    else:
        if not ((cfdgPresent and imagePresent) or (cfdgJson and imageJson)):
            flask.abort(400, u'Upload missing cfdg or PNG file.')
        orig_tags = []
        orig_tagids = []
        d = design.Design(**fdesign)  # Create new design from POST

    d.normalize()
    id = d.save()

    if id is not None:
        try:
            design.UpdateTags(id, orig_tags, orig_tagids, new_tags)
            jpeg = 'compression' not in fdesign or fdesign[
                'compression'] != u'PNG-8'
            if cfdgPresent:
                upload.uploadcfdg(d, flask.request.files['cfdgfile'],
                                  flask.request.files['cfdgfile'].filename)
            if imagePresent:
                upload.uploadpng(d, flask.request.files['imagefile'], jpeg)
            if cfdgJson:
                cfdgtext = base64.standard_b64decode(
                    fdesign['cfdgfile']['contents'])
                cfdgfile = FileStorage(
                    stream=io.BytesIO(cfdgtext),
                    name='cfdgfile',
                    filename=fdesign['cfdgfile']['filename'])
                upload.uploadcfdg(d, cfdgfile, fdesign['cfdgfile']['filename'])
            if imageJson:
                pngdata = base64.standard_b64decode(
                    fdesign['imagefile']['contents'])
                pngfile = FileStorage(
                    stream=io.BytesIO(pngdata),
                    name='imagefile',
                    filename=fdesign['imagefile']['filename'])
                upload.uploadpng(d, pngfile, jpeg)
            newurl = u'http://localhost:8000/main.html#design/' + text(id)
            newdesign = design.DesignbyID(id)

            return (newurl, newdesign)
        except:
            design.UnaddDesign(id)
            raise
    else:
        flask.abort(500, u'Failed to save design.')
Example #8
0
    def translatePathElement(self, name):
        self.output.write(name)
        if name == u'FILL' or name == u'STROKE':
            saveOutput = self.output
            self.output = io.StringIO()
            (params, width) = self.translateMod()
            cmdMod = self.output.getvalue()
            self.output.close()
            self.output = saveOutput
            if params or width:
                self.output.write(u'(')
                if width:
                    self.output.write(width)
                if params and width:
                    self.output.write(u', ')
                if params:
                    self.translateParams(params)
                self.output.write(u')')
            self.output.write(cmdMod)
            return

        (token, toktype) = self.lex()
        if token != u'{':
            raise Exception(u'Error parsing path op at line ' +
                            text(self.lineno))

        args = {}
        ptType = u''
        saveOutput = self.output
        self.output = io.StringIO()
        while True:
            (token, toktype) = self.lex()
            if token == u'}':
                if ptType != u'':
                    args[ptType] = self.output.getvalue()
                self.output.close()
                self.output = saveOutput
                break

            if toktype == Translator.usermod:
                if ptType != u'':
                    args[ptType] = self.output.getvalue()
                    self.output.close()
                    self.output = io.StringIO()

                if token == u'p' or token == u'param':
                    (token, toktype) = self.lex()
                    if toktype != Translator.userstring:
                        raise Exception(
                            u'Error parsing path op parameter at line ' +
                            text(self.lineno))
                    args[u'param'] = token
                    ptType = u''
                else:
                    ptType = token
            else:
                self.output.write(token)

        if name == u'CLOSEPOLY':
            self.output.write(u'(')
            if u'param' in args:
                self.translateParams(args[u'param'])
            self.output.write(u')')
            return

        self.output.write(u'(')
        if u'x' in args:
            self.output.write(args[u'x'])
        else:
            self.output.write(u'0')
        self.output.write(u', ')
        if u'y' in args:
            self.output.write(args[u'y'])
        else:
            self.output.write(u'0')

        if name in [u'MOVETO', u'MOVEREL', u'LINETO', u'LINEREL']:
            pass

        elif name in [u'ARCTO', u'ARCREL']:
            if u'rx' in args or u'ry' in args:
                self.output.write(u', ')
                if u'rx' in args:
                    self.output.write(args[u'rx'])
                else:
                    self.output.write(u'1')
                self.output.write(u', ')
                if u'ry' in args:
                    self.output.write(args[u'ry'])
                else:
                    self.output.write(u'1')
                self.output.write(u', ')
                if u'r' in args:
                    self.output.write(args[u'r'])
                else:
                    self.output.write(u'0')
            else:
                self.output.write(u', ')
                if u'r' in args:
                    self.output.write(args[u'r'])
                else:
                    self.output.write(u'1')

            if u'param' in args:
                self.output.write(u', ')
                self.translateParams(args[u'param'])

        elif name in [u'CURVETO', u'CURVEREL']:
            p1 = u'x1' in args and u'y1' in args
            p2 = u'x2' in args and u'y2' in args
            if p1:
                self.output.write(u', ' + args[u'x1'] + u', ' + args[u'y1'])
            if p2:
                self.output.write(u', ' + args[u'x2'] + u', ' + args[u'y2'])
            if not p1:
                self.output.write(u', CF::Continuous')

        else:
            raise Exception(u'Error parsing path op at line ' +
                            text(self.lineno))

        self.output.write(u')')
Example #9
0
    def translate(self):
        currentshape = ''
        while True:
            (token, toktype) = self.lex()

            if toktype == Translator.usereof:
                return

            if toktype == Translator.userstart:
                currentshape = u''
                self.output.write(u'startshape')
                (token, toktype) = self.lex()
                if toktype == Translator.userstring:
                    self.output.write(token)
                else:
                    print(token, toktype)
                    raise Exception(u'Bad startshape at line ' +
                                    text(self.lineno))

                # Peek at next token to see if it is ( color etc ). This is an
                # old syntax that was dropped. Here we will just drop them.
                (token, toktype) = self.lex()
                if token == '{':
                    while True:
                        (token, toktype) = self.lex()
                        if token == '}':
                            break
                else:
                    self.unlex(token, toktype)

            elif toktype == Translator.userinclude:
                currentshape = u''
                self.output.write(u'import')
                (token, toktype) = self.lex()
                if toktype == Translator.userfilename:
                    self.output.write(token)
                else:
                    raise Exception(u'Bad include at line ' +
                                    text(self.lineno))

            elif toktype == Translator.userrule:
                (token, toktype) = self.lex()
                if toktype == Translator.userstring:
                    if currentshape != token:
                        self.output.write(u'shape ' + token + u'\n')
                        currentshape = token
                else:
                    raise Exception(u'Bad rule at line ' + text(self.lineno))
                self.output.write(u'rule')
                (token, toktype) = self.lex()
                if toktype == Translator.userrational:
                    self.output.write(token)
                    (token, toktype) = self.lex()
                if token != u'{':
                    raise Exception(u'Unrecognized token at line ' +
                                    text(self.lineno))
                self.output.write(u'{')
                self.translateRuleBody()

            elif toktype == Translator.userpath:
                currentshape = u''
                self.output.write(u'path')
                (token, toktype) = self.lex()
                if toktype != Translator.userstring:
                    raise Exception(u'Bad path name at line ' +
                                    text(self.lineno))
                self.output.write(token)
                (token, toktype) = self.lex()
                if token != u'{':
                    raise Exception(u'Unrecognized token at line ' +
                                    text(self.lineno))
                self.output.write(u'{')
                self.translatePathBody()

            elif (toktype == Translator.userconfig
                  or (toktype == Translator.usermod and token == u'size')):
                self.output.write(u'CF::' + token.capitalize() + u' =')
                self.translateMod()
                currentshape = ''

            else:
                raise Exception(u'Unrecognized token at line ' +
                                text(self.lineno))