示例#1
0
    def set_dates(self, start_secs, end_secs):
        self.start = date_utils.make_date_from_timestamp(start_secs)
        self.pretty_start_date = date_utils.pretty_date(self.start)
        self.pretty_start_time = date_utils.pretty_time(self.start)

        self.is_future  = date_utils.is_future(start_secs)
        self.is_past    = date_utils.is_past(end_secs)
        self.is_distant = date_utils.is_distant(self.start)

        if end_secs > 1000:
            end = date_utils.make_date_from_timestamp(end_secs)
            if type(end) is datetime and end > self.start:
                self.end = end
                self.pretty_end_date = date_utils.pretty_date(self.end)
                self.pretty_end_time = date_utils.pretty_time(self.end)
示例#2
0
def make_tab_obj(name, id, date=None, uri=None, append=None):
    """ for the date tab on the stop schedule page, we expect an object that has a name and a url
        this method builds that structure, and most importantly, the url for those tabs
    """
    ret_val = {}

    # put the name of the tab first (and strip off any leading / trailing ZEROs if the name is a date)
    ret_val['name'] = name.lstrip('0').replace('/0', '/')
    ret_val['date'] = date
    ret_val['tooltip'] = date_utils.pretty_date(date)
    ret_val['dow'] = date_utils.dow(date)
    ret_val['dow_abbrv'] = date_utils.dow_abbrv(date)

    # next give the tab object a URL ... date is broken up into month and day parts
    if uri:
        month = ""
        day = ""
        tab_id = ""
        if date:
            month = "&month={0}".format(date.month)
            day = "&day={0}".format(date.day)
        if id:
            tab_id = "&tab_id={0}".format(id)

        ret_val["url"] = "{0}{1}{2}{3}".format(uri, month, day, tab_id)
        if append:
            ret_val["url"] = "{0}&{1}".format(ret_val["url"], append)

    return ret_val
示例#3
0
def make_tab_obj(name, id, date=None, uri=None, append=None):
    ''' for the date tab on the stop schedule page, we expect an object that has a name and a url
        this method builds that structure, and most importantly, the url for those tabs
    '''
    ret_val = {}

    # put the name of the tab first (and strip off any leading / trailing ZEROs if the name is a date)
    ret_val['name'] = name.lstrip('0').replace('/0','/')
    ret_val['date'] = date
    ret_val['tooltip'] = date_utils.pretty_date(date)
    ret_val['dow'] = date_utils.dow(date)
    ret_val['dow_abbrv'] = date_utils.dow_abbrv(date)

    # next give the tab object a URL ... date is broken up into month and day parts
    if uri:
        month = ""
        day = ""
        tab_id = ""
        if date:
            month = "&month={0}".format(date.month)
            day = "&day={0}".format(date.day)
        if id:
            tab_id = "&tab_id={0}".format(id)

        ret_val["url"] = "{0}{1}{2}{3}".format(uri, month, day, tab_id)
        if append:
            ret_val["url"] = "{0}&{1}".format(ret_val["url"], append)

    return ret_val
示例#4
0
def get_tabs(request, url):
    '''
    '''
    date  = html_utils.get_first_param_as_date(request)
    month = html_utils.get_first_param_as_int(request, 'month')
    day   = html_utils.get_first_param_as_int(request, 'day')
    date  = date_utils.set_date(date, month, day)
    more  = html_utils.get_first_param(request, 'more')

    ret_val = {}
    ret_val['more_form']   = date_utils.get_day_info(date)
    ret_val['pretty_date'] = date_utils.pretty_date(date)
    ret_val['tabs'] = get_svc_date_tabs(date, url, more is not None, get_translator(request)) 

    return ret_val
示例#5
0
def get_tabs(request, url):
    ''' make the set of tabs on the schedule page
    '''
    #import pdb; pdb.set_trace()
    is_prev_day = use_previous_day(request)
    if is_prev_day:
        date = date_utils.get_day_before()
    else:
        date  = html_utils.get_first_param_as_date(request)
        month = html_utils.get_first_param_as_int(request, 'month')
        day   = html_utils.get_first_param_as_int(request, 'day')
        date  = date_utils.set_date(date, month, day)

    more   = html_utils.get_first_param(request, 'more')
    tab_id = html_utils.get_first_param_as_int(request, 'tab_id', 0)

    ret_val = {}
    ret_val['more_form']   = date_utils.get_day_info(date)
    ret_val['pretty_date'] = date_utils.pretty_date(date)
    ret_val['tabs'] = make_date_tabs(date, url, is_prev_day, tab_id, more is not None, get_translator(request))

    return ret_val
示例#6
0
def get_tabs(request, url):
    """ make the set of tabs on the schedule page
    """
    #import pdb; pdb.set_trace()
    is_prev_day = use_previous_day(request)
    if is_prev_day:
        date = date_utils.get_day_before()
    else:
        date = html_utils.get_first_param_as_date(request)
        month = html_utils.get_first_param_as_int(request, 'month')
        day = html_utils.get_first_param_as_int(request, 'day')
        year = date_utils.normalize_year(month)
        date = date_utils.set_date(date, month, day, year)

    more = html_utils.get_first_param(request, 'more')
    tab_id = html_utils.get_first_param_as_int(request, 'tab_id', 0)

    ret_val = {}
    ret_val['more_form'] = date_utils.get_day_info(date)
    ret_val['pretty_date'] = date_utils.pretty_date(date)
    ret_val['tabs'] = make_date_tabs(date, url, is_prev_day, tab_id, more
                                     is not None, get_translator(request))

    return ret_val
