Exemplo n.º 1
0
    def run(self):
        module_device_types = ModuleDeviceType.query.all()
        module_macs = [
            'C9:65:6E:EA:B0:8B', 'E0:D5:1B:08:C9:EB', '4F:8E:91:81:60:91'
        ]

        for mac in module_macs:
            module = Module(
                mac=mac,
                module_device_type_id=random.choice(module_device_types).id)
            print(f'Creating module: {module.mac}')
            self.db.session.add(module)
Exemplo n.º 2
0
    def handle(self, *args, **options):

        # process argument (module code)
        if not args:
            print 'Usage: python manage.py refresh <module_code> --option'
            return

        # info
        timestr = time.strftime("%Y.%m.%d.%H.%M.%S")
        out.info('Logging started at %s', timestr)
        # out.info('SITE_ROOT = %s' % SITE_ROOT)
        # out.info('TEX_ROOT  = %s' % TEX_ROOT)

        # iterate over modules
        for module_code in args:
            out.info('BEGIN processing %s', module_code)

            # find main.tex
            main_tex = os.path.join(TEX_ROOT, module_code, 'main.tex')

            # parse
            p = TexParser()
            tree = p.parse_book( main_tex )

            # text output (native format)
            if options['text']:
                print tree

            # xml output
            if options['xml']:
                xml = tree.prettyprint_xml()
                self.stdout.write( xml )

            # labels
            if options['labels']:
                pairs = tree.get_label_mpaths()
                col_width = max( [len(pair[0]) for pair in pairs] ) + 2  # padding
                for pair in pairs:
                    self.stdout.write( pair[0].ljust(col_width) + pair[1] )

            # database
            if options['db'] or options['commit']:

                # check whether this module already exists in the database
                preamble = p.parse_preamble( main_tex )
                code = preamble['module_code']
                year = preamble['academic_year']
                module = Module.objects.filter(code=code, year=year).first()
                if not module:
                    out.info( 'Creating new module %s/%s' % (code, year) )
                    module = Module(code=code, year=year, title=preamble['module_title'])
                    module.save()
                else:
                    out.info( 'Updating existing module %s/%s' % (code, year) )
                number = preamble['book_number']
                book = Book.objects.filter(module=module, number=number).first()
                if book:
                    out.info( 'Existing book %s/%s/%s will be deleted' % (code, year, number) )
                    for booknode in BookNode.objects.filter(mpath__startswith=book.tree.mpath):
                        booknode.delete()
                    book.delete()
        

                book = Book()
                code = preamble['module_code']
                year = preamble['academic_year']
        
                book.module = Module.objects.filter(code=code, year=year).first()
                if 'book_number' in preamble:
                    book.number = int(preamble['book_number'])
                else:
                    book.number = 0
                if 'book_title' in preamble:
                    book.title = preamble['book_title']
                if 'book_author' in preamble:
                    book.author = preamble['book_author']
                if 'book_version' in preamble:
                    book.version = preamble['book_version']
                if 'new_commands' in preamble:
                    book.new_commands = preamble['new_commands']
            
                hexstr = hex( book.number )[2:].zfill(2)
                prefix = code + '.' + hexstr

                # write book database
                if options['commit']:
                    book.tree = tree.write_to_camel_database(prefix=prefix, commit=True)
                    book.save()
            
                else:
                    tree.write_to_camel_database(prefix=prefix, commit=False)
          
                # write labels to database
                pairs = tree.get_label_mpaths()
                for pair in pairs:
                    lab = Label()
                    lab.text = prefix + '.' + pair[0]
                    lab.mpath = prefix + pair[1]
                    if options['commit']:
                    	lab.book = book
                        lab.save()
                    else:
                        print lab
