Exemple #1
0
 def try_eval(obj):
     try:
         return configobj.unrepr(obj)
     except ValueError:
         pass
     try:
         return eval(obj)
     except Exception as e:
         print("Object: {} isn't literal but can't evaluate expression".
               format(obj))
Exemple #2
0
 def load(self):
     rc = self.ring_character.get()
     v = self.io.get(1.0, tk.END)
     val = unrepr(v)
     for d in val:
         s,f,t,r = d
         s = s - 1
         out = t
         if r:
             out += rc
         if not out:
             out += ' '
         self.dots[s][f].set(out)
Exemple #3
0
 def move(self, offset):
     self.save()
     self.clear()
     v = self.io.get(1.0, tk.END)
     val = unrepr(v)
     new_val = []
     for d in val:
         s,f,t,r = d
         f += offset
         if 0 <= f < self.frets:
             new_val.append((s,f,t,r))
         else:
             print f
     self.io.delete(1.0, tk.END)
     self.io.insert(tk.END, str(new_val))
     self.load()
Exemple #4
0
def get_cms_templates():
    print "get cms templates"
    config = get_extend_cfg()
    templates = unrepr( config[ "CMS_TEMPLATES" ] )
    return templates
Exemple #5
0
def typed_list(value, length=None, min=None, max=None,
                      empty=None, force=True, type_=None):
    """
    This will force unrepr if None is given as type_.
    If unrepr is not desired, just use validate.tuple.
    If length is given, min and max will be ignored.
    If empty is set to True, then None can be given
    as a value.
    """
    line_endingRE = re.compile(",\s*?\n", re.DOTALL)
    quoted_listRE = re.compile("^\s*\[\s*(.*?)\]\s*$", re.DOTALL)
    if str(value) == value:
      if (empty is not None):
        if value.lower() == empty.lower():
          value = []
      else:
        # configobj should have already listified single-line lists
        ql_match = quoted_listRE.match(value)
        if line_endingRE.search(value) is not None:
          if ql_match is not None:
            value = ql_match.group(1)
          value = ", ".join(line_endingRE.split(value)).strip()
          # abuse configobj to spite the author
          value = configobj.ConfigObj(["value = " + value])['value']

    if length is None:
      try:
        min = int(min)
        max = int(max)
      except TypeError:
        pass
    else:
      try:
          min = int(length)
          max = int(length)
      except ValueError:
          raise VdtParamError('length', length)

    if force:
      if str(value) == value:
        value = [value]

    if not isinstance(value, (list, tuple)):
      raise VdtTypeError(value)

    emptied = ((empty is not None) and (value == []))
    if not emptied:
      if min is not None:
        if len(value) < min:
          raise VdtValueTooShortError(value)
      if max is not None:
        if len(value) > max:
          raise VdtValueTooLongError(value)

    out = []
    for entry in value:
      if type_ is None:
        try:
          entry = configobj.unrepr(entry)
        except (configobj.UnknownType, SyntaxError):
          # this allows explicit strings to go un-unrepred'
          entry = unquote(entry)
      else:
        try:
          entry = type_(entry)
        except TypeError:
            e = VdtTypeError(value)
            e.msg = "%s not a valid type for conversion." % repr(value)
            raise e
        except ValueError:
            e = VdtValueError(value)
            e.msg = "Can not convert '%s' to a %s." % (entry, type_)
            raise e
      out.append(entry)
    return out
Exemple #6
0
    def done( self, form_list, **kwargs ):
        placeholder_template = " placeholder %(placeholder_name)s group=image tips=\"%(placeholder_tips)s\" " \
            "width=216 height=168 "
        cleaned_data = self.get_all_cleaned_data()
        #print cleaned_data
        row_count = int( cleaned_data.get( 'rows', 0 ) )
        picture_rows = []
        picture_id = 1
        col_ids = []
        for row in range( 1, row_count + 1 ):
            key = 'row' + str( row )
            # may use get
            picture_cols = []
            col_ids.append( cleaned_data[ key] )
            col_count = int( cleaned_data[ key ] )
            for col in range( 0, col_count  ):
                picture = {}
                substitution = {
                    'placeholder_name':  "photo%s" % str( picture_id ),
                    'placeholder_tips':  "Design for photo%s" % ( str( picture_id ) ),
                }
                picture_id += 1
                

                picture[ 'placeholder' ] = mark_safe( "{%" + placeholder_template % substitution \
                    + "%}" )
                picture[ 'url' ] = '#'
                picture[ 'name' ] = substitution[ 'placeholder_name' ]
                picture_cols.append( picture )
                
            picture_rows.append( picture_cols )
        

        template = render_to_string( self.base_template_name, Context( { 'picture_rows': picture_rows, 'template': 1 } ) )
        template =  mark_safe( template )
        
        #prepare template_name
        template_name_template = "photography_%s"
        for i in range( 0, row_count ):
            template_name_template += "_%s"
            ids = tuple( [ unicode(row_count) ] +  col_ids ) 
        template_name = template_name_template % ids
        
        templates_dir = os.path.join( settings.PROJECT_PATH, 'templates')
        template_path = os.path.join( templates_dir, template_name )
  
        """
        fp = open( template_path + '.html', 'w' )
        fp.write( template )
        fp.close()
        """
        # setting the config file
        config = get_extend_cfg()
        cms_templates = unrepr( config[ 'CMS_TEMPLATES' ] )
        
        update_tuple_list( cms_templates, template_path  + ".html", template_name )
        config[ 'CMS_TEMPLATES' ] = repr( cms_templates )
        config.write()

        page_addview_url = reverse('admin:cms_page_add')
        #Todo: generate the template and insert value to database
        template_upload_root = 'media/templates'
        template_media_root = 'templates'
        filename = cleaned_data[ 'filename' ]
        
        filename = os.path.join( template_upload_root, filename )
        full_path = os.path.join( settings.PROJECT_PATH, filename )
        
        name = os.path.split( full_path )[1]
        cms_template = None
        try:
            cms_template = CMS_Template.objects.get( name = name )
        except CMS_Template.DoesNotExist:
            pass
        
        if not cms_template:
            cms_template = CMS_Template(name = name,
                                        cms_template = 'templates/' + cleaned_data[ 'filename' ] + '.html' )
        else:
            print "Duplicate Name"
        if os.path.exists( full_path ):
            print "Already exists"
        
        fp = open( full_path + '.html', 'w' )
        fp.write( template )
        fp.close()
        
        cms_template.save()
        
        #Todo: auto selected the new added template
        attrs = '?template=' + unicode(cms_template.name)
        return HttpResponseRedirect( page_addview_url  + attrs)