パーソナルツール
現在の場所: ホーム firefox forecastfox weather.py
書いた本
Plone 完全活用ガイド の Chapter 1, 2, 3, 11 を執筆しました。
plone のインストール、使い方から、機能・デザインのカスタマイズ、プロダクトの作り方まで、 plone のすべてがぎゅっと詰まっている書籍になっていると思います。
plone に興味がある人から、すでに使いこなしている方まで、ぜひ読んでみてください。
Plone 完全活用ガイドのサポートページ
ナビゲーション

 
文書操作

weather.py

作成者 takanori 最終変更日時 2008年06月04日 06時49分

天気予報データの日本語化フィルタ

Click here to get the file

サイズ 24.0 kB - File type text/python-source

ファイルのコンテンツ

# -*- coding: utf-8 -*-
from mod_python import apache
import os, re, sys, time
import time
from xml.dom import Node
from xml.dom.NodeFilter import NodeFilter
from xml.dom import ext
from xml.dom.ext.reader import HtmlLib

class Pattern:
    dict = {}
    re = re.compile("")
    
    def __init__(self, dict, tag, *args):
        type.__init__(self, *args)
        self.dict = dict
        before = tag.replace("|", ">|<")
        after = tag.replace("|", ">|</")
        self.re = re.compile("(<%s>)([^<]*)(</%s>)" % (before, after))
    