示例#7
0
class OsmRename(object):
    """ Utility for getting stats on an osm file 
    """
    attrib = "streets renamed by OpenTransitTools on {}".format(date_utils.pretty_date())
    bunchsize = 1000000
    rename_cache = {}

    def __init__(self, osm_infile_path, osm_outfile_path, do_bkup=True):
        """ this class will work to rename streets in OSM, abbreviating common street prefix and suffixes
            (e.g., North == N, Southeast == SE, Street == St, Avenue == Ave, etc...)
            
            :note this assumes that each OSM <tag /> falls completely upon a single line of the file 
            and the parser / renamer will break if targeted tags are spread across multiple lines of the file

            @todo look at SAX
            https://gist.github.com/veryhappythings/98604
            :todo ... add unit tests
            TODO: fix up hacky parts...
        """
        self.osm_input_path = osm_infile_path
        if osm_outfile_path is None or len(osm_outfile_path) == 0:
            osm_outfile_path = osm_infile_path
        is_same_input_output = False
        if osm_outfile_path == osm_infile_path:
            self.osm_output_path = osm_outfile_path + "temp"
            is_same_input_output = True
        else:
            self.osm_output_path = osm_outfile_path

        self.abbr_parser = OsmAbbrParser()
        self.process_osm_file()

        if is_same_input_output:
            if do_bkup:
                file_utils.bkup(osm_outfile_path)
            file_utils.mv(self.osm_output_path, osm_outfile_path)

    def process_osm_file(self):
        """ read input xml file line by line.  where we encounter street element tags, look to rename 
            the 'v' attribute (e.g, street name) with 
        """
        bunch = []
        do_rename = False
        is_inside_way = False
        with open(self.osm_input_path, "r") as r, open(self.osm_output_path, "w") as w:
            for line_num, line in enumerate(r):
                # step 1: check to see if this .osm file has already been renamed
                if not do_rename and "<osm " in line:
                    if not self.attrib in line:
                        line = add_xml_attribute_to_osm_tag(line, line_num)
                        do_rename = True

                # step 2: run rename method(s) for this line in the text (xml) file
                if do_rename:
                    if "addr:street" in line:
                        line = self.process_streetname_str(line, line_num, "addr:street")
                    if is_inside_way:
                        if '<tag k="name"' in line or '<tag k="name_' in line:
                            line = self.process_streetname_str(line, line_num, "way:name")
                        elif '<tag k="alt_name' in line:
                            line = self.process_streetname_str(line, line_num, "way:alt_name")
                        elif '<tag k="old_name' in line:
                            line = self.process_streetname_str(line, line_num, "way:old_name")
                        elif '<tag k="bridge:name' in line:
                            line = self.process_streetname_str(line, line_num, "way:bridge:name")
                        elif '<tag k="description' in line:
                            line = self.process_streetname_str(line, line_num, "way:description")
                        elif '<tag k="destination' in line:
                            line = self.process_streetname_str(line, line_num, "way:destination")
                    if '<way ' in line or '<relation ' in line:
                        is_inside_way = True
                    if '</way>' in line or '</relation>' in line:
                        is_inside_way = False

                    # remove ET xml (type html) side effects, so ending tags look trim & proper
                    if line:
                        line = line.replace(" />", "/>").replace("></tag>", "/>")

                # step 3: buffer write the lines of the file to a new file
                bunch.append(line)
                if len(bunch) == self.bunchsize:
                    w.writelines(bunch)
                    bunch = []
            w.writelines(bunch)

    def process_streetname_str(self, line, line_num, type):
        """ parse line of text into XML and look to rename the v attribute in the tag element """
        ret_val = line

        xml = ET.fromstring(line)
        val = xml.get('v')
        if val:
            if len(val) > 0:
                self.rename_xml_value_attirbute(xml, line_num)
                xml_str = ET.tostring(xml, encoding="UTF-8", method="html")
                ret_val = "    {}\n".format(xml_str)
            else:
                log.warning("{} (line {}) xml element {} found an empty street name value".format(type, line_num, xml.attrib))
        else:
            log.warning("{} (line {}) xml element {} is without a value attribute".format(type, line_num, xml.attrib))

        return ret_val

    def rename_xml_value_attirbute(self, xml, line_num):
        """ rename the 'v' value attirbute in this xml element tag """
        street_name = xml.attrib['v']
        if street_name in self.rename_cache:
            xml.attrib['v'] = self.rename_cache[street_name]
            if line_num % 111 == 0:
                sys.stdout.write(":")
                sys.stdout.flush()
        else:
            rename = self.abbr_parser.to_str(street_name)
            xml.set('v', rename)
            self.rename_cache[street_name] = rename
            if line_num % 111 == 0:
                sys.stdout.write(".")
                sys.stdout.flush()

    @classmethod
    def rename(cls, osm_infile_path, osm_outfile_path=None, do_bkup=True):
        """ 
        """
        ret_val = None
        if osm_outfile_path is None:
            osm_outfile_path = osm_infile_path
        ret_val = OsmRename(osm_infile_path, osm_outfile_path, do_bkup=do_bkup)
        return ret_val