Question

I'm trying to create a plugin to import tasks from a file when a new file been added to a certain folder.

After parsing the file I can create a new ticket like this:

tkt = Ticket(env)
tkt['reporter'] = 'me'
tkt['summary'] = 'my new ticket'
tkt['description'] = 'some bogus description'
tkt['status'] = 'new'
tkt.insert()

where env is an Environment variable:

env = Environment('/path/to/trac/env')

The thing is that way you had to hard code path into the plugin code

So the question is: is there a way to get the path to the current Environment?

After some thinking I decided I couldn't write trac plugin the way I want it (I need it to run in endlessly) So I decided to run an external script that would be running non-stop monitoring certain folder after OS start and would add new tickets when new file with ticket info appears.

So the updated question is: how can I get the Environment in that case?

Here is the example of my code:

import sys, os, time
from trac.core import *
from trac.env import *
from trac.ticket.model import Ticket
from trac.ticket.notification import TicketNotifyEmail

env = Environment("C:\\Trac\\TracDB\Planing")

db = env.get_db_cnx()
cursor = db.cursor()

path_to_watch = "C:\\trac_in_work\\upload\\"
before = dict([(f, None) for f in os.listdir(path_to_watch)])
while 1:
    time.sleep(10)
    after = dict([(f, None) for f in os.listdir(path_to_watch)])
    added = [f for f in after if not f in before]
    if added:
        for new_file in added:
            file = open(path_to_watch+new_file)

            tickets = []
            i = 0

            for line in file:
                tickets.append(line.split(','))

            for each in tickets:

                cursor.execute("SELECT ticket FROM ticket_custom WHERE name='ic_id' and value="+each[1])
                exist = cursor.fetchall()
                if not exist:

                    ticket = Ticket(env)
                    ticket['summary'] = each[0]
                    ticket['owner'] = "ako"
                    ticket['status'] = "new"
                    ticket['ic_id'] = each[1]
                    ticket.insert()
                    notification.send(ticket)

                    time.sleep(1)

    before = after
Was it helpful?

Solution

As correctly noted by RjOllos in the comments the common practice for TRAC modules is to subclass the Component class. This will give you easy access to the current environment:

from trac.core import *

class MyModule(Component):
    def createTicket(self):
        return Ticket(self.env)

You can find more examples here.

As for the second part of your question. There is no central repository with every TRAC environment listed in it. Environments in TRAC are just directories with some particular files in them, so any directory on your hard drive can be a TRAC environment. You can try to identify such directories, checking for example if they contain a subdirectory conf with trac.ini file for example, or db subdirectory with trac.db file. You can read about environment folder structure here.

The best solution in your case is for the user to supply the path to an environment as a mandatory parameter for your external process to run. This exactly the case when you run TRAC - you also provide a path to a directory with an environment to run.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top