class Translation:
    """Weather data translation information."""
    dict_state = {
        "Japan": "日本"
        }
    
    dict_city = {
        "Abashiri": "網走",
        "Abiko": "我孫子",
        "Aburatsu": "油津",
        "Ageo": "上尾",
        "Ai": "Ai",
        "Aidomari": "相泊",
        "Aigadani": "",
        "Aikawa": "相川",
        "Aimata": "相俣",
        "Aimoto": "相本",
        "Ajiro": "足代",
        "Akabane": "赤羽",
        "Akakura": "赤倉",
        "Akamatsu": "赤松",
        "Akane": "Akane",
        "Akankohan": "阿寒湖畔",
        "Akano": "Akano",
        "Akasakata": "赤坂田",
        "Akashi": "明石",
        "Akenobe": "Akenobe",
        "Akimoto": "Akimoto",
        "Akita": "秋田",
        "Akkeshi": "厚岸",
        "Ako": "Ako",
        "Akune": "Akune",
        "Amagasaki": "尼崎",
        "Amagase": "天瀬",
        "Amaze": "尼瀬",
        "Amo": "Amo",
        "Anjo": "安城",
        "Aomori": "青森",
        "Aose": "青瀬",
        "Arakawaguchi": "荒川口",
        "Arakura": "Arakura",
        "Araragi": "Araragi",
        "Araso": "Araso",
        "Araya": "Araya",
        "Arikabe": "有壁",
        "Asahi": "Asahi",
        "Asahikawa": "旭川",
        "Asahiyama": "旭山",
        "Asaka": "朝霞",
        "Ashikaga": "足利",
        "Ashio": "足尾",
        "Ashiya": "芦屋",
        "Ashu": "芦生",
        "Aso": "阿蘇",
        "Atsugi": "厚木",
        "Azusayama": "梓山",
        "Bekku": "別宮",
        "Benkei": "弁慶",
        "Beppu": "別府",
        "Chiba": "千葉",
        "Chigasaki": "茅ヶ崎",
        "Chinase": "知名瀬",
        "Chisaka": "千坂",
        "Chitose": "千歳",
        "Chofu": "調布",
        "Choja": "長者",
        "Chojahara": "長者原",
        "Choshi": "銚子",
        "Chusha": "Chusha",
        "Dai": "Dai",
        "Daimommachi": "大門町",
        "Dairakozan": "Dairakozan",
        "Daisen": "Daisen",
        "Daito": "Daito",
        "Deai": "Deai",
        "Doai": "Doai",
        "Doi": "Doi",
        "Ebetsu": "江別",
        "Ebi": "Ebi",
        "Esashi": "江差",
        "Fuchu": "府中",
        "Fuji": "富士",
        "Fujieda": "藤枝",
        "Fujinoike": "Fujinoike",
        "Fujinokawa": "藤の川",
        "Fujinomiya": "富士宮",
        "Fujisawa": "藤沢",
        "Fujita": "藤田",
        "Fujitsuka": "藤塚",
        "Fujitsukahama": "藤塚浜",
        "Fujiwara": "藤原",
        "Fukae": "深江",
        "Fukaura": "深浦",
        "Fukuda": "福田",
        "Fukue": "福江",
        "Fukui": "福井",
        "Fukuoka": "福岡",
        "Fukura": "福良",
        "Fukushima": "福島",
        "Fukuyama": "福山",
        "Funabashi": "船橋",
        "Furebetsu": "Furebetsu",
        "Furukawa": "古河",
        "Furuseki": "古関",
        "Futaba": "Futaba",
        "Futamata": "二俣",
        "Futenma": "普天間",
        "Ganyu": "Ganyu",
        "Gifu": "Gifu",
        "Gongen": "Gongen",
        "Hachinohe": "八戸",
        "Hachioji": "八王子",
        "Hadano": "秦野",
        "Hagi": "萩",
        "Haginari": "Haginari",
        "Hakodate": "函館",
        "Hamada": "Hamada",
        "Hamamatsu": "浜松",
        "Hamano": "浜野",
        "Hanamaki": "花巻",
        "Handa": "半田",
        "Hanezawa": "羽沢",
        "Hateruma": "波照間",
        "Hayakawa": "早川",
        "Hazako": "迫子",
        "Hibara": "Hibara",
        "Hidaka": "日高",
        "Hidariishi": "左石",
        "Hige": "Hige",
        "Hikone": "彦根",
        "Himeji": "姫路",
        "Hinata": "Hinata",
        "Hino": "日野",
        "Hinoemata": "檜枝岐",
        "Hinokage": "日之影",
        "Hirakata": "枚方",
        "Hirase": "平瀬",
        "Hiratsuka": "平塚",
        "Hirayu": "平湯",
        "Hiroo": "広尾",
        "Hirosaki": "弘前",
        "Hiroshima": "広島",
        "Hirugami": "昼神",
        "Hita": "日田",
        "Hitachi": "Hitachi",
        "Hitoyoshi": "Hitoyoshi",
        "Hiura": "日浦",
        "Hofu": "Hofu",
        "Hosoibara": "Hosoibara",
        "Hyotan": "Hyotan",
        "Ibaraki": "茨城",
        "Ibuki": "伊吹",
        "Ichihara": "市原",
        "Ichikawa": "市川",
        "Ichinomiya": "一宮",
        "Ichinosawa": "Ichinosawa",
        "Iizuka": "飯塚",
        "Ikeda": "池田",
        "Ikisan": "一貴山",
        "Ikoma": "生駒",
        "Imabari": "今治",
        "Inuma": "井沼",
        "Iori": "Iori",
        "Irori": "Irori",
        "Iruma": "入間",
        "Ise": "伊勢",
        "Isemachi": "伊勢町",
        "Isesaki": "伊勢崎",
        "Ishigaki": "石垣",
        "Ishino": "Ishino",
        "Ishinomaki": "石巻",
        "Ishizakikozan": "Ishizakikozan",
        "Itami": "伊丹",
        "Itchu": "Itchu",
        "Iwaki": "Iwaki",
        "Iwakuni": "岩国",
        "Iwamatsu": "岩松",
        "Iwamizawa": "岩見沢",
        "Iwatsuki": "岩槻",
        "Izuhara": "厳原",
        "Izumi": "Izumi",
        "Izumo": "出雲",
        "Izuno": "Izuno",
        "Kabutoji": "Kabutoji",
        "Kadoma": "門真",
        "Kae": "Kae",
        "Kagoshima": "鹿児島",
        "Kagumeyoshi": "頂吉",
        "Kaidomari": "貝泊",
        "Kaizawa": "貝沢",
        "Kakamigahara": "各務原",
        "Kakogawa": "加古川",
        "Kamakura": "鎌倉",
        "Kamigo": "Kamigo",
        "Kamigoto": "神事",
        "Kamiji": "Kamiji",
        "Kamikita": "上北",
        "Kamikitakozan": "Kamikitakozan",
        "Kamikoshi": "上越",
        "Kamimatsuo": "上松尾",
        "Kanazawa": "金沢",
        "Kaneo": "Kaneo",
        "Kanjikine": "Kanjikine",
        "Kanoya": "鹿屋",
        "Kariya": "Kariya",
        "Kasegi": "Kasegi",
        "Kashihara": "橿原",
        "Kashiwa": "柏",
        "Kasuga": "春日",
        "Kasugai": "春日井",
        "Kasukabe": "春日部",
        "Katashima": "片島",
        "Katatomari": "Katatomari",
        "Katsuma": "勝間",
        "Katsuta": "勝田",
        "Katsuura": "勝浦",
        "Kawagoe": "川越",
        "Kawaguchi": "川口",
        "Kawanishi": "川西",
        "Kawasaki": "川崎",
        "Kiryu": "桐生",
        "Kisarazu": "木更津",
        "Kishiwada": "岸和田",
        "Kitakyushu": "北九州",
        "Kitami": "北見",
        "Kobe": "神戸",
        "Kochi": "高知",
        "Kodaira": "小平",
        "Kofu": "甲府",
        "Koganei": "小金井",
        "Koike": "小池",
        "Komaki": "小牧",
        "Komatsu": "小松",
        "Koriyama": "郡山",
        "Koshigaya": "越谷",
        "Kowa": "Kowa",
        "Kumagaya": "熊谷",
        "Kumamoto": "熊本",
        "Kurashiki": "倉敷",
        "Kure": "呉",
        "Kurume": "久留米",
        "Kushiro": "釧路",
        "Kutchan": "倶知安",
        "Kutsukake": "沓掛",
        "Kuwana": "桑名",
        "Kuzakai": "区界",
        "Kyoto": "京都",
        "Machida": "町田",
        "Maebashi": "前橋",
        "Maizuru": "舞鶴",
        "Makinokuchi": "Makinokuchi",
        "Makurazaki": "枕崎",
        "Matsubara": "松原",
        "Matsudo": "松戸",
        "Matsue": "松江",
        "Matsumoto": "松本",
        "Matsuo": "松尾",
        "Matsusaka": "松阪",
        "Matsuyama": "松山",
        "Miboro": "御母衣",
        "Miho": "Miho",
        "Minoo": "箕面",
        "Misato": "Misato",
        "Mishima": "三島",
        "Mitaka": "三鷹",
        "Mito": "水戸",
        "Miyako": "Miyako",
        "Miyakojima": "宮古島",
        "Miyakonojo": "都城",
        "Miyazaki": "宮崎",
        "Mizusawa": "水沢",
        "Mochikoshi": "Mochikoshi",
        "Mombetsu": "紋別",
        "Monbetsu": "紋別",
        "Moriguchi": "守口",
        "Morioka": "盛岡",
        "Mui": "Mui",
        "Muroran": "室蘭",
        "Musashino": "武蔵野",
        "Mutsu": "陸奥",
        "Myojincho": "明神町",
        "Nagahama": "長原",
        "Nagano": "長野",
        "Nagaoka": "長岡",
        "Nagareyama": "流山",
        "Nagasaki": "長崎",
        "Nagasawa": "長沢",
        "Nago": "名護",
        "Nagoya": "名古屋",
        "Naha": "那覇",
        "Nakachi": "Nakachi",
        "Nakanoshima": "中ノ島",
        "Nama": "Nama",
        "Namiai": "浪合",
        "Nango": "Nango",
        "Nara": "奈良",
        "Narashino": "習志野",
        "Naze": "名瀬",
        "Neginohata": "Neginohata",
        "Nemuro": "根室",
        "Niigata": "新潟",
        "Niihama": "新居浜",
        "Nikko": "日光",
        "Nishinomiya": "西宮",
        "Nishiyama": "西山",
        "Nishizawakinzan": "Nishizawakinzan",
        "Nitto": "Nitto",
        "Nobeoka": "延岡",
        "Noda": "Noda",
        "Nodaoibara": "Nodaoibara",
        "Numajiri": "沼尻",
        "Numazu": "沼津",
        "Obe": "Obe",
        "Obihiro": "帯広",
        "Oda": "Oda",
        "Odawara": "小田原",
        "Ofunato": "大船渡",
        "Ogaki": "大垣",
        "Ogushi": "Ogushi",
        "Ohara": "Ohara",
        "Oita": "大分",
        "Okayama": "岡山",
        "Okazaki": "岡崎",
        "Oki": "Oki",
        "Okinawa": "沖縄",
        "Ome": "青梅",
        "Ominato": "大湊",
        "Omiya": "大宮",
        "Omu": "Omu",
        "Omuta": "大牟田",
        "Osaka": "大阪",
        "Oshima": "大島",
        "Ota": "Ota",
        "Otaru": "小樽",
        "Otsu": "大津",
        "Owase": "尾鷲",
        "Oyama": "小山",
        "Ozuki": "尾月",
        "Rumoi": "留萌",
        "Sado": "佐渡",
        "Saga": "佐賀",
        "Sagamihara": "相模原",
        "Saigo": "西郷",
        "Sakai": "境",
        "Sakata": "Sakata",
        "Sakura": "佐倉",
        "Sapporo": "札幌",
        "Sarugakyo": "猿ケ京",
        "Sasebo": "佐世保",
        "Sayama": "Sayama",
        "Sazarekozan": "Sazarekozan",
        "Sendai": "仙台",
        "Seto": "瀬戸",
        "Shimizu": "清水",
        "Shimonoseki": "下関",
        "Shinjo": "新庄",
        "Shirakawa": "白河",
        "Shiranita": "白仁田",
        "Shiriya": "Shiriya",
        "Shitooke": "志戸桶",
        "Shizuoka": "静岡",
        "Soka": "草加",
        "Suita": "吹田",
        "Sukomo": "宿毛",
        "Sumoto": "洲本",
        "Suttsu": "寿都",
        "Suwa": "諏訪",
        "Suzuka": "鈴鹿",
        "Tachikawa": "立川",
        "Tadotsu": "多度津",
        "Takamatsu": "高松",
        "Takaoka": "高岡",
        "Takarazuka": "宝塚",
        "Takasaki": "高崎",
        "Takatsuki": "高槻",
        "Takayu": "孝勇",
        "Tama": "多摩",
        "Tateyama": "立山",
        "Tokorozawa": "所沢",
        "Tokushima": "徳島",
        "Tokuyama": "徳山",
        "Tokyo": "東京",
        "Tomakomai": "苫小牧",
        "Tondabayashi": "富田林",
        "Tono": "遠野",
        "Tottori": "鳥取",
        "Toyama": "富山",
        "Toyoha": "Toyoha",
        "Toyohashi": "豊橋",
        "Toyokawa": "豊川",
        "Toyonaka": "豊中",
        "Toyooka": "豊岡",
        "Toyota": "豊田",
        "Tsu": "津",
        "Tsuiki": "Tsuiki",
        "Tsukuba": "つくば",
        "Tsuruga": "敦賀",
        "Tsuruoka": "鶴岡",
        "Tsuyama": "津山",
        "Ube": "宇部",
        "Uchinoura": "内之浦",
        "Ueda": "上田",
        "Ueno": "上野",
        "Uji": "宇治",
        "Urakawa": "浦川",
        "Urawa": "浦和",
        "Urayasu": "浦安",
        "Usa": "宇佐",
        "Ushibuka": "牛深",
        "Utsunomiya": "宇都宮",
        "Uwajima": "宇和島",
        "Wajima": "輪島",
        "Wakayama": "和歌山",
        "Wakkanai": "稚内",
        "Washiaimori": "Washiaimori",
        "Yachiyo": "八千代",
        "Yaizu": "焼津",
        "Yamagata": "山形",
        "Yamaguchi": "山口",
        "Yamato": "大和",
        "Yao": "八尾",
        "Yatsushiro": "八代",
        "Yokkaichi": "四日市",
        "Yokohama": "横浜",
        "Yokono": "Yokono",
        "Yokooka": "横岡",
        "Yokosuka": "横須賀",
        "Yonago": "米子",
        "Yonaguni": "与那国",
        "Yotsuura": "四浦",
        "Yuyama": "湯山",
        "Zama": "座間",
        }
    
    dict_daycode = {
        "Sunday": "日曜日",
        "Monday": "月曜日",
        "Tuesday": "火曜日",
        "Wednesday": "水曜日",
        "Thursday": "木曜日",
        "Friday": "金曜日",
        "Saturday": "土曜日"
        }

    dict_winddirection = {
        "N": "北",
        "E": "東",
        "W": "西",
        "S": "南",
        "NE": "北東",
        "NW": "北西",
        "SE": "南東",
        "SW": "南西",
        "NNE": "北北東",
        "ENE": "東北東",
        "NNW": "北北西",
        "WNW": "西北西",
        "SSE": "南南東",
        "ESE": "東南東",
        "SSW": "南南西",
        "WSW": "西南西"
        }

    dict_weathertext = {
        "Sunny": "快晴",
        "Clear": "晴れ(晴天)",
        "Fair": "晴れ",
        "Sunny/Windy": "快晴/強風",
        "Clear/Windy": "晴れ(晴天)/強風",
        "Fair/Windy": "晴れ/強風",
        "Windy": "強風",
        "Partly Sunny": "曇り一時快晴",
        "Mostly Cloudy": "薄曇り",
        "Mostly cloudy": "薄曇り",
        "Partly Cloudy": "所により曇り",
        "Partly cloudy": "所により曇り",
        "Mostly Clear": "晴れ所により曇り",
        "Partly Sunny/Windy": "曇り一時快晴/強風",
        "Mostly Clear/Windy": "晴れ所により曇り/強風",
        "Mostly Sunny": "快晴所により曇り",
        "Mostly Sunny/Windy": "快晴所により曇り/強風",
        "Cloudy": "曇り",
        "Overcast": "曇り(雲量多)",
        "Cloudy/Windy": "曇り/強風",
        "Overcast/Windy": "曇り(雲量多)/強風",
        "Mostly Cloudy/Windy": "薄曇り / 強風",
        "Mostly Cloudy": "薄曇り",
        "Light Drizzle": "弱い霧雨",
        "Drizzle": "霧雨",
        "Light Rain": "小雨",
        "Rain": "雨",
        "Heavy Rain": "大雨",
        "Light Rain/Fog": "弱い雨/霧",
        "Rain/Fog": "雨/霧",
        "Light Drizzle/Windy": "弱い霧雨/強風",
        "Drizzle/Windy": "霧雨/強風",
        "Light Rain/Windy": "小雨/強風",
        "Rain/Windy": "雨/強風",
        "Heavy Rain/Windy": "大雨/強風",
        "AM Light Rain": "午前中小雨",
        "PM Light Rain": "午後より小雨",
        "AM Light Rain/Windy": "午前中 弱いにわか雪/強風",
        "PM Light Rain/Windy": "午後から 弱いにわか雪/強風",
        "Rainshower": "にわか雨",
        "Shower": "にわか雨",
        "Heavy Rainshower": "強いにわか雨",
        "Heavy Rain Shower/Windy": "強いにわか雨/強風",
        "Light Rain Shower": "弱いにわか雨",
        "AM Shower": "午前 にわか雨",
        "PM Shower": "午後 にわか雨",
        "Scattered Shower": "所により にわか雨",
        "Rain Shower/Windy": "にわか雨/強風",
        "Shower/Windy": "にわか雨/強風",
        "Light Rain Shower/Windy": "弱いにわか雨/強風",
        "AM Shower/Windy": "午前 にわか雨/強風",
        "PM Shower/Windy": "午後 にわか雨/強風",
        "Scattered Shower/Windy": "所により にわか雨/強風",
        "Few Showers": "曇り時々にわか雨",
        "Few Showers/Windy": "曇り時々にわか雨/強風",
        "Showers in the Vicinity": "狭い範囲でにわか雨",
        "Thunder in the Vicinity": "狭い範囲で雷",
        "Light Snow": "小雪",
        "Snow": "雪",
        "Heavy Snow": "大雪",
        "Light Snow Pellets": "弱い霙(みぞれ)",
        "Snow Pellets": "霙(みぞれ)",
        "Light Ice Pellets": "霰(あられ)",
        "Ice Pellets": "雹(ヒョウ)",
        "Wintery Weather": "厳しい寒さ",
        "Light Freezing Rain": "弱い凍雨",
        "Freezing Rain": "凍雨",
        "Flurries/Windy": "曇り一時雨又は雪/強風",
        "Light Flurries/Windy": "曇り一時小雨又は小雪/強風",
        "Light Snow/Windy": "小雪/強風",
        "Snow/Windy": "吹雪",
        "Heavy Snow/Windy": "強い吹雪",
        "Light Snow Pellets/Windy": "弱い霙(みぞれ)/強風",
        "Snow Pellets/Windy": "霙(みぞれ)/強風",
        "Light Ice Pellets/Windy": "霰(あられ)/強風",
        "Ice Pellets/Windy": "雹(ヒョウ)/強風",
        "Light Freezing Rain/Windy": "弱い凍雨/強風",
        "Freezing Rain/Windy": "凍雨/強風",
        "Wintery Mix": "厳冬",
        "Light Snow Grains": "弱い霰(あられ)",
        "Snow Grains": "霰(あられ)",
        "Rain/Snow": "雨時々雪",
        "Rain/Show/Windy": "雨時々雪/強風",
        "Light Rain/Freezing Rain": "弱い雨時々みぞれ",
        "Rain/Freezing Rain": "雨時々みぞれ",
        "Light Rain/Freezing Rain/Windy": "弱い雨時々みぞれ/強風",
        "Rain/Freezing Rain/Windy": "雨時々みぞれ/強風",
        "AM Snow": "午前中雪",
        "PM Snow": "午後より雪",
        "AM Light Snow": "午前中小雪",
        "PM Light Snow": "午後より小雪",
        "Ice Crystals": "氷晶",
        "Ice Crystals/Windy": "氷晶/強風",
        "Snow Showers": "にわか雪",
        "Heavy Snow Shower": "強いにわか雪",
        "Heavy Snow Shower/Windy": "強いにわか雪/強風",
        "PM Snow Showers": "午後にわか雪",
        "AM Snow Showers": "午前にわか雪",
        "Rain/Snow Showers": "雨のちにわか雪",
        "Snow Showers/Windy": "にわか雪/強風",
        "PM Snow Showers/Windy": "午後にわか雪/強風",
        "AM Snow Showers/Windy": "午前にわか雪/強風",
        "Rain/Snow Showers/Windy": "雨のちにわか雪/強風",
        "Light Snow Showers": "弱いにわか雪",
        "Light Snow Showers/Windy": "弱いにわか雪/強風",
        "Flurries": "曇り一時雨又は雪",
        "Light Flurries": "曇り一時小雨又は小雪",
        "Scattered Flurries": "所により 曇り一時雨又は雪",
        "Few Flurries": "曇り 所により一時雨又は雪",
        "Few Flurries/Windy": "曇り 所により一時雨又は吹雪",
        "Scattered Snow Showers": "所によりにわか雪",
        "Scattered Snow Showers/Windy": "所によりにわか雪/強風",
        "Few Snow Showers": "曇り時々にわか雪",
        "Few Snow Showers/Windy": "曇り時々にわか雪/強風",
        "Freezing Drizzle": "霧氷",
        "Light Freezing Drizzle": "弱い霧氷",
        "Freezing Drizzle/Windy": "霧氷/強風",
        "Light Freezing Drizzle/Windy": "弱い霧氷/強風",
        "Thunderstorms": "雷又は雷雨",
        "Scattered Thunderstorms": "所により雷又は雷雨",
        "Thunderstorms/Windy": "雷又は雷雨/強風",
        "Scattered Thunderstorms/Windy": "所により雷又は雷雨/強風",
        "Rain/Thunder": "雷雨",
        "Light Thunderstorms/Rain": "弱い雷又は雷雨",
        "Thunderstorms/Rain": "雷又は雷雨",
        "Light Rain with Thunder": "弱い雷雨",
        "Rain with Thunder": "雷雨",
        "Fog": "霧",
        "Haze": "靄(もや)",
        "Mist": "霞(かすみ)",
        "Fog/Windy": "霧/強風",
        "Haze/Windy": "靄(もや)/強風",
        "Mist/Windy": "霞(かすみ)/強風",
        "Partial Fog": "部分的に霧気味",
        "Smoke": "もや",
        "Foggy": "霧がかった",
        "AM Fog/PM Sun": "午前霧/午後晴れ"
}
    
    def __init__(self, *args):
        type.__init__(self, *args)
        self._instances = {}
        self._state = Pattern(self.dict_state, "state")
        self._city = Pattern(self.dict_city, "city")
        self._daycode = Pattern(self.dict_daycode, "daycode")
        self._winddirection = Pattern(self.dict_winddirection, "winddirection")
        self._weathertext = Pattern(self.dict_weathertext, "weathertext|txtshort|txtlong")
    
    def __call__(self, *args):
        if not args in self._instances:
            self._instances[args] = type.__call__(self, *args)
        return self._instances[args]

    def state(self, content):
        return self._trans(content, self._state)

    def city(self, content):
        return self._trans(content, self._city)

    def daycode(self, content):
        return self._trans(content, self._daycode)

    def winddirection(self, content):
        return self._trans(content, self._winddirection)

    def weathertext(self, content):
        return self._trans(content, self._weathertext)

    def _trans(self, content, pattern):
        end = 0
        new_content = ''
        for m in pattern.re.finditer(content):
            new_content += content[end:m.start()]
            grps = m.groups()
            value = grps[1]
            if pattern.dict.has_key(value):
                value = pattern.dict[value]
            new_content += '%s%s%s'%(grps[0],value,grps[2])
            end = m.end()

        new_content += content[end:]
        content = new_content

        return new_content