Exemplo n.º 3
0
    def handle(self, *args, **options):

        # process argument (module code)
        if not args:
            print 'Usage: python manage.py refresh <module_code> --option'
            return

        # info
        timestr = time.strftime("%Y.%m.%d.%H.%M.%S")
        out.info('Logging started at %s', timestr)
        # out.info('SITE_ROOT = %s' % SITE_ROOT)
        # out.info('TEX_ROOT  = %s' % TEX_ROOT)

        # iterate over modules
        for module_code in args:
            out.info('BEGIN processing %s', module_code)

            # find main.tex
            main_tex = os.path.join(TEX_ROOT, module_code, 'main.tex')

            # parse
            p = TexParser()
            tree = p.parse_book(main_tex)

            # text output (native format)
            if options['text']:
                print tree

            # xml output
            if options['xml']:
                xml = tree.prettyprint_xml()
                self.stdout.write(xml)

            # labels
            if options['labels']:
                pairs = tree.get_label_mpaths()
                col_width = max([len(pair[0])
                                 for pair in pairs]) + 2  # padding
                for pair in pairs:
                    self.stdout.write(pair[0].ljust(col_width) + pair[1])

            # database
            if options['db'] or options['commit']:

                # check whether this module already exists in the database
                preamble = p.parse_preamble(main_tex)
                code = preamble['module_code']
                year = preamble['academic_year']
                module = Module.objects.filter(code=code, year=year).first()
                if not module:
                    out.info('Creating new module %s/%s' % (code, year))
                    module = Module(code=code,
                                    year=year,
                                    title=preamble['module_title'])
                    module.save()
                else:
                    out.info('Updating existing module %s/%s' % (code, year))
                number = preamble['book_number']
                book = Book.objects.filter(module=module,
                                           number=number).first()
                if book:
                    out.info('Existing book %s/%s/%s will be deleted' %
                             (code, year, number))
                    for booknode in BookNode.objects.filter(
                            mpath__startswith=book.tree.mpath):
                        booknode.delete()
                    book.delete()

                book = Book()
                code = preamble['module_code']
                year = preamble['academic_year']

                book.module = Module.objects.filter(code=code,
                                                    year=year).first()
                if 'book_number' in preamble:
                    book.number = int(preamble['book_number'])
                else:
                    book.number = 0
                if 'book_title' in preamble:
                    book.title = preamble['book_title']
                if 'book_author' in preamble:
                    book.author = preamble['book_author']
                if 'book_version' in preamble:
                    book.version = preamble['book_version']
                if 'new_commands' in preamble:
                    book.new_commands = preamble['new_commands']

                hexstr = hex(book.number)[2:].zfill(2)
                prefix = code + '.' + hexstr

                # write book database
                if options['commit']:
                    book.tree = tree.write_to_camel_database(prefix=prefix,
                                                             commit=True)
                    book.save()

                else:
                    tree.write_to_camel_database(prefix=prefix, commit=False)

                # write labels to database
                pairs = tree.get_label_mpaths()
                for pair in pairs:
                    lab = Label()
                    lab.text = prefix + '.' + pair[0]
                    lab.mpath = prefix + pair[1]
                    if options['commit']:
                        lab.book = book
                        lab.save()
                    else:
                        print lab
Exemplo n.º 4
0
    def handle(self, *args, **options):

        # process argument (module code)
        if not args:
            print 'Usage: python manage.py refresh <module_code> --option'
            return

        # info
        timestr = time.strftime("%Y.%m.%d.%H.%M.%S")
        out.info('Logging started at %s', timestr)
        # out.info('SITE_ROOT = %s' % SITE_ROOT)
        # out.info('TEX_ROOT  = %s' % TEX_ROOT)

        # iterate over modules
        for module_code in args:
            out.info('BEGIN processing %s', module_code)

            # find main.tex
            main_tex = os.path.join(TEX_ROOT, module_code, 'main.tex')

            # create book tree
            p = TexParser()
            preamble = p.parse_preamble( main_tex )
            book = p.parse_book( main_tex )
            book.title = preamble['book_title']

            # xml output
            if options['xml']:
                xml = book.prettyprint_xml()
                self.stdout.write( xml )

            # labels
            elif options['labels']:
                pairs = book.get_label_mpaths()
                col_width = max( [len(pair[0]) for pair in pairs] ) + 2  # padding
                for pair in pairs:
                    self.stdout.write( pair[0].ljust(col_width) + pair[1] )

            # database
            elif options['db'] or options['commit']:

                # check whether this module already exists in the database
                code = preamble['module_code']
                year = preamble['academic_year']
                modules = Module.objects.filter(code=code, year=year)
                if not modules:
                    out.info( 'Creating new module %s/%s' % (code, year) )
                    mo = Module(code=code, year=year, title=preamble['module_title'], newcommands='')
                    mo.save()
                elif len(modules) == 1:
                    out.info( 'Updating existing module %s/%s (existing doctree will be deleted)' % (code, year) )
                    mo = modules[0]
                    for bo in BookNode.objects.filter(module=mo):
                        bo.delete()
                else:
                    out.error('Error: database contains more than one (%s, %s)!' % (code, year) )
                    continue

                # hack to set user-defined latex macros
                nc = ''
                nc += r'\newcommand{\N}{\mathbb{N}}'
                nc += r'\newcommand{\Z}{\mathbb{Z}}'
                nc += r'\newcommand{\R}{\mathbb{R}}'
                nc += r'\newcommand{\C}{\mathbb{C}}'
                nc += r'\newcommand{\prob}{\mathbb{P}}'
                nc += r'\newcommand{\expe}{\mathbb{E}}'
                nc += r'\newcommand{\var}{\text{Var}}'
                nc += r'\newcommand{\cov}{\text{Cov}}'
                nc += r'\newcommand{\supp}{\text{supp}}'
                mo.newcommands = nc

                # save module
                mo.save()

                # write to database
                if options['commit']:
                    book.write_to_camel_database(module=mo, commit=True)
                else:
                    book.write_to_camel_database(module=mo, commit=False)

            # default: text output
            else:
                print book