class Buffer:
    """Weather data buffer(for translation)"""
    buffer = {}

    def __init__(self, *args):
        type.__init__(self, *args)
        self._instances = {}
    
    def __call__(self, *args):
        if not args in self._instances:
            self._instances[args] = type.__call__(self, *args)
        return self._instances[args]

    def add(self, key, s):
        # create buffer for current connection
        if not self.buffer.has_key(key):
            #apache.log_error("rtime: " + key)
            self.buffer[key] = ""
        self.buffer[key] += s

    def get(self, key):
        return self.buffer[key]

    def remove(self, key):
        del self.buffer[key]

def outputfilter(filter):
    """Weather data output filet"""
    # get request time(for buffering key)
    rtime = str(filter.req.request_time)
    
    trans = Translation()
    buffer = Buffer()

    s = filter.read()
    while s:
        buffer.add(rtime, s)
        s = filter.read()

    if s is None:
        wdata = buffer.get(rtime)
        orig = buffer.get(rtime)

        wdata = trans.state(wdata)
        wdata = trans.city(wdata)
        wdata = trans.daycode(wdata)
        wdata = trans.winddirection(wdata)
        wdata = trans.weathertext(wdata)
	wdata = wdata.replace("</use>", """</use>
    <use-j>このドキュメントは AccuWeather.com によって許可された認可者による使用のみを目的とします。未許可の使用は禁止されます。All Rights Reserved.</use-j>""")
        wdata = wdata.replace("</redistribution>", """</redistribution>
    <translator>鈴木たかのり</translator>""")

	wdata = wdata.replace('<?xml version="1.0" ?>', '<?xml version="1.0" encoding="utf-8" ?>')
        filter.write(wdata)
        buffer.remove(rtime)
        filter.close()

Powered by vine linux, python, zope, plone, coreblog